Skip to content

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

fix(agent-vault): refuse a variable value of only spaces

Scott Wilsoncc306557b1cb
diff --git a/backend/e2e-test/routes/v1/agent-vault.spec.ts b/backend/e2e-test/routes/v1/agent-vault.spec.ts
index c9dd578fce..8ce7a68ab4 100644
--- a/backend/e2e-test/routes/v1/agent-vault.spec.ts
+++ b/backend/e2e-test/routes/v1/agent-vault.spec.ts
@@ -1232,6 +1232,19 @@ describe("Agent Vault V1 Router", async () => {
expect(row.encryptedValue.toString("utf-8")).not.toContain("hidden_value");
});
+ test("a value can't be only spaces, and one with spaces around it is kept as typed", async () => {
+ const bundle = await createAccessBundle("variables-blank-value");
+ const blank = await inject("POST", variablesUrl(bundle.id), { key: "BLANK", value: " " });
+ expect(blank.statusCode).toBe(422);
+ expect(blank.payload).toContain("A value can't be only spaces.");
+
+ const padded = await createVariable(bundle.id, { key: "PADDED", value: " kept ", isSecret: false });
+ expect(JSON.parse((await inject("GET", variablesUrl(bundle.id))).payload).variables).toEqual([
+ expect.objectContaining({ key: "PADDED", value: " kept " })
+ ]);
+ expect((await inject("PATCH", `${variablesUrl(bundle.id)}/${padded.id}`, { value: " " })).statusCode).toBe(422);
+ });
+
test("a key is unique in its bundle and has to be upper snake case", async () => {
const bundle = await createAccessBundle("variables-keys");
await createVariable(bundle.id, { key: "TOKEN", value: "one" });
diff --git a/backend/src/ee/routes/v1/agent-vault-routers/agent-vault-schemas.ts b/backend/src/ee/routes/v1/agent-vault-routers/agent-vault-schemas.ts
index 743a82046b..6beacb29a2 100644
--- a/backend/src/ee/routes/v1/agent-vault-routers/agent-vault-schemas.ts
+++ b/backend/src/ee/routes/v1/agent-vault-routers/agent-vault-schemas.ts
@@ -239,12 +239,14 @@ export const AgentVaultVariableKeySchema = z
.regex(AGENT_VAULT_VARIABLE_KEY_RE, AGENT_VAULT_VARIABLE_KEY_MESSAGE);
// Braces are allowed and sent as they are. A value is never expanded again, which is also how a service can
-// send a literal {{ it could not take directly.
+// send a literal {{ it could not take directly. Never trimmed, since a password can start or end with a space,
+// but one that is only spaces is refused: listed or revealed, it can't be told from an empty value.
export const AgentVaultVariableValueSchema = z
.string()
.min(1)
.max(AGENT_VAULT_VARIABLE_VALUE_MAX_LENGTH)
- .regex(AGENT_VAULT_NO_CONTROL_CHARS_RE, AGENT_VAULT_NO_CONTROL_CHARS_MESSAGE);
+ .regex(AGENT_VAULT_NO_CONTROL_CHARS_RE, AGENT_VAULT_NO_CONTROL_CHARS_MESSAGE)
+ .refine((value) => value.trim().length > 0, "A value can't be only spaces.");
export const AgentVaultVariableSchema = z.object({
id: z.string().uuid().describe(AGENT_VAULT.VARIABLE.variableId),
diff --git a/frontend/src/components/agent-vault/VariableFormDialog.tsx b/frontend/src/components/agent-vault/VariableFormDialog.tsx
index ba0b748f12..e5e643ad79 100644
--- a/frontend/src/components/agent-vault/VariableFormDialog.tsx
+++ b/frontend/src/components/agent-vault/VariableFormDialog.tsx
@@ -66,7 +66,8 @@ const buildSchema = (takenKeys: string[], isValueRequired: boolean) =>
NO_CONTROL_CHARS_RE,
"A value can't contain line breaks or other control characters. Check for a stray newline if you pasted it."
)
- .refine((value) => !isValueRequired || value.length > 0, "Required"),
+ .refine((value) => !isValueRequired || value.length > 0, "Required")
+ .refine((value) => !value || value.trim().length > 0, "A value can't be only spaces."),
isSecret: z.boolean()
});