Skip to content

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

merge two rules into one

Matheus Nogueira8ec20a298edd
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index d80f84f788..aea1e6186c 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -4098,7 +4098,7 @@ export const SecretValidationRules = {
reusePrevention:
"Rejects a value for repeating one already in use. Omit to allow any value the other constraints accept.",
previousVersions: `How many of the secret's own previous versions the new value must differ from. Between 1 and ${MAX_PREVENT_DUPLICATE_SECRET_VALUE_VERSIONS}.`,
- otherSecrets:
+ otherSecretsInScope:
"Set to true to reject a value that another secret in the rule's scope already holds. Requires blind indexing on the project."
},
STATIC_SECRETS: {
diff --git a/backend/src/server/plugins/secret-scanner-v2.ts b/backend/src/server/plugins/secret-scanner-v2.ts
index 3c0813be32..75f4e17e91 100644
--- a/backend/src/server/plugins/secret-scanner-v2.ts
+++ b/backend/src/server/plugins/secret-scanner-v2.ts
@@ -43,8 +43,6 @@ export const registerSecretScanningV2Webhooks = async (server: FastifyZodProvide
secret: appCfg.INF_APP_CONNECTION_GITHUB_RADAR_APP_WEBHOOK_SECRET as string
});
- await probot.load(probotApp);
-
// github push event webhook
server.route({
method: "POST",
diff --git a/backend/src/services/secret-validation-rule/secret-validation-rule-schemas.ts b/backend/src/services/secret-validation-rule/secret-validation-rule-schemas.ts
index 11f19e5086..e9a78e58cd 100644
--- a/backend/src/services/secret-validation-rule/secret-validation-rule-schemas.ts
+++ b/backend/src/services/secret-validation-rule/secret-validation-rule-schemas.ts
@@ -51,7 +51,7 @@ const ReusePreventionSchema = z
.max(MAX_PREVENT_DUPLICATE_SECRET_VALUE_VERSIONS)
.optional()
.describe(SecretValidationRules.REUSE_PREVENTION.previousVersions),
- otherSecrets: z.boolean().optional().describe(SecretValidationRules.REUSE_PREVENTION.otherSecrets)
+ otherSecretsInScope: z.boolean().optional().describe(SecretValidationRules.REUSE_PREVENTION.otherSecretsInScope)
})
.refine((reusePrevention) => Object.values(reusePrevention).some((value) => value !== undefined), {
message: "Set at least one reuse prevention option, or leave reusePrevention out entirely"
@@ -108,7 +108,7 @@ export const buildValueConstraintsSchema = (target: ConstraintTarget) =>
export type TConstraints = z.infer<typeof BaseConstraintsSchema>;
-export type TReusePrevention = { previousVersions?: number; otherSecrets?: boolean };
+export type TReusePrevention = { previousVersions?: number; otherSecretsInScope?: boolean };
// a secret value additionally supports being checked against values it has already had
export type TValueConstraints = TConstraints & { reusePrevention?: TReusePrevention };
diff --git a/backend/src/services/secret-validation-rule/secret-validation-rule-service.ts b/backend/src/services/secret-validation-rule/secret-validation-rule-service.ts
index a3cdd57ff9..12bc10ba69 100644
--- a/backend/src/services/secret-validation-rule/secret-validation-rule-service.ts
+++ b/backend/src/services/secret-validation-rule/secret-validation-rule-service.ts
@@ -53,7 +53,7 @@ import { TDuplicateSecret, TStaticSecretsRuleConfig } from "./static-secrets";
/** Only static-secret rules can ask for a value no other secret already holds. */
const $wantsCrossSecretUniqueness = (config: TSecretValidationRuleConfig) =>
- Boolean((config as TStaticSecretsRuleConfig).valueConstraints?.reusePrevention?.otherSecrets);
+ Boolean((config as TStaticSecretsRuleConfig).valueConstraints?.reusePrevention?.otherSecretsInScope);
type TSecretValidationRuleServiceFactoryDep = {
secretValidationRuleDAL: TSecretValidationRuleDALFactory;
diff --git a/backend/src/services/secret-validation-rule/static-secrets/static-secrets-rule-fns.ts b/backend/src/services/secret-validation-rule/static-secrets/static-secrets-rule-fns.ts
index 5baa64d8d4..f915222a79 100644
--- a/backend/src/services/secret-validation-rule/static-secrets/static-secrets-rule-fns.ts
+++ b/backend/src/services/secret-validation-rule/static-secrets/static-secrets-rule-fns.ts
@@ -51,7 +51,7 @@ export const evaluateStaticSecretConstraints = (
}
const { duplicateOf } = secret;
- if (config.valueConstraints.reusePrevention?.otherSecrets && duplicateOf) {
+ if (config.valueConstraints.reusePrevention?.otherSecretsInScope && duplicateOf) {
violations.push({
kind: ConstraintKind.ReuseOtherSecrets,
label: CONSTRAINT_LABELS[ConstraintKind.ReuseOtherSecrets],
diff --git a/docs/documentation/platform/secret-validation-rules.mdx b/docs/documentation/platform/secret-validation-rules.mdx
index 6811d7608c..054fcba9ba 100644
--- a/docs/documentation/platform/secret-validation-rules.mdx
+++ b/docs/documentation/platform/secret-validation-rules.mdx
@@ -40,22 +40,29 @@ A rule groups its constraints by what they apply to. Set only the fields you wan
| **Regex pattern** | Regular expression the value has to match | `^[A-Z][A-Z0-9_]*$` |
| **Required prefix** | Text the value has to start with | `APP_` |
| **Required suffix** | Text the value has to end with | `_KEY` |
-| **Prevent value reuse** | Prior versions the new value has to differ from | `10` |
+| **Prevent value reuse** | Values the new value has to differ from: the secret's own prior versions, values other secrets in scope hold, or both | `10` |
Which targets a rule offers depends on its type:
- A **Static Secrets** rule constrains the secret **key**, the secret **value**, or both.
- A **Dynamic Secrets** or **Secret Rotations** rule constrains the **generated password**.
-**Prevent value reuse** is available on the secret value only. A key has no version history, and a generated credential is produced rather than submitted. It sits under its own `reusePrevention` object, which is where the options for rejecting a repeated value will live:
+**Prevent value reuse** is available on the secret value only. A key has no version history, and a generated credential is produced rather than submitted. It sits under its own `reusePrevention` object, which holds the options for rejecting a repeated value:
```json
"valueConstraints": {
- "reusePrevention": { "previousVersions": 10 }
+ "reusePrevention": {
+ "previousVersions": 10,
+ "otherSecretsInScope": true
+ }
}
```
-`previousVersions` is a whole number between 1 and 25. Leave `reusePrevention` out to accept any value the other constraints allow.
+`previousVersions` is a whole number between 1 and 25. It rejects a new value that matches any of that many of the secret's own prior versions.
+
+`otherSecretsInScope` rejects a value that another secret the rule covers already holds. It requires secret blind indexing on the project; Infisical offers to enable it when you turn the option on.
+
+Set either option on its own or both together. Leave `reusePrevention` out to accept any value the other constraints allow.
A rule has to set at least one constraint. Infisical rejects a rule that constrains nothing.
diff --git a/frontend/src/hooks/api/secretValidationRules/types.ts b/frontend/src/hooks/api/secretValidationRules/types.ts
index b47e41b18b..9b198920df 100644
--- a/frontend/src/hooks/api/secretValidationRules/types.ts
+++ b/frontend/src/hooks/api/secretValidationRules/types.ts
@@ -48,7 +48,7 @@ export type TConstraints = {
export type TReusePrevention = {
previousVersions?: number;
- otherSecrets?: boolean;
+ otherSecretsInScope?: boolean;
};
export type TValueConstraints = TConstraints & {
diff --git a/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/ConstraintCard.tsx b/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/ConstraintCard.tsx
index f4ec4fb8fc..7f0d9dc67a 100644
--- a/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/ConstraintCard.tsx
+++ b/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/ConstraintCard.tsx
@@ -10,6 +10,7 @@ import {
SelectItem,
SelectTrigger,
SelectValue,
+ Toggle,
Tooltip,
TooltipContent,
TooltipTrigger
@@ -22,7 +23,6 @@ import {
CONSTRAINT_VALUE_LABELS,
ConstraintTarget,
ConstraintType,
- isValuelessConstraint,
MAX_PREVENT_VALUE_REUSE_VERSIONS,
RuleType,
TRuleForm
@@ -54,13 +54,11 @@ export const ConstraintCard = ({ index, onRemove }: Props) => {
const Icon = constraintOption?.icon;
const placeholder = constraintOption?.placeholder;
const isPreventValueReuse = constraintType === ConstraintType.PreventValueReuse;
- const isValueless = isValuelessConstraint(constraintType);
- // Both of these only ever apply to the secret value, so there is nothing to choose.
- const hasFixedTarget = isPreventValueReuse || isValueless;
const isNumericInput =
- constraintType === ConstraintType.MinLength ||
- constraintType === ConstraintType.MaxLength ||
- constraintType === ConstraintType.PreventValueReuse;
+ constraintType === ConstraintType.MinLength || constraintType === ConstraintType.MaxLength;
+ const checkOtherSecretsInScope = watch(
+ `enforcement.constraints.${index}.checkOtherSecretsInScope`
+ );
return (
<div className="rounded-md border border-border bg-card p-4">
@@ -80,75 +78,144 @@ export const ConstraintCard = ({ index, onRemove }: Props) => {
<p className="mt-1.5 text-xs text-muted">{constraintOption.cardDescription}</p>
)}
- <div className={`mt-3 grid gap-3 ${isValueless ? "grid-cols-1" : "grid-cols-2"}`}>
- {(() => {
- if (isGeneratedCredentialRule) {
- return (
- <div className="flex flex-col gap-1">
- <label className="text-xs font-medium text-muted">Applies to</label>
- <Input value="Generated Password" readOnly className="cursor-default opacity-60" />
+ {isPreventValueReuse ? (
+ <div className="mt-3 flex flex-col">
+ <Controller
+ control={control}
+ name={`enforcement.constraints.${index}.checkOtherSecretsInScope`}
+ render={({ field: { value, onChange } }) => (
+ <div className="flex items-start justify-between gap-4 border-t border-border pt-3">
+ <div className="flex flex-col">
+ <span className="text-sm text-foreground">Other secrets in scope</span>
+ <span className="text-xs text-muted">
+ Reject values currently used by other secrets in this rule&apos;s scope
+ </span>
+ </div>
+ <Toggle
+ aria-label="Check the new value against other secrets in this rule's scope"
+ checked={Boolean(value)}
+ onCheckedChange={onChange}
+ variant="project"
+ />
</div>
- );
- }
- if (hasFixedTarget) {
+ )}
+ />
+
+ <Controller
+ control={control}
+ name={`enforcement.constraints.${index}.checkPreviousVersions`}
+ render={({ field: { value, onChange }, fieldState: { error } }) => (
+ <div className="mt-3 flex flex-col border-t border-border pt-3">
+ <div className="flex items-start justify-between gap-4">
+ <div className="flex flex-col">
+ <span className="text-sm text-foreground">
+ This secret&apos;s previous versions
+ </span>
+ <span className="text-xs text-muted">Reject values this secret held before</span>
+ </div>
+ <Toggle
+ aria-label="Check the new value against this secret's previous versions"
+ checked={Boolean(value)}
+ onCheckedChange={onChange}
+ variant="project"
+ />
+ </div>
+ {error?.message && <p className="mt-2 text-xs text-danger">{error.message}</p>}
+ {value && (
+ <div className="mt-3 flex items-center justify-between gap-4">
+ <label
+ htmlFor={`reuse-previous-versions-count-${index}`}
+ className="flex items-center gap-1 text-xs font-medium text-muted"
+ >
+ {CONSTRAINT_VALUE_LABELS[constraintType]}
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <InfoIcon className="size-3.5 text-muted" />
+ </TooltipTrigger>
+ <TooltipContent side="left" align="start" className="max-w-xs">
+ <p className="text-sm">
+ How many of the secret&apos;s own previous versions the new value must
+ differ from.
+ </p>
+ <p className="mt-2 text-xs text-muted">
+ Maximum: {MAX_PREVENT_VALUE_REUSE_VERSIONS} versions
+ </p>
+ </TooltipContent>
+ </Tooltip>
+ </label>
+ <Controller
+ control={control}
+ name={`enforcement.constraints.${index}.value`}
+ render={({ field, fieldState: { error: valueError } }) => (
+ <div className="w-24">
+ <Input
+ {...field}
+ id={`reuse-previous-versions-count-${index}`}
+ type="number"
+ min={1}
+ max={MAX_PREVENT_VALUE_REUSE_VERSIONS}
+ placeholder={placeholder?.toString() || undefined}
+ isError={Boolean(valueError)}
+ />
+ {valueError?.message && (
+ <p className="mt-1 text-xs text-danger">{valueError.message}</p>
+ )}
+ </div>
+ )}
+ />
+ </div>
+ )}
+ </div>
+ )}
+ />
+
+ {checkOtherSecretsInScope && <BlindIndexAlert />}
+ </div>
+ ) : (
+ <div className="mt-3 grid grid-cols-2 gap-3">
+ {(() => {
+ if (isGeneratedCredentialRule) {
+ return (
+ <div className="flex flex-col gap-1">
+ <label className="text-xs font-medium text-muted">Applies to</label>
+ <Input value="Generated Password" readOnly className="cursor-default opacity-60" />
+ </div>
+ );
+ }
return (
<div className="flex flex-col gap-1">
<label className="text-xs font-medium text-muted">Applies to</label>
- <Input value="Secret Value" readOnly className="cursor-default opacity-60" />
+ <Controller
+ control={control}
+ name={`enforcement.constraints.${index}.appliesTo`}
+ render={({ field: { value, onChange } }) => (
+ <Select value={value} onValueChange={onChange}>
+ <SelectTrigger>
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent position="popper">
+ <SelectItem
+ value={ConstraintTarget.SecretKey}
+ disabled={otherTargets.has(ConstraintTarget.SecretKey)}
+ >
+ Secret Key
+ </SelectItem>
+ <SelectItem
+ value={ConstraintTarget.SecretValue}
+ disabled={otherTargets.has(ConstraintTarget.SecretValue)}
+ >
+ Secret Value
+ </SelectItem>
+ </SelectContent>
+ </Select>
+ )}
+ />
</div>
);
- }
- return (
- <div className="flex flex-col gap-1">
- <label className="text-xs font-medium text-muted">Applies to</label>
- <Controller
- control={control}
- name={`enforcement.constraints.${index}.appliesTo`}
- render={({ field: { value, onChange } }) => (
- <Select value={value} onValueChange={onChange}>
- <SelectTrigger>
- <SelectValue />
- </SelectTrigger>
- <SelectContent position="popper">
- <SelectItem
- value={ConstraintTarget.SecretKey}
- disabled={otherTargets.has(ConstraintTarget.SecretKey)}
- >
- Secret Key
- </SelectItem>
- <SelectItem
- value={ConstraintTarget.SecretValue}
- disabled={otherTargets.has(ConstraintTarget.SecretValue)}
- >
- Secret Value
- </SelectItem>
- </SelectContent>
- </Select>
- )}
- />
- </div>
- );
- })()}
- {!isValueless && (
+ })()}
<div className="flex flex-col gap-1">
<label className="text-xs font-medium text-muted">
- <div className="flex items-center gap-1">
- {CONSTRAINT_VALUE_LABELS[constraintType]}
- {constraintType === ConstraintType.PreventValueReuse && (
- <Tooltip>
- <TooltipTrigger asChild>
- <InfoIcon className="ml-1 size-3.5 text-muted" />
- </TooltipTrigger>
- <TooltipContent side="left" align="start" className="max-w-xs">
- <p className="text-sm">
- When a secret is updated, its new value is validated against the specified
- number of prior versions.
- </p>
- <p className="mt-2 text-xs text-muted">Maximum: 25 versions</p>
- </TooltipContent>
- </Tooltip>
- )}
- </div>
+ {CONSTRAINT_VALUE_LABELS[constraintType]}
</label>
<Controller
control={control}
@@ -158,8 +225,6 @@ export const ConstraintCard = ({ index, onRemove }: Props) => {
<Input
{...field}
type={isNumericInput ? "number" : "text"}
- min={isPreventValueReuse ? 1 : undefined}
- max={isPreventValueReuse ? MAX_PREVENT_VALUE_REUSE_VERSIONS : undefined}
placeholder={placeholder?.toString() || undefined}
isError={Boolean(error)}
/>
@@ -168,10 +233,8 @@ export const ConstraintCard = ({ index, onRemove }: Props) => {
)}
/>
</div>
- )}
- </div>
-
- {isValueless && <BlindIndexAlert />}
+ </div>
+ )}
</div>
);
};
diff --git a/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/SecretValidationRulesSection.tsx b/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/SecretValidationRulesSection.tsx
index 82e2f272b8..7d8706da18 100644
--- a/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/SecretValidationRulesSection.tsx
+++ b/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/SecretValidationRulesSection.tsx
@@ -86,6 +86,7 @@ import {
CONSTRAINT_OPTIONS,
ConstraintTarget,
ConstraintType,
+ DEFAULT_PREVENT_VALUE_REUSE_VERSIONS,
DYNAMIC_SECRET_PROVIDER_OPTIONS,
DYNAMIC_SECRET_RULE_DISALLOWED_CONSTRAINTS,
DynamicSecretRuleProvider,
@@ -462,10 +463,13 @@ const RuleFormContent = ({
append({
type: opt.type,
appliesTo: defaultTarget,
- value:
- opt.type === ConstraintType.PreventValueReuse
- ? String(opt.placeholder || 10)
- : ""
+ ...(opt.type === ConstraintType.PreventValueReuse
+ ? {
+ value: String(DEFAULT_PREVENT_VALUE_REUSE_VERSIONS),
+ checkPreviousVersions: true,
+ checkOtherSecretsInScope: false
+ }
+ : { value: "" })
})
}
>
diff --git a/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/SecretValidationRulesSection.utils.ts b/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/SecretValidationRulesSection.utils.ts
index aef00a0dc4..2e64efcf1d 100644
--- a/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/SecretValidationRulesSection.utils.ts
+++ b/frontend/src/pages/secret-manager/SettingsPage/components/SecretValidationRulesSection/SecretValidationRulesSection.utils.ts
@@ -1,8 +1,7 @@
import {
- CopyIcon,
DatabaseIcon,
+ EqualNotIcon,
HashIcon,
- HistoryIcon,
KeyRoundIcon,
LayersIcon,
LucideIcon,
@@ -35,8 +34,7 @@ export enum ConstraintType {
RegexPattern = "regex-pattern",
RequiredPrefix = "required-prefix",
RequiredSuffix = "required-suffix",
- PreventValueReuse = "prevent-value-reuse",
- PreventDuplicateValues = "prevent-duplicate-values"
+ PreventValueReuse = "prevent-value-reuse"
}
export enum ConstraintTarget {
@@ -92,20 +90,10 @@ export const CONSTRAINT_OPTIONS: {
{
type: ConstraintType.PreventValueReuse,
label: "Prevent Value Reuse",
- description: "Prevent reusing previous secret values",
- cardDescription:
- "When a secret is updated, its new value is validated against the specified number of prior versions.",
+ description: "Reject values already in use",
+ cardDescription: "Rejects an update when the new value matches a value already in use.",
placeholder: 10,
- icon: HistoryIcon,
- allowedTargets: [ConstraintTarget.SecretValue]
- },
- {
- type: ConstraintType.PreventDuplicateValues,
- label: "Prevent Duplicate Values",
- description: "Prevent reusing a value another secret holds",
- cardDescription:
- "A secret cannot be given a value that another secret in this rule's scope already holds. Requires secret blind indexing on the project.",
- icon: CopyIcon,
+ icon: EqualNotIcon,
allowedTargets: [ConstraintTarget.SecretValue]
}
];
@@ -116,8 +104,7 @@ export const CONSTRAINT_VALUE_LABELS: Record<ConstraintType, string> = {
[ConstraintType.RegexPattern]: "Pattern",
[ConstraintType.RequiredPrefix]: "Text",
[ConstraintType.RequiredSuffix]: "Text",
- [ConstraintType.PreventValueReuse]: "Previous versions",
- [ConstraintType.PreventDuplicateValues]: "Enabled"
+ [ConstraintType.PreventValueReuse]: "Versions to check"
};
export const CONSTRAINT_TYPE_LABELS: Record<ConstraintType, string> = {
@@ -126,8 +113,7 @@ export const CONSTRAINT_TYPE_LABELS: Record<ConstraintType, string> = {
[ConstraintType.RegexPattern]: "Regex Pattern",
[ConstraintType.RequiredPrefix]: "Required Prefix",
[ConstraintType.RequiredSuffix]: "Required Suffix",
- [ConstraintType.PreventValueReuse]: "Prevent Value Reuse",
- [ConstraintType.PreventDuplicateValues]: "Prevent Duplicate Values"
+ [ConstraintType.PreventValueReuse]: "Prevent Value Reuse"
};
export const RULE_TYPE_LABELS: Record<RuleType, string> = {
@@ -180,50 +166,56 @@ export const SECRET_ROTATION_PROVIDER_OPTIONS: TProviderOption<SecretRotationRul
}
];
-// Both reuse checks are intentionally static-secret-only. For dynamic secrets
-// each lease is independent so reuse has no meaning; for rotations we drive
-// uniqueness through password generation (length/regex) rather than failing a
-// rotation at issue time because the generator happened to land on a value
-// that is already in use.
+// Reuse prevention is intentionally static-secret-only. For dynamic secrets each
+// lease is independent so reuse has no meaning; for rotations we drive uniqueness
+// through password generation (length/regex) rather than failing a rotation at
+// issue time because the generator happened to land on a value already in use.
export const DYNAMIC_SECRET_RULE_DISALLOWED_CONSTRAINTS: ConstraintType[] = [
- ConstraintType.PreventValueReuse,
- ConstraintType.PreventDuplicateValues
+ ConstraintType.PreventValueReuse
];
export const SECRET_ROTATION_RULE_DISALLOWED_CONSTRAINTS: ConstraintType[] = [
- ConstraintType.PreventValueReuse,
- ConstraintType.PreventDuplicateValues
+ ConstraintType.PreventValueReuse
];
export const MAX_PREVENT_VALUE_REUSE_VERSIONS = MAX_PREVENT_DUPLICATE_SECRET_VALUE_VERSIONS;
-
-// Switching this one on is the whole setting, so the form carries no value alongside it.
-export const isValuelessConstraint = (type: ConstraintType) =>
- type === ConstraintType.PreventDuplicateValues;
+export const DEFAULT_PREVENT_VALUE_REUSE_VERSIONS = 10;
export const constraintSchema = z
.object({
type: z.nativeEnum(ConstraintType),
appliesTo: z.nativeEnum(ConstraintTarget),
- value: z.string()
+ value: z.string(),
+ // Reuse prevention holds two independently toggleable checks; every other
+ // constraint is a single setting carried in `value`.
+ checkPreviousVersions: z.boolean().optional(),
+ checkOtherSecretsInScope: z.boolean().optional()
+ })
+ .refine((c) => c.type === ConstraintType.PreventValueReuse || c.value.length > 0, {
+ message: "Value is required",
+ path: ["value"]
})
- .refine(
- (c) =>
- c.type === ConstraintType.PreventValueReuse ||
- isValuelessConstraint(c.type) ||
- c.value.length > 0,
- {
- message: "Value is required",
- path: ["value"]
- }
- )
.superRefine((c, ctx) => {
if (c.type === ConstraintType.PreventValueReuse) {
- const num = Number(c.value);
+ if (!c.checkPreviousVersions && !c.checkOtherSecretsInScope) {
+ ctx.addIssue({
+ path: ["checkPreviousVersions"],
+ code: z.ZodIssueCode.custom,
+ message: "Turn on at least one check"
+ });
+ return;
+ }
- const isAboveMaxVersions =
- Number.isInteger(num) && (num < 1 || num > MAX_PREVENT_VALUE_REUSE_VERSIONS);
+ if (!c.checkPreviousVersions) return;
- if (isAboveMaxVersions) {
+ const num = Number(c.value);
+
+ if (!c.value.length || !Number.isInteger(num)) {
+ ctx.addIssue({
+ path: ["value"],
+ code: z.ZodIssueCode.custom,
+ message: "Number of previous versions is required"
+ });
+ } else if (num < 1 || num > MAX_PREVENT_VALUE_REUSE_VERSIONS) {
ctx.addIssue({
path: ["value"],
code: z.ZodIssueCode.custom,
@@ -327,18 +319,16 @@ const NUMERIC_CONSTRAINTS: ConstraintType[] = [ConstraintType.MinLength, Constra
export const groupConstraintsByTarget = (constraints: TConstraint[]) => {
const grouped: Partial<Record<ConstraintTarget, TValueConstraints>> = {};
- constraints.forEach(({ type, appliesTo, value }) => {
+ constraints.forEach(({ type, appliesTo, value, checkPreviousVersions, checkOtherSecretsInScope }) => {
const current = grouped[appliesTo] ?? {};
if (type === ConstraintType.PreventValueReuse) {
grouped[appliesTo] = {
...current,
- reusePrevention: { ...current.reusePrevention, previousVersions: Number(value) }
- };
- } else if (type === ConstraintType.PreventDuplicateValues) {
- grouped[appliesTo] = {
- ...current,
- reusePrevention: { ...current.reusePrevention, otherSecrets: true }
+ reusePrevention: {
+ ...(checkPreviousVersions && { previousVersions: Number(value) }),
+ ...(checkOtherSecretsInScope && { otherSecretsInScope: true })
+ }
};
} else {
grouped[appliesTo] = {
@@ -357,21 +347,20 @@ export const flattenConstraints = (
): TConstraint[] => {
const { reusePrevention, ...fields } = constraints ?? {};
- const flattened = Object.entries(fields)
+ const flattened: TConstraint[] = Object.entries(fields)
.filter(([, value]) => value !== undefined)
.map(([field, value]) => ({ type: CONSTRAINT_TYPES[field], appliesTo, value: String(value) }));
- if (reusePrevention?.previousVersions !== undefined) {
+ // `otherSecretsInScope` is absent on rules saved before the two checks were merged.
+ if (reusePrevention?.previousVersions !== undefined || reusePrevention?.otherSecretsInScope) {
flattened.push({
type: ConstraintType.PreventValueReuse,
appliesTo,
- value: String(reusePrevention.previousVersions)
+ value: String(reusePrevention.previousVersions ?? DEFAULT_PREVENT_VALUE_REUSE_VERSIONS),
+ checkPreviousVersions: reusePrevention.previousVersions !== undefined,
+ checkOtherSecretsInScope: Boolean(reusePrevention.otherSecretsInScope)
});
}
- if (reusePrevention?.otherSecrets) {
- flattened.push({ type: ConstraintType.PreventDuplicateValues, appliesTo, value: "" });
- }
-
return flattened;
};