Skip to content

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

fix filter comparison

Thiago Araujo da Silva5787477a896c
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 854afc69e0..1f5b40b178 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -439,11 +439,16 @@ whenever the asserted subject differs from what SCIM last wrote, so **a SCIM rea
provisioned user vanish from `GET /Users?filter=userName eq "..."` the first time they logged in
under a different subject, and the IdP answered that empty lookup by provisioning them again. It was
intermittent because `replaceScimUser` rewrites `externalId` on *all* of a user's aliases, so the
-next PUT healed it until the next login. `$buildScimMembershipQuery` (`org-dal.ts`) therefore joins
-every alias and collapses the fan-out with `DISTINCT ON` *after* the filter has run, which keeps the
-alias the caller asked for when it filtered and the newest one when it did not. The list query also
-carries a total order, because an IdP walking `startIndex`/`count` over an unordered result loses
-users the same way.
+next PUT healed it until the next login. `$buildScimMembershipQuery` (`org-dal.ts`) therefore
+answers every `userName` comparison with an `EXISTS` over the user's aliases, so the predicate is per
+user rather than per alias row. That matters for negation: evaluated per row, `userName ne "x"` or
+`not (userName eq "x")` would keep a user through their other alias, and a member with no alias at
+all would fall out through a NULL comparison. The parser (`lib/knex/scim.ts`) lets an attribute
+resolve to a handler instead of a column for exactly this. Display is separate: the query still
+joins every alias and collapses the fan-out with `DISTINCT ON`, ranked so the alias row that
+satisfies the filter wins and the newest is the fallback, which is how a lookup by an older alias
+echoes that alias back. The list query also carries a total order, because an IdP walking
+`startIndex`/`count` over an unordered result loses users the same way.
A related case sits on the login side: provisioning can name someone before they have ever logged
in, leaving a placeholder account keyed on the identifier instead of the mailbox.
diff --git a/backend/e2e-test/routes/v1/scim.spec.ts b/backend/e2e-test/routes/v1/scim.spec.ts
index 46bc28d284..b9f7d6edf0 100644
--- a/backend/e2e-test/routes/v1/scim.spec.ts
+++ b/backend/e2e-test/routes/v1/scim.spec.ts
@@ -984,6 +984,107 @@ describe("SCIM v1 Router", () => {
expect(payload.Resources[0].userName).toBe(samlSubject);
});
+ test("should exclude a user from `userName ne` when any of their aliases is the excluded one", async () => {
+ const db = getDb();
+ const label = `ne-${crypto.randomUUID().slice(0, 8)}`;
+ const { membershipId, externalId, actorUserId } = await seedScimUser(db, label);
+
+ await db(TableName.UserAliases).insert({
+ userId: actorUserId,
+ orgId: ORG_ID,
+ aliasType: "saml",
+ externalId: `saml-subject-${label}`
+ });
+
+ const res = await testServer.inject({
+ method: "GET",
+ url: `/api/v1/scim/Users?filter=${encodeURIComponent(`userName ne "${externalId}"`)}`,
+ headers: { authorization: `Bearer ${scimToken}` }
+ });
+
+ expect(res.statusCode).toBe(200);
+ const payload = JSON.parse(res.payload) as { Resources: { id: string }[] };
+
+ // The other alias must not smuggle the user back in.
+ expect(payload.Resources.map((r) => r.id)).not.toContain(membershipId);
+ });
+
+ test("should exclude a user from `not (userName eq)` through their newest alias", async () => {
+ const db = getDb();
+ const label = `not-${crypto.randomUUID().slice(0, 8)}`;
+ const { membershipId, actorUserId } = await seedScimUser(db, label);
+
+ const samlSubject = `saml-subject-${label}`;
+ await db(TableName.UserAliases).insert({
+ userId: actorUserId,
+ orgId: ORG_ID,
+ aliasType: "saml",
+ externalId: samlSubject
+ });
+
+ const res = await testServer.inject({
+ method: "GET",
+ url: `/api/v1/scim/Users?filter=${encodeURIComponent(`not (userName eq "${samlSubject}")`)}`,
+ headers: { authorization: `Bearer ${scimToken}` }
+ });
+
+ expect(res.statusCode).toBe(200);
+ const payload = JSON.parse(res.payload) as { Resources: { id: string }[] };
+
+ expect(payload.Resources.map((r) => r.id)).not.toContain(membershipId);
+ });
+
+ test("should keep a member with no alias in a `userName ne` result", async () => {
+ const db = getDb();
+ const [seedMembership] = await db(TableName.Membership)
+ .where({ scopeOrgId: ORG_ID, scope: AccessScope.Organization, actorUserId: seedData1.id })
+ .select("id");
+ await db(TableName.UserAliases).where({ userId: seedData1.id, orgId: ORG_ID, aliasType: "saml" }).del();
+
+ const res = await testServer.inject({
+ method: "GET",
+ url: `/api/v1/scim/Users?filter=${encodeURIComponent(`userName ne "nobody-${crypto.randomUUID()}"`)}`,
+ headers: { authorization: `Bearer ${scimToken}` }
+ });
+
+ expect(res.statusCode).toBe(200);
+ const payload = JSON.parse(res.payload) as { Resources: { id: string }[] };
+
+ // Before this was a set comparison, a NULL alias made `ne` silently drop the member.
+ expect(payload.Resources.map((r) => r.id)).toContain(seedMembership.id);
+ });
+
+ test("should match a user through either alias with `or` and still echo the alias asked for", async () => {
+ const db = getDb();
+ const label = `or-${crypto.randomUUID().slice(0, 8)}`;
+ const { membershipId, externalId, actorUserId } = await seedScimUser(db, label);
+
+ await db(TableName.UserAliases).insert({
+ userId: actorUserId,
+ orgId: ORG_ID,
+ aliasType: "saml",
+ externalId: `saml-subject-${label}`
+ });
+
+ const res = await testServer.inject({
+ method: "GET",
+ url: `/api/v1/scim/Users?filter=${encodeURIComponent(
+ `userName eq "${externalId}" or userName eq "never-${label}"`
+ )}`,
+ headers: { authorization: `Bearer ${scimToken}` }
+ });
+
+ expect(res.statusCode).toBe(200);
+ const payload = JSON.parse(res.payload) as {
+ totalResults: number;
+ Resources: { id: string; userName: string }[];
+ };
+
+ expect(payload.totalResults).toBe(1);
+ expect(payload.Resources[0].id).toBe(membershipId);
+ expect(payload.Resources[0].userName).toBe(externalId);
+ });
+
test("should report totalResults as the match count rather than the page size", async () => {
const db = getDb();
const suffix = crypto.randomUUID().slice(0, 8);
diff --git a/backend/src/lib/knex/scim.ts b/backend/src/lib/knex/scim.ts
index 3758ed984b..79816e9774 100644
--- a/backend/src/lib/knex/scim.ts
+++ b/backend/src/lib/knex/scim.ts
@@ -1,11 +1,21 @@
import { Knex } from "knex";
-import { Compare, Filter, parse } from "scim2-parse-filter";
+import { Compare, Filter, parse, Suffix } from "scim2-parse-filter";
import { TableName } from "@app/db/schemas";
import { BadRequestError } from "../errors";
import { sanitizeSqlLikeString } from "../fn";
+export type TScimCompareOp = Compare["op"] | "pr";
+export type TScimCompareValue = Compare["compValue"] | undefined;
+
+export type TScimAttributeHandler = (query: Knex.QueryBuilder, op: TScimCompareOp, value: TScimCompareValue) => void;
+
+export type TScimAttributeResolver = (attrPath: string) => string | TScimAttributeHandler | null;
+
+const isScimCompareOp = (filter: Filter): filter is Compare | Suffix =>
+ filter.op !== "and" && filter.op !== "or" && filter.op !== "not" && filter.op !== "[]";
+
const appendParentToGroupingOperator = (parentPath: string, filter: Filter) => {
if (filter.op !== "[]" && filter.op !== "and" && filter.op !== "or" && filter.op !== "not") {
return { ...filter, attrPath: `${parentPath}.${(filter as Compare).attrPath}` };
@@ -13,10 +23,58 @@ const appendParentToGroupingOperator = (parentPath: string, filter: Filter) => {
return filter;
};
+export const applyScimComparison = (
+ query: Knex.QueryBuilder,
+ column: string,
+ op: TScimCompareOp,
+ rawValue: TScimCompareValue
+) => {
+ const value: Compare["compValue"] =
+ column === `${TableName.Users}.email` && typeof rawValue === "string" ? rawValue.toLowerCase() : (rawValue ?? null);
+
+ switch (op) {
+ case "eq":
+ void query.where(column, value);
+ break;
+ case "ne":
+ void query.whereNot(column, "=", value);
+ break;
+ case "pr":
+ void query.whereNotNull(column);
+ break;
+ case "gt":
+ void query.where(column, ">", value);
+ break;
+ case "ge":
+ void query.where(column, ">=", value);
+ break;
+ case "lt":
+ void query.where(column, "<", value);
+ break;
+ case "le":
+ void query.where(column, "<=", value);
+ break;
+ case "sw":
+ if (!value) {
+ throw new BadRequestError({ message: "compValue is required for sw filter" });
+ }
+ void query.whereILike(column, `${sanitizeSqlLikeString(String(value))}%`);
+ break;
+ case "ew":
+ if (value) void query.whereILike(column, `%${sanitizeSqlLikeString(String(value))}`);
+ break;
+ case "co":
+ if (value) void query.whereILike(column, `%${sanitizeSqlLikeString(String(value))}%`);
+ break;
+ default:
+ break;
+ }
+};
+
const processDynamicQuery = (
rootQuery: Knex.QueryBuilder,
scimRootFilterAst: Filter,
- getAttributeField: (attr: string) => string | null,
+ getAttributeField: TScimAttributeResolver,
depth = 0
) => {
if (depth > 20) return;
@@ -30,78 +88,19 @@ const processDynamicQuery = (
while (stack.length) {
const { scimFilterAst, query } = stack.pop()!;
- switch (scimFilterAst.op) {
- case "eq": {
- let sanitizedValue = scimFilterAst.compValue;
- const attrPath = getAttributeField(scimFilterAst.attrPath);
- if (attrPath === `${TableName.Users}.email` && typeof sanitizedValue === "string") {
- sanitizedValue = sanitizedValue.toLowerCase();
- }
- if (attrPath) void query.where(attrPath, sanitizedValue);
- break;
- }
- case "pr": {
- const attrPath = getAttributeField(scimFilterAst.attrPath);
- if (attrPath) void query.whereNotNull(attrPath);
- break;
- }
- case "gt": {
- const attrPath = getAttributeField(scimFilterAst.attrPath);
- if (attrPath) void query.where(attrPath, ">", scimFilterAst.compValue);
- break;
- }
- case "ge": {
- const attrPath = getAttributeField(scimFilterAst.attrPath);
- if (attrPath) void query.where(attrPath, ">=", scimFilterAst.compValue);
- break;
- }
- case "lt": {
- const attrPath = getAttributeField(scimFilterAst.attrPath);
- if (attrPath) void query.where(attrPath, "<", scimFilterAst.compValue);
- break;
- }
- case "le": {
- const attrPath = getAttributeField(scimFilterAst.attrPath);
- if (attrPath) void query.where(attrPath, "<=", scimFilterAst.compValue);
- break;
- }
- case "sw": {
- const attrPath = getAttributeField(scimFilterAst.attrPath);
- if (!scimFilterAst.compValue || !attrPath) {
- throw new BadRequestError({ message: "compValue is required for sw filter" });
- }
- void query.whereILike(attrPath, `${sanitizeSqlLikeString(String(scimFilterAst.compValue))}%`);
- break;
- }
- case "ew": {
- let sanitizedValue = scimFilterAst.compValue;
- const attrPath = getAttributeField(scimFilterAst.attrPath);
- if (attrPath === `${TableName.Users}.email` && typeof sanitizedValue === "string") {
- sanitizedValue = sanitizedValue.toLowerCase();
- }
- if (attrPath && sanitizedValue)
- void query.whereILike(attrPath, `%${sanitizeSqlLikeString(String(sanitizedValue))}`);
- break;
- }
- case "co": {
- let sanitizedValue = scimFilterAst.compValue;
- const attrPath = getAttributeField(scimFilterAst.attrPath);
- if (attrPath === `${TableName.Users}.email` && typeof sanitizedValue === "string") {
- sanitizedValue = sanitizedValue.toLowerCase();
- }
- if (attrPath && sanitizedValue)
- void query.whereILike(attrPath, `%${sanitizeSqlLikeString(String(sanitizedValue))}%`);
- break;
- }
- case "ne": {
- let sanitizedValue = scimFilterAst.compValue;
- const attrPath = getAttributeField(scimFilterAst.attrPath);
- if (attrPath === `${TableName.Users}.email` && typeof sanitizedValue === "string") {
- sanitizedValue = sanitizedValue.toLowerCase();
- }
- if (attrPath) void query.whereNot(attrPath, "=", sanitizedValue);
- break;
+
+ if (isScimCompareOp(scimFilterAst)) {
+ const attr = getAttributeField(scimFilterAst.attrPath);
+ const value = "compValue" in scimFilterAst ? scimFilterAst.compValue : undefined;
+ if (typeof attr === "function") {
+ attr(query, scimFilterAst.op, value);
+ } else if (attr) {
+ applyScimComparison(query, attr, scimFilterAst.op, value);
}
+ continue;
+ }
+
+ switch (scimFilterAst.op) {
case "and": {
scimFilterAst.filters.forEach((el) => {
void query.andWhere((subQueryBuilder) => {
@@ -144,7 +143,7 @@ const processDynamicQuery = (
export const generateKnexQueryFromScim = (
rootQuery: Knex.QueryBuilder,
rootScimFilter: string,
- getAttributeField: (attr: string) => string | null
+ getAttributeField: TScimAttributeResolver
) => {
const scimRootFilterAst = parse(rootScimFilter);
return processDynamicQuery(rootQuery, scimRootFilterAst, getAttributeField);
diff --git a/backend/src/services/org/org-dal.ts b/backend/src/services/org/org-dal.ts
index 1e7fb6bc5f..4abf622062 100644
--- a/backend/src/services/org/org-dal.ts
+++ b/backend/src/services/org/org-dal.ts
@@ -27,7 +27,7 @@ import {
TFindOpt,
withTransaction
} from "@app/lib/knex";
-import { generateKnexQueryFromScim } from "@app/lib/knex/scim";
+import { applyScimComparison, generateKnexQueryFromScim, TScimAttributeResolver } from "@app/lib/knex/scim";
import { OrderByDirection } from "@app/lib/types";
import { ActorType } from "../auth/auth-type";
@@ -947,38 +947,58 @@ export const orgDALFactory = (db: TDbClient) => {
orgAuthMethod: string | undefined,
{ tx, membershipId }: { tx?: Knex; membershipId?: string } = {}
) => {
+ const conn = tx || db.replicaNode();
// Determine alias type from org auth method (default to saml for backwards compatibility)
const aliasType = orgAuthMethod === OrgAuthMethod.OIDC ? OrgAuthMethod.OIDC : OrgAuthMethod.SAML;
- const query = (tx || db.replicaNode())(TableName.Membership)
+ const resolveColumn = (attrPath: string) => {
+ switch (attrPath) {
+ case "id":
+ return `${TableName.Membership}.id`;
+ case "active":
+ return `${TableName.Membership}.isActive`;
+ case "name.givenName":
+ return `${TableName.Users}.firstName`;
+ case "name.familyName":
+ return `${TableName.Users}.lastName`;
+ case "email.value":
+ return `${TableName.Users}.email`;
+ default:
+ return null;
+ }
+ };
+
+ const resolveFilterAttribute: TScimAttributeResolver = (attrPath) => {
+ if (attrPath !== "userName") return resolveColumn(attrPath);
+ return (qb, op, value) => {
+ const matchingAliases = conn(TableName.UserAliases)
+ .select(conn.raw("1"))
+ .whereRaw("?? = ??", [`${TableName.UserAliases}.userId`, `${TableName.Membership}.actorUserId`])
+ .where(`${TableName.UserAliases}.orgId`, orgId)
+ .where(`${TableName.UserAliases}.aliasType`, aliasType);
+ if (op === "ne") {
+ applyScimComparison(matchingAliases, `${TableName.UserAliases}.externalId`, "eq", value);
+ void qb.whereNotExists(matchingAliases);
+ } else {
+ applyScimComparison(matchingAliases, `${TableName.UserAliases}.externalId`, op, value);
+ void qb.whereExists(matchingAliases);
+ }
+ };
+ };
+
+ const resolveDisplayAttribute: TScimAttributeResolver = (attrPath) =>
+ attrPath === "userName" ? `${SCIM_USER_ALIAS}.externalId` : resolveColumn(attrPath);
+
+ const query = conn(TableName.Membership)
.where(`${TableName.Membership}.scopeOrgId`, orgId)
.where(`${TableName.Membership}.scope`, AccessScope.Organization)
.whereNotNull(`${TableName.Membership}.actorUserId`)
.where((qb) => {
- // Direct membership ID filter (safe, parameterized)
if (membershipId) {
void qb.where(`${TableName.Membership}.id`, membershipId);
}
- // SCIM filter parsing (for list queries from external IdPs)
if (scimFilter) {
- void generateKnexQueryFromScim(qb, scimFilter, (attrPath) => {
- switch (attrPath) {
- case "id":
- return `${TableName.Membership}.id`;
- case "active":
- return `${TableName.Membership}.isActive`;
- case "userName":
- return `${SCIM_USER_ALIAS}.externalId`;
- case "name.givenName":
- return `${TableName.Users}.firstName`;
- case "name.familyName":
- return `${TableName.Users}.lastName`;
- case "email.value":
- return `${TableName.Users}.email`;
- default:
- return null;
- }
- });
+ void generateKnexQueryFromScim(qb, scimFilter, resolveFilterAttribute);
}
})
.join(TableName.Users, `${TableName.Users}.id`, `${TableName.Membership}.actorUserId`)
@@ -986,12 +1006,21 @@ export const orgDALFactory = (db: TDbClient) => {
.whereNull(`${TableName.Organization}.rootOrgId`)
.leftJoin(`${TableName.UserAliases} as ${SCIM_USER_ALIAS}`, function joinScimUserAlias() {
this.on(`${SCIM_USER_ALIAS}.userId`, "=", `${TableName.Membership}.actorUserId`)
- .andOn(`${SCIM_USER_ALIAS}.orgId`, "=", (tx || db).raw("?", [orgId]))
- .andOn(`${SCIM_USER_ALIAS}.aliasType`, "=", (tx || db).raw("?", [aliasType]));
+ .andOn(`${SCIM_USER_ALIAS}.orgId`, "=", conn.raw("?", [orgId]))
+ .andOn(`${SCIM_USER_ALIAS}.aliasType`, "=", conn.raw("?", [aliasType]));
})
.where({ isGhost: false });
- return query;
+ const aliasRowMatchesFilter = scimFilter
+ ? conn
+ .queryBuilder()
+ .select(conn.raw("1"))
+ .where((qb) => {
+ void generateKnexQueryFromScim(qb, scimFilter, resolveDisplayAttribute);
+ })
+ : undefined;
+
+ return { query, aliasRowMatchesFilter };
};
const findMembershipWithScimFilter = async (
@@ -1012,9 +1041,18 @@ export const orgDALFactory = (db: TDbClient) => {
})[]
> => {
try {
- // Fan out to one row per alias so a userName filter can match any of them, not just the newest.
- // DISTINCT ON collapses it back after the filter runs, so the row we keep is the one that matched.
- const dedupedMemberships = $buildScimMembershipQuery(orgId, scimFilter, orgAuthMethod, { tx, membershipId })
+ const conn = tx || db.replicaNode();
+ const { query: memberships, aliasRowMatchesFilter } = $buildScimMembershipQuery(
+ orgId,
+ scimFilter,
+ orgAuthMethod,
+ {
+ tx,
+ membershipId
+ }
+ );
+
+ const dedupedMemberships = memberships
.distinctOn(`${TableName.Membership}.id`)
.select(
selectAllTableCols(TableName.Membership),
@@ -1027,13 +1065,15 @@ export const orgDALFactory = (db: TDbClient) => {
db.ref("defaultMembershipRole").withSchema(TableName.Organization),
db.ref("externalId").withSchema(SCIM_USER_ALIAS)
)
- .orderBy([
- { column: `${TableName.Membership}.id` },
- { column: `${SCIM_USER_ALIAS}.createdAt`, order: "desc", nulls: "last" }
- ])
- .as("scim_membership");
+ .orderBy(`${TableName.Membership}.id`);
+
+ if (aliasRowMatchesFilter) {
+ void dedupedMemberships.orderByRaw("CASE WHEN EXISTS (?) THEN 0 ELSE 1 END", [aliasRowMatchesFilter]);
+ }
+
+ void dedupedMemberships.orderBy(`${SCIM_USER_ALIAS}.createdAt`, "desc", "last").as("scim_membership");
- const query = (tx || db.replicaNode()).select("*").from(dedupedMemberships);
+ const query = conn.select("*").from(dedupedMemberships);
if (sort) {
void query.orderBy(sort.map(([column, order, nulls]) => ({ column: column as string, order, nulls })));
@@ -1060,7 +1100,7 @@ export const orgDALFactory = (db: TDbClient) => {
): Promise<number> => {
try {
const doc = await $buildScimMembershipQuery(orgId, scimFilter, orgAuthMethod, { tx })
- .countDistinct(`${TableName.Membership}.id as count`)
+ .query.countDistinct(`${TableName.Membership}.id as count`)
.first();
return Number(doc?.count ?? 0);