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
fix(agent-vault): bound filled-in fields by the reference limit
Scott Wilson088e084f1713
diff --git a/backend/e2e-test/routes/v1/agent-vault.spec.ts b/backend/e2e-test/routes/v1/agent-vault.spec.tsindex e7a14107ab..8b12d460ac 100644--- a/backend/e2e-test/routes/v1/agent-vault.spec.ts+++ b/backend/e2e-test/routes/v1/agent-vault.spec.ts@@ -1005,75 +1005,44 @@ describe("Agent Vault V1 Router", async () => {expect(resolved.substitutions[0].value).toBe("org-42");});- test("a save its variables would fill past 8,192 characters is refused, from the service and from the variable", async () => {- const bundle = await createAccessBundle("variables-overlong");- await createVariable(bundle.id, { key: "LONG", value: "x".repeat(8192) });- const short = await createVariable(bundle.id, { key: "SHORT", value: "s" });-- const doubled = await inject("POST", servicesUrl(bundle.id), {- name: "doubled",- hostPattern: "doubled.example.com",- credential: { type: "bearer", value: "{{LONG}}{{LONG}}" }- });- expect(doubled.statusCode).toBe(400);- expect(JSON.parse(doubled.payload).message).toContain("The token would be longer than 8192 characters");+ test("a field at the reference limit reaches the host whole with its variables at their longest, from either save", async () => {+ const bundle = await createAccessBundle("variables-longest");+ const long = await createVariable(bundle.id, { key: "LONG", value: "x" });const created = await inject("POST", servicesUrl(bundle.id), {- name: "at-cap",- hostPattern: "at-cap.example.com",- credential: { type: "bearer", value: "{{SHORT}}{{SHORT}}" },- customHeaders: [{ name: "X-Long", value: "{{LONG}}" }]+ name: "longest",+ hostPattern: "longest.example.com",+ credential: { type: "bearer", value: "{{LONG}}".repeat(3) }});expect(created.statusCode).toBe(200);- const { service } = JSON.parse(created.payload) as { service: { id: string; customHeaders: { id: string }[] } };-- const prefixed = await inject("PATCH", `${servicesUrl(bundle.id)}/${service.id}`, {- customHeaders: [{ id: service.customHeaders[0].id, name: "X-Long", value: "x{{LONG}}" }]- });- expect(prefixed.statusCode).toBe(400);- expect(JSON.parse(prefixed.payload).message).toContain("The custom header 'X-Long' would be longer");- // The token uses SHORT twice, so half the cap is the most SHORT can hold.- const grown = await inject("PATCH", `${variablesUrl(bundle.id)}/${short.id}`, { value: "y".repeat(4097) });- expect(grown.statusCode).toBe(400);- expect(JSON.parse(grown.payload).message).toContain("the token of the service 'at-cap' would be longer");+ // Nothing measures a field filled in, so a value change can't be refused on a service's behalf.+ const grown = await inject("PATCH", `${variablesUrl(bundle.id)}/${long.id}`, { value: "y".repeat(8192) });+ expect(grown.statusCode).toBe(200);- const resolve = await resolverFor(bundle, "variables-overlong");- expect((await resolve()).services[0].credential).toMatchObject({ value: "ss" });-- const atCap = await inject("PATCH", `${variablesUrl(bundle.id)}/${short.id}`, { value: "y".repeat(4096) });- expect(atCap.statusCode).toBe(200);- const [resolved] = (await resolve()).services;- expect(resolved.credential).toMatchObject({ value: "y".repeat(8192) });- expect(resolved.customHeaders[0].value).toBe("x".repeat(8192));+ const resolve = await resolverFor(bundle, "variables-longest");+ expect((await resolve()).services[0].credential).toMatchObject({ value: "y".repeat(8192 * 3) });});- test("a field that ends up past 8,192 characters anyway is sent as stored, and its service keeps its restrictions", async () => {- const bundle = await createAccessBundle("variables-oversized");- const long = await createVariable(bundle.id, { key: "LONG", value: "x" });+ test("a stored id with no variable behind it is sent as stored, and its service keeps its restrictions", async () => {+ const bundle = await createAccessBundle("variables-missing");+ const gone = await createVariable(bundle.id, { key: "GONE", value: "x" });const created = await inject("POST", servicesUrl(bundle.id), {- name: "oversized",+ name: "missing",hostPattern: "api.example.com",allowedMethods: ["GET"],allowedPathPrefixes: ["/v1"],- credential: { type: "bearer", value: "{{LONG}}{{LONG}}" },- customHeaders: [{ name: "X-Long", value: "{{LONG}}" }]+ credential: { type: "bearer", value: "{{GONE}}" }});expect(created.statusCode).toBe(200);- // Sealed past the value check, the way a change that raced the service save would land.- const { encryptor } = await cipherFor(bundle.id);- await testDb("agent_vault_variables")- .where({ id: long.id })- .update({- encryptedValue: encryptor({ plainText: Buffer.from(JSON.stringify({ value: "x".repeat(8192) })) })- .cipherTextBlob- });+ // The delete refusal never lets this happen, so the rows go straight from the database.+ await testDb("agent_vault_service_variable_references").where({ variableId: gone.id }).delete();+ await testDb("agent_vault_variables").where({ id: gone.id }).delete();- const resolve = await resolverFor(bundle, "variables-oversized");+ const resolve = await resolverFor(bundle, "variables-missing");const [resolved] = (await resolve()).services;- expect(resolved.credential).toMatchObject({ type: "bearer", value: `{{${long.id}}}{{${long.id}}}` });- expect(resolved.customHeaders[0].value).toBe("x".repeat(8192));+ expect(resolved.credential).toMatchObject({ type: "bearer", value: `{{${gone.id}}}` });expect(resolved.allowedMethods).toEqual(["GET"]);expect(resolved.allowedPathPrefixes).toEqual(["/v1"]);});@@ -1179,24 +1148,24 @@ describe("Agent Vault V1 Router", async () => {expect(await testDb("agent_vault_services").where({ accessBundleId: bundle.id })).toHaveLength(0);});- test("a value takes ten variable references, a repeat counted each time, and an eleventh fails validation", async () => {+ test("a value takes three variable references, a repeat counted each time, and a fourth fails validation", async () => {const bundle = await createAccessBundle("variables-reference-limit");await createVariable(bundle.id, { key: "TOKEN", value: "t" });const atLimit = await inject("POST", servicesUrl(bundle.id), {name: "at-limit",hostPattern: "at-limit.example.com",- credential: { type: "bearer", value: "{{TOKEN}}".repeat(10) }+ credential: { type: "bearer", value: "{{TOKEN}}".repeat(3) }});expect(atLimit.statusCode).toBe(200);const overLimit = await inject("POST", servicesUrl(bundle.id), {name: "over-limit",hostPattern: "over-limit.example.com",- credential: { type: "bearer", value: "{{TOKEN}}".repeat(11) }+ credential: { type: "bearer", value: "{{TOKEN}}".repeat(4) }});expect(overLimit.statusCode).toBe(422);- expect(overLimit.payload).toContain("at most 10 variable references");+ expect(overLimit.payload).toContain("at most 3 variable references");const services = (await testDb("agent_vault_services").where({ accessBundleId: bundle.id })) as {name: string;diff --git a/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-service.ts b/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-service.tsindex a253a850b8..df377fbf9e 100644--- a/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-service.ts+++ b/backend/src/ee/services/agent-vault-access-bundle/agent-vault-access-bundle-service.ts@@ -44,9 +44,7 @@ import {} from "../agent-vault/agent-vault-enums";import { getAgentVaultReachability } from "../agent-vault/agent-vault-permission";import {- AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH,AGENT_VAULT_MAX_VARIABLES,- filledInVariableLength,findStoredVariableIds,findVariableKeys,toStoredVariableReferences,@@ -497,66 +495,6 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBundreturn { idOfKey, variableIds: new Set(variables.map((variable) => variable.id)) };};- // What an error calls each part of a sealed credential.- const CREDENTIAL_PART_NOUNS: Record<string, string> = { value: "token", username: "username", password: "password" };-- // Resolve sends a field its variables would fill in past the cap as stored text, which reaches the host in place- // of the credential, so every write that could build one measures it first. Measured before the lock, so a- // service save racing a change to one of its variables can still land a field over the cap, and resolve then- // holds it there.- const findOverlongText = async <T extends { text: string }>({- projectId,- accessBundleId,- texts,- cipher,- replacing- }: {- projectId: string;- accessBundleId: string;- texts: T[];- cipher: TProjectCipher | null;- replacing?: { variableId: string; value: string };- }): Promise<T | undefined> => {- const variableIds = new Set(texts.flatMap(({ text }) => findStoredVariableIds(text)));- if (replacing) variableIds.delete(replacing.variableId);-- const lengthOf = new Map<string, number>();- if (variableIds.size) {- const [variables, { decryptor }] = await Promise.all([- agentVaultVariableDAL.findByAccessBundleId(accessBundleId),- cipher ?? getProjectCipher(projectId)- ]);- variables.forEach((variable) => {- if (variableIds.has(variable.id)) {- lengthOf.set(variable.id, openVariableValue(decryptor, variable.encryptedValue).length);- }- });- }- if (replacing) lengthOf.set(replacing.variableId, replacing.value.length);-- return texts.find(- ({ text }) =>- filledInVariableLength(text, (variableId) => lengthOf.get(variableId)) > AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH- );- };-- const assertServiceFieldsFitWhenFilledIn = async (params: {- projectId: string;- accessBundleId: string;- texts: TReferencingText[];- cipher: TProjectCipher | null;- }) => {- const overlong = await findOverlongText(params);- if (overlong) {- throw new BadRequestError({- message: `${overlong.label} would be longer than ${AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH} characters once its variables are filled in. Use fewer variables in it, or shorten their values.`- });- }- };-- const storedCredentialTexts = (storedSecret: TCredentialWrite["secret"]): TReferencingText[] =>- Object.entries(storedSecret ?? {}).map(([part, text]) => ({ label: `The ${CREDENTIAL_PART_NOUNS[part]}`, text }));-// Only text that arrived with the request is stored. A basic credential's kept half is already stored text,// and one saved before variables existed has to keep reaching the host exactly as it was.const storeCredentialReferences = (@@ -874,8 +812,7 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBund);assertCustomHeadersDoNotShadowCredential(config, customHeaders);- const cipher = await getProjectCipher(rest.projectId);- const { encryptor } = cipher;+ const { encryptor } = await getProjectCipher(rest.projectId);const seal = (value: string) => encryptor({ plainText: Buffer.from(JSON.stringify({ value })) }).cipherTextBlob;const encryptedCredential = secret@@ -884,22 +821,6 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBund// Stored once, so the sealed text and the references read off it are the same string.const storedCustomHeaderValues = (customHeaders ?? []).map((header) => store(header.value));const storedSubstitutionValues = (substitutions ?? []).map((substitution) => store(substitution.value));- await assertServiceFieldsFitWhenFilledIn({- projectId: rest.projectId,- accessBundleId: bundle.id,- cipher,- texts: [- ...storedCredentialTexts(secret),- ...(customHeaders ?? []).map((header, position) => ({- label: `The custom header '${header.name}'`,- text: storedCustomHeaderValues[position]- })),- ...(substitutions ?? []).map((substitution, position) => ({- label: `The substitution for '${substitution.placeholder}'`,- text: storedSubstitutionValues[position]- }))- ]- });const customHeaderRows = (customHeaders ?? []).map((header, position) => ({name: header.name,prefix: header.prefix ?? "",@@ -1108,22 +1029,6 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBundconst storedSubstitutionValues = (substitutions ?? []).map((substitution) =>substitution.value === undefined ? undefined : store(substitution.value));- await assertServiceFieldsFitWhenFilledIn({- projectId: rest.projectId,- accessBundleId: bundle.id,- cipher: storedSecretCipher ?? cipherForTransformations,- texts: [- ...storedCredentialTexts(writtenSecret),- ...(customHeaders ?? []).flatMap((header, index) => {- const text = storedCustomHeaderValues[index];- return text === undefined ? [] : [{ label: `The custom header '${header.name}'`, text }];- }),- ...(substitutions ?? []).flatMap((substitution, index) => {- const text = storedSubstitutionValues[index];- return text === undefined ? [] : [{ label: `The substitution for '${substitution.placeholder}'`, text }];- })- ]- });const customHeaderWrites: TTransformationWrite<{ name: string; prefix: string }>[] | undefined = customHeaders?.map((header, index) => ({@@ -1463,88 +1368,6 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBund}};- // A new value reaches every field that uses the variable on the next resolve, so each of them is measured with it.- const assertValueFitsItsServices = async ({- projectId,- accessBundleId,- variableId,- value,- cipher- }: {- projectId: string;- accessBundleId: string;- variableId: string;- value: string;- cipher: TProjectCipher;- }) => {- const references = await agentVaultServiceVariableReferenceDAL.findByVariableIds([variableId]);- if (!references.length) return;-- const serviceIds = [...new Set(references.map((reference) => reference.serviceId))];- const [services, { customHeaders, substitutions }] = await Promise.all([- agentVaultServiceDAL.find({ accessBundleId, $in: { id: serviceIds } }),- loadTransformations(serviceIds, undefined, { withVariableReferences: false })- ]);- const serviceNameOf = new Map(services.map((service) => [service.id, service.name]));-- const usedInCredential = new Set(- references- .filter(- (reference) =>- reference.field === AgentVaultVariableReferenceField.CredentialValue ||- reference.field === AgentVaultVariableReferenceField.CredentialUsername- )- .map((reference) => reference.serviceId)- );- const usedInRow = new Set(- references.flatMap((reference) => {- const rowId = reference.customHeaderId ?? reference.substitutionId;- return rowId ? [rowId] : [];- })- );-- const open = (sealed: Buffer) =>- JSON.parse(cipher.decryptor({ cipherTextBlob: sealed }).toString("utf-8")) as Record<string, string>;- const fields = [- ...services.flatMap((service) =>- usedInCredential.has(service.id) && service.encryptedCredential- ? Object.entries(open(service.encryptedCredential)).map(([part, text]) => ({- serviceId: service.id,- label: `the ${CREDENTIAL_PART_NOUNS[part]}`,- text- }))- : []- ),- ...customHeaders- .filter((row) => usedInRow.has(row.id) && serviceNameOf.has(row.serviceId))- .map((row) => ({- serviceId: row.serviceId,- label: `the custom header '${row.name}'`,- text: open(row.encryptedValue).value- })),- ...substitutions- .filter((row) => usedInRow.has(row.id) && serviceNameOf.has(row.serviceId))- .map((row) => ({- serviceId: row.serviceId,- label: `the substitution for '${row.placeholder}'`,- text: open(row.encryptedValue).value- }))- ];-- const overlong = await findOverlongText({- projectId,- accessBundleId,- texts: fields,- cipher,- replacing: { variableId, value }- });- if (overlong) {- throw new BadRequestError({- message: `With this value, ${overlong.label} of the service '${serviceNameOf.get(overlong.serviceId)}' would be longer than ${AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH} characters once its variables are filled in. Use a shorter value, or fewer variables in it.`- });- }- };-const updateVariable = async ({ accessBundleId, variableId, key, value, isSecret, ...rest }: TUpdateVariableDTO) => {const bundle = await resolveVariableBundle({ ...rest, accessBundleId });@@ -1554,15 +1377,6 @@ export const agentVaultAccessBundleServiceFactory = (deps: TAgentVaultAccessBundconst cipher =value !== undefined || !(isSecret ?? variable.isSecret) ? await getProjectCipher(rest.projectId) : null;const encryptedValue = value === undefined ? undefined : sealVariableValue(cipher!.encryptor, value);- if (value !== undefined) {- await assertValueFitsItsServices({- projectId: rest.projectId,- accessBundleId: bundle.id,- variableId: variable.id,- value,- cipher: cipher!- });- }const write = () =>agentVaultVariableDAL.transaction(async (tx) => {diff --git a/backend/src/ee/services/agent-vault-proxy/agent-vault-proxy-service.ts b/backend/src/ee/services/agent-vault-proxy/agent-vault-proxy-service.tsindex 8f4aa8572c..0e74f5ac1a 100644--- a/backend/src/ee/services/agent-vault-proxy/agent-vault-proxy-service.ts+++ b/backend/src/ee/services/agent-vault-proxy/agent-vault-proxy-service.ts@@ -25,11 +25,7 @@ import { TOrgDALFactory } from "@app/services/org/org-dal";import { isUniqueViolation } from "../agent-vault/agent-vault-db-error-fns";import { AgentVaultCredentialType, AgentVaultTrafficPolicy } from "../agent-vault/agent-vault-enums";import { findReachableAccessBundleIds, liveGroupIdsFrom } from "../agent-vault/agent-vault-permission";-import {- AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH,- expandStoredVariableReferences,- findStoredVariableIds-} from "../agent-vault/agent-vault-variable-fns";+import { expandStoredVariableReferences, findStoredVariableIds } from "../agent-vault/agent-vault-variable-fns";import { TAgentVaultServiceCustomHeaderDALFactory } from "../agent-vault-access-bundle/agent-vault-service-custom-header-dal";import { TAgentVaultServiceSubstitutionDALFactory } from "../agent-vault-access-bundle/agent-vault-service-substitution-dal";import { TAgentVaultVariableDALFactory } from "../agent-vault-access-bundle/agent-vault-variable-dal";@@ -345,9 +341,8 @@ export const agentVaultProxyServiceFactory = ({};// The ids come out of the decrypted fields rather than the reference rows, so a service saved mid-poll can- // never leave the two describing different versions of a field. An id with nothing behind it stays as text,- // and so does a field its variables would fill past the length cap: dropping the service instead would also- // drop its method and path restrictions.+ // never leave the two describing different versions of a field. An id with nothing behind it stays as text:+ // dropping the service instead would also drop its method and path restrictions.const $expandVariableReferences = async ({sessionId,services,@@ -382,7 +377,6 @@ export const agentVaultProxyServiceFactory = ({const opened = new Map<string, string>();const unresolvedServiceIds = new Set<string>();- const oversizedServiceIds = new Set<string>();const expanded = services.map((service) => {const valueOf = (variableId: string) => {@@ -399,11 +393,7 @@ export const agentVaultProxyServiceFactory = ({}return value;};- const expand = (text: string) => {- const filledIn = expandStoredVariableReferences(text, valueOf);- if (filledIn === undefined) oversizedServiceIds.add(service.id);- return filledIn ?? text;- };+ const expand = (text: string) => expandStoredVariableReferences(text, valueOf);let { credential } = service;if (credential.type === "bearer") credential = { ...credential, value: expand(credential.value) };@@ -427,12 +417,6 @@ export const agentVaultProxyServiceFactory = ({`agentVaultResolve: stored variable reference with no variable [sessionId=${sessionId}] [serviceIds=${[...unresolvedServiceIds].join(",")}]`);}- if (oversizedServiceIds.size) {- logger.warn(- `agentVaultResolve: variables fill a field past ${AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH} characters [sessionId=${sessionId}] [serviceIds=${[...oversizedServiceIds].join(",")}]`- );- }-return expanded;};diff --git a/backend/src/ee/services/agent-vault/CLAUDE.md b/backend/src/ee/services/agent-vault/CLAUDE.mdindex 1a11d41ab7..1d1dc49d18 100644--- a/backend/src/ee/services/agent-vault/CLAUDE.md+++ b/backend/src/ee/services/agent-vault/CLAUDE.md@@ -199,20 +199,18 @@ path decides what a service can reach, so it stays literal.refused without quoting it, because the text was cut from a secret. Placeholders can't contain doublebraces. A variable's own value is sent as is and never expanded again, which is also how a service sends aliteral `{{`.-- **A value takes at most 10 references** (`AGENT_VAULT_MAX_REFERENCES_PER_FIELD`), and a repeat counts each+- **A value takes at most 3 references** (`AGENT_VAULT_MAX_REFERENCES_PER_FIELD`), and a repeat counts eachtime: repeating one short reference is what fills a field in to millions of characters. The field schemas- refuse an eleventh with a 422, and the service sheet checks the same limit before it saves.+ refuse a fourth with a 422, and the service sheet checks the same limit before it saves.+- **That limit is the only bound on a filled-in field**: what was typed plus three 8,192 character values,+ about 32K characters, and nothing measures a field filled in. Settled over an exact 8,192 cap, which every+ service save and value change had to measure against other rows' current values, so two of them measured+ before the bundle lock and together pushed a field past it. Closing that under the lock would have meant+ decrypting every field that uses the variable inside the transaction.- Resolve reads variable values on a replica, then asks the primary for any id the replica lacks, since eachquery can land on a different replica. An id with no variable behind it on either stays as text and islogged rather than dropping the service, which would also drop the service's method and path restrictions.Values saved before variables existed hold no id tokens, so they pass through untouched.-- **A field is held to 8,192 characters filled in** (`AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH`), measured- before anything is built. The input limits don't hold it there: ten references to 8,192 character values- fill one field in to over 80,000 characters, and a service has up to 42 such fields. Saves refuse a field- that would pass it, since resolve would send its stored text to the host in place of the credential: a- service save measures each value it seals, and a value change measures every field that uses the variable.- Both measure before the lock, so a service save racing a value change can still land a field over the cap.- Resolve sends that field as stored text and logs it, for the same reason as an unresolved id.- Admin only, reads included: every variable route checks `Edit` on access bundles, which a member lacks,and the bundle read gives a member its services without `variableReferences` (left out, not empty, sincean empty list would claim they use none). Every value is sealed;diff --git a/backend/src/ee/services/agent-vault/agent-vault-variable-fns.test.ts b/backend/src/ee/services/agent-vault/agent-vault-variable-fns.test.tsindex 4e5ffc5199..bfcf3593b5 100644--- a/backend/src/ee/services/agent-vault/agent-vault-variable-fns.test.ts+++ b/backend/src/ee/services/agent-vault/agent-vault-variable-fns.test.ts@@ -1,10 +1,9 @@import { describe, expect, it } from "vitest";import {- AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH,AGENT_VAULT_MAX_REFERENCES_PER_FIELD,+ AGENT_VAULT_VARIABLE_VALUE_MAX_LENGTH,expandStoredVariableReferences,- filledInVariableLength,findStoredVariableIds,findVariableKeys,hasMalformedVariableReference,@@ -104,29 +103,6 @@ describe("agent vault variable references", () => {});});- describe("measuring filled-in length", () => {- const lengths: Record<string, number> = { [GITHUB_ID]: 100, [DATADOG_ID]: 7 };-- it("counts each reference as its value, a repeat included, plus the text around it", () => {- expect(filledInVariableLength(`a{{${GITHUB_ID}}}b{{${DATADOG_ID}}}{{${GITHUB_ID}}}`, (id) => lengths[id])).toBe(- 2 + 100 + 7 + 100- );- });-- it("counts a stored id with no length behind it, and a key-form reference, as the text they stay as", () => {- expect(filledInVariableLength(`{{${GITHUB_ID}}}`, () => undefined)).toBe(`{{${GITHUB_ID}}}`.length);- expect(filledInVariableLength("{{GITHUB_TOKEN}}", () => 100)).toBe("{{GITHUB_TOKEN}}".length);- });-- it("agrees with the length expanding produces", () => {- const values: Record<string, string> = { [GITHUB_ID]: "ghp_alpha", [DATADOG_ID]: "dd_beta" };- const stored = `Bearer {{${GITHUB_ID}}}:{{${DATADOG_ID}}}`;- expect(filledInVariableLength(stored, (id) => values[id]?.length)).toBe(- expandStoredVariableReferences(stored, (id) => values[id])!.length- );- });- });-describe("expanding", () => {const values: Record<string, string> = { [GITHUB_ID]: "ghp_alpha", [DATADOG_ID]: "dd_beta" };@@ -164,23 +140,13 @@ describe("agent vault variable references", () => {expect(expandStoredVariableReferences(stored, (id) => values[id])).toBe("Bearer ghp_alpha");});- it("fills in up to the length cap, counting the text around the references", () => {- const value = "v".repeat(AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH - 1);- expect(expandStoredVariableReferences(`x{{${GITHUB_ID}}}`, () => value)).toBe(`x${value}`);- expect(expandStoredVariableReferences(`xy{{${GITHUB_ID}}}`, () => value)).toBeUndefined();- });-- it("gives up on a field that short references would fill to millions of characters", () => {+ it("fills in a field at the reference limit whole, with every value at its longest", () => {const idOfKey = new Map([["A", GITHUB_ID]]);- const stored = toStoredVariableReferences("{{A}}".repeat(1638), idOfKey);- expect(expandStoredVariableReferences(stored, () => "v".repeat(8192))).toBeUndefined();- });-- it("measures a field by its filled-in length, not its stored one", () => {- const idOfKey = new Map([["A", GITHUB_ID]]);- const stored = toStoredVariableReferences("{{A}}".repeat(1638), idOfKey);- expect(stored.length).toBeGreaterThan(AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH);- expect(expandStoredVariableReferences(stored, () => "a")).toBe("a".repeat(1638));+ const stored = toStoredVariableReferences(`x${"{{A}}".repeat(AGENT_VAULT_MAX_REFERENCES_PER_FIELD)}`, idOfKey);+ const value = "v".repeat(AGENT_VAULT_VARIABLE_VALUE_MAX_LENGTH);+ expect(expandStoredVariableReferences(stored, () => value)).toBe(+ `x${value.repeat(AGENT_VAULT_MAX_REFERENCES_PER_FIELD)}`+ );});});});diff --git a/backend/src/ee/services/agent-vault/agent-vault-variable-fns.ts b/backend/src/ee/services/agent-vault/agent-vault-variable-fns.tsindex aa501886d1..3c9a64b08d 100644--- a/backend/src/ee/services/agent-vault/agent-vault-variable-fns.ts+++ b/backend/src/ee/services/agent-vault/agent-vault-variable-fns.ts@@ -3,11 +3,10 @@ import RE2 from "re2";export const AGENT_VAULT_VARIABLE_KEY_MAX_LENGTH = 64;export const AGENT_VAULT_VARIABLE_VALUE_MAX_LENGTH = 8192;export const AGENT_VAULT_MAX_VARIABLES = 100;-// Counts every {{...}}, a repeat included: repeating one short reference is what lets a field fill in to-// millions of characters.-export const AGENT_VAULT_MAX_REFERENCES_PER_FIELD = 10;-// No field that takes a variable accepts more than this typed out, and filled in it is held to the same.-export const AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH = 8192;+// Counts every {{...}}, a repeat included: repeating one short reference is what would let a field fill in to+// millions of characters. With the value limit this is the whole bound on a filled-in field, the text as typed+// plus three values, so no save reads another row to hold a field to it and none can race another past it.+export const AGENT_VAULT_MAX_REFERENCES_PER_FIELD = 3;export const AGENT_VAULT_VARIABLE_KEY_RE = new RE2(/^[A-Z][A-Z0-9_]*$/);@@ -55,35 +54,14 @@ export const findStoredVariableIds = (text: string) => [...new Set(findReferenceTokens(text).filter((token) => STORED_REFERENCE_RE.test(token)))];-/**- * How long stored text comes out once its references are filled in, measured without building it. An id with- * no length behind it counts as its own token, which is what expanding leaves in its place.- */-export const filledInVariableLength = (text: string, lengthOfId: (variableId: string) => number | undefined) =>- Array.from(text.matchAll(REFERENCE_RE)).reduce((length, [token, inner]) => {- const valueLength = STORED_REFERENCE_RE.test(inner) ? lengthOfId(inner) : undefined;- return valueLength === undefined ? length : length + valueLength - token.length;- }, text.length);-/*** Anything that is not a known variable id stays as it was stored. A value saved before variables existed* can hold literal braces, and it has to keep reaching the host unchanged.- *- * Returns undefined, without building the text, when it would come out longer than- * AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH. The input limits alone don't hold it there: ten references to 8,192- * character values fill one field in to over 80,000 characters.*/export const expandStoredVariableReferences = (text: string,valueOfId: (variableId: string) => string | undefined-): string | undefined => {+): string => {const valueOf = (inner: string) => (STORED_REFERENCE_RE.test(inner) ? valueOfId(inner) : undefined);-- if (- filledInVariableLength(text, (variableId) => valueOfId(variableId)?.length) > AGENT_VAULT_EXPANDED_FIELD_MAX_LENGTH- ) {- return undefined;- }-return text.replace(REFERENCE_RE, (token: string, inner: string) => valueOf(inner) ?? token);};diff --git a/docs/documentation/platform/agent-vault/variables.mdx b/docs/documentation/platform/agent-vault/variables.mdxindex 768843c72a..0eeb3713a6 100644--- a/docs/documentation/platform/agent-vault/variables.mdx+++ b/docs/documentation/platform/agent-vault/variables.mdx@@ -57,9 +57,7 @@ To use a variable:A reference can be the whole value or part of it. For example, if a custom header's value is `org-{{ORG_ID}}`, the proxy sends `org-` followed by the value of `ORG_ID`.-A value can contain up to 10 references. If a value uses the same key more than once, each use counts as a reference, so `{{ORG_ID}}/{{ORG_ID}}` contains two.--A value can be at most 8,192 characters long after the proxy replaces its references. If a value would be longer, Infisical won't save the service.+A value can contain up to 3 references. If a value uses the same key more than once, each use counts as a reference, so `{{ORG_ID}}/{{ORG_ID}}` contains two.The **Sends** preview shows each reference in the headers the proxy attaches. If a reference names a key the bundle doesn't have, Infisical marks the reference in red and won't save the service until you correct the key or add the variable.@@ -73,8 +71,6 @@ To change a variable, open its actions menu in the **Variables** list and selectIf you rename a variable, every service that uses it keeps working without any changes. If you change a value, running agents get the new value within one [poll interval](/documentation/platform/agent-vault/proxies#poll-interval), without a restart.-If the new value would make one of a service's values longer than 8,192 characters, Infisical won't save the change, and the error names the service.-To delete a variable, open its actions menu and select **Delete**. If any service still uses the variable, Infisical lists those services and won't delete the variable until you remove the reference from each one.## Secret valuesdiff --git a/frontend/src/helpers/agentVaultVariables.ts b/frontend/src/helpers/agentVaultVariables.tsindex db4212ff8f..c03600e51b 100644--- a/frontend/src/helpers/agentVaultVariables.ts+++ b/frontend/src/helpers/agentVaultVariables.ts@@ -1,6 +1,6 @@export const VARIABLE_KEY_MAX_LENGTH = 64;export const VARIABLE_VALUE_MAX_LENGTH = 8192;-export const MAX_REFERENCES_PER_FIELD = 10;+export const MAX_REFERENCES_PER_FIELD = 3;export const VARIABLE_KEY_RE = /^[A-Z][A-Z0-9_]*$/;