Skip to content

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

feat: add glob and new validation

adilson92f961eb1c34
diff --git a/backend/src/ee/services/permission/permission-fns.test.ts b/backend/src/ee/services/permission/permission-fns.test.ts
index e994b68416..90cc75da67 100644
--- a/backend/src/ee/services/permission/permission-fns.test.ts
+++ b/backend/src/ee/services/permission/permission-fns.test.ts
@@ -263,28 +263,70 @@ describe("trimSuffix handlebars helper", () => {
const render = (template: string, value: string) =>
handlebarsClient.compile(template)({ identity: { auth: { kubernetes: { namespace: value } } } });
- const trim = (value: string, suffix: string) =>
- render(`{{ trimSuffix identity.auth.kubernetes.namespace '${suffix}' }}`, value);
+ const trim = (value: string, pattern: string) =>
+ render(`{{ trimSuffix identity.auth.kubernetes.namespace '${pattern}' }}`, value);
- test("trims a matching suffix", () => {
- expect(trim("myapp-prod", "-prod")).toBe("myapp");
+ test("trims a matching glob suffix", () => {
+ expect(trim("myapp-pr-1", "-pr-*")).toBe("myapp");
+ expect(trim("myapp-pr-42", "-pr-*")).toBe("myapp");
+ });
+
+ test("returns the value unchanged when nothing matches the pattern", () => {
+ expect(trim("myapp", "-pr-*")).toBe("myapp");
+ expect(trim("myapp-mr-1", "-pr-*")).toBe("myapp-mr-1");
+ });
+
+ test("removes the shortest matching suffix, not the longest", () => {
+ expect(trim("app-pr-1", "-*")).toBe("app-pr");
});
- test("returns the value unchanged when it does not end with the suffix", () => {
+ test("trims a literal suffix when the pattern has no glob syntax", () => {
+ expect(trim("myapp-prod", "-prod")).toBe("myapp");
expect(trim("myapp", "-prod")).toBe("myapp");
- expect(trim("myapp-staging", "-prod")).toBe("myapp-staging");
+ });
+
+ test("supports the wider glob syntax used by permission conditions", () => {
+ expect(trim("myapp-pr-1", "-{pr,mr}-*")).toBe("myapp");
+ expect(trim("myapp-pr-1", "-pr-?")).toBe("myapp");
});
test("leaves an unresolved attribute literal intact so the condition fails closed", () => {
- expect(trim("{{identity.auth.kubernetes.namespace}}", "-prod")).toBe("{{identity.auth.kubernetes.namespace}}");
+ expect(trim("{{identity.auth.kubernetes.namespace}}", "-pr-*")).toBe("{{identity.auth.kubernetes.namespace}}");
});
test("returns an empty string for an empty value", () => {
- expect(trim("", "-prod")).toBe("");
+ expect(trim("", "-pr-*")).toBe("");
+ });
+
+ test("returns the value unchanged for a malformed pattern instead of throwing", () => {
+ expect(trim("myapp-pr-1", "-pr-+(")).toBe("myapp-pr-1");
+ });
+
+ test("returns the value unchanged when no pattern is supplied", () => {
+ expect(render("{{ trimSuffix identity.auth.kubernetes.namespace }}", "myapp-pr-1")).toBe("myapp-pr-1");
+ });
+
+ test("trims a pattern carrying the maximum number of wildcards", () => {
+ expect(trim("myapp-1-2-3-4-5", "-*-*-*-*-*")).toBe("myapp");
+ expect(trim("myapp-1-2-3-4-5", "-?-?-?-?-?")).toBe("myapp");
+ });
+
+ test("returns the value unchanged for a pattern with more wildcards than the limit", () => {
+ expect(trim("myapp-1-2-3-4-5-6", "-*-*-*-*-*-*")).toBe("myapp-1-2-3-4-5-6");
+ expect(trim("myapp-1-2-3-4-5-6", "-?-?-?-?-?-?")).toBe("myapp-1-2-3-4-5-6");
+ });
+
+ test("counts each character of a globstar toward the wildcard limit", () => {
+ expect(trim("myapp-a-b", "-**-**")).toBe("myapp");
+ expect(trim("myapp-a-b-c", "-**-**-**")).toBe("myapp-a-b-c");
+ });
+
+ test("does not count brace expansion toward the wildcard limit", () => {
+ expect(trim("myapp-pr-1-2-3-4-5", "-{pr,mr}-*-*-*-*-*")).toBe("myapp");
});
- test("returns the value unchanged when no suffix is supplied", () => {
- expect(render("{{ trimSuffix identity.auth.kubernetes.namespace }}", "myapp-prod")).toBe("myapp-prod");
+ test("counts wildcards in the pattern, not in the value", () => {
+ expect(trim("my*app*x*y*z*w-pr-1", "-pr-*")).toBe("my*app*x*y*z*w");
});
});
describe("getProjectPermissionFingerprint", () => {
diff --git a/backend/src/ee/services/permission/permission-fns.ts b/backend/src/ee/services/permission/permission-fns.ts
index 9063553d31..bc16680e01 100644
--- a/backend/src/ee/services/permission/permission-fns.ts
+++ b/backend/src/ee/services/permission/permission-fns.ts
@@ -1,6 +1,7 @@
/* eslint-disable no-nested-ternary */
import { ForbiddenError, MongoAbility, PureAbility, RawRuleOf, subject } from "@casl/ability";
import handlebars from "handlebars";
+import picomatch from "picomatch";
import { z } from "zod";
import { SecretFolderRole, TOrganizations } from "@app/db/schemas";
@@ -435,6 +436,9 @@ const expandLegacyForbidActions = <T extends RawRuleOf<MongoAbility<ProjectPermi
});
};
+const HBS_TRIM_SUFFIX_MAX_GLOB_INPUT_LENGTH = 256;
+const HBS_TRIM_SUFFIX_MAX_GLOB_WILDCARDS = 5;
+
const hbsStripPrefix = (text: string, prefix: string) => {
const textStr = String(text || "");
if (!textStr) return textStr;
@@ -448,7 +452,31 @@ const hbsTrimSuffix = (text: string, suffix: string) => {
if (typeof suffix !== "string" || !suffix) return textStr;
- return textStr.endsWith(suffix) ? textStr.slice(0, -suffix.length) : textStr;
+ if (suffix.length > HBS_TRIM_SUFFIX_MAX_GLOB_INPUT_LENGTH) return textStr;
+
+ if (!picomatch.scan(suffix).isGlob) {
+ return textStr.endsWith(suffix) ? textStr.slice(0, -suffix.length) : textStr;
+ }
+
+ // the matcher is run once per suffix position below, so every variable-length wildcard multiplies
+ // the backtracking across that whole scan.
+ const wildcardCount = [...suffix].filter((char) => char === "*" || char === "?").length;
+ if (wildcardCount > HBS_TRIM_SUFFIX_MAX_GLOB_WILDCARDS) return textStr;
+
+ if (textStr.length > HBS_TRIM_SUFFIX_MAX_GLOB_INPUT_LENGTH) return textStr;
+
+ let isSuffixMatch: (input: string) => boolean;
+ try {
+ isSuffixMatch = picomatch(suffix, { dot: true });
+ } catch {
+ return textStr;
+ }
+
+ for (let i = textStr.length; i >= 0; i -= 1) {
+ if (isSuffixMatch(textStr.slice(i))) return textStr.slice(0, i);
+ }
+
+ return textStr;
};
const handlebarsClient = (() => {
diff --git a/docs/documentation/platform/access-controls/abac/managing-machine-identity-attributes.mdx b/docs/documentation/platform/access-controls/abac/managing-machine-identity-attributes.mdx
index 4bac751eae..5cbfac6492 100644
--- a/docs/documentation/platform/access-controls/abac/managing-machine-identity-attributes.mdx
+++ b/docs/documentation/platform/access-controls/abac/managing-machine-identity-attributes.mdx
@@ -119,18 +119,37 @@ Given `identity.auth.aws.arn` = `arn:aws:iam::123456789012:user/example-user`:
**trimSuffix**
-Removes a suffix from the end of a string. If the string does not end with the given suffix, it is returned unchanged.
+Removes a suffix from the end of a string. The suffix is a glob pattern, using the same syntax as
+the **Glob Match** condition operator, so it can match a part of the value that varies. The
+shortest matching suffix is removed. If nothing at the end of the string matches the pattern, the
+string is returned unchanged.
+
+This is useful when a namespace or resource name carries a generated trailing segment. For
+example, an ArgoCD ApplicationSet using the pull request generator creates one namespace per open
+pull request, such as `myapp-pr-1` and `myapp-pr-42`. Trimming the generated segment lets every one
+of those namespaces resolve to the same secret path as the base namespace, without writing a
+condition for each pull request.
+
+A pattern can contain at most five `*` or `?` wildcards. A pattern with more than that trims
+nothing, and the string is returned unchanged.
**Examples:**
-Given `identity.auth.kubernetes.namespace` = `myapp-prod`:
+Given `identity.auth.kubernetes.namespace` = `myapp-pr-42`:
+
+```handlebars
+{{ trimSuffix identity.auth.kubernetes.namespace '-pr-*' }} -> myapp
+```
+
+Given `identity.auth.kubernetes.namespace` = `myapp` (nothing matches the pattern):
```handlebars
-{{ trimSuffix identity.auth.kubernetes.namespace '-prod' }} -> myapp
+{{ trimSuffix identity.auth.kubernetes.namespace '-pr-*' }} -> myapp
```
-Given `identity.auth.kubernetes.namespace` = `myapp` (nothing matches the suffix):
+You can combine this with a secret path condition so both the base namespace and its pull request
+namespaces read from one path:
```handlebars
-{{ trimSuffix identity.auth.kubernetes.namespace '-prod' }} -> myapp
+/{{ trimSuffix identity.auth.kubernetes.namespace '-pr-*' }}/**
```