diff --git a/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.requests.test.ts b/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.requests.test.ts index d74ab2ea9b..328d15b8d8 100644 --- a/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.requests.test.ts +++ b/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.requests.test.ts @@ -126,13 +126,39 @@ for (const implementation of ['native', 'fallback'] as const) { token_endpoint_auth_method: authMethod, redirect_uri: 'http://localhost:12345/oauth/callback', } - const resource = new URL('https://mcp.example.com/mcp') + // The token endpoint is a server on this computer, which an MCP server may send the client to only when + // it is on this computer too + const resource = new URL('http://localhost:3000/mcp') if (grant === 'refresh') { return (OAuthClient as any).refreshGrant(meta, reg, resource, 'test-refresh-token') } return (OAuthClient as any).pkceGrant(meta, reg, resource, [], reg.redirect_uri, callback) } + it('registers the callback listener before opening the browser', async () => { + // Deliver the redirect synchronously while showDocument is still pending. The listener must already be + // registered by then, otherwise the callback is dropped and the sign-in waits for the timeout. + showDocument.callsFake(async ({ uri }: { uri: string }) => { + const state = new URL(uri).searchParams.get('state')! + const response = { writeHead: () => response, end: () => response } + callback.emit('request', { url: `/oauth/callback?code=test-code&state=${state}` }, response) + return { success: true } + }) + const token = await exchange('authorization_code', 'none') + assert.equal(token.access_token, 'test-access-token') + assert.equal(requests[0].body.get('code'), 'test-code') + sinon.assert.calledOnce(showDocument) + }) + + it('opens a loopback HTTP authorization endpoint in the external browser', async () => { + const token = await exchange('authorization_code', 'none', endpointUrl, 'http://127.0.0.1:9/authorize') + assert.equal(token.access_token, 'test-access-token') + sinon.assert.calledOnce(showDocument) + const opened = new URL(showDocument.firstCall.args[0].uri) + assert.equal(opened.origin + opened.pathname, 'http://127.0.0.1:9/authorize') + assert.equal(showDocument.firstCall.args[0].external, true) + }) + for (const grant of ['refresh', 'authorization_code'] as const) { for (const authMethod of ['client_secret_post', 'client_secret_basic', 'none']) { for (const status of [301, 302, 303, 307, 308]) { @@ -186,68 +212,43 @@ for (const implementation of ['native', 'fallback'] as const) { } } - for (const authorizationEndpoint of [ - 'file:///tmp/payload.sh', - 'javascript:alert(1)', - 'command:workbench.action.terminal.new', - 'vscode://file/tmp/payload.sh', - 'http://auth.example.com/authorize', - 'not a url', - ]) { - it(`does not open ${authorizationEndpoint} as the authorization endpoint`, async () => { - const fetch = sinon.stub(OAuthClient as any, 'fetchCompat') - await assert.rejects( - exchange('authorization_code', 'none', endpointUrl, authorizationEndpoint), - /authorization endpoint/ - ) - sinon.assert.notCalled(showDocument) - sinon.assert.notCalled(fetch) - }) - } - - it('accepts a callback that arrives before showDocument resolves', async () => { - showDocument.callsFake(async ({ uri }: { uri: string }) => { - const state = new URL(uri).searchParams.get('state')! - const response = { writeHead: () => response, end: () => response } - callback.emit('request', { url: `/oauth/callback?code=test-code&state=${state}` }, response) - return { success: true } - }) - - const token = await exchange('authorization_code', 'none') - - assert.equal(token.access_token, 'test-access-token') - assert.equal(requests[0].body.get('code'), 'test-code') - }) - - it('opens a loopback HTTP authorization endpoint', async () => { - const token = await exchange('authorization_code', 'none', endpointUrl, 'http://127.0.0.1:9/authorize') - assert.equal(token.access_token, 'test-access-token') - sinon.assert.calledOnce(showDocument) - const opened = new URL(showDocument.firstCall.args[0].uri) - assert.equal(opened.origin + opened.pathname, 'http://127.0.0.1:9/authorize') - assert.equal(showDocument.firstCall.args[0].external, true) + it('does not send the refresh token to a loopback token endpoint of a public MCP server', async () => { + sinon.stub(OAuthClient as any, 'lookupHost').resolves([{ address: '93.184.216.34', family: 4 }]) + const refresh = (OAuthClient as any).refreshGrant( + { authorization_endpoint: 'https://auth.example.com/authorize', token_endpoint: endpointUrl }, + { client_id: 'test-client', client_secret: 'test-secret', redirect_uri: 'http://localhost:1/cb' }, + new URL('https://mcp.example.com/mcp'), + 'test-refresh-token' + ) + await assert.rejects(refresh, /token endpoint is on a loopback address/) + assert.equal(requests.length, 0) }) it('returns no token or cache update after a redirected silent refresh', async () => { responseStatus = 307 - sinon.stub(OAuthClient as any, 'read').callsFake(async (file: unknown) => - String(file).endsWith('.token.json') - ? { - access_token: 'expired', - refresh_token: 'test-refresh-token', - obtained_at: 0, - expires_in: 1, - } + sinon.stub(OAuthClient as any, 'read').callsFake(async (file: unknown) => { + if (String(file).endsWith('.token.json')) { + return { + access_token: 'expired', + refresh_token: 'test-refresh-token', + obtained_at: 0, + expires_in: 1, + } + } + // A silent refresh sends the refresh token only to the token endpoint pinned at the last sign-in + return String(file).endsWith('.meta.json') + ? { token_endpoint: endpointUrl } : { client_id: 'test-client', client_secret: 'test-secret' } - ) - sinon.stub(OAuthClient as any, 'discoverAS').resolves({ token_endpoint: endpointUrl }) + }) + const discover = sinon.stub(OAuthClient as any, 'discoverAS') const write = sinon.stub(OAuthClient as any, 'write') - const token = await OAuthClient.getValidAccessToken(new URL('https://mcp.example.com/mcp'), { + const token = await OAuthClient.getValidAccessToken(new URL('http://localhost:3000/mcp'), { interactive: false, }) assert.equal(token, undefined) assert.equal(requests.length, 1) assert.equal(destinationRequests, 0) + sinon.assert.notCalled(discover) sinon.assert.notCalled(write) sinon.assert.notCalled(showDocument) }) diff --git a/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.test.ts b/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.test.ts index adbe4176a9..832e7ea216 100644 --- a/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.test.ts +++ b/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.test.ts @@ -76,6 +76,11 @@ function stubHttpServer(): void { }) } +/** Resolves every host name to a public address, so that the target check of OAuthClient needs no DNS */ +function stubPublicDns(): sinon.SinonStub { + return sinon.stub(OAuthClient as any, 'lookupHost').resolves([{ address: '93.184.216.34', family: 4 }]) +} + describe('OAuthClient helpers', () => { it('computeKey() generates deterministic SHA-256 hex', () => { const url = new URL('https://example.com/api') @@ -93,8 +98,8 @@ describe('OAuthClient helpers', () => { expect(actual).to.equal('aGVsbG8') }) - describe('validateEndpoint()', () => { - const validate = (endpoint: string) => (OAuthClient as any).validateEndpoint(endpoint, 'token') + describe('validateTokenEndpoint()', () => { + const validate = (endpoint: string) => (OAuthClient as any).validateTokenEndpoint(endpoint) for (const endpoint of [ 'https://auth.example.com/token', @@ -122,22 +127,49 @@ describe('OAuthClient helpers', () => { 'http://[::]/token', 'http://127.0.0.1@auth.example.com/token', 'ftp://auth.example.com/token', - 'file:///tmp/token', - 'javascript:alert(1)', - 'command:workbench.action.terminal.new', - 'vscode://file/tmp/token', 'not a url', ]) { it(`rejects ${endpoint}`, () => { expect(() => validate(endpoint)).to.throw() }) } + }) - it('names the endpoint kind in the error', () => { - expect(() => (OAuthClient as any).validateEndpoint('file:///tmp/x', 'authorization')).to.throw( - 'OAuth: authorization endpoint must use HTTPS unless it is a loopback address' - ) + describe('validateAuthorizationEndpoint()', () => { + const validate = (endpoint: string) => (OAuthClient as any).validateAuthorizationEndpoint(endpoint) + + for (const endpoint of [ + 'https://auth.example.com/authorize', + 'http://localhost:8080/authorize', + 'http://127.0.0.1/authorize', + 'http://[::1]/authorize', + ]) { + it(`allows ${endpoint}`, () => { + expect(() => validate(endpoint)).not.to.throw() + }) + } + + it('returns the parsed URL for a valid endpoint', () => { + const url = validate('https://auth.example.com/authorize') + expect(url).to.be.instanceOf(URL) + expect(url.href).to.equal('https://auth.example.com/authorize') }) + + // The browser opens only https URLs and loopback http URLs. + for (const endpoint of [ + 'javascript:void(0)', + 'file:///tmp/authorize', + 'data:text/plain,authorize', + 'http://auth.example.com/authorize', + 'ftp://auth.example.com/authorize', + 'vscode://extension/other', + 'command:workbench.action.terminal.new', + 'not a url', + ]) { + it(`rejects ${endpoint}`, () => { + expect(() => validate(endpoint)).to.throw() + }) + } }) }) @@ -148,6 +180,7 @@ describe('OAuthClient.refreshGrant()', () => { sinon.restore() OAuthClient.initialize(fakeWorkspace, fakeLogger as any, fakeLsp) fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + stubPublicDns() }) afterEach(() => sinon.restore()) @@ -169,6 +202,58 @@ describe('OAuthClient.refreshGrant()', () => { expect(fetchStub.called).to.be.false }) }) + +describe('OAuthClient.pkceGrant() authorization endpoint', () => { + // showDocument rejects loudly: if validation is ever skipped and the browser + // is launched, the grant rejects with this marker instead of the validation + // error, so the test fails fast rather than waiting on the real PKCE timeout. + let showDocument: sinon.SinonStub + + beforeEach(() => { + sinon.restore() + showDocument = sinon.stub().rejects(new Error('BROWSER_LAUNCHED')) + OAuthClient.initialize(fakeWorkspace, fakeLogger as any, { window: { showDocument } } as any) + stubPublicDns() + }) + + afterEach(() => sinon.restore()) + + // The token endpoint is a valid public https URL so the flow reaches the browser step; + // only the authorization_endpoint fails the check. + for (const authorization_endpoint of [ + 'javascript:void(0)', + 'file:///tmp/authorize', + 'data:text/plain,authorize', + 'http://insecure.example.com/authorize', + 'vscode://extension/other', + 'command:workbench.action.terminal.new', + 'not a url', + ]) { + it(`refuses to open ${authorization_endpoint} and never calls showDocument`, async () => { + const meta = { authorization_endpoint, token_endpoint: 'https://auth.example.com/token' } + const reg = { client_id: 'cid', redirect_uri: 'http://localhost:12345/oauth/callback' } + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + + try { + await (OAuthClient as any).pkceGrant( + meta, + reg, + new URL('https://mcp.example.com/mcp'), + ['openid'], + 'http://localhost:12345/oauth/callback', + new EventEmitter() + ) + expect.fail('should have thrown') + } catch (e: any) { + expect(e.message).to.include('authorization endpoint') + } + + expect(showDocument.called).to.be.false + expect(fetchStub.called).to.be.false + }) + } +}) + describe('OAuthClient.selectAuthMethod()', () => { const selectAuthMethod = (reg: any, meta?: any) => (OAuthClient as any).selectAuthMethod(reg, meta) @@ -264,6 +349,7 @@ describe('OAuthClient.discoverAS()', () => { sinon.restore() OAuthClient.initialize(fakeWorkspace, fakeLogger as any, fakeLsp) fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + stubPublicDns() }) afterEach(() => sinon.restore()) @@ -358,6 +444,102 @@ describe('OAuthClient.discoverAS()', () => { const result = await (OAuthClient as any).discoverAS(new URL('https://mcp.example.com/mcp')) expect(result.scopes_supported).to.deep.equal(['custom:scope']) }) + + it('ignores a plaintext HTTP authorization server from PRM and never fetches it', async () => { + fetchStub.callsFake(async (url: string) => { + if (url.includes('.well-known/oauth-protected-resource')) { + return { ok: true, json: async () => ({ authorization_servers: ['http://insecure.example.com'] }) } + } + return { ok: false, status: 404, text: async () => '', headers: { get: () => '' } } + }) + + const result = await (OAuthClient as any).discoverAS(new URL('https://mcp.example.com/mcp')) + + // Falls through to synthesized endpoints on the original https server, never the http AS. + expect(result.authorization_endpoint).to.equal('https://mcp.example.com/mcp/authorize') + const fetchedInsecure = fetchStub + .getCalls() + .some((c: sinon.SinonSpyCall) => String(c.args[0]).includes('insecure.example.com')) + expect(fetchedInsecure).to.be.false + }) + + it('ignores a plaintext resource_metadata URL from the WWW-Authenticate header', async () => { + fetchStub.callsFake(async (url: string, init?: any) => { + if (url.includes('.well-known/oauth-protected-resource')) { + return { ok: false, status: 404 } // step 1 (PRM) fails + } + if (init?.method === 'HEAD') { + return { + ok: true, + status: 401, + headers: { + get: (h: string) => + h.toLowerCase() === 'www-authenticate' + ? 'Bearer resource_metadata="http://meta.example.com/meta"' + : '', + }, + } + } + return { ok: false, status: 404, text: async () => '' } // step 3 well-known fails + }) + + const result = await (OAuthClient as any).discoverAS(new URL('https://mcp.example.com/mcp')) + + // The http resource_metadata link is rejected (the host is public, so only the https rule refuses it); + // discovery falls through to synthesized endpoints, and the link is never fetched. + expect(result.authorization_endpoint).to.equal('https://mcp.example.com/mcp/authorize') + const fetchedMeta = fetchStub + .getCalls() + .some((c: sinon.SinonSpyCall) => String(c.args[0]).includes('meta.example.com')) + expect(fetchedMeta).to.be.false + }) +}) + +describe('OAuthClient.fetchASFromResourceMeta()', () => { + beforeEach(() => { + sinon.restore() + OAuthClient.initialize(fakeWorkspace, fakeLogger as any, fakeLsp) + stubPublicDns() + }) + + afterEach(() => sinon.restore()) + + for (const authServer of ['http://auth.example.com', 'ftp://auth.example.com', 'http://169.254.169.254']) { + it(`rejects a non-secure authorization server URL (${authServer}) before fetching`, async () => { + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + try { + await (OAuthClient as any).fetchASFromResourceMeta( + { authorization_server: authServer }, + 'https://mcp.example.com/.well-known/oauth-protected-resource', + new URL('https://mcp.example.com/mcp') + ) + expect.fail('should have thrown') + } catch (e: any) { + expect(e.message).to.include('authorization server') + } + expect(fetchStub.called).to.be.false + }) + } + + it('allows an https authorization server URL', async () => { + const asMeta = { + authorization_endpoint: 'https://auth.example.com/authorize', + token_endpoint: 'https://auth.example.com/token', + } + sinon.stub(OAuthClient as any, 'fetchCompat').callsFake(async (url: unknown) => { + if (String(url).includes('.well-known/oauth-authorization-server')) { + return { ok: true, json: async () => asMeta } + } + return { ok: false, status: 404, text: async () => '' } + }) + + const result = await (OAuthClient as any).fetchASFromResourceMeta( + { authorization_server: 'https://auth.example.com' }, + 'https://mcp.example.com/.well-known/oauth-protected-resource', + new URL('https://mcp.example.com/mcp') + ) + expect(result.token_endpoint).to.equal('https://auth.example.com/token') + }) }) describe('OAuthClient.obtainClient()', () => { @@ -367,6 +549,7 @@ describe('OAuthClient.obtainClient()', () => { sinon.restore() OAuthClient.initialize(fakeWorkspace, fakeLogger as any, fakeLsp) fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + stubPublicDns() sinon.stub(fakeWorkspace.fs, 'exists').resolves(false) sinon.stub(fakeWorkspace.fs, 'readFile').resolves(Buffer.from('{}')) sinon.stub(fakeWorkspace.fs, 'writeFile').resolves() @@ -400,7 +583,8 @@ describe('OAuthClient.obtainClient()', () => { meta, '/tmp/test.registration.json', ['mcp:connect'], - 'http://localhost:12345/oauth/callback' + 'http://localhost:12345/oauth/callback', + new URL('https://mcp.example.com/mcp') ) // DCR body should NOT contain token_endpoint_auth_method or scope @@ -423,12 +607,111 @@ describe('OAuthClient.obtainClient()', () => { } try { - await (OAuthClient as any).obtainClient(meta, '/tmp/test.json', [], 'http://localhost:12345/oauth/callback') + await (OAuthClient as any).obtainClient( + meta, + '/tmp/test.json', + [], + 'http://localhost:12345/oauth/callback', + new URL('https://mcp.example.com/mcp') + ) expect.fail('should have thrown') } catch (e: any) { expect(e.message).to.include('does not support dynamic registration') } }) + + for (const registration_endpoint of ['http://insecure.example.com/register', 'http://169.254.169.254/register']) { + it(`rejects a non-secure registration endpoint (${registration_endpoint}) before sending DCR`, async () => { + const meta = { + authorization_endpoint: 'https://auth.example.com/authorize', + token_endpoint: 'https://auth.example.com/token', + registration_endpoint, + } + + try { + await (OAuthClient as any).obtainClient( + meta, + '/tmp/test.json', + [], + 'http://localhost:12345/oauth/callback', + new URL('https://mcp.example.com/mcp') + ) + expect.fail('should have thrown') + } catch (e: any) { + expect(e.message).to.include('registration endpoint') + } + + expect(fetchStub.called).to.be.false + }) + } +}) + +describe('OAuthClient.obtainClient() with a cached registration', () => { + const meta = { + authorization_endpoint: 'https://auth.example.com/authorize', + token_endpoint: 'https://auth.example.com/token', + registration_endpoint: 'https://auth.example.com/register', + } + const cached = { client_id: 'cached_id', client_secret: 'cached_secret', redirect_uri: 'http://localhost:1/cb' } + let json: sinon.SinonStub + let write: sinon.SinonStub + + beforeEach(() => { + sinon.restore() + OAuthClient.initialize(fakeWorkspace, fakeLogger as any, fakeLsp) + json = sinon.stub(OAuthClient as any, 'json').resolves({ client_id: 'new_id', client_secret: 'new_secret' }) + write = sinon.stub(OAuthClient as any, 'write').resolves() + }) + + afterEach(() => sinon.restore()) + + function obtain(registration: unknown, pinned?: unknown): Promise { + sinon.stub(OAuthClient as any, 'read').resolves(registration) + return (OAuthClient as any).obtainClient( + meta, + '/tmp/test.registration.json', + [], + 'http://localhost:1/cb', + new URL('https://mcp.example.com/mcp'), + pinned + ) + } + + const cases: [string, unknown, unknown, boolean][] = [ + [ + 'issued for the discovered token endpoint', + { ...cached, token_endpoint: meta.token_endpoint }, + undefined, + true, + ], + [ + 'issued for another token endpoint', + { ...cached, token_endpoint: 'https://other.example.com/token' }, + meta, + false, + ], + ['saved without its token endpoint, with a pin for the discovered one', cached, meta, true], + [ + 'saved without its token endpoint, with a pin for another one', + cached, + { token_endpoint: 'https://other.example.com/token' }, + false, + ], + ['saved without its token endpoint, with nothing pinned', cached, undefined, false], + ] + for (const [situation, registration, pinned, reused] of cases) { + it(`${reused ? 'reuses' : 'does not reuse'} a registration ${situation}`, async () => { + const reg = await obtain(registration, pinned) + + expect(reg.client_id).to.equal(reused ? 'cached_id' : 'new_id') + expect(json.called).to.equal(!reused) + if (!reused) { + expect(reg.token_endpoint).to.equal(meta.token_endpoint) + } + // The caller saves the registration after the sign-in with it succeeds + expect(write.called).to.be.false + }) + } }) describe('OAuthClient.discoverProtectedResourceMetadata()', () => { @@ -438,6 +721,7 @@ describe('OAuthClient.discoverProtectedResourceMetadata()', () => { sinon.restore() OAuthClient.initialize(fakeWorkspace, fakeLogger as any, fakeLsp) fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + stubPublicDns() }) afterEach(() => sinon.restore()) @@ -501,6 +785,7 @@ describe('OAuthClient getValidAccessToken()', () => { OAuthClient.initialize(fakeWorkspace, fakeLogger as any, fakeLsp) sinon.stub(OAuthClient as any, 'computeKey').returns('testkey') stubHttpServer() + stubPublicDns() ;(fakeLsp.window.showDocument as sinon.SinonStub).resetHistory() }) @@ -535,7 +820,7 @@ describe('OAuthClient getValidAccessToken()', () => { expect(token).to.be.undefined }) - it('does not send an expired refresh token to a non-loopback HTTP endpoint', async () => { + it('does not send an expired refresh token to a pinned non-loopback HTTP endpoint', async () => { const expiredToken = { access_token: 'expired', expires_in: 1, @@ -547,11 +832,13 @@ describe('OAuthClient getValidAccessToken()', () => { client_secret: 'csecret', redirect_uri: 'http://localhost:12345/oauth/callback', } - stubFileSystem(expiredToken, cachedReg) - sinon.stub(OAuthClient as any, 'discoverAS').resolves({ - authorization_endpoint: 'https://auth.example.com/authorize', - token_endpoint: 'http://auth.example.com/token', + sinon.stub(OAuthClient as any, 'read').callsFake(async (file: unknown) => { + if (String(file).endsWith('.token.json')) return expiredToken + if (String(file).endsWith('.registration.json')) return cachedReg + if (String(file).endsWith('.meta.json')) return { token_endpoint: 'http://auth.example.com/token' } + return undefined }) + const discover = sinon.stub(OAuthClient as any, 'discoverAS') const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') const token = await OAuthClient.getValidAccessToken(new URL('https://api.example.com/mcp'), { @@ -560,6 +847,7 @@ describe('OAuthClient getValidAccessToken()', () => { expect(token).to.be.undefined expect(fetchStub.called).to.be.false + expect(discover.called).to.be.false }) it('uses scopes from discovery metadata when available', async () => { @@ -646,4 +934,923 @@ describe('OAuthClient getValidAccessToken()', () => { const scopesArg = pkceStub.firstCall.args[3] expect(scopesArg).to.deep.equal(['openid', 'offline_access']) }) + + it('pins the discovered endpoints to disk after an interactive sign-in', async () => { + stubFileSystem() + const meta = { + authorization_endpoint: 'https://auth.example.com/authorize', + token_endpoint: 'https://auth.example.com/token', + registration_endpoint: 'https://auth.example.com/register', + } + sinon.stub(OAuthClient as any, 'discoverAS').resolves(meta) + sinon.stub(OAuthClient as any, 'obtainClient').resolves({ + client_id: 'cid', + redirect_uri: 'http://localhost:12345/oauth/callback', + }) + sinon.stub(OAuthClient as any, 'pkceGrant').resolves({ + access_token: 'new_token', + expires_in: 3600, + obtained_at: Date.now(), + }) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + const token = await OAuthClient.getValidAccessToken(new URL('https://api.example.com/mcp'), { + interactive: true, + }) + + expect(token).to.equal('new_token') + const metaWrite = write.getCalls().find((c: sinon.SinonSpyCall) => String(c.args[0]).endsWith('.meta.json')) + expect(metaWrite, 'expected a .meta.json write').to.not.be.undefined + expect(metaWrite!.args[1]).to.deep.equal(meta) + }) + + it('silent refresh reuses the pinned token endpoint and does not re-discover', async () => { + const expiredToken = { + access_token: 'expired', + expires_in: 1, + refresh_token: 'refresh-token', + obtained_at: now - 10_000, + } + const cachedReg = { client_id: 'cid', client_secret: 'csecret' } + const pinnedMeta = { + authorization_endpoint: 'https://auth.example.com/authorize', + token_endpoint: 'https://pinned.example.com/token', + } + + sinon.stub(OAuthClient as any, 'read').callsFake(async (file: unknown) => { + if (String(file).endsWith('.token.json')) return expiredToken + if (String(file).endsWith('.registration.json')) return cachedReg + if (String(file).endsWith('.meta.json')) return pinnedMeta + return undefined + }) + const discover = sinon.stub(OAuthClient as any, 'discoverAS').resolves({ + authorization_endpoint: 'https://discovered.example.com/authorize', + token_endpoint: 'https://discovered.example.com/token', + }) + const refresh = sinon.stub(OAuthClient as any, 'refreshGrant').resolves({ + access_token: 'refreshed_access', + expires_in: 3600, + obtained_at: Date.now(), + }) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + const token = await OAuthClient.getValidAccessToken(new URL('https://api.example.com/mcp'), { + interactive: false, + }) + + expect(token).to.equal('refreshed_access') + expect(discover.called).to.be.false + expect(refresh.calledOnce).to.be.true + // refreshGrant must receive the pinned endpoints, not freshly discovered ones. + expect(refresh.firstCall.args[0]).to.deep.equal(pinnedMeta) + // A valid pin is reused as-is, not rewritten. + const metaWrite = write.getCalls().find((c: sinon.SinonSpyCall) => String(c.args[0]).endsWith('.meta.json')) + expect(metaWrite, 'a valid pin must not be rewritten on silent refresh').to.be.undefined + // The refreshed token is saved with the token endpoint that issued it + const tokenWrite = write.getCalls().find((c: sinon.SinonSpyCall) => String(c.args[0]).endsWith('.token.json')) + expect(tokenWrite?.args[1]).to.include({ + access_token: 'refreshed_access', + token_endpoint: pinnedMeta.token_endpoint, + }) + }) + + for (const [answer, response, saved] of [ + ['without a new refresh token, keeps the current one', {}, 'refresh-token'], + ['with a new refresh token, saves the new one', { refresh_token: 'new-refresh-token' }, 'new-refresh-token'], + ] as const) { + it(`saves a refresh that the authorization server answers ${answer}`, async () => { + const pinnedMeta = { token_endpoint: 'https://pinned.example.com/token' } + sinon.stub(OAuthClient as any, 'read').callsFake(async (file: unknown) => { + if (String(file).endsWith('.token.json')) { + return { + access_token: 'expired', + expires_in: 1, + refresh_token: 'refresh-token', + obtained_at: now - 10_000, + token_endpoint: pinnedMeta.token_endpoint, + } + } + if (String(file).endsWith('.registration.json')) { + return { client_id: 'cid', redirect_uri: 'http://localhost:1/cb' } + } + return String(file).endsWith('.meta.json') ? pinnedMeta : undefined + }) + sinon + .stub(OAuthClient as any, 'refreshGrant') + .resolves({ access_token: 'refreshed_access', expires_in: 3600, obtained_at: now, ...response }) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + const token = await OAuthClient.getValidAccessToken(new URL('https://api.example.com/mcp'), { + interactive: false, + }) + + expect(token).to.equal('refreshed_access') + const tokenWrite = write + .getCalls() + .find((c: sinon.SinonSpyCall) => String(c.args[0]).endsWith('.token.json')) + expect(tokenWrite?.args[1]).to.include({ refresh_token: saved, token_endpoint: pinnedMeta.token_endpoint }) + }) + } + + describe('silent refresh without valid pinned endpoints', () => { + const expiredToken = { + access_token: 'expired', + expires_in: 1, + refresh_token: 'refresh-token', + obtained_at: now - 10_000, + } + const cachedReg = { client_id: 'cid', client_secret: 'csecret' } + + function stubDisk(meta: unknown, reg: unknown = cachedReg): void { + sinon.stub(OAuthClient as any, 'read').callsFake(async (file: unknown) => { + if (String(file).endsWith('.token.json')) return expiredToken + if (String(file).endsWith('.registration.json')) return reg + if (String(file).endsWith('.meta.json')) return meta + return undefined + }) + } + + const cases: [string, unknown, unknown][] = [ + // A token cached before pinning existed has no pinned meta on disk + ['no endpoints were pinned', undefined, cachedReg], + [ + 'the pinned token endpoint is not secure', + { token_endpoint: 'http://insecure.example.com/token' }, + cachedReg, + ], + ['no client registration is saved', { token_endpoint: 'https://pinned.example.com/token' }, null], + [ + 'the client registration is for another token endpoint', + { token_endpoint: 'https://pinned.example.com/token' }, + { ...cachedReg, token_endpoint: 'https://other.example.com/token' }, + ], + ] + for (const [situation, meta, reg] of cases) { + it(`does not send the refresh token or run discovery when ${situation}`, async () => { + stubDisk(meta, reg) + const discover = sinon.stub(OAuthClient as any, 'discoverAS').resolves({ + authorization_endpoint: 'https://discovered.example.com/authorize', + token_endpoint: 'https://discovered.example.com/token', + }) + const refresh = sinon.stub(OAuthClient as any, 'refreshGrant') + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + const token = await OAuthClient.getValidAccessToken(new URL('https://api.example.com/mcp'), { + interactive: false, + }) + + // The caller then asks the user to sign in again, which pins the endpoints that it finds + expect(token).to.be.undefined + expect(discover.called).to.be.false + expect(refresh.called).to.be.false + expect(fetchStub.called).to.be.false + expect(write.called).to.be.false + }) + } + + for (const [failure, outcome] of [ + ['is rejected', () => Promise.resolve(undefined)], + ['throws', () => Promise.reject(new Error('connection reset'))], + ] as const) { + it(`returns no token and does not run discovery when the refresh with the pinned endpoints ${failure}`, async () => { + const pinnedMeta = { token_endpoint: 'https://pinned.example.com/token' } + stubDisk(pinnedMeta) + const discover = sinon.stub(OAuthClient as any, 'discoverAS') + const refresh = sinon.stub(OAuthClient as any, 'refreshGrant').callsFake(outcome) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + const token = await OAuthClient.getValidAccessToken(new URL('https://api.example.com/mcp'), { + interactive: false, + }) + + expect(token).to.be.undefined + expect(refresh.calledOnce).to.be.true + expect(refresh.firstCall.args[0]).to.deep.equal(pinnedMeta) + expect(discover.called).to.be.false + expect(write.called).to.be.false + }) + } + }) + + describe('interactive refresh', () => { + const expiredToken = { + access_token: 'expired', + expires_in: 1, + refresh_token: 'refresh-token', + obtained_at: now - 10_000, + } + const cachedReg = { + client_id: 'cid', + client_secret: 'csecret', + redirect_uri: 'http://localhost:12345/oauth/callback', + } + const pinnedMeta = { + authorization_endpoint: 'https://pinned.example.com/authorize', + token_endpoint: 'https://pinned.example.com/token', + } + const discovered = { + authorization_endpoint: 'https://discovered.example.com/authorize', + token_endpoint: 'https://discovered.example.com/token', + registration_endpoint: 'https://discovered.example.com/register', + } + const browserToken = { access_token: 'browser_access', expires_in: 3600, obtained_at: now } + + function stubDisk(meta: unknown, reg: unknown = cachedReg): void { + sinon.stub(OAuthClient as any, 'read').callsFake(async (file: unknown) => { + if (String(file).endsWith('.token.json')) return expiredToken + if (String(file).endsWith('.registration.json')) return reg + if (String(file).endsWith('.meta.json')) return meta + return undefined + }) + } + + function pinsWritten(write: sinon.SinonStub): unknown[] { + return write + .getCalls() + .filter((c: sinon.SinonSpyCall) => String(c.args[0]).endsWith('.meta.json')) + .map((c: sinon.SinonSpyCall) => c.args[1]) + } + + function signIn(): Promise { + return OAuthClient.getValidAccessToken(new URL('https://api.example.com/mcp'), { interactive: true }) + } + + it('refreshes with the pinned endpoints, does not run discovery, and keeps the pin', async () => { + stubDisk(pinnedMeta) + const discover = sinon.stub(OAuthClient as any, 'discoverAS').resolves(discovered) + const refresh = sinon.stub(OAuthClient as any, 'refreshGrant').resolves({ + access_token: 'refreshed_access', + expires_in: 3600, + obtained_at: now, + }) + const pkce = sinon.stub(OAuthClient as any, 'pkceGrant') + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + expect(await signIn()).to.equal('refreshed_access') + expect(discover.called).to.be.false + expect(pkce.called).to.be.false + expect(refresh.calledOnce).to.be.true + expect(refresh.firstCall.args[0]).to.deep.equal(pinnedMeta) + expect(refresh.firstCall.args[1]).to.deep.equal(cachedReg) + expect(pinsWritten(write)).to.deep.equal([]) + expect(tokensWritten(write)).to.have.length(1) + expect(tokensWritten(write)[0]).to.include({ + access_token: 'refreshed_access', + token_endpoint: pinnedMeta.token_endpoint, + }) + }) + + it('does not send the refresh token when no endpoints were pinned, and pins after the browser sign-in', async () => { + stubDisk(undefined) + sinon.stub(OAuthClient as any, 'discoverAS').resolves(discovered) + sinon.stub(OAuthClient as any, 'obtainClient').resolves(cachedReg) + const refresh = sinon.stub(OAuthClient as any, 'refreshGrant') + const pkce = sinon.stub(OAuthClient as any, 'pkceGrant').resolves(browserToken) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + expect(await signIn()).to.equal('browser_access') + expect(refresh.called).to.be.false + expect(pkce.firstCall.args[0]).to.deep.equal(discovered) + expect(pinsWritten(write)).to.deep.equal([discovered]) + }) + + it('does not send the refresh token to a pinned token endpoint that is not secure', async () => { + stubDisk({ token_endpoint: 'http://insecure.example.com/token' }) + sinon.stub(OAuthClient as any, 'discoverAS').resolves(discovered) + sinon.stub(OAuthClient as any, 'obtainClient').resolves(cachedReg) + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + sinon.stub(OAuthClient as any, 'pkceGrant').resolves(browserToken) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + expect(await signIn()).to.equal('browser_access') + expect(fetchStub.called).to.be.false + expect(pinsWritten(write)).to.deep.equal([discovered]) + }) + + it('does not send the refresh token when no client registration is saved', async () => { + stubDisk(pinnedMeta, null) + sinon.stub(OAuthClient as any, 'discoverAS').resolves(discovered) + sinon.stub(OAuthClient as any, 'obtainClient').resolves(cachedReg) + const refresh = sinon.stub(OAuthClient as any, 'refreshGrant') + sinon.stub(OAuthClient as any, 'pkceGrant').resolves(browserToken) + sinon.stub(OAuthClient as any, 'write').resolves() + + expect(await signIn()).to.equal('browser_access') + expect(refresh.called).to.be.false + }) + + for (const [failure, outcome] of [ + ['is rejected', () => Promise.resolve(undefined)], + ['throws', () => Promise.reject(new Error('connection reset'))], + ] as const) { + it(`signs in in the browser and re-pins when the refresh with the pinned endpoints ${failure}`, async () => { + stubDisk(pinnedMeta) + sinon.stub(OAuthClient as any, 'discoverAS').resolves(discovered) + sinon.stub(OAuthClient as any, 'obtainClient').resolves(cachedReg) + const refresh = sinon.stub(OAuthClient as any, 'refreshGrant').callsFake(outcome) + const pkce = sinon.stub(OAuthClient as any, 'pkceGrant').resolves(browserToken) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + expect(await signIn()).to.equal('browser_access') + // The refresh token went to the pinned token endpoint only, never to a discovered one + expect(refresh.callCount).to.equal(1) + expect(refresh.firstCall.args[0]).to.deep.equal(pinnedMeta) + expect(pkce.firstCall.args[0]).to.deep.equal(discovered) + expect(pinsWritten(write)).to.deep.equal([discovered]) + }) + } + + function registrationsWritten(write: sinon.SinonStub): any[] { + return write + .getCalls() + .filter((c: sinon.SinonSpyCall) => String(c.args[0]).endsWith('.registration.json')) + .map((c: sinon.SinonSpyCall) => c.args[1]) + } + + it('registers again when discovery finds another token endpoint, and saves the registration after the sign-in', async () => { + stubDisk(pinnedMeta, { ...cachedReg, token_endpoint: pinnedMeta.token_endpoint }) + sinon.stub(OAuthClient as any, 'refreshGrant').resolves(undefined) + sinon.stub(OAuthClient as any, 'discoverAS').resolves(discovered) + const json = sinon + .stub(OAuthClient as any, 'json') + .resolves({ client_id: 'new_id', client_secret: 'new_secret' }) + const pkce = sinon.stub(OAuthClient as any, 'pkceGrant').resolves(browserToken) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + expect(await signIn()).to.equal('browser_access') + // The registration of the pinned server, with its client secret, is not used with the discovered endpoints + sinon.assert.calledOnce(json) + expect(pkce.firstCall.args[1].client_id).to.equal('new_id') + const saved = registrationsWritten(write) + expect(saved).to.have.length(1) + expect(saved[0]).to.include({ client_id: 'new_id', token_endpoint: discovered.token_endpoint }) + expect(pinsWritten(write)).to.deep.equal([discovered]) + }) + + it('keeps the saved registration, token and endpoints when the sign-in in the browser fails', async () => { + // An expired registration makes the sign-in register again + stubDisk(pinnedMeta, { ...cachedReg, token_endpoint: pinnedMeta.token_endpoint, expires_at: 1 }) + sinon.stub(OAuthClient as any, 'refreshGrant').resolves(undefined) + sinon.stub(OAuthClient as any, 'discoverAS').resolves(discovered) + sinon.stub(OAuthClient as any, 'json').resolves({ client_id: 'new_id', client_secret: 'new_secret' }) + sinon.stub(OAuthClient as any, 'pkceGrant').rejects(new Error('authorization_timed_out')) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + let error: Error | undefined + try { + await signIn() + } catch (e: any) { + error = e + } + + expect(error?.message).to.include('authorization_timed_out') + expect(registrationsWritten(write)).to.deep.equal([]) + expect(tokensWritten(write)).to.deep.equal([]) + expect(pinsWritten(write)).to.deep.equal([]) + }) + + function tokensWritten(write: sinon.SinonStub): any[] { + return write + .getCalls() + .filter((c: sinon.SinonSpyCall) => String(c.args[0]).endsWith('.token.json')) + .map((c: sinon.SinonSpyCall) => c.args[1]) + } + + // The saved callback port is in use, so the sign-in listens on another port + const otherPortUri = 'http://localhost:23456/oauth/callback' + function savedPortInUse(): void { + sinon.stub(OAuthClient as any, 'listen').callsFake(async (_server: unknown, port: unknown) => { + if (port === 12345) { + throw Object.assign(new Error('listen EADDRINUSE'), { code: 'EADDRINUSE' }) + } + }) + sinon + .stub(OAuthClient as any, 'buildCallbackServer') + .callsFake(async () => ({ server: http.createServer(), redirectUri: otherPortUri })) + } + const samePin = { ...discovered, token_endpoint: pinnedMeta.token_endpoint } + + it('keeps the saved registration when the callback port is in use, and refreshes with it', async () => { + const savedReg = { ...cachedReg, token_endpoint: pinnedMeta.token_endpoint } + stubDisk(pinnedMeta, savedReg) + savedPortInUse() + const rm = sinon.stub(fakeWorkspace.fs, 'rm').resolves() + const refresh = sinon + .stub(OAuthClient as any, 'refreshGrant') + .resolves({ access_token: 'refreshed_access', expires_in: 3600, obtained_at: now }) + const discover = sinon.stub(OAuthClient as any, 'discoverAS') + sinon.stub(OAuthClient as any, 'write').resolves() + + expect(await signIn()).to.equal('refreshed_access') + sinon.assert.notCalled(rm) + sinon.assert.notCalled(discover) + expect(refresh.firstCall.args[1]).to.deep.equal(savedReg) + }) + + it('registers again for the new callback port when the saved port is in use', async () => { + stubDisk(pinnedMeta, { ...cachedReg, token_endpoint: pinnedMeta.token_endpoint }) + savedPortInUse() + const rm = sinon.stub(fakeWorkspace.fs, 'rm').resolves() + sinon.stub(OAuthClient as any, 'refreshGrant').resolves(undefined) + sinon.stub(OAuthClient as any, 'discoverAS').resolves(samePin) + const json = sinon + .stub(OAuthClient as any, 'json') + .resolves({ client_id: 'new_id', client_secret: 'new_secret' }) + const pkce = sinon.stub(OAuthClient as any, 'pkceGrant').resolves(browserToken) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + expect(await signIn()).to.equal('browser_access') + sinon.assert.notCalled(rm) + // The authorization server knows the saved registration only with the redirect URI of the saved port + sinon.assert.calledOnce(json) + expect(pkce.firstCall.args[1]).to.include({ client_id: 'new_id', redirect_uri: otherPortUri }) + expect(registrationsWritten(write)).to.deep.equal([ + { ...pkce.firstCall.args[1], token_endpoint: pinnedMeta.token_endpoint }, + ]) + }) + + it('keeps the saved registration when the saved port is in use and the sign-in in the browser fails', async () => { + stubDisk(pinnedMeta, { ...cachedReg, token_endpoint: pinnedMeta.token_endpoint }) + savedPortInUse() + const rm = sinon.stub(fakeWorkspace.fs, 'rm').resolves() + sinon.stub(OAuthClient as any, 'refreshGrant').resolves(undefined) + sinon.stub(OAuthClient as any, 'discoverAS').resolves(samePin) + sinon.stub(OAuthClient as any, 'json').resolves({ client_id: 'new_id', client_secret: 'new_secret' }) + sinon.stub(OAuthClient as any, 'pkceGrant').rejects(new Error('authorization_timed_out')) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + let error: Error | undefined + try { + await signIn() + } catch (e: any) { + error = e + } + + expect(error?.message).to.include('authorization_timed_out') + sinon.assert.notCalled(rm) + expect(registrationsWritten(write)).to.deep.equal([]) + expect(tokensWritten(write)).to.deep.equal([]) + expect(pinsWritten(write)).to.deep.equal([]) + }) + + it('saves a registration from an earlier version with the token endpoint of the pin that it was used with', async () => { + // The registration has no token endpoint; the pin and the discovery name the same one + stubDisk(pinnedMeta, cachedReg) + sinon.stub(OAuthClient as any, 'refreshGrant').resolves(undefined) + sinon.stub(OAuthClient as any, 'discoverAS').resolves(samePin) + const json = sinon.stub(OAuthClient as any, 'json') + const pkce = sinon.stub(OAuthClient as any, 'pkceGrant').resolves(browserToken) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + expect(await signIn()).to.equal('browser_access') + sinon.assert.notCalled(json) + expect(pkce.firstCall.args[1]).to.deep.equal(cachedReg) + expect(registrationsWritten(write)).to.deep.equal([ + { ...cachedReg, token_endpoint: pinnedMeta.token_endpoint }, + ]) + }) + + it('saves the token with the token endpoint that issued it', async () => { + stubDisk(undefined) + sinon.stub(OAuthClient as any, 'discoverAS').resolves(discovered) + sinon.stub(OAuthClient as any, 'obtainClient').resolves(cachedReg) + sinon.stub(OAuthClient as any, 'pkceGrant').resolves(browserToken) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + await signIn() + + expect(tokensWritten(write)).to.deep.equal([{ ...browserToken, token_endpoint: discovered.token_endpoint }]) + }) + + // For example after a sign-in whose token was saved but whose registration and endpoints were not + function stubTokenFromOtherEndpoint(): void { + sinon.stub(OAuthClient as any, 'read').callsFake(async (file: unknown) => { + if (String(file).endsWith('.token.json')) { + return { ...expiredToken, token_endpoint: 'https://other.example.com/token' } + } + if (String(file).endsWith('.registration.json')) { + return { ...cachedReg, token_endpoint: pinnedMeta.token_endpoint } + } + if (String(file).endsWith('.meta.json')) { + return pinnedMeta + } + return undefined + }) + } + + it('does not send a refresh token that another token endpoint issued to the pinned one', async () => { + stubTokenFromOtherEndpoint() + const refresh = sinon.stub(OAuthClient as any, 'refreshGrant') + const discover = sinon.stub(OAuthClient as any, 'discoverAS') + + const token = await OAuthClient.getValidAccessToken(new URL('https://api.example.com/mcp'), { + interactive: false, + }) + + expect(token).to.be.undefined + sinon.assert.notCalled(refresh) + sinon.assert.notCalled(discover) + }) + + it('signs in in the browser instead of sending a refresh token that another token endpoint issued', async () => { + stubTokenFromOtherEndpoint() + const refresh = sinon.stub(OAuthClient as any, 'refreshGrant') + sinon.stub(OAuthClient as any, 'discoverAS').resolves(samePin) + const pkce = sinon.stub(OAuthClient as any, 'pkceGrant').resolves(browserToken) + const write = sinon.stub(OAuthClient as any, 'write').resolves() + + expect(await signIn()).to.equal('browser_access') + sinon.assert.notCalled(refresh) + expect(pkce.firstCall.args[0]).to.deep.equal(samePin) + expect(tokensWritten(write)).to.deep.equal([{ ...browserToken, token_endpoint: pinnedMeta.token_endpoint }]) + }) + }) +}) + +describe('OAuthClient check of the addresses that an MCP server sends the client to', () => { + const publicServer = new URL('https://mcp.example.com/mcp') + let lookup: sinon.SinonStub + + beforeEach(() => { + sinon.restore() + OAuthClient.initialize(fakeWorkspace, fakeLogger as any, fakeLsp) + lookup = sinon.stub(OAuthClient as any, 'lookupHost').callsFake(async (host: unknown) => { + const addresses: Record = { + 'mcp.example.com': ['93.184.216.34'], + 'auth.example.com': ['93.184.216.35'], + 'metadata.example.com': ['169.254.169.254'], + 'intranet.example.com': ['10.1.2.3'], + 'mixed.example.com': ['93.184.216.36', '192.168.0.10'], + 'mcp.corp.example.com': ['10.0.0.2'], + 'sso.corp.example.com': ['10.0.0.3'], + // MCP server names that add addresses close to this computer to a public one + 'mcp.mixed.example.com': ['93.184.216.34', '127.0.0.1'], + 'mcp.metadata.example.com': ['93.184.216.34', '169.254.169.254'], + 'mcp.split.example.com': ['10.0.0.4', '127.0.0.1'], + } + const found = addresses[String(host)] + if (!found) { + throw Object.assign(new Error(`getaddrinfo ENOTFOUND ${host}`), { code: 'ENOTFOUND' }) + } + return found.map(address => ({ address, family: 4 })) + }) + }) + + afterEach(() => sinon.restore()) + + describe('hostScope()', () => { + const cases: [string, string | undefined][] = [ + ['https://93.184.216.34/', 'public'], + ['https://[2606:4700::1111]/', 'public'], + ['https://172.32.0.1/', 'public'], + ['https://100.128.0.1/', 'public'], + ['https://10.0.0.5/', 'private'], + ['https://172.16.0.1/', 'private'], + ['https://172.31.255.255/', 'private'], + ['https://192.168.1.1/', 'private'], + ['https://100.64.0.1/', 'private'], + ['https://[fd12:3456::1]/', 'private'], + ['https://127.0.0.1/', 'loopback'], + ['https://127.1.2.3/', 'loopback'], + ['https://0x7f.1/', 'loopback'], + ['https://2130706433/', 'loopback'], + ['https://[::1]/', 'loopback'], + ['https://[::ffff:127.0.0.1]/', 'loopback'], + ['https://localhost/', 'loopback'], + ['https://app.localhost./', 'loopback'], + ['https://169.254.169.254/', 'blocked'], + ['https://0.0.0.0/', 'blocked'], + ['https://224.0.0.1/', 'blocked'], + ['https://255.255.255.255/', 'blocked'], + ['https://[::]/', 'blocked'], + ['https://[fe80::1]/', 'blocked'], + ['https://[ff02::1]/', 'blocked'], + ['https://[::ffff:169.254.169.254]/', 'blocked'], + ['https://[64:ff9b::a9fe:a9fe]/', 'blocked'], + ['https://auth.example.com/', 'public'], + ['https://intranet.example.com/', 'private'], + ['https://metadata.example.com/', 'blocked'], + ['https://mixed.example.com/', 'private'], + ['https://unknown.example.com/', undefined], + ] + for (const [url, scope] of cases) { + it(`classifies ${url} as ${scope ?? 'unknown'}`, async () => { + expect(await (OAuthClient as any).hostScope(new URL(url).hostname)).to.equal(scope) + }) + } + + it('does not resolve an IP address or a localhost name', async () => { + await (OAuthClient as any).hostScope('10.0.0.5') + await (OAuthClient as any).hostScope('[::1]') + await (OAuthClient as any).hostScope('localhost') + expect(lookup.called).to.be.false + }) + }) + + describe('assertAllowedTarget()', () => { + const cases: [string, string, boolean][] = [ + // A public MCP server may send the client to public addresses only + ['https://mcp.example.com/mcp', 'https://auth.example.com/token', true], + ['https://mcp.example.com/mcp', 'https://10.0.0.5/token', false], + ['https://mcp.example.com/mcp', 'https://intranet.example.com/token', false], + ['https://mcp.example.com/mcp', 'https://mixed.example.com/token', false], + ['https://mcp.example.com/mcp', 'http://127.0.0.1:8080/token', false], + ['https://mcp.example.com/mcp', 'http://localhost:8080/token', false], + ['https://mcp.example.com/mcp', 'https://169.254.169.254/', false], + ['https://mcp.example.com/mcp', 'https://metadata.example.com/', false], + // An MCP server on a private network may also use private addresses + ['https://mcp.corp.example.com/mcp', 'https://sso.corp.example.com/token', true], + ['https://mcp.corp.example.com/mcp', 'https://auth.example.com/token', true], + ['https://mcp.corp.example.com/mcp', 'http://127.0.0.1:8080/token', false], + ['https://mcp.corp.example.com/mcp', 'https://169.254.169.254/', false], + // An MCP server on this computer may also use loopback addresses + ['http://localhost:3000/mcp', 'http://127.0.0.1:8080/token', true], + ['http://localhost:3000/mcp', 'https://10.0.0.5/token', true], + ['http://localhost:3000/mcp', 'https://[fe80::1]/token', false], + // The host that the user chose is allowed + ['https://10.9.9.9/mcp', 'https://10.9.9.9:8443/token', true], + // The address of the MCP server farthest from this computer decides where it may send the client + ['https://mcp.mixed.example.com/mcp', 'http://127.0.0.1:8080/token', false], + ['https://mcp.mixed.example.com/mcp', 'https://10.0.0.5/token', false], + ['https://mcp.metadata.example.com/mcp', 'https://10.0.0.5/token', false], + ['https://mcp.split.example.com/mcp', 'https://10.0.0.5/token', true], + ['https://mcp.split.example.com/mcp', 'http://127.0.0.1:8080/token', false], + ['https://169.254.1.1/mcp', 'https://10.0.0.5/token', false], + ] + for (const [server, target, allowed] of cases) { + it(`${allowed ? 'allows' : 'rejects'} ${target} for the MCP server ${server}`, async () => { + const check = (OAuthClient as any).assertAllowedTarget(new URL(target), new URL(server), 'endpoint') + if (allowed) { + await check + } else { + try { + await check + expect.fail('should have thrown') + } catch (e: any) { + expect(e.message).to.match(/^OAuth: endpoint is on a .* address/) + } + } + }) + } + + it('rejects a host name that does not resolve, unless it is the host of the MCP server', async () => { + try { + await (OAuthClient as any).assertAllowedTarget( + new URL('https://unknown.example.com/token'), + publicServer, + 'token endpoint' + ) + expect.fail('should have thrown') + } catch (e: any) { + expect(e.message).to.equal('OAuth: token endpoint has a host name that could not be resolved') + } + await (OAuthClient as any).assertAllowedTarget( + new URL('https://unknown.example.com/token'), + new URL('https://unknown.example.com/mcp'), + 'token endpoint' + ) + }) + }) + + describe('discoveryFetch()', () => { + function redirectTo(location: string) { + return { + ok: false, + status: 302, + headers: { get: (name: string) => (name === 'location' ? location : null) }, + } + } + const done = { ok: true, status: 200, headers: { get: () => null }, json: async () => ({}) } + + it('follows a redirect itself and checks its target', async () => { + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + fetchStub.onFirstCall().resolves(redirectTo('/.well-known/oauth-authorization-server/next')) + fetchStub.onSecondCall().resolves(done) + + const response = await (OAuthClient as any).discoveryFetch( + 'https://auth.example.com/.well-known/oauth-authorization-server', + publicServer, + 'authorization server' + ) + + expect(response).to.equal(done) + expect(fetchStub.secondCall.args[0]).to.equal( + 'https://auth.example.com/.well-known/oauth-authorization-server/next' + ) + for (const call of fetchStub.getCalls()) { + expect(call.args[1].redirect).to.equal('manual') + } + }) + + for (const location of [ + 'http://169.254.169.254/metadata', + 'https://10.0.0.5/internal', + 'https://metadata.example.com/', + 'http://auth.example.com/plaintext', + 'file:///tmp/authorize', + ]) { + it(`does not follow a redirect to ${location}`, async () => { + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat').resolves(redirectTo(location)) + try { + await (OAuthClient as any).discoveryFetch( + 'https://auth.example.com/.well-known/openid-configuration', + publicServer, + 'authorization server' + ) + expect.fail('should have thrown') + } catch (e: any) { + expect(e.message).to.include('authorization server') + } + expect(fetchStub.calledOnce).to.be.true + }) + } + + it('stops after five redirects', async () => { + const cancel = sinon.stub().resolves() + const fetchStub = sinon + .stub(OAuthClient as any, 'fetchCompat') + .resolves({ ...redirectTo('https://auth.example.com/again'), body: { cancel } }) + try { + await (OAuthClient as any).discoveryFetch( + 'https://auth.example.com/', + publicServer, + 'resource metadata' + ) + expect.fail('should have thrown') + } catch (e: any) { + expect(e.message).to.include('too many redirects') + } + expect(fetchStub.callCount).to.equal(6) + // The body of the last redirect response is released too + expect(cancel.callCount).to.equal(6) + }) + + it('releases a Node stream body, as the fallback fetch returns', async () => { + const destroy = sinon.stub() + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + fetchStub.onFirstCall().resolves({ ...redirectTo('/next'), body: { destroy } }) + fetchStub.onSecondCall().resolves(done) + + await (OAuthClient as any).discoveryFetch('https://auth.example.com/', publicServer, 'authorization server') + + sinon.assert.calledOnce(destroy) + }) + + it('releases the body of a redirect response before it follows the redirect', async () => { + const cancel = sinon.stub().resolves() + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + fetchStub.onFirstCall().resolves({ ...redirectTo('/next'), body: { cancel } }) + fetchStub.onSecondCall().resolves(done) + + await (OAuthClient as any).discoveryFetch('https://auth.example.com/', publicServer, 'authorization server') + + sinon.assert.calledOnce(cancel) + sinon.assert.callOrder(cancel, fetchStub.withArgs('https://auth.example.com/next')) + }) + + describe('over http, for an MCP server on this computer', () => { + const loopbackServer = new URL('http://localhost:3000/mcp') + + it('follows a redirect to a loopback address', async () => { + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + fetchStub.onFirstCall().resolves(redirectTo('http://127.0.0.1:9/metadata')) + fetchStub.onSecondCall().resolves(done) + + const response = await (OAuthClient as any).discoveryFetch( + 'http://localhost:3000/.well-known/oauth-protected-resource', + loopbackServer, + 'resource metadata' + ) + + expect(response).to.equal(done) + expect(fetchStub.secondCall.args[0]).to.equal('http://127.0.0.1:9/metadata') + }) + + it('does not follow a redirect over http to another host', async () => { + const fetchStub = sinon + .stub(OAuthClient as any, 'fetchCompat') + .resolves(redirectTo('http://other.example.com/metadata')) + try { + await (OAuthClient as any).discoveryFetch( + 'http://localhost:3000/.well-known/oauth-protected-resource', + loopbackServer, + 'resource metadata' + ) + expect.fail('should have thrown') + } catch (e: any) { + expect(e.message).to.equal( + 'OAuth: resource metadata must use HTTPS unless it is a loopback address' + ) + } + expect(fetchStub.calledOnce).to.be.true + }) + }) + }) + + it('sends the client registration once, and fails on a redirect', async () => { + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat').resolves({ + ok: true, + status: 201, + json: async () => ({ client_id: 'new_id' }), + }) + sinon.stub(fakeWorkspace.fs, 'exists').resolves(false) + sinon.stub(fakeWorkspace.fs, 'writeFile').resolves() + sinon.stub(fakeWorkspace.fs, 'mkdir').resolves() + + await (OAuthClient as any).obtainClient( + { + authorization_endpoint: 'https://auth.example.com/authorize', + token_endpoint: 'https://auth.example.com/token', + registration_endpoint: 'https://auth.example.com/register', + }, + '/tmp/test.json', + [], + 'http://localhost:12345/oauth/callback', + publicServer + ) + + sinon.assert.calledOnce(fetchStub) + expect(fetchStub.firstCall.args[1]).to.include({ method: 'POST', redirect: 'error' }) + }) + + it('discovery skips an authorization server on a private address for a public MCP server', async () => { + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat').callsFake(async (url: unknown) => { + if (String(url).includes('oauth-protected-resource')) { + return { ok: true, status: 200, json: async () => ({ authorization_servers: ['https://10.0.0.5'] }) } + } + return { ok: false, status: 404, headers: { get: () => null }, text: async () => '' } + }) + + const meta = await (OAuthClient as any).discoverAS(publicServer) + + expect(fetchStub.getCalls().some((c: sinon.SinonSpyCall) => String(c.args[0]).includes('10.0.0.5'))).to.be.false + // Discovery falls through to the endpoints on the MCP server itself + expect(meta.token_endpoint).to.equal('https://mcp.example.com/mcp/access_token') + }) + + it('does not send the client registration to a link-local address', async () => { + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + sinon.stub(fakeWorkspace.fs, 'exists').resolves(false) + try { + await (OAuthClient as any).obtainClient( + { + authorization_endpoint: 'https://auth.example.com/authorize', + token_endpoint: 'https://auth.example.com/token', + registration_endpoint: 'https://169.254.169.254/register', + }, + '/tmp/test.json', + [], + 'http://localhost:12345/oauth/callback', + publicServer + ) + expect.fail('should have thrown') + } catch (e: any) { + expect(e.message).to.include('registration endpoint') + } + expect(fetchStub.called).to.be.false + }) + + it('does not send the refresh token to a token endpoint on a private address', async () => { + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + try { + await (OAuthClient as any).refreshGrant( + { + authorization_endpoint: 'https://auth.example.com/authorize', + token_endpoint: 'https://10.0.0.5/token', + }, + { client_id: 'cid', redirect_uri: 'http://localhost:12345/oauth/callback' }, + publicServer, + 'refresh-token' + ) + expect.fail('should have thrown') + } catch (e: any) { + expect(e.message).to.include('token endpoint') + } + expect(fetchStub.called).to.be.false + }) + + it('does not open the browser when the token endpoint is on a loopback address of a public MCP server', async () => { + const showDocument = sinon.stub().resolves({ success: true }) + OAuthClient.initialize(fakeWorkspace, fakeLogger as any, { window: { showDocument } } as any) + const fetchStub = sinon.stub(OAuthClient as any, 'fetchCompat') + try { + await (OAuthClient as any).pkceGrant( + { + authorization_endpoint: 'https://auth.example.com/authorize', + token_endpoint: 'http://127.0.0.1:9/token', + }, + { client_id: 'cid', redirect_uri: 'http://localhost:12345/oauth/callback' }, + publicServer, + ['openid'], + 'http://localhost:12345/oauth/callback', + new EventEmitter() + ) + expect.fail('should have thrown') + } catch (e: any) { + expect(e.message).to.include('token endpoint') + } + expect(showDocument.called).to.be.false + expect(fetchStub.called).to.be.false + }) }) diff --git a/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.ts b/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.ts index fe17d37879..1eb93c9585 100644 --- a/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.ts +++ b/server/aws-lsp-codewhisperer/src/language-server/agenticChat/tools/mcp/mcpOauthClient.ts @@ -4,10 +4,11 @@ */ import * as crypto from 'crypto' +import { promises as dns } from 'dns' import * as path from 'path' import { URL, URLSearchParams } from 'url' import * as http from 'http' -import { isIPv4 } from 'net' +import { isIPv4, isIPv6 } from 'net' import * as os from 'os' import { Logger, Workspace, Lsp } from '@aws/language-server-runtimes/server-interface' @@ -16,6 +17,8 @@ interface Token { expires_in: number refresh_token?: string obtained_at: number + /** The token endpoint that issued the token. A refresh sends the refresh token only to that endpoint. */ + token_endpoint?: string } interface Meta { @@ -32,6 +35,92 @@ interface Registration { expires_at?: number redirect_uri: string token_endpoint_auth_method?: string + /** + * The token endpoint of the authorization server that issued the registration. The registration is used only + * with that token endpoint, so that its client secret does not go to another server. + */ + token_endpoint?: string +} + +/** + * Where an address is, for the check of the URLs that an MCP server supplies during an OAuth sign-in (see + * OAuthClient.assertAllowedTarget). 'blocked' covers addresses that no MCP server may send the client to. + */ +type AddressScope = 'public' | 'private' | 'loopback' | 'blocked' + +/** Order of the scopes that an MCP server may send the client to. A higher rank is closer to this computer. */ +const SCOPE_RANK: Record, number> = { public: 0, private: 1, loopback: 2 } + +/** Redirects that the client follows for one discovery request */ +const MAX_DISCOVERY_REDIRECTS = 5 + +function ipv4Scope([a, b, c]: number[]): AddressScope { + if (a === 127) { + return 'loopback' + } + // "This network", link-local, multicast, reserved and broadcast + if (a === 0 || (a === 169 && b === 254) || a >= 224) { + return 'blocked' + } + if ( + a === 10 || + (a === 172 && b >= 16 && b <= 31) || + (a === 192 && b === 168) || + (a === 100 && b >= 64 && b <= 127) || // shared address space (carrier-grade NAT) + (a === 192 && b === 0 && c === 0) || // IETF protocol assignments + (a === 198 && (b === 18 || b === 19)) // benchmarking + ) { + return 'private' + } + return 'public' +} + +/** The eight 16-bit groups of an IPv6 address, or undefined if the text is not one */ +function ipv6Groups(address: string): number[] | undefined { + let host: string + try { + // The URL parser writes the address in canonical form, with an embedded IPv4 address in hexadecimal + host = new URL(`http://[${address.replace(/%.*$/, '')}]/`).hostname.slice(1, -1) + } catch { + return undefined + } + const [head, tail] = host.split('::') + const left = head ? head.split(':') : [] + const right = tail ? tail.split(':') : [] + const zeros = tail === undefined ? 0 : 8 - left.length - right.length + const groups = [...left, ...Array(Math.max(zeros, 0)).fill('0'), ...right].map(g => parseInt(g, 16)) + return groups.length === 8 && groups.every(g => g >= 0 && g <= 0xffff) ? groups : undefined +} + +function ipv6Scope(g: number[]): AddressScope { + if (g.every(x => x === 0)) { + return 'blocked' // unspecified + } + if (g.slice(0, 7).every(x => x === 0) && g[7] === 1) { + return 'loopback' + } + const embedsIPv4 = + (g.slice(0, 5).every(x => x === 0) && (g[5] === 0xffff || g[5] === 0)) || // IPv4-mapped and IPv4-compatible + (g[0] === 0x64 && g[1] === 0xff9b && g.slice(2, 6).every(x => x === 0)) // NAT64 + if (embedsIPv4) { + return ipv4Scope([g[6] >> 8, g[6] & 0xff, g[7] >> 8, g[7] & 0xff]) + } + if ((g[0] & 0xffc0) === 0xfe80 || (g[0] & 0xff00) === 0xff00) { + return 'blocked' // link-local, multicast + } + if ((g[0] & 0xfe00) === 0xfc00 || (g[0] & 0xffc0) === 0xfec0) { + return 'private' // unique local, site-local + } + return 'public' +} + +/** The scope of an IP address, or undefined if the text is not an IP address */ +function addressScope(address: string): AddressScope | undefined { + if (isIPv4(address)) { + return ipv4Scope(address.split('.').map(Number)) + } + const groups = isIPv6(address) ? ipv6Groups(address) : undefined + return groups ? ipv6Scope(groups) : undefined } export class OAuthClient { @@ -57,6 +146,7 @@ export class OAuthClient { const key = this.computeKey(mcpBase) const regPath = path.join(this.cacheDir, `${key}.registration.json`) const tokPath = path.join(this.cacheDir, `${key}.token.json`) + const metaPath = path.join(this.cacheDir, `${key}.meta.json`) // Silent branch: try cached token, then refresh, never opens a browser if (!interactive) { @@ -70,18 +160,21 @@ export class OAuthClient { this.logger.info('OAuth: cached token expired, trying refresh (silent)') } - const savedReg = await this.read(regPath) - if (cachedTok?.refresh_token && savedReg) { - try { - const meta = await this.discoverAS(mcpBase) - const refreshed = await this.refreshGrant(meta, savedReg, mcpBase, cachedTok.refresh_token) - if (refreshed) { - await this.write(tokPath, refreshed) - this.logger.info('OAuth: refresh grant succeeded (silent)') - return refreshed.access_token - } - } catch (e) { - this.logger.warn(`OAuth: silent refresh failed — ${e instanceof Error ? e.message : String(e)}`) + // A silent refresh uses only the endpoints and the client registration of the last sign-in in the + // browser, so the refresh token goes only to the token endpoint that the user signed in with. With + // nothing pinned, for example for a token cached before pinning existed, nothing is sent and the user + // signs in in the browser once. + if (cachedTok?.refresh_token) { + const refreshed = await this.refreshWithPinnedEndpoints( + await this.readPinnedMeta(metaPath), + regPath, + mcpBase, + cachedTok + ) + if (refreshed) { + await this.write(tokPath, refreshed) + this.logger.info('OAuth: refresh grant succeeded (silent)') + return refreshed.access_token } } @@ -107,8 +200,9 @@ export class OAuthClient { /* ignore */ } this.logger.warn(`OAuth: port ${port} in use, falling back to new random port`) + // The saved registration stays: the refresh below can still use it, and a sign-in in the browser + // registers a client for the new port (obtainClient) ;({ server, redirectUri } = await this.buildCallbackServer()) - await this.workspace.fs.rm(regPath) } else { throw e } @@ -127,6 +221,19 @@ export class OAuthClient { } } + // Try the refresh token before opening a browser, but only with the endpoints and the client + // registration of the last sign-in, so the refresh token goes only to the token endpoint that the + // user signed in with. + const pinned = await this.readPinnedMeta(metaPath) + if (cached?.refresh_token) { + const refreshed = await this.refreshWithPinnedEndpoints(pinned, regPath, mcpBase, cached) + if (refreshed) { + await this.write(tokPath, refreshed) + this.logger.info('OAuth: refresh grant succeeded') + return refreshed.access_token + } + } + const meta = await this.discoverAS(mcpBase) // Use scopes from discovery metadata, fall back to OIDC defaults @@ -135,21 +242,15 @@ export class OAuthClient { ? meta.scopes_supported : ['openid', 'offline_access'] - const reg = await this.obtainClient(meta, regPath, scopes, redirectUri) + const reg = await this.obtainClient(meta, regPath, scopes, redirectUri, mcpBase, pinned) - // Try refresh-token grant first - if (cached?.refresh_token) { - const refreshed = await this.refreshGrant(meta, reg, mcpBase, cached.refresh_token) - if (refreshed) { - await this.write(tokPath, refreshed) - this.logger.info('OAuth: refresh grant succeeded') - return refreshed.access_token - } - } - - // PKCE interactive flow + // PKCE flow in the browser. The token, the client registration and the endpoints found now are saved only + // after it succeeds, each with the token endpoint, so that a failed sign-in leaves those of the last one and + // a refresh can check that they belong together. const fresh = await this.pkceGrant(meta, reg, mcpBase, scopes, redirectUri, server) - await this.write(tokPath, fresh) + await this.write(tokPath, { ...fresh, token_endpoint: meta.token_endpoint }) + await this.write(regPath, { ...reg, token_endpoint: meta.token_endpoint }) + await this.write(metaPath, meta) return fresh.access_token } finally { if (server) { @@ -166,6 +267,80 @@ export class OAuthClient { return { server, redirectUri: `http://localhost:${port}/oauth/callback` } } + /** The port of a callback URI, or undefined when the URI does not parse */ + private static callbackPort(uri: string): string | undefined { + try { + return new URL(uri).port + } catch { + return undefined + } + } + + /** + * The endpoints pinned at the last sign-in, or undefined when none were pinned (a token cached before + * pinning existed) or when the pinned token endpoint is not secure. + */ + private static async readPinnedMeta(metaPath: string): Promise { + const pinned = await this.read(metaPath) + if (!pinned?.token_endpoint) { + return undefined + } + try { + this.validateTokenEndpoint(pinned.token_endpoint) + return pinned + } catch (e) { + this.logger.warn( + `OAuth: ignoring invalid pinned token endpoint — ${e instanceof Error ? e.message : String(e)}` + ) + return undefined + } + } + + /** + * One refresh_token grant, for a silent refresh or before a sign-in in the browser, with the pinned endpoints and + * the client registration on disk only. Returns undefined, and sends nothing, when no valid endpoints were pinned, + * no registration is saved, or the registration or the token was issued for another token endpoint than the + * pinned one. Also returns undefined when the grant fails. This never falls back to discovery, and it never changes + * the pin: only a sign-in in the browser pins endpoints, so a token cached before pinning existed needs one sign-in + * in the browser. + */ + private static async refreshWithPinnedEndpoints( + pinned: Meta | undefined, + regPath: string, + mcpBase: URL, + token: Token + ): Promise { + const reg = await this.read(regPath) + if (!pinned || !reg || !token.refresh_token) { + this.logger.info('OAuth: no pinned endpoints or client registration; skipping the refresh grant') + return undefined + } + // A token or a registration that records its token endpoint is used only with that endpoint + const issuedFor = [reg.token_endpoint, token.token_endpoint] + if (issuedFor.some(endpoint => endpoint !== undefined && endpoint !== pinned.token_endpoint)) { + this.logger.info( + 'OAuth: the client registration or the token is for another token endpoint; skipping the refresh grant' + ) + return undefined + } + try { + const refreshed = await this.refreshGrant(pinned, reg, mcpBase, token.refresh_token) + // An authorization server can answer a refresh without a new refresh token. The current one then stays valid. + return ( + refreshed && { + ...refreshed, + refresh_token: refreshed.refresh_token ?? token.refresh_token, + token_endpoint: pinned.token_endpoint, + } + ) + } catch (e) { + this.logger.warn( + `OAuth: refresh grant with the pinned endpoints failed — ${e instanceof Error ? e.message : String(e)}` + ) + return undefined + } + } + /** * Discover OAuth endpoints using the following chain (aligned with MCP SDK): * 1. RFC 9728 Protected Resource Metadata @@ -180,7 +355,8 @@ export class OAuthClient { if (prm) { const asUrl = prm.authorization_servers?.[0] if (asUrl) { - const asMeta = await this.discoverAuthorizationServerMetadata(new URL(asUrl)) + this.assertSecureEndpoint(String(asUrl), 'authorization server') + const asMeta = await this.discoverAuthorizationServerMetadata(new URL(asUrl), rs) if (asMeta) { if (!asMeta.scopes_supported && prm.scopes_supported) { asMeta.scopes_supported = prm.scopes_supported @@ -195,20 +371,22 @@ export class OAuthClient { // Step 2: HEAD → WWW-Authenticate → resource_metadata link try { - const h = await this.fetchCompat(rs.toString(), { method: 'HEAD' }) + const h = await this.discoveryFetch(rs.toString(), rs, 'MCP server', { method: 'HEAD' }) const header = h.headers.get('www-authenticate') || '' const m = /resource_metadata=(?:"([^"]+)"|([^,\s]+))/i.exec(header) if (m) { const metaUrl = new URL(m[1] || m[2], rs).toString() - const raw = await this.json(metaUrl) - return await this.fetchASFromResourceMeta(raw, metaUrl) + // The resource_metadata link comes from a server-supplied header; require a secure transport. + this.assertSecureEndpoint(metaUrl, 'resource metadata') + const raw = await this.json(metaUrl, rs, 'resource metadata') + return await this.fetchASFromResourceMeta(raw, metaUrl, rs) } } catch (e) { this.logger.info(`OAuth: WWW-Authenticate discovery failed — ${e instanceof Error ? e.message : String(e)}`) } // Step 3: Well-known endpoints on the resource server - const asMeta = await this.discoverAuthorizationServerMetadata(new URL('/', rs)) + const asMeta = await this.discoverAuthorizationServerMetadata(new URL('/', rs), rs) if (asMeta) { return asMeta } @@ -235,7 +413,7 @@ export class OAuthClient { for (const url of urlsToTry) { try { - const resp = await this.fetchCompat(url, { + const resp = await this.discoveryFetch(url, rs, 'resource metadata', { headers: { 'MCP-Protocol-Version': '2025-03-26' }, }) if (resp.ok) { @@ -252,7 +430,7 @@ export class OAuthClient { * Discover Authorization Server Metadata via RFC 8414 and OIDC Discovery. * Matches the MCP SDK's buildDiscoveryUrls pattern. */ - private static async discoverAuthorizationServerMetadata(asUrl: URL): Promise { + private static async discoverAuthorizationServerMetadata(asUrl: URL, rs: URL): Promise { const pathname = asUrl.pathname.endsWith('/') ? asUrl.pathname.slice(0, -1) : asUrl.pathname const hasPath = pathname !== '' && pathname !== '/' @@ -268,7 +446,7 @@ export class OAuthClient { for (const url of urlsToTry) { try { - return await this.json(url) + return await this.json(url, rs, 'authorization server') } catch { // Try next URL } @@ -277,7 +455,7 @@ export class OAuthClient { } /** Follow authorization_server(s) in resource_metadata JSON */ - private static async fetchASFromResourceMeta(raw: any, metaUrl: string): Promise { + private static async fetchASFromResourceMeta(raw: any, metaUrl: string, rs: URL): Promise { let asBase = raw.authorization_server if (!asBase && Array.isArray(raw.authorization_servers)) { asBase = raw.authorization_servers[0] @@ -285,10 +463,11 @@ export class OAuthClient { if (!asBase) { throw new Error(`resource_metadata at ${metaUrl} lacked authorization_server(s)`) } + this.assertSecureEndpoint(String(asBase), 'authorization server') for (const p of ['.well-known/oauth-authorization-server', '.well-known/openid-configuration']) { try { - return await this.json(new URL(p, asBase).toString()) + return await this.json(new URL(p, asBase).toString(), rs, 'authorization server') } catch { // next } @@ -305,17 +484,32 @@ export class OAuthClient { meta: Meta, file: string, _scopes: string[], - redirectUri: string + redirectUri: string, + rs: URL, + pinned?: Meta ): Promise { const existing = await this.read(file) if (existing && (!existing.expires_at || existing.expires_at * 1000 > Date.now())) { - this.logger.info(`OAuth: reusing cached client_id ${existing.client_id}`) - return existing + // A registration belongs to the authorization server that issued it, and to the callback port that it was + // registered with. Reuse it only with the token endpoint that it was issued for. For a registration saved + // before that was recorded, that is the token endpoint that the last sign-in pinned. With nothing pinned, + // the token endpoint of the registration is not known, so the sign-in registers again. + const issuedFor = existing.token_endpoint ?? pinned?.token_endpoint + const samePort = this.callbackPort(existing.redirect_uri) === this.callbackPort(redirectUri) + if (samePort && issuedFor !== undefined && issuedFor === meta.token_endpoint) { + this.logger.info(`OAuth: reusing cached client_id ${existing.client_id}`) + return existing + } + this.logger.info( + 'OAuth: the cached client registration is for another token endpoint or callback port; registering again' + ) } if (!meta.registration_endpoint) { throw new Error('OAuth: AS does not support dynamic registration') } + // The registration endpoint is server-supplied via discovery; require a secure transport. + this.assertSecureEndpoint(meta.registration_endpoint, 'registration endpoint') // Let the server decide token_endpoint_auth_method and scope const body = { @@ -324,21 +518,21 @@ export class OAuthClient { response_types: ['code'], redirect_uris: [redirectUri], } - const resp: any = await this.json(meta.registration_endpoint, { + const resp: any = await this.json(meta.registration_endpoint, rs, 'registration endpoint', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body), }) - const reg: Registration = { + // The caller saves the registration after the sign-in with it succeeds + return { client_id: resp.client_id, client_secret: resp.client_secret, expires_at: resp.client_secret_expires_at, redirect_uri: redirectUri, token_endpoint_auth_method: resp.token_endpoint_auth_method, + token_endpoint: meta.token_endpoint, } - await this.write(file, reg) - return reg } /** Try one refresh_token grant; returns new Token or undefined */ @@ -348,7 +542,8 @@ export class OAuthClient { rs: URL, refresh: string ): Promise { - this.validateEndpoint(meta.token_endpoint, 'token') + this.validateTokenEndpoint(meta.token_endpoint) + await this.assertAllowedTarget(new URL(meta.token_endpoint), rs, 'token endpoint') const formParams: Record = { grant_type: 'refresh_token', refresh_token: refresh, @@ -383,15 +578,16 @@ export class OAuthClient { redirectUri: string, server: http.Server ): Promise { - this.validateEndpoint(meta.token_endpoint, 'token') - this.validateEndpoint(meta.authorization_endpoint, 'authorization') + this.validateTokenEndpoint(meta.token_endpoint) + // Check the token endpoint before the browser opens, so that a sign-in that cannot finish does not start + await this.assertAllowedTarget(new URL(meta.token_endpoint), rs, 'token endpoint') const DEFAULT_PKCE_TIMEOUT_MS = 90_000 const verifier = this.b64url(crypto.randomBytes(32)) const challenge = this.b64url(crypto.createHash('sha256').update(verifier).digest()) const state = this.b64url(crypto.randomBytes(16)) - const authz = new URL(meta.authorization_endpoint) + const authz = this.validateAuthorizationEndpoint(meta.authorization_endpoint) authz.search = new URLSearchParams({ client_id: reg.client_id, response_type: 'code', @@ -417,7 +613,6 @@ export class OAuthClient { }) await this.lsp.window.showDocument({ uri: authz.toString(), external: true }) - const { code, rxState, err, errDesc } = await Promise.race([ waitForFlow, new Promise((_, reject) => @@ -430,7 +625,7 @@ export class OAuthClient { if (!code || rxState !== state) throw new Error('Invalid authorization response (state mismatch)') // Exchange code for token using the auth method from DCR - this.validateEndpoint(meta.token_endpoint, 'token') + this.validateTokenEndpoint(meta.token_endpoint) const tokenParams: Record = { grant_type: 'authorization_code', code, @@ -457,9 +652,20 @@ export class OAuthClient { return { ...(tk as object), obtained_at: Date.now() } as Token } - /** Fetch + error-check + parse JSON */ - private static async json(url: string, init?: RequestInit): Promise { - const r = await this.fetchCompat(url, init) + /** + * Fetch + error-check + parse JSON, for the MCP server at `rs`. A GET follows redirects through discoveryFetch. + * Any other request, such as the client registration, is sent once, after the target check, and fails on a + * redirect. + */ + private static async json(url: string, rs: URL, label: string, init?: RequestInit): Promise { + const method = (init?.method ?? 'GET').toUpperCase() + let r: any + if (method === 'GET') { + r = await this.discoveryFetch(url, rs, label, init) + } else { + await this.assertAllowedTarget(new URL(url), rs, label) + r = await this.fetchCompat(url, { ...init, redirect: 'error' }) + } if (!r.ok) { const txt = await r.text().catch(() => '') throw new Error(`HTTP ${r.status}@${url} — ${txt}`) @@ -549,19 +755,40 @@ export class OAuthClient { } /** - * Token exchanges carry credentials, and the authorization endpoint is opened in the user's browser. + * Token exchanges carry refresh tokens, authorization codes, and potentially client credentials. * Require HTTPS for remote endpoints while retaining the OAuth loopback exception for local development. */ - private static validateEndpoint(endpoint: string, kind: 'token' | 'authorization'): void { + private static validateTokenEndpoint(endpoint: string): void { + this.assertSecureEndpoint(endpoint, 'token endpoint') + } + + /** + * The authorization endpoint is handed to the OS browser via showDocument, + * and an MCP server supplies it through discovery. Restrict it to HTTPS (or + * a loopback HTTP address for local development), so that the browser opens + * only a web URL and never a plaintext remote login page. Returns the parsed + * URL for the caller. + */ + private static validateAuthorizationEndpoint(endpoint: string): URL { + return this.assertSecureEndpoint(endpoint, 'authorization endpoint') + } + + /** + * Require HTTPS for a remote OAuth endpoint, keeping the loopback HTTP + * exception for local development. Rejects every other scheme (javascript:, + * file:, data:, ftp:, ...) and malformed URLs. Returns the parsed URL so + * callers can reuse it without parsing twice. + */ + private static assertSecureEndpoint(endpoint: string, label: string): URL { let url: URL try { url = new URL(endpoint) } catch { - throw new Error(`OAuth: ${kind} endpoint is not a valid URL`) + throw new Error(`OAuth: ${label} is not a valid URL`) } if (url.protocol === 'https:') { - return + return url } const hostname = url.hostname.toLowerCase() @@ -569,10 +796,145 @@ export class OAuthClient { url.protocol === 'http:' && (hostname === 'localhost' || hostname === '[::1]' || (isIPv4(hostname) && hostname.startsWith('127.'))) if (isLoopbackHttp) { + return url + } + + throw new Error(`OAuth: ${label} must use HTTPS unless it is a loopback address`) + } + + /** + * Throws unless the client may send a request to `target`, a URL from the OAuth metadata of the MCP server at + * `rs` or a redirect of a discovery request. A public MCP server may send the client to public addresses only, + * an MCP server on a private network also to private addresses, and an MCP server on this computer also to + * loopback addresses. Link-local, unspecified, multicast and reserved addresses are never allowed. Where the + * MCP server is comes from serverScope, which takes its address farthest from this computer. + * + * A host name is checked by the addresses that it resolves to now: one blocked address blocks it, and otherwise + * the address closest to this computer decides. A name that does not resolve is refused. A URL on the host name + * of the MCP server is allowed, because the user chose that host. + */ + private static async assertAllowedTarget(target: URL, rs: URL, label: string): Promise { + if (target.hostname === rs.hostname) { return } + const targetScope = await this.hostScope(target.hostname) + if (targetScope === 'public') { + return + } + if (targetScope === undefined) { + throw new Error(`OAuth: ${label} has a host name that could not be resolved`) + } + if (targetScope !== 'blocked') { + const serverRank = SCOPE_RANK[await this.serverScope(rs.hostname)] + if (serverRank >= SCOPE_RANK[targetScope]) { + return + } + } + const where = targetScope === 'blocked' ? 'a link-local or reserved' : `a ${targetScope}` + throw new Error(`OAuth: ${label} is on ${where} address, which this MCP server cannot send the client to`) + } + + /** + * The scope of a URL host as a target: the scope of the address, or of the addresses that the name resolves to, + * where a blocked address decides first and then the address closest to this computer. Undefined when the name + * does not resolve. + */ + private static async hostScope(hostname: string): Promise { + const scopes = await this.hostScopes(hostname) + if (scopes === undefined) { + return undefined + } + if (scopes.includes('blocked')) { + return 'blocked' + } + return (scopes as Exclude[]).reduce | undefined>( + (closest, s) => (closest === undefined || SCOPE_RANK[s] > SCOPE_RANK[closest] ? s : closest), + undefined + ) + } + + /** + * The scope of the host of the MCP server, which decides where it may send the client. The address farthest + * from this computer decides: a name with any public or blocked address counts as public, and so does a name + * that does not resolve. + */ + private static async serverScope(hostname: string): Promise> { + const scopes = (await this.hostScopes(hostname)) ?? [] + if (scopes.length === 0 || scopes.some(s => s === 'public' || s === 'blocked')) { + return 'public' + } + return scopes.includes('private') ? 'private' : 'loopback' + } + + /** + * The scopes of a URL host: one for an IP address or a localhost name, or one for each address that the name + * resolves to. Undefined when the name does not resolve. + */ + private static async hostScopes(hostname: string): Promise { + const host = hostname + .replace(/^\[(.*)\]$/, '$1') + .replace(/\.$/, '') + .toLowerCase() + const literal = addressScope(host) + if (literal) { + return [literal] + } + if (host === 'localhost' || host.endsWith('.localhost')) { + return ['loopback'] + } + let addresses: { address: string }[] + try { + addresses = await this.lookupHost(host) + } catch { + return undefined + } + const scopes = addresses.map(a => addressScope(a.address)).filter((s): s is AddressScope => s !== undefined) + return scopes.length > 0 ? scopes : undefined + } - throw new Error(`OAuth: ${kind} endpoint must use HTTPS unless it is a loopback address`) + /** Resolves a host name to all its addresses. A separate method so that tests can replace it. */ + private static lookupHost(host: string): Promise<{ address: string; family: number }[]> { + return dns.lookup(host, { all: true }) + } + + /** + * Sends a GET or HEAD discovery request for the MCP server at `rs`. Follows up to MAX_DISCOVERY_REDIRECTS + * redirects itself, so that every URL passes assertAllowedTarget, a redirect does not switch from https to + * http, and a redirect to another host over http needs a loopback address, as assertSecureEndpoint requires. + */ + private static async discoveryFetch(url: string, rs: URL, label: string, init: RequestInit = {}): Promise { + let current = new URL(url) + for (let redirects = 0; ; redirects++) { + await this.assertAllowedTarget(current, rs, label) + const response = await this.fetchCompat(current.toString(), { ...init, redirect: 'manual' }) + const status = typeof response?.status === 'number' ? response.status : 0 + const location = status >= 300 && status < 400 ? response.headers?.get?.('location') : undefined + if (!location) { + return response + } + // Release the connection of the redirect response: a web stream with the built-in fetch, a Node stream + // with the fallback + try { + const body = response.body + await (typeof body?.cancel === 'function' ? body.cancel() : body?.destroy?.()) + } catch { + // The body is not needed + } + if (redirects >= MAX_DISCOVERY_REDIRECTS) { + throw new Error(`OAuth: too many redirects for the ${label}`) + } + const next = new URL(location, current) + if ( + (next.protocol !== 'https:' && next.protocol !== 'http:') || + (current.protocol === 'https:' && next.protocol !== 'https:') + ) { + throw new Error(`OAuth: the ${label} redirected to a URL that does not use HTTPS`) + } + if (next.protocol === 'http:' && next.host !== current.host) { + this.assertSecureEndpoint(next.toString(), label) + } + current = next + } } /** Await server.listen() with error rejection for immediate handling. */