Skip to content

Commit

Permalink
Parse error code query parameter on workspace create setep
Browse files Browse the repository at this point in the history
Signed-off-by: Igor <ivinokur@redhat.com>
  • Loading branch information
vinokurig committed Sep 25, 2023
1 parent 8d29c17 commit cbd370e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export * from './dto/cluster-config';
export { helpers, api };

export const FACTORY_LINK_ATTR = 'factoryLink';
export const ERROR_CODE_ATTR = 'error_code';

const common = {
helpers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ describe('Factory Acceptance Redirect', () => {
expect(res.statusCode).toEqual(302);
expect(res.headers.location).toEqual(`/dashboard/#/load-factory?url=${factoryUrl}`);
});

it('should redirect "/dashboard/f?factoryLink%3Durl%3DfactoryUrl"', async () => {
const factoryUrl = 'factoryUrl';
const res = await app.inject({
url: `/dashboard/f?factoryLink%3Durl%3D${factoryUrl}`,
});
expect(res.statusCode).toEqual(302);
expect(res.headers.location).toEqual(`/dashboard/#/load-factory?url=${factoryUrl}`);
});
});
15 changes: 10 additions & 5 deletions packages/dashboard-backend/src/routes/factoryAcceptanceRedirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Red Hat, Inc. - initial API and implementation
*/

import { FACTORY_LINK_ATTR } from '@eclipse-che/common';
import { ERROR_CODE_ATTR, FACTORY_LINK_ATTR } from '@eclipse-che/common';
import { sanitizeSearchParams } from '@eclipse-che/common/src/helpers/sanitize';
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import querystring from 'querystring';
Expand All @@ -20,15 +20,20 @@ export function registerFactoryAcceptanceRedirect(instance: FastifyInstance): vo
function redirectFactoryFlow(path: string) {
instance.register(async server => {
server.get(path, async (request: FastifyRequest, reply: FastifyReply) => {
let queryStr = request.url.replace(path, '').replace(/^\?/, '');
let factoryLinkStr = request.url.replace(path, '').replace(/^\?/, '');

const query = querystring.parse(queryStr);
const query = querystring.parse(decodeURIComponent(factoryLinkStr));
if (query[FACTORY_LINK_ATTR] !== undefined) {
// restore the factory link from the query string
queryStr = querystring.unescape(query[FACTORY_LINK_ATTR] as string);
factoryLinkStr = querystring.unescape(query[FACTORY_LINK_ATTR] as string);
}

const sanitizedQueryParams = sanitizeSearchParams(new URLSearchParams(queryStr));
const params = new URLSearchParams(factoryLinkStr);
if (query[ERROR_CODE_ATTR] !== undefined) {
params.append(ERROR_CODE_ATTR, querystring.unescape(query[ERROR_CODE_ATTR] as string));
}

const sanitizedQueryParams = sanitizeSearchParams(params);

return reply.redirect('/dashboard/#/load-factory?' + sanitizedQueryParams.toString());
});
Expand Down

0 comments on commit cbd370e

Please sign in to comment.