Skip to content

Commit 94b08a7

Browse files
committed
wip: existing verifier pins seed the client row the grant read now starts at
Claude-Session: https://claude.ai/code/session_01QUs3BuLtRmSkjNPDYrwA9T Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5d33b5a commit 94b08a7

3 files changed

Lines changed: 46 additions & 11 deletions

File tree

‎packages/plugins/plugin-auth/src/auth-manager.mcp-oauth.test.ts‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ const savedEnv: Record<string, string | undefined> = {};
4747
* adapter dialect: `findMany` / `findOne` over the provider's camelCase field
4848
* names with `eq` clauses, the only shape the consent read sends. `reads`
4949
* counts every call, so a leg can pin how many reads one verification costs.
50+
*
51+
* [objectstack-ai/cloud#3820, card 2] The grant read starts at the client row:
52+
* a disabled client, or one whose row is gone, admits nothing. So the default
53+
* store holds the one client every token below names, enabled, and a leg that
54+
* is about the client passes its own rows.
5055
*/
5156
function consentStore(
5257
consents: Array<{ clientId: string; userId: string; referenceId?: string | null; createdAt: Date }>,
53-
clients: Array<{ clientId: string; skipConsent?: boolean }> = [],
58+
clients: Array<{ clientId: string; skipConsent?: boolean; disabled?: boolean }> = [{ clientId: 'client-abc' }],
5459
) {
5560
const tables: Record<string, Array<Record<string, unknown>>> = { oauthConsent: consents, oauthClient: clients };
5661
const reads: string[] = [];
@@ -573,14 +578,28 @@ describe('verifyMcpAccessToken reads the consent the token was minted under (rul
573578
.sign(privateKey);
574579
}
575580

576-
it('admits a token whose consent stands — at the cost of exactly ONE read', async () => {
581+
it('admits a token whose consent stands — at the cost of exactly TWO reads, the client and then the consent', async () => {
577582
const store = consentStore([{ clientId: 'client-abc', userId: 'user-1', referenceId: null, createdAt: HOUR_AGO }]);
578583
expect(await managerOver(store).verifyMcpAccessToken(await signToken())).toEqual({
579584
userId: 'user-1',
580585
scopes: ['data:read'],
581586
clientId: 'client-abc',
582587
});
583-
expect(store.reads).toEqual(['findMany oauthConsent']);
588+
// [objectstack-ai/cloud#3820, card 2] The client row is read first, so a
589+
// disabled client's token is refused before its consent is looked at.
590+
expect(store.reads).toEqual(['findOne oauthClient', 'findMany oauthConsent']);
591+
});
592+
593+
it('[objectstack-ai/cloud#3820, card 2] refuses the token of a disabled client, or of a client whose row is gone, though its consent stands', async () => {
594+
const consent = [{ clientId: 'client-abc', userId: 'user-1', referenceId: null, createdAt: HOUR_AGO }];
595+
const disabled = consentStore(consent, [{ clientId: 'client-abc', disabled: true }]);
596+
expect(await managerOver(disabled).verifyMcpAccessToken(await signToken())).toBeNull();
597+
// Refused on the client row alone: the consent is never read.
598+
expect(disabled.reads).toEqual(['findOne oauthClient']);
599+
expect(await managerOver(consentStore(consent, [])).verifyMcpAccessToken(await signToken())).toBeNull();
600+
// The control: the same consent under the same client, enabled, admits.
601+
const enabled = consentStore(consent, [{ clientId: 'client-abc', disabled: false }]);
602+
expect(await managerOver(enabled).verifyMcpAccessToken(await signToken())).not.toBeNull();
584603
});
585604

586605
it('refuses a token whose consent is gone (revoked, or withdrawn in the Console)', async () => {
@@ -630,7 +649,8 @@ describe('verifyMcpAccessToken reads the consent the token was minted under (rul
630649
it('a server-only `skip_consent` client has no consent row and is admitted without one (ruling A, step 5)', async () => {
631650
const skip = consentStore([], [{ clientId: 'client-abc', skipConsent: true }]);
632651
expect(await managerOver(skip).verifyMcpAccessToken(await signToken())).not.toBeNull();
633-
expect(skip.reads).toEqual(['findMany oauthConsent', 'findOne oauthClient']);
652+
// One client read answers both questions: enabled, and consent-less.
653+
expect(skip.reads).toEqual(['findOne oauthClient', 'findMany oauthConsent']);
634654
// The control: the same client without the flag is refused.
635655
const consenting = consentStore([], [{ clientId: 'client-abc', skipConsent: false }]);
636656
expect(await managerOver(consenting).verifyMcpAccessToken(await signToken())).toBeNull();

‎packages/plugins/plugin-auth/src/auth-manager.mcp-verifier-keyring.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ async function verifierRig() {
111111
{ client_id: CLIENT, user_id: USER, scopes: JSON.stringify(['data:read']), created_at: new Date(Date.now() - 60_000) },
112112
SYS,
113113
);
114+
// [objectstack-ai/cloud#3820, card 2] …and the client it was granted to,
115+
// enabled: the verifier reads the client row before the consent.
116+
await (engine as any).insert(
117+
'sys_oauth_application',
118+
{ id: `app_${CLIENT}`, client_id: CLIENT, name: 'Keyring Probe', redirect_uris: JSON.stringify(['http://localhost:56789/cb']) },
119+
SYS,
120+
);
114121

115122
/**
116123
* A token for this deployment's MCP resource, signed by the jwt plugin with
@@ -259,10 +266,11 @@ describe('[#24186] the bound on the fresh read an unknown key earns', () => {
259266
},
260267
} as any);
261268
// The consent these tokens are minted under (ruling A): a standing grant,
262-
// so each leg counts key-set reads and nothing else decides its answer.
269+
// under an enabled client (cloud#3820 card 2: the client row is read
270+
// first), so each leg counts key-set reads and nothing else decides its answer.
263271
const consent = { clientId: CLIENT, userId: USER, referenceId: null, createdAt: new Date(Date.now() - 60_000) };
264272
vi.spyOn(manager, 'getAuthContext').mockResolvedValue({
265-
adapter: { findMany: async () => [{ ...consent }], findOne: async () => null },
273+
adapter: { findMany: async () => [{ ...consent }], findOne: async () => ({ clientId: CLIENT, disabled: false }) },
266274
});
267275
const token = (privateKey: CryptoKey, kid: string) => {
268276
const now = Math.floor(Date.now() / 1000);

‎packages/plugins/plugin-auth/src/mcp-client-last-use.real-engine.test.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ async function rig() {
117117
const manager = makeManager();
118118
const api: any = await manager.getApi();
119119

120+
// [objectstack-ai/cloud#3820, card 2] The clients these tokens name, as the
121+
// provider stores them, enabled: the verifier reads the client row before
122+
// the consent, and a client with no row admits nothing.
123+
const authCtx = await manager.getAuthContext();
124+
for (const clientId of [CLIENT, OTHER_CLIENT]) {
125+
await authCtx.adapter.create({
126+
model: 'oauthClient',
127+
data: { clientId, name: 'Desk Assistant', scopes: ['openid', 'data:read'], redirectUris: ['http://localhost:56789/cb'] },
128+
});
129+
}
130+
120131
// Every write of the consent table, and every read the STAMP makes of it
121132
// (the one that selects `last_used_at`), counted at the engine. The
122133
// verifier's own per-call consent read (#30879) goes through better-auth's
@@ -410,11 +421,7 @@ describe('[#30918] nothing a client sends reaches the stamp', () => {
410421
.filter(Boolean)
411422
.join('; ');
412423

413-
const ctx = await r.manager.getAuthContext();
414-
await ctx.adapter.create({
415-
model: 'oauthClient',
416-
data: { clientId: CLIENT, name: 'Desk Assistant', scopes: ['openid', 'data:read'], redirectUris: ['http://localhost:56789/cb'] },
417-
});
424+
// The client row is the rig's own (`CLIENT`, 'Desk Assistant').
418425
const id = await r.consent({ userId });
419426

420427
const res = await r.manager.handleRequest(

0 commit comments

Comments
 (0)