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(insights): credit only references that expanded, and only the returned secret's
Dimitris Staratzisd910ed22c4da
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.mdindex 84a912f776..1713b207cb 100644--- a/backend/CLAUDE.md+++ b/backend/CLAUDE.md@@ -1123,8 +1123,10 @@ owns the two read endpoints and hands `recordRead` / `recordBulkRead` through toreferences, which is why `objectType` is part of the primary key. A read that expands references with the reader'spersonal overrides (v4 bulk with `includePersonalOverrides`, v4 single read of a personal secret) records thereturned secrets plus the ones its references resolved to, all as `secret-unexpanded`: matching references by key- would credit a shared secret an override replaced. The expander reports those through `onSecretReferenced`, and- the bulk read caches them with its result (only for these reads, so every other cached payload and ETag is+ would credit a shared secret an override replaced. The expander reports a secret's referenced ids through+ `onSecretExpanded` only once that secret's expansion succeeds, tagged with the secret, so a failed expansion+ credits nothing and the single read passes on only the returned secret's (imports expand every imported secret).+ The bulk read caches them with its result (only for these reads, so every other cached payload and ETag isunchanged) so a cache hit replays them. A Redis `SET NX` gate (25h TTL) keyed onthe reader, the day and the request (or the hash of the returned ids) lets only the first identical readper day through. It is keyed on the request, not the ETag, since older clients never send one. Gated reads go to adiff --git a/backend/src/services/secret-v2-bridge/secret-reference-fns.test.ts b/backend/src/services/secret-v2-bridge/secret-reference-fns.test.tsindex a7d3fd83e0..8cffbc60f1 100644--- a/backend/src/services/secret-v2-bridge/secret-reference-fns.test.ts+++ b/backend/src/services/secret-v2-bridge/secret-reference-fns.test.ts@@ -218,11 +218,11 @@ describe("getAllSecretReferences", () => {});});-describe("expandSecretReferencesFactory onSecretReferenced", () => {- const row = (id: string, value: string, userId: string | null) => ({+describe("expandSecretReferencesFactory onSecretExpanded", () => {+ const row = (id: string, key: string, value: string, userId: string | null = null) => ({id,_id: id,- key: "DB_PASSWORD",+ key,version: 1,type: userId ? "personal" : "shared",folderId: "folder",@@ -233,34 +233,47 @@ describe("expandSecretReferencesFactory onSecretReferenced", () => {secretMetadata: [],userId});- const expand = async (rows: ReturnType<typeof row>[], userId?: string) => {- const referenced: string[] = [];+ const expand = async (+ rows: ReturnType<typeof row>[],+ value: string,+ {+ userId,+ forbidden,+ reported = []+ }: { userId?: string; forbidden?: string; reported?: { secretKey: string; referencedSecretIds: string[] }[] } = {}+ ) => {const { findBySecretPath } = makeDALs();const { expandSecretReferences } = expandSecretReferencesFactory({projectId: "project",folderDAL: { findBySecretPath },secretDAL: { findByFolderId: vi.fn<Args["secretDAL"]["findByFolderId"]>().mockResolvedValue(rows) },- decryptSecretValue: (value) => value?.toString(),- canExpandValue: vi.fn(() => true),+ decryptSecretValue: (encrypted) => encrypted?.toString(),+ canExpandValue: vi.fn((_environment: string, _secretPath: string, secretKey: string) => secretKey !== forbidden),userId,- onSecretReferenced: (secretId) => referenced.push(secretId)+ onSecretExpanded: ({ secretKey }, referencedSecretIds) => reported.push({ secretKey, referencedSecretIds })});- const value = await expandSecretReferences({- secretKey: "URL",- value: `postgres://\${DB_PASSWORD}@db`,- environment: "prod",- secretPath: "/"- });- return { value, referenced };+ const expanded = await expandSecretReferences({ secretKey: "URL", value, environment: "prod", secretPath: "/" });+ return { expanded, reported };};it("reports the reader's own override, not the shared secret it replaced", async () => {- const result = await expand([row("shared-db", "shared", null), row("alice-db", "mine", "alice")], "alice");- expect(result).toEqual({ value: "postgres://mine@db", referenced: ["alice-db"] });+ const rows = [row("shared-db", "DB_PASSWORD", "shared"), row("alice-db", "DB_PASSWORD", "mine", "alice")];+ const result = await expand(rows, `postgres://\${DB_PASSWORD}@db`, { userId: "alice" });+ expect(result).toEqual({+ expanded: "postgres://mine@db",+ reported: [{ secretKey: "URL", referencedSecretIds: ["alice-db"] }]+ });});it("reports the shared secret when no override is used", async () => {- const result = await expand([row("shared-db", "shared", null)]);- expect(result).toEqual({ value: "postgres://shared@db", referenced: ["shared-db"] });+ const result = await expand([row("shared-db", "DB_PASSWORD", "shared")], `postgres://\${DB_PASSWORD}@db`);+ expect(result.reported).toEqual([{ secretKey: "URL", referencedSecretIds: ["shared-db"] }]);+ });++ it("reports nothing for an expansion that fails part way", async () => {+ const rows = [row("readable", "READABLE", "ok"), row("forbidden", "FORBIDDEN", "no")];+ const reported: { secretKey: string; referencedSecretIds: string[] }[] = [];+ await expect(expand(rows, `\${READABLE}\${FORBIDDEN}`, { forbidden: "FORBIDDEN", reported })).rejects.toThrow();+ expect(reported).toEqual([]);});});diff --git a/backend/src/services/secret-v2-bridge/secret-reference-fns.ts b/backend/src/services/secret-v2-bridge/secret-reference-fns.tsindex 4950b920d4..cefe0345e5 100644--- a/backend/src/services/secret-v2-bridge/secret-reference-fns.ts+++ b/backend/src/services/secret-v2-bridge/secret-reference-fns.ts@@ -112,14 +112,17 @@ type TInterpolateSecretArg = {// same-project relative references; cross-project reads must stay raw so source// project imports are not resolved through the target project context.crossProjectSecretDAL?: Pick<TSecretV2BridgeDALFactory, "findByFolderId">;- // Called with the id of each secret in this project whose value a reference resolved to, a personal override- // included, so a caller can tell which secrets a read actually delivered.- onSecretReferenced?: (secretId: string) => void;+ // Called once a secret's references expand without error, with the ids of the secrets in this project they+ // resolved to (a personal override included), so a caller can tell which secrets a read actually delivered.+ onSecretExpanded?: (+ secret: { environment: string; secretPath: string; secretKey: string },+ referencedSecretIds: string[]+ ) => void;abortSignal?: AbortSignal;tx?: Knex;};-// A cross-project secret carries no id: only this project's secrets are reported to onSecretReferenced.+// A cross-project secret carries no id: only this project's secrets are reported to onSecretExpanded.type TCachedReferencedSecret = { id?: string; value: string; tags: string[]; exists: boolean };const MAX_SECRET_REFERENCE_DEPTH = 10;@@ -136,7 +139,7 @@ export const expandSecretReferencesFactory = ({projectDAL,kmsService,crossProjectSecretDAL,- onSecretReferenced,+ onSecretExpanded,abortSignal,tx}: TInterpolateSecretArg) => {@@ -241,6 +244,7 @@ export const expandSecretReferencesFactory = ({environment: string;shouldStackTrace?: boolean;secretKey: string;+ referencedSecretIds?: Set<string>;}) => {const stackTrace = { ...dto, key: "root", children: [] } as TSecretReferenceTraceNode;@@ -325,7 +329,7 @@ export const expandSecretReferencesFactory = ({const cacheKey = getCacheUniqueKey(environment, secretPath);if (!secretCache[cacheKey]) secretCache[cacheKey] = {};secretCache[cacheKey][secretKey] = referredValue;- if (referredValue.id) onSecretReferenced?.(referredValue.id);+ if (referredValue.id) dto.referencedSecretIds?.add(referredValue.id);referencedSecretValue = referredValue.value;referencedSecretExists = referredValue.exists;@@ -450,7 +454,7 @@ export const expandSecretReferencesFactory = ({const cacheKey = getCacheUniqueKey(secretReferenceEnvironment, secretReferencePath);if (!secretCache[cacheKey]) secretCache[cacheKey] = {};secretCache[cacheKey][secretReferenceKey] = referedValue;- if (referedValue.id) onSecretReferenced?.(referedValue.id);+ if (referedValue.id) dto.referencedSecretIds?.add(referedValue.id);referencedSecretValue = referedValue.value;referencedSecretExists = referedValue.exists;@@ -520,7 +524,12 @@ export const expandSecretReferencesFactory = ({const shouldExpand = INTERPOLATION_TEST_REGEX.test(inputSecret.value);if (!shouldExpand) return inputSecret.value;- const { expandedValue } = await recursivelyExpandSecret(inputSecret);+ const referencedSecretIds = onSecretExpanded ? new Set<string>() : undefined;+ const { expandedValue } = await recursivelyExpandSecret({ ...inputSecret, referencedSecretIds });+ if (referencedSecretIds?.size) {+ const { environment, secretPath, secretKey } = inputSecret;+ onSecretExpanded?.({ environment, secretPath, secretKey }, [...referencedSecretIds]);+ }return inputSecret.skipMultilineEncoding ? formatMultiValueEnv(expandedValue) : expandedValue;};diff --git a/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.ts b/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.tsindex a145d2b154..df3cabf2f2 100644--- a/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.ts+++ b/backend/src/services/secret-v2-bridge/secret-v2-bridge-fns.ts@@ -1477,7 +1477,7 @@ type TCreateRelativeImportExpanderArg = {decryptSecretValue: (value?: Buffer | null) => string;canExpandValue: (environment: string, secretPath: string, secretKey: string, secretTagSlugs: string[]) => boolean;userId?: string;- onSecretReferenced?: (secretId: string) => void;+ onSecretExpanded?: Parameters<typeof expandSecretReferencesFactory>[0]["onSecretExpanded"];abortSignal?: AbortSignal;};@@ -1491,7 +1491,7 @@ export const createRelativeImportExpander = ({decryptSecretValue,canExpandValue,userId,- onSecretReferenced,+ onSecretExpanded,abortSignal}: TCreateRelativeImportExpanderArg): {expandImportedSecretReferences: (inputSecret: {@@ -1572,7 +1572,7 @@ export const createRelativeImportExpander = ({return canExpandValue(environment, secretPath, secretKey, secretTagSlugs);},userId,- onSecretReferenced,+ onSecretExpanded,abortSignal});diff --git a/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts b/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.tsindex 9b36931b3a..42e36a942c 100644--- a/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts+++ b/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts@@ -1640,10 +1640,8 @@ export const secretV2BridgeServiceFactory = ({// since matching a reference by key would credit the shared secret an override replaced. They are cached with// the result, as a cache hit expands nothing. Every other read caches exactly what it did before.const referencedSecretIds = new Set<string>();- const collectReferencedSecret = expandPersonalOverrides- ? (secretId: string) => {- referencedSecretIds.add(secretId);- }+ const collectReferencedSecrets = expandPersonalOverrides+ ? (_secret: unknown, secretIds: string[]) => secretIds.forEach((secretId) => referencedSecretIds.add(secretId)): undefined;const withReferencedSecretIds = <T extends object>(payload: T) =>referencedSecretIds.size ? { ...payload, referencedSecretIds: [...referencedSecretIds].sort() } : payload;@@ -1680,7 +1678,7 @@ export const secretV2BridgeServiceFactory = ({// mainExpanderSecretDAL may be import-aware in relative mode. Keep cross-project// reads on the raw DAL so source imports are not resolved through this target project.crossProjectSecretDAL: secretDAL,- onSecretReferenced: collectReferencedSecret,+ onSecretExpanded: collectReferencedSecrets,abortSignal});@@ -1738,7 +1736,7 @@ export const secretV2BridgeServiceFactory = ({secretTags: expandSecretTags}),userId: expandPersonalOverrides ? actorId : undefined,- onSecretReferenced: collectReferencedSecret,+ onSecretExpanded: collectReferencedSecrets,abortSignal});@@ -1991,6 +1989,7 @@ export const secretV2BridgeServiceFactory = ({// this will throw if the user doesn't have read value permission no matter what// because if its an expansion, it will fully depend on the value.+ const referencedSecretIdsByLocation = new Map<string, string[]>();const { expandSecretReferences } = expandSecretReferencesFactory({projectId,folderDAL,@@ -2010,8 +2009,16 @@ export const secretV2BridgeServiceFactory = ({projectFolderGrantDAL,projectDAL,kmsService,- onSecretReferenced+ onSecretExpanded: onSecretReferenced+ ? ({ environment: expandedEnvironment, secretPath: expandedPath, secretKey }, secretIds) =>+ referencedSecretIdsByLocation.set(`${expandedEnvironment}:${expandedPath}:${secretKey}`, secretIds)+ : undefined});+ // The imports expand every imported secret, so only the references of the secret returned are passed on.+ const reportReferencesOf = (secretEnvironment: string, secretPath: string, secretKey: string) =>+ referencedSecretIdsByLocation+ .get(`${secretEnvironment}:${secretPath}:${secretKey}`)+ ?.forEach((secretId) => onSecretReferenced?.(secretId));// now if secret is not found// then search for imported secrets@@ -2077,6 +2084,7 @@ export const secretV2BridgeServiceFactory = ({secretValueHidden = false;}+ reportReferencesOf(importedSecrets[i].environment, importedSecrets[i].secretPath, secretName);return reshapeBridgeSecret(projectId,importedSecrets[i].environment,@@ -2131,6 +2139,7 @@ export const secretV2BridgeServiceFactory = ({secretValueHidden = false;}+ reportReferencesOf(environment, path, secret.key);return reshapeBridgeSecret(projectId,environment,