Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
Address greptile comments
Carlos Monastyrskic574c23cfe4e
diff --git a/backend/e2e-test/routes/v1/certificate-policy-domain-components.spec.ts b/backend/e2e-test/routes/v1/certificate-policy-domain-components.spec.tsindex 1f7862a9ae..83d6ecfa63 100644--- a/backend/e2e-test/routes/v1/certificate-policy-domain-components.spec.ts+++ b/backend/e2e-test/routes/v1/certificate-policy-domain-components.spec.ts@@ -116,6 +116,18 @@ describe("Certificate policy domain component rules", async () => {expect(domainComponentRule(subjectOf(read.payload))?.allowed).toEqual([["corp", "example", "com"]]);});+ test("reads a flat deny list as one sequence per label", async () => {+ const res = await createPolicy("dc-flat-deny", [{ type: "domain_component", denied: ["evil", "bad"] }]);++ expect(res.statusCode).toBe(200);+ const policyId = trackPolicy(res.payload);++ expect(domainComponentRule(subjectOf(res.payload))?.denied).toEqual([["evil"], ["bad"]]);++ const read = await getPolicy(policyId);+ expect(domainComponentRule(subjectOf(read.payload))?.denied).toEqual([["evil"], ["bad"]]);+ });+test("leaves the rules of other subject attributes flat", async () => {const res = await createPolicy("dc-other-attributes", [{ type: "common_name", allowed: ["*.example.com"], denied: ["admin.example.com"] },diff --git a/backend/src/db/migrations/20260805143012_certificate-policy-domain-component-sequences.ts b/backend/src/db/migrations/20260805143012_certificate-policy-domain-component-sequences.tsindex dbbaf727ae..8d68db3f5c 100644--- a/backend/src/db/migrations/20260805143012_certificate-policy-domain-component-sequences.ts+++ b/backend/src/db/migrations/20260805143012_certificate-policy-domain-component-sequences.ts@@ -6,7 +6,10 @@ import { TableName } from "../schemas";import { rewriteJsonColumnInBatches } from "./utils/rewrite-json-column";const DOMAIN_COMPONENT_ATTRIBUTE_TYPE = "domain_component";-const RULE_FIELDS = ["allowed", "required", "denied"] as const;+const ORDERED_RULE_FIELDS = ["allowed", "required"] as const;+const DENIED_RULE_FIELD = "denied";++type TRewriteField = (values: unknown) => string[] | string[][] | undefined;type TStoredSubjectRule = {type?: string;@@ -21,8 +24,13 @@ const isFlatLabelList = (values: unknown): values is string[] =>const isSingleSequence = (values: unknown): values is [string[]] =>Array.isArray(values) && values.length === 1 && isFlatLabelList(values[0]);+const isSingleLabelSequenceList = (values: unknown): values is string[][] =>+ Array.isArray(values) &&+ values.length > 0 &&+ values.every((value) => Array.isArray(value) && value.length === 1 && typeof value[0] === "string");+const rewriteDomainComponentRule =- (rewriteField: (values: unknown) => string[] | string[][] | undefined) =>+ (rewriteOrderedField: TRewriteField, rewriteDeniedField: TRewriteField) =>(subject: TStoredSubjectRule[]): TStoredSubjectRule[] | undefined => {if (!Array.isArray(subject)) return undefined;@@ -31,7 +39,11 @@ const rewriteDomainComponentRule =if (rule?.type !== DOMAIN_COMPONENT_ATTRIBUTE_TYPE) return rule;const rewrittenRule = { ...rule };- for (const field of RULE_FIELDS) {+ const fields = [+ ...ORDERED_RULE_FIELDS.map((field) => [field, rewriteOrderedField] as const),+ [DENIED_RULE_FIELD, rewriteDeniedField] as const+ ];+ for (const [field, rewriteField] of fields) {const rewritten = rewriteField(rewrittenRule[field]);if (rewritten !== undefined) {rewrittenRule[field] = rewritten;@@ -58,7 +70,10 @@ export async function up(knex: Knex): Promise<void> {table: TableName.PkiCertificatePolicy,column: "subject",narrow: narrowToDomainComponentPolicies,- rewrite: rewriteDomainComponentRule((values) => (isFlatLabelList(values) ? [values] : undefined))+ rewrite: rewriteDomainComponentRule(+ (values) => (isFlatLabelList(values) ? [values] : undefined),+ (values) => (isFlatLabelList(values) ? values.map((label) => [label]) : undefined)+ )});if (migratedCount) {@@ -74,6 +89,9 @@ export async function down(knex: Knex): Promise<void> {table: TableName.PkiCertificatePolicy,column: "subject",narrow: narrowToDomainComponentPolicies,- rewrite: rewriteDomainComponentRule((values) => (isSingleSequence(values) ? values[0] : undefined))+ rewrite: rewriteDomainComponentRule(+ (values) => (isSingleSequence(values) ? values[0] : undefined),+ (values) => (isSingleLabelSequenceList(values) ? values.map(([label]) => label) : undefined)+ )});}diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.tsindex e0d645f405..f3e4154576 100644--- a/backend/src/lib/api-docs/constants.ts+++ b/backend/src/lib/api-docs/constants.ts@@ -2127,11 +2127,13 @@ export const CERTIFICATES = {const domainComponentRule = (rule: string) =>`Domain component sequences that are ${rule}, each ordered as the distinguished name reads it, most specific component first: [["*", "example", "com"]] matches DC=host,DC=example,DC=com. A request matches a sequence only when its components line up position by position, so the same labels in another order do not match. A flat list of labels is read as the single sequence it describes.`;+const DOMAIN_COMPONENT_DENIED_RULE = `Domain component sequences that are rejected, each ordered as the distinguished name reads it, most specific component first. A sequence is rejected wherever it appears in the chain, so [["example", "com"]] rejects DC=example,DC=com and DC=host,DC=example,DC=com alike. A flat list of labels rejects each label on its own, wherever it appears.`;+export const CERTIFICATE_POLICIES = {SUBJECT_DOMAIN_COMPONENT_RULE: {allowed: domainComponentRule("permitted"),required: domainComponentRule("required"),- denied: domainComponentRule("rejected")+ denied: DOMAIN_COMPONENT_DENIED_RULE}};diff --git a/backend/src/services/certificate-policy/certificate-policy-fns.test.ts b/backend/src/services/certificate-policy/certificate-policy-fns.test.tsindex c68f5145de..5c123ba565 100644--- a/backend/src/services/certificate-policy/certificate-policy-fns.test.ts+++ b/backend/src/services/certificate-policy/certificate-policy-fns.test.ts@@ -16,6 +16,12 @@ describe("normalizeSubjectRules", () => {]);});+ it("should lift a legacy flat deny list into one sequence per label", () => {+ expect(normalizeSubjectRules([{ type: "domain_component", denied: ["evil", "bad"] }])).toEqual([+ { type: "domain_component", allowed: undefined, required: undefined, denied: [["evil"], ["bad"]] }+ ]);+ });+it("should leave sequences that are already nested alone", () => {const sequences = [["corp", "example", "com"],@@ -95,7 +101,50 @@ describe("validateDomainComponentsAgainstRule", () => {it("should reject a denied sequence and report it once", () => {const errors = validate(["evil", "example", "com"], domainComponentRule({ denied: [["evil", "example", "com"]] }));- expect(errors).toEqual(["Domain components 'DC=evil,DC=example,DC=com' are denied by template policy"]);+ expect(errors).toEqual([+ "Domain components 'DC=evil,DC=example,DC=com' are denied by template policy. Denied sequences: 'DC=evil,DC=example,DC=com'"+ ]);+ });++ it("should deny everything under a denied sequence", () => {+ const rule = domainComponentRule({ denied: [["corp", "example", "com"]] });+ expect(validate(["dev", "corp", "example", "com"], rule)).toHaveLength(1);+ expect(validate(["other", "example", "com"], rule)).toEqual([]);+ expect(validate(["example", "com"], rule)).toEqual([]);+ });++ it("should deny a label wherever it appears in the chain", () => {+ const rule = domainComponentRule({ denied: [["evil"]] });+ expect(validate(["evil"], rule)).toHaveLength(1);+ expect(validate(["evil", "example"], rule)).toHaveLength(1);+ expect(validate(["a", "evil", "b"], rule)).toHaveLength(1);+ expect(validate(["good", "example"], rule)).toEqual([]);+ });++ it("should keep each label of a legacy flat deny list denied on its own", () => {+ const legacyRule = {+ type: CertSubjectAttributeType.DOMAIN_COMPONENT,+ denied: ["evil", "bad"]+ } as unknown as TDomainComponentSubjectRule;++ expect(validate(["evil"], legacyRule)).toHaveLength(1);+ expect(validate(["bad", "example"], legacyRule)).toHaveLength(1);+ expect(validate(["ok", "example"], legacyRule)).toEqual([]);+ });++ it("should deny one subtree while still allowing its siblings", () => {+ const rule = domainComponentRule({+ allowed: [["*", "example", "com"]],+ denied: [["dev", "example", "com"]]+ });+ expect(validate(["prod", "example", "com"], rule)).toEqual([]);+ expect(validate(["dev", "example", "com"], rule)).toHaveLength(1);+ });++ it("should honour a wildcard inside a denied sequence", () => {+ const rule = domainComponentRule({ denied: [["*", "internal", "com"]] });+ expect(validate(["a", "internal", "com"], rule)).toHaveLength(1);+ expect(validate(["internal", "com"], rule)).toEqual([]);});it("should treat a rule with no populated list as no constraint", () => {diff --git a/backend/src/services/certificate-policy/certificate-policy-fns.ts b/backend/src/services/certificate-policy/certificate-policy-fns.tsindex 5a3a3687e5..ec572ad60c 100644--- a/backend/src/services/certificate-policy/certificate-policy-fns.ts+++ b/backend/src/services/certificate-policy/certificate-policy-fns.ts@@ -6,12 +6,9 @@ import { TDomainComponentSubjectRule, TSubjectRule } from "./certificate-policy-export const isWildcardPattern = (value: string): boolean => value.includes("*");const createWildcardRegex = (pattern: string): RE2 => {- const wildcardRegex = new RE2(/\*/g);- const withPlaceholder = pattern.replace(wildcardRegex, "__WILDCARD__");const escapeRegex = new RE2(/[.+?^${}()|[\]\\]/g);- const escaped = withPlaceholder.replace(escapeRegex, "\\$&");- const placeholderRegex = new RE2(/__WILDCARD__/g);- return new RE2(`^${escaped.replace(placeholderRegex, ".*")}$`);+ const wildcardRegex = new RE2(/\*/g);+ return new RE2(`^${pattern.replace(escapeRegex, "\\$&").replace(wildcardRegex, ".*")}$`);};// both sides must already be normalized by the caller (lower-cased, or URI-normalized for URI SANs)@@ -45,6 +42,19 @@ const matchesDomainComponentSequence = (requestSequence: string[], policySequencconst matchesAnySequence = (requestSequence: string[], policySequences: string[][]): boolean =>policySequences.some((policySequence) => matchesDomainComponentSequence(requestSequence, policySequence));+const containsSequence = (requestSequence: string[], policySequence: string[]): boolean => {+ if (policySequence.length === 0 || policySequence.length > requestSequence.length) return false;++ return requestSequence.some(+ (_, offset) =>+ offset + policySequence.length <= requestSequence.length &&+ matchesDomainComponentSequence(requestSequence.slice(offset, offset + policySequence.length), policySequence)+ );+};++const containsAnySequence = (requestSequence: string[], policySequences: string[][]): boolean =>+ policySequences.some((policySequence) => containsSequence(requestSequence, policySequence));+const isSequenceOfLabels = (value: unknown): value is string[] =>Array.isArray(value) && value.every((component) => typeof component === "string");@@ -54,12 +64,17 @@ const normalizeDomainComponentSequences = (values: unknown): unknown => {return isSequenceOfLabels(values) ? [values] : values;};+const normalizeDeniedDomainComponentSequences = (values: unknown): unknown => {+ if (!Array.isArray(values) || values.length === 0) return values;+ return isSequenceOfLabels(values) ? values.map((label) => [label]) : values;+};+const normalizeDomainComponentRule = (rule: TDomainComponentSubjectRule): TDomainComponentSubjectRule =>({type: rule.type,allowed: normalizeDomainComponentSequences(rule.allowed),required: normalizeDomainComponentSequences(rule.required),- denied: normalizeDomainComponentSequences(rule.denied)+ denied: normalizeDeniedDomainComponentSequences(rule.denied)}) as TDomainComponentSubjectRule;export const normalizeSubjectRules = (rules: unknown): unknown => {@@ -109,9 +124,11 @@ export const validateDomainComponentsAgainstRule = ({const hasDomainComponents = requestDomainComponents.length > 0;const requestSequence = formatDomainComponentSequence(requestDomainComponents);- const isDenied = hasDomainComponents && matchesAnySequence(requestDomainComponents, denied);+ const isDenied = hasDomainComponents && containsAnySequence(requestDomainComponents, denied);if (isDenied) {- errors.push(`Domain components '${requestSequence}' are denied by template policy`);+ errors.push(+ `Domain components '${requestSequence}' are denied by template policy. Denied sequences: ${formatDomainComponentSequences(denied)}`+ );}const satisfiesRequired = required.length > 0 && matchesAnySequence(requestDomainComponents, required);diff --git a/backend/src/services/certificate-policy/certificate-policy-schemas.ts b/backend/src/services/certificate-policy/certificate-policy-schemas.tsindex 5c457213c6..f2d86dc4fd 100644--- a/backend/src/services/certificate-policy/certificate-policy-schemas.ts+++ b/backend/src/services/certificate-policy/certificate-policy-schemas.ts@@ -33,7 +33,11 @@ const requestSubjectValueSchema = z.min(1, "Value cannot be empty").max(PKI_TEXT_COLUMN_MAX_LENGTH, `Value cannot exceed ${PKI_TEXT_COLUMN_MAX_LENGTH} characters`);-const buildDomainComponentSequenceListSchema = (componentSchema: z.ZodType<string>, isRequest: boolean) => {+const buildDomainComponentSequenceListSchema = (+ componentSchema: z.ZodType<string>,+ isRequest: boolean,+ liftFlatList: (labels: string[]) => string[][]+) => {const sequenceSchema = isRequest? z.array(componentSchema)@@ -43,7 +47,7 @@ const buildDomainComponentSequenceListSchema = (componentSchema: z.ZodType<strinconst listSchema = z.union([sequenceSchema, z.array(sequenceSchema)]).transform((value): string[][] => {const isFlatLabelList = value.length > 0 && value.every((entry) => typeof entry === "string");- return isFlatLabelList ? [value as string[]] : (value as string[][]);+ return isFlatLabelList ? liftFlatList(value as string[]) : (value as string[][]);});if (!isRequest) return listSchema;@@ -55,9 +59,12 @@ const buildDomainComponentSequenceListSchema = (componentSchema: z.ZodType<strin};const buildPolicySubjectSchema = (valueSchema: z.ZodType<string>, isRequest: boolean) => {- const domainComponentListSchema = buildDomainComponentSequenceListSchema(- isRequest ? domainComponentSchema : z.string(),- isRequest+ const componentSchema = isRequest ? domainComponentSchema : z.string();+ const domainComponentListSchema = buildDomainComponentSequenceListSchema(componentSchema, isRequest, (labels) => [+ labels+ ]);+ const deniedDomainComponentListSchema = buildDomainComponentSequenceListSchema(componentSchema, isRequest, (labels) =>+ labels.map((label) => [label]));const singleValuedSchema = z.object({@@ -73,7 +80,9 @@ const buildPolicySubjectSchema = (valueSchema: z.ZodType<string>, isRequest: boorequired: domainComponentListSchema.optional().describe(CERTIFICATE_POLICIES.SUBJECT_DOMAIN_COMPONENT_RULE.required),- denied: domainComponentListSchema.optional().describe(CERTIFICATE_POLICIES.SUBJECT_DOMAIN_COMPONENT_RULE.denied)+ denied: deniedDomainComponentListSchema+ .optional()+ .describe(CERTIFICATE_POLICIES.SUBJECT_DOMAIN_COMPONENT_RULE.denied)});const unionSchema = z.discriminatedUnion("type", [singleValuedSchema, domainComponentSubjectSchema]);diff --git a/backend/src/services/certificate-policy/certificate-policy-service.test.ts b/backend/src/services/certificate-policy/certificate-policy-service.test.tsindex 343a3db82a..7eff159b93 100644--- a/backend/src/services/certificate-policy/certificate-policy-service.test.ts+++ b/backend/src/services/certificate-policy/certificate-policy-service.test.ts@@ -868,7 +868,9 @@ describe("CertificatePolicyService", () => {domainComponents: ["app", "example", "auth"]});expect(denied.isValid).toBe(false);- expect(denied.errors).toContain("Domain components 'DC=app,DC=example,DC=auth' are denied by template policy");+ expect(denied.errors).toContain(+ "Domain components 'DC=app,DC=example,DC=auth' are denied by template policy. Denied sequences: 'DC=app,DC=example,DC=auth'"+ );const reordered = await service.validateCertificateRequest("template-123", {commonName: "auth-AD-MANAGER02-CA",diff --git a/docs/documentation/platform/pki/settings/policies.mdx b/docs/documentation/platform/pki/settings/policies.mdxindex 505514a291..877e523513 100644--- a/docs/documentation/platform/pki/settings/policies.mdx+++ b/docs/documentation/platform/pki/settings/policies.mdx@@ -102,6 +102,9 @@ For each attribute, configure enforcement:rule as one from OpenSSL.- Wildcards apply within a single component. `*,example,com` matches any three-component sequenceending in `example`, `com`.+ - Deny is the exception to position-by-position matching: a denied sequence is rejected wherever it+ appears in the chain, so denying `example,com` also rejects `CN=host,DC=corp,DC=example,DC=com`.+ Denying a domain therefore denies everything under it.- Add one rule per sequence you want to accept, deny, or require.</Note>diff --git a/frontend/src/pages/cert-manager/PoliciesPage/components/CertificatePoliciesTab/CreatePolicyModal.tsx b/frontend/src/pages/cert-manager/PoliciesPage/components/CertificatePoliciesTab/CreatePolicyModal.tsxindex 80676d83a3..abe65929cc 100644--- a/frontend/src/pages/cert-manager/PoliciesPage/components/CertificatePoliciesTab/CreatePolicyModal.tsx+++ b/frontend/src/pages/cert-manager/PoliciesPage/components/CertificatePoliciesTab/CreatePolicyModal.tsx@@ -216,7 +216,7 @@ const STEPS = [}] as const;-const DomainComponentHelp = () => (+const DomainComponentHelp = ({ isDenyRule }: { isDenyRule: boolean }) => (<Tooltip><TooltipTrigger asChild><IconButton variant="ghost" size="xs" aria-label="Domain component format">@@ -231,11 +231,18 @@ const DomainComponentHelp = () => (<dl className="mt-2 grid grid-cols-[auto_1fr] gap-x-3 gap-y-0.5"><dt className="text-muted">Value</dt><dd className="font-mono">corp,example,com</dd>- <dt className="text-muted">Matches</dt>+ <dt className="text-muted">{isDenyRule ? "Rejects" : "Matches"}</dt><dd className="font-mono">DC=corp,DC=example,DC=com</dd>- <dt className="text-muted">Rejects</dt>- <dd className="font-mono">DC=com,DC=example,DC=corp</dd>+ <dt className="text-muted">{isDenyRule ? "Also rejects" : "Rejects"}</dt>+ <dd className="font-mono">+ {isDenyRule ? "DC=host,DC=corp,DC=example,DC=com" : "DC=com,DC=example,DC=corp"}+ </dd></dl>+ <p className="mt-2">+ {isDenyRule+ ? "A denied chain is rejected wherever it appears, so denying a domain denies everything under it."+ : "A request matches only when its components line up position by position."}+ </p><p className="mt-2">A <span className="font-mono">*</span> matches a single component:{" "}<span className="font-mono">*,internal,example,com</span>.@@ -1232,7 +1239,9 @@ export const CreatePolicyModal = ({</SelectContent></Select>{attr.type === CertSubjectAttributeType.DOMAIN_COMPONENT && (- <DomainComponentHelp />+ <DomainComponentHelp+ isDenyRule={attr.include === CertSubjectAttributeInclude.PROHIBIT}+ />)}<Buttontype="button"