Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

lock on read for revokation

Thiago Araujo da Silva0ffed2c50cec
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 2deff2284f..2f8f49f5a7 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -319,9 +319,15 @@ blanket revocation would just sign everyone out. The session tag is per client,
because `assertValidOauthClientGrantConfig` rejects the exchange grant alongside `authorization_code`, so
a service needing both registers twice.
-That sweep only reaps sessions that already exist, so **the exchange rechecks the client on the primary
-after creating its own session** (`findByIdOnPrimary` + `hasClientAuthorityChanged`, re-running the
-sweep). The other grants read an existing session rather than creating one, so they don't need it.
+That sweep only reaps sessions that already exist, so **the exchange rechecks the client after creating
+its own session** (`findByIdForUpdate` + `hasClientAuthorityChanged`, re-running the sweep). The other
+grants read an existing session rather than creating one, so they don't need it. The recheck has to be a
+**locking** read on the primary, and both halves of that are load-bearing: each withdrawal path writes the
+client and sweeps sessions inside one transaction, so a plain read still sees the pre-withdrawal row until
+that transaction commits, and would clear a token whose session the sweep has already scanned past. A
+replica read has the same hole for the length of the replication lag. Keeping the write and the sweep
+atomic is what makes the lock the fix here; splitting them so the write commits first would reopen a
+window where the sweep can fail on its own and leave the tokens live.
**Everything the exchange fetches from the IdP is cached for 10 minutes; nothing read from our own
database is**, so an admin editing the SSO config takes effect on the next request while the provider
diff --git a/backend/src/services/oauth-client/oauth-client-dal.ts b/backend/src/services/oauth-client/oauth-client-dal.ts
index b2f2288a8e..c7f0fc5bd2 100644
--- a/backend/src/services/oauth-client/oauth-client-dal.ts
+++ b/backend/src/services/oauth-client/oauth-client-dal.ts
@@ -8,13 +8,13 @@ export type TOauthClientDALFactory = ReturnType<typeof oauthClientDALFactory>;
export const oauthClientDALFactory = (db: TDbClient) => {
const oauthClientOrm = ormify(db, TableName.OauthClient);
- const findByIdOnPrimary = async (id: string) => {
+ const findByIdForUpdate = async (id: string) => {
try {
- return await db(TableName.OauthClient).where({ id }).first();
+ return await db(TableName.OauthClient).where({ id }).forUpdate().first();
} catch (error) {
- throw new DatabaseError({ error, name: "FindOauthClientByIdOnPrimary" });
+ throw new DatabaseError({ error, name: "FindOauthClientByIdForUpdate" });
}
};
- return { ...oauthClientOrm, findByIdOnPrimary };
+ return { ...oauthClientOrm, findByIdForUpdate };
};
diff --git a/backend/src/services/oauth-client/oauth-client-service.ts b/backend/src/services/oauth-client/oauth-client-service.ts
index d1bb44b097..8fcf38690b 100644
--- a/backend/src/services/oauth-client/oauth-client-service.ts
+++ b/backend/src/services/oauth-client/oauth-client-service.ts
@@ -741,11 +741,7 @@ export const oauthClientServiceFactory = ({
message: "Failed to create a session for the user this token identifies."
});
- // A revocation can land between authenticating the client and inserting the session above, deleting
- // nothing because the session did not exist yet. Every withdrawal path commits its client write
- // before deleting sessions, so re-reading the client here catches it -- on the primary, since the
- // replica can still be serving the pre-revocation row.
- const currentClient = await oauthClientDAL.findByIdOnPrimary(client.id);
+ const currentClient = await oauthClientDAL.findByIdForUpdate(client.id);
if (hasClientAuthorityChanged(client, currentClient, OauthGrantType.TokenExchange)) {
await tokenService.revokeSessionsByUserAgent(getOauthClientSessionUserAgent(client.clientId));
throw new OauthTokenError({