Skip to content

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

fix(template): reject unescaped expressions in permission templates

Vlad Ligai94552dfbdc09
diff --git a/backend/src/lib/template/validate-handlebars.test.ts b/backend/src/lib/template/validate-handlebars.test.ts
new file mode 100644
index 0000000000..5d1fb50c08
--- /dev/null
+++ b/backend/src/lib/template/validate-handlebars.test.ts
@@ -0,0 +1,53 @@
+import { describe, expect, it, vi } from "vitest";
+
+// logger is initialized at app boot and is undefined under unit tests; stub it so the reject path
+// surfaces the BadRequestError rather than a "logger is undefined" error.
+vi.mock("@app/lib/logger", () => ({
+ logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn(), debug: vi.fn() }
+}));
+
+// eslint-disable-next-line import/first
+import { isValidHandleBarTemplate, validateHandlebarTemplate } from "./validate-handlebars";
+
+const allow = (expression: string) => expression.includes("identity");
+const lenient = { allowedExpressions: allow }; // default: escaping not enforced (e.g. dynamic-secret callers)
+const strict = { allowedExpressions: allow, rejectUnescaped: true }; // permission callers (role / additional-privilege)
+
+describe("validateHandlebarTemplate", () => {
+ it("accepts an escaped, allowed expression (strict and lenient)", () => {
+ expect(() => validateHandlebarTemplate("test", "{{identity.id}}", strict)).not.toThrow();
+ expect(() => validateHandlebarTemplate("test", "{{identity.id}}", lenient)).not.toThrow();
+ });
+
+ it("strict: rejects an unescaped triple-mustache expression even when the path is allowed", () => {
+ expect(() => validateHandlebarTemplate("test", "{{{identity.id}}}", strict)).toThrow(
+ /Template sanitization failed/
+ );
+ });
+
+ it("strict: rejects the unescaped ampersand form ({{& }}) even when the path is allowed", () => {
+ expect(() => validateHandlebarTemplate("test", "{{&identity.id}}", strict)).toThrow(/Template sanitization failed/);
+ });
+
+ it("lenient (default): allows an unescaped expression, preserving behavior for non-permission callers", () => {
+ expect(() => validateHandlebarTemplate("test", "{{{identity.id}}}", lenient)).not.toThrow();
+ });
+
+ it("rejects a disallowed expression regardless of mode", () => {
+ expect(() => validateHandlebarTemplate("test", "{{secret.value}}", strict)).toThrow(/Template sanitization failed/);
+ });
+});
+
+describe("isValidHandleBarTemplate", () => {
+ it("returns true for an escaped, allowed expression", () => {
+ expect(isValidHandleBarTemplate("{{identity.id}}", strict)).toBe(true);
+ });
+
+ it("strict: returns false for an unescaped triple-mustache expression even when the path is allowed", () => {
+ expect(isValidHandleBarTemplate("{{{identity.id}}}", strict)).toBe(false);
+ });
+
+ it("lenient (default): returns true for an unescaped expression with an allowed path", () => {
+ expect(isValidHandleBarTemplate("{{{identity.id}}}", lenient)).toBe(true);
+ });
+});
diff --git a/backend/src/lib/template/validate-handlebars.ts b/backend/src/lib/template/validate-handlebars.ts
index ac0447793c..9c8f14a6b9 100644
--- a/backend/src/lib/template/validate-handlebars.ts
+++ b/backend/src/lib/template/validate-handlebars.ts
@@ -6,6 +6,9 @@ import { logger } from "../logger";
type SanitizationArg = {
allowedExpressions?: (arg: string) => boolean;
allowedHelpers?: string[];
+ // When true, accept only escaped expressions ({{ }}) and reject unescaped ones ({{{ }}} / {{& }}).
+ // Opt-in so callers that render with noEscape (e.g. dynamic-secret statements) keep current behavior.
+ rejectUnescaped?: boolean;
};
const isValidExpression = (expression: string, dto: SanitizationArg): boolean => {
@@ -23,8 +26,15 @@ export const validateHandlebarTemplate = (templateName: string, template: string
parsedAst.body.forEach((el) => {
if (el.type === "ContentStatement") return;
if (el.type === "MustacheStatement" && "path" in el) {
- const { path } = el as { type: "MustacheStatement"; path: { type: "PathExpression"; original: string } };
- if (path.type === "PathExpression" && isValidExpression(path.original, dto)) return;
+ const { path, escaped } = el as {
+ type: "MustacheStatement";
+ escaped?: boolean;
+ path: { type: "PathExpression"; original: string };
+ };
+ // When rejectUnescaped is set, accept only escaped expressions ({{ }}); otherwise escaping is not
+ // enforced (preserving existing behavior for callers that render with noEscape).
+ const escapedOk = !dto.rejectUnescaped || escaped === true;
+ if (escapedOk && path.type === "PathExpression" && isValidExpression(path.original, dto)) return;
}
logger.error(el, "Template sanitization failed");
throw new BadRequestError({ message: `Template sanitization failed: ${templateName}` });
@@ -36,8 +46,15 @@ export const isValidHandleBarTemplate = (template: string, dto: SanitizationArg)
return parsedAst.body.every((el) => {
if (el.type === "ContentStatement") return true;
if (el.type === "MustacheStatement" && "path" in el) {
- const { path } = el as { type: "MustacheStatement"; path: { type: "PathExpression"; original: string } };
- if (path.type === "PathExpression" && isValidExpression(path.original, dto)) return true;
+ const { path, escaped } = el as {
+ type: "MustacheStatement";
+ escaped?: boolean;
+ path: { type: "PathExpression"; original: string };
+ };
+ // When rejectUnescaped is set, only escaped expressions ({{ }}) are permitted; otherwise escaping
+ // is not enforced (preserving existing behavior for callers that render with noEscape).
+ const escapedOk = !dto.rejectUnescaped || escaped === true;
+ if (escapedOk && path.type === "PathExpression" && isValidExpression(path.original, dto)) return true;
}
return false;
});
diff --git a/backend/src/services/additional-privilege/additional-privilege-service.ts b/backend/src/services/additional-privilege/additional-privilege-service.ts
index a05516de8c..b4941f7d6c 100644
--- a/backend/src/services/additional-privilege/additional-privilege-service.ts
+++ b/backend/src/services/additional-privilege/additional-privilege-service.ts
@@ -70,7 +70,8 @@ export const additionalPrivilegeServiceFactory = ({
validateHandlebarTemplate("Additional Privilege Create", JSON.stringify(data.permissions || []), {
allowedExpressions: (val) => val.includes("identity."),
- allowedHelpers: ["stripPrefix"]
+ allowedHelpers: ["stripPrefix"],
+ rejectUnescaped: true
});
if (!data.isTemporary) {
@@ -134,7 +135,8 @@ export const additionalPrivilegeServiceFactory = ({
validateHandlebarTemplate("Additional Privilege Create", JSON.stringify(data.permissions || []), {
allowedExpressions: (val) => val.includes("identity."),
- allowedHelpers: ["stripPrefix"]
+ allowedHelpers: ["stripPrefix"],
+ rejectUnescaped: true
});
const updatedData = { ...existingPrivilege, ...data };
diff --git a/backend/src/services/role/role-service.ts b/backend/src/services/role/role-service.ts
index aebe0792bd..0d507e3a48 100644
--- a/backend/src/services/role/role-service.ts
+++ b/backend/src/services/role/role-service.ts
@@ -105,7 +105,8 @@ export const roleServiceFactory = ({
validateHandlebarTemplate("Role Creation", JSON.stringify(data.permissions || []), {
allowedExpressions: (val) => val.includes("identity."),
- allowedHelpers: ["stripPrefix"]
+ allowedHelpers: ["stripPrefix"],
+ rejectUnescaped: true
});
const role = await roleDAL.create({
@@ -151,7 +152,8 @@ export const roleServiceFactory = ({
validateHandlebarTemplate("Role Update", JSON.stringify(data.permissions || []), {
allowedExpressions: (val) => val.includes("identity."),
- allowedHelpers: ["stripPrefix"]
+ allowedHelpers: ["stripPrefix"],
+ rejectUnescaped: true
});
const role = await roleDAL.updateById(existingRole.id, {