From 0754225107cb4326a8c56062d08a231706e84f5e Mon Sep 17 00:00:00 2001 From: ftaakash Date: Tue, 20 Oct 2026 21:29:30 +0530 Subject: [PATCH] fix(e2e): fake EHR redirects only to the registered redirect URI The SMART launch spec's fake authorization server answered /authorize with a 302 to whatever redirect_uri the request carried (CodeQL js/server-side-unvalidated-url-redirection, alert #2). It now checks client_id and redirect_uri against the registered values like a real authorization server, answers 400 invalid_request on a mismatch, and builds Location from the registered URI with code and state encoded through searchParams. --- frontend/e2e/smart-launch.spec.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/frontend/e2e/smart-launch.spec.js b/frontend/e2e/smart-launch.spec.js index bccfc35..0b1cf71 100644 --- a/frontend/e2e/smart-launch.spec.js +++ b/frontend/e2e/smart-launch.spec.js @@ -43,10 +43,18 @@ const server = http.createServer((req, res) => { return json(res, 200, { keys: [{ ...publicKey.export({ format: 'jwk' }), kid: 'e2e-1', alg: 'RS256', use: 'sig' }] }); } if (url.pathname === '/authorize') { + // Like a real authorization server, only the registered client and redirect URI are accepted, and the browser + // goes back to the registered URI, never to one taken from the request. + if (url.searchParams.get('client_id') !== CLIENT_ID || url.searchParams.get('redirect_uri') !== REDIRECT) { + return json(res, 400, { error: 'invalid_request' }); + } // The EHR user is already signed in to the EHR: approve and send the browser back. const code = crypto.randomUUID(); codes.set(code, Object.fromEntries(url.searchParams)); - res.writeHead(302, { Location: `${url.searchParams.get('redirect_uri')}?code=${code}&state=${url.searchParams.get('state')}` }); + const location = new URL(REDIRECT); + location.searchParams.set('code', code); + location.searchParams.set('state', url.searchParams.get('state') ?? ''); + res.writeHead(302, { Location: location.href }); return res.end(); } if (url.pathname === '/token' && req.method === 'POST') {