Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ beforeEach(() => {
issuer: 'https://mcp.linear.app',
authorization_endpoint: 'https://mcp.linear.app/authorize',
token_endpoint: TOKEN_ENDPOINT,
// A revoke goes only to the endpoint the issuer advertises (TASK-252), so the
// stub advertises the one the revoking arms pin, as the real vendors do.
revocation_endpoint: 'https://mcp.linear.app/revoke',
});
connectorSecrets.put.mockResolvedValueOnce('ref-access').mockResolvedValueOnce('ref-refresh');
global.fetch = jest.fn().mockResolvedValue(okTokenResponse);
Expand Down
62 changes: 53 additions & 9 deletions backend/__tests__/unit/services/connectionRemoval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,29 @@ describe('a row whose entry is gone', () => {
});

describe('revokeTokenAtVendor (RFC 7009)', () => {
// TASK-252: every revoke first reads the issuer's RFC 8414 metadata, and the
// request goes out only when the entry's pinned endpoint is the one the issuer
// advertises. `vendor` answers that GET and hands every other request to `answer`.
const metadataFor = (entry, over = {}) => ({
issuer: entry.issuer,
authorization_endpoint: `${entry.issuer}/authorize`,
token_endpoint: `${entry.issuer}/token`,
revocation_endpoint: entry.revoke.endpoint,
...over,
});
const vendor = (entry, answer, metadata = metadataFor(entry)) => jest.fn(async (url, init) => (
String(url).includes('/.well-known/oauth-authorization-server')
? { ok: true, status: 200, json: async () => metadata }
: answer(url, init)
));
const revokeCalls = (fetchImpl) => fetchImpl.mock.calls
.filter(([url]) => !String(url).includes('/.well-known/'));

it('marks only the refused DCR client when the vendor returns invalid_client', async () => {
const registry = require('../../../services/hostedMcpDcrRegistry');
const refuse = jest.spyOn(registry, 'refuseDcrClient').mockResolvedValue();
const dcrEntry = { ...ENTRY, client: 'dcr' };
const fetchImpl = jest.fn(async () => ({
const fetchImpl = vendor(dcrEntry, async () => ({
ok: false, status: 401, json: async () => ({ error: 'invalid_client' }),
}));
try {
Expand All @@ -552,15 +570,15 @@ describe('revokeTokenAtVendor (RFC 7009)', () => {
tokenTypeHint: 'refresh_token',
}, fetchImpl)).rejects.toMatchObject({ code: 'invalid_client' });
expect(refuse).toHaveBeenCalledWith(dcrEntry, 'retired-client');
expect(new URLSearchParams(fetchImpl.mock.calls[0][1].body).get('client_id'))
expect(new URLSearchParams(revokeCalls(fetchImpl)[0][1].body).get('client_id'))
.toBe('retired-client');
} finally {
refuse.mockRestore();
}
});
it('a post-registered DCR client revokes with post, though a dcr removal reads no metadata', async () => {
it('a post-registered DCR client revokes with post', async () => {
const dcrEntry = { ...ENTRY, client: 'dcr' };
const fetchImpl = jest.fn(async () => ({ ok: true, status: 200, json: async () => ({}) }));
const fetchImpl = vendor(dcrEntry, async () => ({ ok: true, status: 200, json: async () => ({}) }));
await revokeTokenAtVendor({
entry: dcrEntry,
clientId: 'conf-1',
Expand All @@ -569,16 +587,16 @@ describe('revokeTokenAtVendor (RFC 7009)', () => {
token: 'held-token',
tokenTypeHint: 'refresh_token',
}, fetchImpl);
expect(fetchImpl).toHaveBeenCalledTimes(1);
const [, init] = fetchImpl.mock.calls[0];
expect(revokeCalls(fetchImpl)).toHaveLength(1);
const [, init] = revokeCalls(fetchImpl)[0];
expect(init.headers.Authorization).toBeUndefined();
const body = new URLSearchParams(init.body);
expect(body.get('client_id')).toBe('conf-1');
expect(body.get('client_secret')).toBe('registered-secret');
});

const call = (response) => {
const fetchImpl = jest.fn(async () => response);
const call = (response, metadata = metadataFor(ENTRY)) => {
const fetchImpl = vendor(ENTRY, async () => response, metadata);
return {
fetchImpl,
run: () => revokeTokenAtVendor({
Expand All @@ -593,7 +611,7 @@ describe('revokeTokenAtVendor (RFC 7009)', () => {
it("the provider revoke sends the token to the entry's revocation endpoint", async () => {
const { fetchImpl, run } = call({ ok: true, status: 200, json: async () => ({}) });
await run();
const [url, init] = fetchImpl.mock.calls[0];
const [url, init] = revokeCalls(fetchImpl)[0];
expect(url).toBe(ENDPOINT);
expect(init.method).toBe('POST');
expect(init.headers['Content-Type']).toBe('application/x-www-form-urlencoded');
Expand Down Expand Up @@ -685,6 +703,32 @@ describe('revokeTokenAtVendor (RFC 7009)', () => {
await expect(run()).rejects.toThrow(/HTTP 502/);
});

it('sends nothing when the issuer advertises a different revocation endpoint than the entry pins', async () => {
const { fetchImpl, run } = call(
{ ok: true, status: 200, json: async () => ({}) },
metadataFor(ENTRY, { revocation_endpoint: 'https://mcp.linear.app/oauth/revoke' }),
);
await expect(run()).rejects.toThrow(/not the revocation_endpoint .* advertises/);
expect(revokeCalls(fetchImpl)).toHaveLength(0);
});

it('sends nothing when the issuer advertises no revocation endpoint at all', async () => {
const metadata = metadataFor(ENTRY);
delete metadata.revocation_endpoint;
const { fetchImpl, run } = call({ ok: true, status: 200, json: async () => ({}) }, metadata);
await expect(run()).rejects.toThrow(/not the revocation_endpoint/);
expect(revokeCalls(fetchImpl)).toHaveLength(0);
});

it('sends nothing when the metadata is written for another issuer', async () => {
const { fetchImpl, run } = call(
{ ok: true, status: 200, json: async () => ({}) },
metadataFor(ENTRY, { issuer: 'https://elsewhere.example' }),
);
await expect(run()).rejects.toMatchObject({ code: 'issuer_mismatch' });
expect(revokeCalls(fetchImpl)).toHaveLength(0);
});

it('refuses an entry that names no endpoint rather than guessing from the page', async () => {
const fetchImpl = jest.fn(async () => ({ ok: true, status: 200, json: async () => ({}) }));
await expect(revokeTokenAtVendor({
Expand Down
19 changes: 15 additions & 4 deletions backend/services/connectionRemovalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,23 @@ export const revokeTokenAtVendor = async (input: {
// would answer the empty string 200 for a request that revoked nothing.
throw new Error('provider revoke refused: no token to revoke');
}
const metadata = input.entry.client === 'pre-registered'
? await discoverAuthorizationServer(input.entry.issuer, fetchImpl)
: {};
// TASK-252: this request carries the token and, for a registered client, its
// secret, so it goes only to the endpoint the issuer itself advertises. The
// pin is reviewed by PR, but a vendor can move its endpoint, and an unchecked
// pin would keep sending credentials to the old host. The metadata is read
// with its identity checked (`discoverAuthorizationServer`), and the compare is
// exact, as the issuer's is. Every pin equalled its issuer's
// `revocation_endpoint` when this landed (measured 2026-11-01).
const metadata = await discoverAuthorizationServer(input.entry.issuer, fetchImpl);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if (metadata.revocation_endpoint !== target.endpoint) {
throw new Error(
`provider revoke refused: ${target.endpoint} is not the revocation_endpoint ${input.entry.issuer} advertises`,
);
}
const request = buildRevocationRequest(
input.entry,
metadata,
// Only a registered client reads its auth method from the metadata.
input.entry.client === 'pre-registered' ? metadata : {},
{
clientId: input.clientId,
...(input.clientSecret ? { clientSecret: input.clientSecret } : {}),
Expand Down
Loading