Skip to content

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

Revert "fix: filter revocations in memory instead of pushing OR predicate to PG"

Claude15e50e8321e8
diff --git a/backend/src/services/identity-access-token/identity-access-token-revocation-dal.ts b/backend/src/services/identity-access-token/identity-access-token-revocation-dal.ts
index c91a02cba4..1d7e11da6e 100644
--- a/backend/src/services/identity-access-token/identity-access-token-revocation-dal.ts
+++ b/backend/src/services/identity-access-token/identity-access-token-revocation-dal.ts
@@ -38,19 +38,34 @@ export const identityAccessTokenRevocationDALFactory = (db: TDbClient) => {
}
};
- // Returns every active revocation row for the identity. Per-token / identity-wide /
- // scoped matching is done in the service so the query stays a single (identityId,
- // expiresAt) index range — no OR predicates that the planner could decompose into
- // a sequential scan based on `scope` selectivity stats.
- const findActiveRevocationsForIdentity = async (identityId: string): Promise<TRevocationRow[]> => {
+ const findActiveRevocationsForToken = async ({
+ tokenId,
+ identityId,
+ scopes
+ }: {
+ tokenId: string;
+ identityId: string;
+ scopes: string[];
+ }): Promise<TRevocationRow[]> => {
try {
- return (await db
- .replicaNode()(TableName.IdentityAccessTokenRevocation)
- .select("id", "identityId", "revokedAt", "createdAt", "scope")
- .where("expiresAt", ">", db.fn.now())
- .where("identityId", identityId)) as TRevocationRow[];
+ return (
+ (await db
+ .replicaNode()(TableName.IdentityAccessTokenRevocation)
+ .select("id", "identityId", "revokedAt", "createdAt", "scope")
+ .where("expiresAt", ">", db.fn.now())
+ .where("identityId", identityId)
+ // Both halves of the OR must be equality predicates so the planner can
+ // serve `id IN (...)` from the PK and the `scope IN (...)` filter stays
+ // selective. `scope IS NOT NULL` would force a non-sargable scan.
+ .andWhere((qb) => {
+ void qb.whereIn("id", [tokenId, identityId]);
+ if (scopes.length > 0) {
+ void qb.orWhereIn("scope", scopes);
+ }
+ })) as TRevocationRow[]
+ );
} catch (error) {
- throw new DatabaseError({ error, name: "IdentityAccessTokenRevocationFindActiveForIdentity" });
+ throw new DatabaseError({ error, name: "IdentityAccessTokenRevocationFindActiveForToken" });
}
};
@@ -64,7 +79,7 @@ export const identityAccessTokenRevocationDALFactory = (db: TDbClient) => {
return {
insertRevocation,
- findActiveRevocationsForIdentity,
+ findActiveRevocationsForToken,
removeExpiredRevocations
};
};
diff --git a/backend/src/services/identity-access-token/identity-access-token-service.test.ts b/backend/src/services/identity-access-token/identity-access-token-service.test.ts
index 89230d3b68..87c6e23096 100644
--- a/backend/src/services/identity-access-token/identity-access-token-service.test.ts
+++ b/backend/src/services/identity-access-token/identity-access-token-service.test.ts
@@ -54,7 +54,7 @@ const createService = ({
setItemWithExpiry: vi.fn()
};
const identityAccessTokenRevocationDAL = {
- findActiveRevocationsForIdentity: vi.fn().mockResolvedValue(activeRevocations),
+ findActiveRevocationsForToken: vi.fn().mockResolvedValue(activeRevocations),
insertRevocation: vi.fn()
};
const identityDAL = {
diff --git a/backend/src/services/identity-access-token/identity-access-token-service.ts b/backend/src/services/identity-access-token/identity-access-token-service.ts
index 20da516176..0fbfe02f3b 100644
--- a/backend/src/services/identity-access-token/identity-access-token-service.ts
+++ b/backend/src/services/identity-access-token/identity-access-token-service.ts
@@ -104,7 +104,15 @@ export const identityAccessTokenServiceFactory = ({
authMethod?: string;
messagePrefix?: "Failed to authorize" | "Cannot renew";
}) => {
- const activeRevocations = await identityAccessTokenRevocationDAL.findActiveRevocationsForIdentity(identityId);
+ const scopes: string[] = [];
+ if (clientSecretId) scopes.push(clientSecretId);
+ if (authMethod) scopes.push(authMethod);
+
+ const activeRevocations = await identityAccessTokenRevocationDAL.findActiveRevocationsForToken({
+ tokenId,
+ identityId,
+ scopes
+ });
for (const revocation of activeRevocations) {
// Legacy markers have scope=null and key off polymorphic `id`.