Skip to content

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

feat: add trimSuffix operation

adilsonaf7091e07d9a
diff --git a/backend/src/ee/services/permission/permission-fns.test.ts b/backend/src/ee/services/permission/permission-fns.test.ts
index 3ff6baf8ed..88bec8fa22 100644
--- a/backend/src/ee/services/permission/permission-fns.test.ts
+++ b/backend/src/ee/services/permission/permission-fns.test.ts
@@ -3,7 +3,11 @@ import { describe, expect, test } from "vitest";
import { conditionsMatcher } from "@app/lib/casl";
-import { expandLegacyForbidActions, throwIfMissingSecretReadValueOrDescribePermission } from "./permission-fns";
+import {
+ expandLegacyForbidActions,
+ handlebarsClient,
+ throwIfMissingSecretReadValueOrDescribePermission
+} from "./permission-fns";
import {
ProjectPermissionActions,
ProjectPermissionGroupActions,
@@ -251,3 +255,51 @@ describe("throwIfMissingSecretReadValueOrDescribePermission", () => {
).toThrow();
});
});
+
+describe("trimSuffix handlebars helper", () => {
+ const render = (template: string, value: string) =>
+ handlebarsClient.compile(template)({ identity: { auth: { kubernetes: { namespace: value } } } });
+
+ const trim = (value: string, pattern: string) =>
+ render(`{{ trimSuffix identity.auth.kubernetes.namespace '${pattern}' }}`, value);
+
+ 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("trims a literal suffix when the pattern has no glob syntax", () => {
+ expect(trim("myapp-prod", "-prod")).toBe("myapp");
+ expect(trim("myapp", "-prod")).toBe("myapp");
+ });
+
+ 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}}", "-pr-*")).toBe("{{identity.auth.kubernetes.namespace}}");
+ });
+
+ test("returns an empty string for an empty value", () => {
+ 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");
+ });
+});
diff --git a/backend/src/ee/services/permission/permission-fns.ts b/backend/src/ee/services/permission/permission-fns.ts
index 078461e1a1..f04ecd08fe 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 { TOrganizations } from "@app/db/schemas";
@@ -423,6 +424,8 @@ const expandLegacyForbidActions = <T extends RawRuleOf<MongoAbility<ProjectPermi
});
};
+const HBS_TRIM_SUFFIX_MAX_GLOB_INPUT_LENGTH = 256;
+
const hbsStripPrefix = (text: string, prefix: string) => {
const textStr = String(text || "");
if (!textStr) return textStr;
@@ -430,10 +433,35 @@ const hbsStripPrefix = (text: string, prefix: string) => {
return textStr.startsWith(prefix) ? textStr.substring(prefix.length) : textStr;
};
+const hbsTrimSuffix = (text: string, suffix: string) => {
+ const textStr = String(text || "");
+ if (!textStr) return textStr;
+
+ if (!picomatch.scan(suffix).isGlob) {
+ return textStr.endsWith(suffix) ? textStr.slice(0, -suffix.length) : 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 = (() => {
const hbs = handlebars.create();
hbs.registerHelper("stripPrefix", hbsStripPrefix);
+ hbs.registerHelper("trimSuffix", hbsTrimSuffix);
return hbs;
})();
diff --git a/backend/src/ee/services/project-template/project-template-service.ts b/backend/src/ee/services/project-template/project-template-service.ts
index 694b599648..51cedf691d 100644
--- a/backend/src/ee/services/project-template/project-template-service.ts
+++ b/backend/src/ee/services/project-template/project-template-service.ts
@@ -268,7 +268,7 @@ export const projectTemplateServiceFactory = ({
roles.forEach((role) => {
validateHandlebarTemplate("Project Template Role", JSON.stringify(role.permissions || []), {
allowedExpressions: (val) => val.includes("identity."),
- allowedHelpers: ["stripPrefix"],
+ allowedHelpers: ["stripPrefix", "trimSuffix"],
rejectUnescaped: true
});
});
@@ -520,7 +520,7 @@ export const projectTemplateServiceFactory = ({
roles.forEach((role) => {
validateHandlebarTemplate("Project Template Role", JSON.stringify(role.permissions || []), {
allowedExpressions: (val) => val.includes("identity."),
- allowedHelpers: ["stripPrefix"],
+ allowedHelpers: ["stripPrefix", "trimSuffix"],
rejectUnescaped: true
});
});
diff --git a/backend/src/services/additional-privilege/additional-privilege-service.ts b/backend/src/services/additional-privilege/additional-privilege-service.ts
index b4941f7d6c..78ca6fb62e 100644
--- a/backend/src/services/additional-privilege/additional-privilege-service.ts
+++ b/backend/src/services/additional-privilege/additional-privilege-service.ts
@@ -70,7 +70,7 @@ export const additionalPrivilegeServiceFactory = ({
validateHandlebarTemplate("Additional Privilege Create", JSON.stringify(data.permissions || []), {
allowedExpressions: (val) => val.includes("identity."),
- allowedHelpers: ["stripPrefix"],
+ allowedHelpers: ["stripPrefix", "trimSuffix"],
rejectUnescaped: true
});
@@ -135,7 +135,7 @@ export const additionalPrivilegeServiceFactory = ({
validateHandlebarTemplate("Additional Privilege Create", JSON.stringify(data.permissions || []), {
allowedExpressions: (val) => val.includes("identity."),
- allowedHelpers: ["stripPrefix"],
+ allowedHelpers: ["stripPrefix", "trimSuffix"],
rejectUnescaped: true
});
diff --git a/backend/src/services/role/role-service.ts b/backend/src/services/role/role-service.ts
index 0d507e3a48..4f98ad2fb0 100644
--- a/backend/src/services/role/role-service.ts
+++ b/backend/src/services/role/role-service.ts
@@ -105,7 +105,7 @@ export const roleServiceFactory = ({
validateHandlebarTemplate("Role Creation", JSON.stringify(data.permissions || []), {
allowedExpressions: (val) => val.includes("identity."),
- allowedHelpers: ["stripPrefix"],
+ allowedHelpers: ["stripPrefix", "trimSuffix"],
rejectUnescaped: true
});
@@ -152,7 +152,7 @@ export const roleServiceFactory = ({
validateHandlebarTemplate("Role Update", JSON.stringify(data.permissions || []), {
allowedExpressions: (val) => val.includes("identity."),
- allowedHelpers: ["stripPrefix"],
+ allowedHelpers: ["stripPrefix", "trimSuffix"],
rejectUnescaped: true
});
diff --git a/docs/.vale/styles/config/vocabularies/Infisical/canonical/accept.txt b/docs/.vale/styles/config/vocabularies/Infisical/canonical/accept.txt
index 71dfa1caff..9079870205 100644
--- a/docs/.vale/styles/config/vocabularies/Infisical/canonical/accept.txt
+++ b/docs/.vale/styles/config/vocabularies/Infisical/canonical/accept.txt
@@ -584,6 +584,7 @@ lastName
firstName
givenName
stripPrefix
+trimSuffix
demo_mount
db_owner
data_writer
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 95d8836ed6..4be2e1a81a 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
@@ -97,7 +97,7 @@ For methods like OIDC, these come as claims in the token and can be made availab
#### Template helper functions
-In addition to referencing attributes directly, you can use the `stripPrefix` helper function to transform attribute values within your policy templates.
+In addition to referencing attributes directly, you can use helper functions to transform attribute values within your policy templates.
**stripPrefix**
@@ -116,3 +116,37 @@ Given `identity.auth.aws.arn` = `arn:aws:iam::123456789012:user/example-user`:
```handlebars
{{ stripPrefix identity.auth.aws.arn 'arn:aws:iam::123456789012:' }} -> user/example-user
```
+
+**trimSuffix**
+
+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.
+
+**Examples:**
+
+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 '-pr-*' }} -> myapp
+```
+
+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 '-pr-*' }}/**
+```