close
Skip to content

fix(auth): OAuth2 redirect fails when "code" is the first query parameter #312105

@amberiitd

Description

@amberiitd

Does this issue occur when all extensions are disabled?: Yes/No

  • VS Code Version: 1.116.0 (used in the latest code-server)
  • OS Version: Browser (Chrome/Edge/Safari) running code-server in a Linux Container.

Environment Note: This issue is specific to VS Code for the Web (code-server). The regex is strictly applied to the incoming URI.

Steps to Reproduce (Web Specific):

  1. Launch code-server or VS Code for the Web.
  2. Trigger an OAuth flow that uses LocalStorageURLCallbackProvider (in my case a custom MCP server as OAuth 2.1 client).
  3. Identity provider (Cognito) redirects to https://<your-domain>/callback?code=xxx&state=yyy.
  4. Just after a successful redirect to callback url, the following error message appears in Output tab: 'Authentication failed: No authorization code received'

Technical Specifics for the Report

In the web version, after successful redirect the callback html script writes authorization 'code' (and other parameter) to the localStorage. The Workbench (Main Thread) receives the 'LocalStorageURLCallback' and it fires an event with the localStorage item to the Extension Host Process running in backend api/node.

The bug exists in the code that receives this message inside the 'Extension Host Process'. In this repo the source code is present here:
file: /src/vs/workbench/api/common/extHostAuthentication.ts
function: private async waitForAuthorizationCode(expectedState: URI): Promise<{ code: string }>
line code: const codeMatch = /[?&]code=([^&]+)/.exec(result.query || '');

The above code has restrictive regular expression, and if the query string has code as the first parameter then it ignores it, since it does not find leading & or ? character.

My temporary fix

I was able to fix this locally in my code-server Docker container by patching the compiled output:

FROM codercom/code-server:latest

USER root

RUN sed -i 's/\[?&\]code=/\[?\&\]\?code=/g' \
    /usr/lib/code-server/lib/vscode/out/vs/workbench/api/worker/extensionHostWorkerMain.js

RUN sed -i 's/\[?&\]code=/\[?\&\]\?code=/g' \
    /usr/lib/code-server/lib/vscode/out/vs/workbench/api/node/extensionHostProcess.js

Just made the leading character optional.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions