Skip to content

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

test: enhance permission boundary tests to handle extglob syntax in deny rules

Victor Hugo dos Santosd26ff281dbab
diff --git a/backend/src/lib/casl/boundary.test.ts b/backend/src/lib/casl/boundary.test.ts
index fa22e0dfb0..468c4cdb02 100644
--- a/backend/src/lib/casl/boundary.test.ts
+++ b/backend/src/lib/casl/boundary.test.ts
@@ -845,6 +845,35 @@ describe("Validate Permission Boundary: inverted (deny) rule overlap", () => {
expect(validatePermissionBoundary(parentPermission, childPermission).isValid).toBeFalsy();
});
+ // Picomatch supports extglob syntax (e.g. `@(a|b)`, `+(a|b)`, `!(...)`) at runtime and in the
+ // $glob schema, so the disjointness heuristic used by the deny-overlap check must treat extglob
+ // metacharacters as wildcards rather than literal text. Otherwise a deny rule like
+ // `/@(secret|restricted)/**` looks disjoint from `/secret/**` even though both match
+ // `/secret/foo` at runtime.
+ test("Child overlapping extglob deny region is rejected (deny '/@(secret|restricted)/**' + child '/secret/**')", () => {
+ const parentPermission = createMongoAbility([
+ { action: "read", subject: "secrets" },
+ {
+ action: "read",
+ subject: "secrets",
+ inverted: true,
+ conditions: {
+ secretPath: { [PermissionConditionOperators.$GLOB]: "/@(secret|restricted)/**" }
+ }
+ }
+ ]);
+ const childPermission = createMongoAbility([
+ {
+ action: "read",
+ subject: "secrets",
+ conditions: {
+ secretPath: { [PermissionConditionOperators.$GLOB]: "/secret/**" }
+ }
+ }
+ ]);
+ expect(validatePermissionBoundary(parentPermission, childPermission).isValid).toBeFalsy();
+ });
+
test("Child without the deny-region's constrained field is rejected (deny path='/secret/**' + child {})", () => {
const parentPermission = createMongoAbility([
{ action: "read", subject: "secrets" },
diff --git a/backend/src/lib/casl/glob-subset.test.ts b/backend/src/lib/casl/glob-subset.test.ts
index 6ad6ae10f2..8cd9b5eabf 100644
--- a/backend/src/lib/casl/glob-subset.test.ts
+++ b/backend/src/lib/casl/glob-subset.test.ts
@@ -62,7 +62,12 @@ describe("literalPrefix", () => {
["/apps/**", "/apps/"],
["/**", "/"],
["**", ""],
- ["*foo", ""]
+ ["*foo", ""],
+ ["/@(secret|restricted)/**", "/"],
+ ["/+(foo|bar)/**", "/"],
+ ["/!(public)/**", "/"],
+ ["/?(opt)/**", "/"],
+ ["/foo(bar)/**", "/foo"]
])("literalPrefix(%s) === %s", (input, expected) => expect(literalPrefix(input)).toBe(expected));
});
@@ -73,6 +78,10 @@ describe("haveDisjointLiteralPrefixes", () => {
["/foo/bar", "/foo/baz", true],
["/foo/bar", "/foo/bar", false],
["/**", "/secret/**", false],
- ["/apps/*", "/apps/**", false]
+ ["/apps/*", "/apps/**", false],
+ // Extglob deny region must not be proven disjoint from a literal path it actually matches.
+ ["/@(secret|restricted)/**", "/secret/**", false],
+ ["/+(foo|bar)/**", "/foo/**", false],
+ ["/!(public)/**", "/secret/**", false]
])("disjoint(%s, %s) === %s", (a, b, expected) => expect(haveDisjointLiteralPrefixes(a, b)).toBe(expected));
});
diff --git a/backend/src/lib/casl/glob-subset.ts b/backend/src/lib/casl/glob-subset.ts
index a4b5022217..0eaae49b5d 100644
--- a/backend/src/lib/casl/glob-subset.ts
+++ b/backend/src/lib/casl/glob-subset.ts
@@ -97,13 +97,29 @@ export const isGlobSubsetOfGlob = (parentGlob: string, subsetGlob: string): bool
};
/**
- * Returns the static text of a glob — the prefix up to (but not including) the first glob
+ * Returns the static text of a glob — the prefix up to (but not including) the first picomatch
* metacharacter. Used by callers that need a sound disjointness heuristic for two globs.
+ *
+ * Stops at any character that picomatch may interpret specially, including extglob syntax
+ * (`@(...)`, `?(...)`, `+(...)`, `*(...)`, `!(...)`), so the returned prefix is guaranteed to be
+ * literal text.
*/
export const literalPrefix = (glob: string): string => {
for (let i = 0; i < glob.length; i += 1) {
const c = glob[i];
- if (c === "*" || c === "?" || c === "[" || c === "{") return glob.slice(0, i);
+ if (
+ c === "*" ||
+ c === "?" ||
+ c === "[" ||
+ c === "{" ||
+ c === "(" ||
+ c === ")" ||
+ c === "@" ||
+ c === "+" ||
+ c === "!"
+ ) {
+ return glob.slice(0, i);
+ }
}
return glob;
};