Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(oauth): an access token's created_at is its issuance time, so a lock wait cannot push expiry past the 90-day maximum

time-attack697cd49795c3
diff --git a/src/core/oauth-grants.ts b/src/core/oauth-grants.ts
index 4f3dbc4dce..1c87ceaf28 100644
--- a/src/core/oauth-grants.ts
+++ b/src/core/oauth-grants.ts
@@ -325,13 +325,13 @@ export class OAuthGrants {
const ttl = Math.min(Number.isFinite(override) && override > 0 ? override : this.options.tokenTtl, TOKEN_TTL_MAX_SECONDS);
const now = Math.floor(this.now() / 1000);
const access = generateToken('gbrain_at_');
- await sql`INSERT INTO oauth_tokens (token_hash, token_type, client_id, scopes, expires_at, resource)
- VALUES (${hashToken(access)}, ${'access'}, ${clientId}, ${oauthPgArray(scopes)}, ${now + ttl}, ${resource?.toString() ?? null})`;
+ await sql`INSERT INTO oauth_tokens (token_hash, token_type, client_id, scopes, expires_at, resource, created_at)
+ VALUES (${hashToken(access)}, ${'access'}, ${clientId}, ${oauthPgArray(scopes)}, ${now + ttl}, ${resource?.toString() ?? null}, to_timestamp(${now}))`;
const result: OAuthTokens = { access_token: access, token_type: 'bearer', expires_in: ttl, scope: scopes.join(' ') };
if (refresh) {
const token = generateToken('gbrain_rt_');
- await sql`INSERT INTO oauth_tokens (token_hash, token_type, client_id, scopes, expires_at, resource)
- VALUES (${hashToken(token)}, ${'refresh'}, ${clientId}, ${oauthPgArray(scopes)}, ${now + this.options.refreshTtl}, ${resource?.toString() ?? null})`;
+ await sql`INSERT INTO oauth_tokens (token_hash, token_type, client_id, scopes, expires_at, resource, created_at)
+ VALUES (${hashToken(token)}, ${'refresh'}, ${clientId}, ${oauthPgArray(scopes)}, ${now + this.options.refreshTtl}, ${resource?.toString() ?? null}, to_timestamp(${now}))`;
result.refresh_token = token;
}
return result;
diff --git a/test/oauth-token-ttl-clamp.test.ts b/test/oauth-token-ttl-clamp.test.ts
index c8e5058e31..9223dcfa1d 100644
--- a/test/oauth-token-ttl-clamp.test.ts
+++ b/test/oauth-token-ttl-clamp.test.ts
@@ -149,6 +149,30 @@ for (const kind of testBackends()) {
expect(rescoped.after.tokenTtlSeconds).toBe(NINETY_DAYS);
}, 60_000);
+ test('a token issued after waiting on the client lock expires within the maximum of its own created_at', async () => {
+ const { clientId, clientSecret } = await machineClient('ttl-lock-wait-example');
+ await storeTtl(clientId, TEN_YEARS);
+ let release!: () => void;
+ const held = new Promise<void>(resolve => { release = resolve; });
+ let lockTaken!: () => void;
+ const taken = new Promise<void>(resolve => { lockTaken = resolve; });
+ const holder = engine.transaction(async tx => {
+ await tx.executeRaw('SELECT 1 FROM oauth_clients WHERE client_id = $1 FOR UPDATE', [clientId]);
+ lockTaken();
+ await held;
+ });
+ await taken;
+ const issued = provider.exchangeClientCredentials(clientId, clientSecret!, 'read');
+ await new Promise(resolve => setTimeout(resolve, 1_100));
+ release();
+ await holder;
+ await issued;
+ const rows = await engine.executeRaw<{ over: string }>(
+ `SELECT count(*) AS over FROM oauth_tokens WHERE client_id = $1 AND token_type = 'access'
+ AND expires_at > FLOOR(EXTRACT(EPOCH FROM created_at))::bigint + $2`, [clientId, NINETY_DAYS]);
+ expect(Number(rows[0]!.over)).toBe(0);
+ }, 60_000);
+
test('issuance racing the upgrade never leaves a token past the maximum', async () => {
const { clientId, clientSecret } = await machineClient('ttl-race-example');
await storeTtl(clientId, TEN_YEARS);