Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(migrations): v210 leaves access tokens within 5 minutes of the maximum alone
time-attack3365d46fa523
diff --git a/src/core/schema-migrations/v210-clamp-oauth-token-ttl.ts b/src/core/schema-migrations/v210-clamp-oauth-token-ttl.tsindex 0c13d7f92e..e6223713c8 100644--- a/src/core/schema-migrations/v210-clamp-oauth-token-ttl.ts+++ b/src/core/schema-migrations/v210-clamp-oauth-token-ttl.ts@@ -16,6 +16,14 @@ import type { Migration } from './types.ts';// tokens are untouched. Every change writes an oauth_grant_audit row. All// client rows are locked first so in-flight issuance commits before tokens// are shortened. Re-running finds nothing left to change.+/**+ * Older releases stamped created_at at transaction start on the database clock+ * and expires_at from the application clock after the client lock, so a token+ * issued at exactly the maximum can read a few seconds over it. Tokens within+ * this slack were never out of bounds and are left alone (no audit row).+ */+const ISSUANCE_CLOCK_SLACK_SECONDS = 300;+export async function clampOAuthTokenTtls(engine: BrainEngine): Promise<void> {await engine.transaction(async tx => {const sql = sqlQueryForEngine(tx);@@ -35,7 +43,7 @@ export async function clampOAuthTokenTtls(engine: BrainEngine): Promise<void> {}const shortened = await sql`UPDATE oauth_tokensSET expires_at = FLOOR(EXTRACT(EPOCH FROM created_at))::bigint + ${TOKEN_TTL_MAX_SECONDS}- WHERE token_type = 'access' AND expires_at > FLOOR(EXTRACT(EPOCH FROM created_at))::bigint + ${TOKEN_TTL_MAX_SECONDS}+ WHERE token_type = 'access' AND expires_at > FLOOR(EXTRACT(EPOCH FROM created_at))::bigint + ${TOKEN_TTL_MAX_SECONDS + ISSUANCE_CLOCK_SLACK_SECONDS}RETURNING client_id`;const perClient = new Map<string, number>();for (const row of shortened) perClient.set(String(row.client_id), (perClient.get(String(row.client_id)) ?? 0) + 1);diff --git a/test/fixtures/goldens/migrations/records.json b/test/fixtures/goldens/migrations/records.jsonindex 49c579e994..a857a46bca 100644--- a/test/fixtures/goldens/migrations/records.json+++ b/test/fixtures/goldens/migrations/records.json@@ -11,13 +11,14 @@"ALIAS_TABLES": "cddd37c636af42d862c08ca7754a57dfe9d12576b769fa8f74a5f02c803f67a0","FACT_RELINK_TABLE_SQL": "1fdde4b99d0077365274096f294be64a992961c7b4544af794224de238b03630","FACT_UNLINKED_INDEX_SQL": "87dcf23c3fa570d93bfb214b6bbb024faa9ff9bff227aa78869a29580be03298",+ "ISSUANCE_CLOCK_SLACK_SECONDS": "8f36c80392df6f9b9c754826289b63969d9d41ca8955213291e4413d614dce35","PERSISTENCE_DATABASE_PENDING_INDEX_SQL": "6e0f618be33d037454933ea4dea11438413d45aef35d3bd4f33e89624d6e199e","PERSISTENCE_EFFECT_PARKED_INDEX_SQL": "0107863883415378d7c0dce7a0cbd5311c2c5a59fc0f81917cc8b767fd235cf2","PERSISTENCE_SYNC_RUN_INDEXES": "e1b12613c5fd2facb850e6857b0bddcf49b2478ec258fa226fe15e71e55df9c1","TOKEN_TTL_MAX_SECONDS": "f4a45bec6ec0bacc859dc57732ceb76cec7eb72f242b5a6c89d47190d0525ab4","TOKEN_TTL_MIN_SECONDS": "441d8ce6bd21f753de90abd3013eaa9992a469ea60c590f1005533618199aa25","buildIndexOnline": "c3c076436d7a1d161b94862125cafb9d3ba14d06e296a6379d6149452610285c",- "clampOAuthTokenTtls": "092945a432b8d1885068cce483cb959a62fd00ccd459fe97481414e8b5615bb6",+ "clampOAuthTokenTtls": "b1e94ea8f5b3c3870a56b0f6d00d0a274c2e9dcad912b828848aec250732645f","dropInvalidConcurrentIndex": "6864d844b7dfb2fbecdd0fb9c73e06248473376feb794b3fe58f036c70d2d44c","getFtsLanguage": "0d790b4c4af1f67a2a1bbd59470beb13f434917b381931baf8404a41c5d903db","grantFromRow": "2d0603bb0f964d7e45e04aa76bac54cd4f6f63c91a8d9f028b60b4792bc1695e",@@ -3104,6 +3105,7 @@"handler": {"form": "identifier","helpers": [+ "ISSUANCE_CLOCK_SLACK_SECONDS","TOKEN_TTL_MAX_SECONDS","TOKEN_TTL_MIN_SECONDS","clampOAuthTokenTtls",diff --git a/test/oauth-token-ttl-clamp.test.ts b/test/oauth-token-ttl-clamp.test.tsindex 9223dcfa1d..7fe69e0057 100644--- a/test/oauth-token-ttl-clamp.test.ts+++ b/test/oauth-token-ttl-clamp.test.ts@@ -149,6 +149,15 @@ for (const kind of testBackends()) {expect(rescoped.after.tokenTtlSeconds).toBe(NINETY_DAYS);}, 60_000);+ test('a token issued at the maximum a few seconds off its created_at is not shortened or marked clamped', async () => {+ const { clientId } = await machineClient('ttl-at-max-example');+ await storeTtl(clientId, NINETY_DAYS);+ const atMax = await seedToken(clientId, 'access', DAY, NINETY_DAYS - DAY + 2);+ await rerunClamp();+ expect(await expiryOf(atMax.token)).toBe(atMax.expiresAt);+ expect(await auditRows(clientId)).toEqual([]);+ }, 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);