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): count a personal secret's references only when the personal secret itself is read
Dimitris Staratzisae0e0810608b
diff --git a/backend/AGENTS.md b/backend/AGENTS.mdindex 3ab78993d8..5f673e5833 100644--- a/backend/AGENTS.md+++ b/backend/AGENTS.md@@ -1145,8 +1145,9 @@ owns the two read endpoints and hands `recordRead` / `recordBulkRead` through tostarts no earlier than the secret's and its folder's creation (`trackedSince`), nor before the table existed.- **Read path.** A read counts when it reaches the secret through its folder, an import of it, a secretreferencing it, or the source of a replication import (imports and references check the reader's own access- to the source, so revoking it would break them). A personal secret referencing it counts only for its owner, and- nothing referencing the personal secret is followed: a reference resolves to the shared secret without overrides. The card computes the whole project once and caches it+ to the source, so revoking it would break them). A personal secret referencing it counts only when the personal secret+ itself is read, not its folder (a read without overrides leaves personal values out), and nothing referencing the+ personal secret is followed: a reference resolves to the shared secret without overrides. The card computes the whole project once and caches it(`KeyStoreTtls.InsightsUnusedAccessCacheInSeconds`), grouping actors by identical rules so each rule set is checked onceper secret. The per-secret endpoint checks the caller's access to the secret before building its reach, whichdecrypts every secret that could reference it.diff --git a/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.test.ts b/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.test.tsindex ca4874f473..5468ee4ec2 100644--- a/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.test.ts+++ b/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.test.ts@@ -113,7 +113,7 @@ describe("classifySecretReads", () => {).toBeUndefined();});- test("credits reads through a personal secret referencing it only to that secret's owner", () => {+ test("counts a personal secret referencing it only when that secret itself is read, not its folder", () => {const aliceUrl: TReachSecret = { id: "alice-db-url", folderId: "app", key: "DB_URL", userId: "alice" };const reach = computeSecretReach(dbPass, {getReferencers: (secret) => (secret.id === dbPass.id ? [aliceUrl] : []),@@ -122,8 +122,7 @@ describe("classifySecretReads", () => {const readerIds = (reads: TSecretLastRead[]) => [...classifySecretReads(indexSecretReads(reads), reach, trackedSince).keys()];- expect(readerIds([read({ objectId: "app", actorId: "bob" })])).toEqual([]);- expect(readerIds([read({ objectId: "app", actorId: "alice" })])).toEqual(["alice"]);+ expect(readerIds([read({ objectId: "app", actorId: "alice" })])).toEqual([]);expect(readerIds([read({ objectId: aliceUrl.id, objectType: SecretReadObjectType.Secret, actorId: "alice" })])).toEqual(["alice"]);diff --git a/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.ts b/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.tsindex fe0a64e109..b143168b2f 100644--- a/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.ts+++ b/backend/src/ee/services/secret-read-activity/secret-read-activity-fns.ts@@ -84,18 +84,15 @@ export const loadProjectFolderTree = async (export const computeSecretReach = (secret: TReachSecret, graph: TSecretReachGraph): TSecretReach => {const secretIds = new Set([secret.id]);const folderIds = new Set([secret.folderId]);- const ownerFolderIds = new Map<string, Set<string>>();const pendingSecrets = [secret];while (pendingSecrets.length) {for (const referencer of graph.getReferencers(pendingSecrets.pop() as TReachSecret)) {- // A personal secret is returned only to its owner, and a reference to its key resolves to the shared secret- // unless the reader expands their own overrides, which records exactly what resolved. So it hands the value on- // to its owner alone, and nothing that references or imports it is followed.+ // A personal secret is left out of folder reads that skip personal values, so only a read of the personal secret+ // itself counts, not of its folder. Nothing referencing it is followed either: a reference to its key resolves to+ // the shared secret unless the reader expands their own overrides, which records exactly what resolved.if (referencer.userId) {secretIds.add(referencer.id);- const owners = ownerFolderIds.get(referencer.folderId) ?? new Set<string>();- ownerFolderIds.set(referencer.folderId, owners.add(referencer.userId));} else {folderIds.add(referencer.folderId);if (!secretIds.has(referencer.id)) {@@ -116,8 +113,7 @@ export const computeSecretReach = (secret: TReachSecret, graph: TSecretReachGrap}}- folderIds.forEach((folderId) => ownerFolderIds.delete(folderId));- return { secretId: secret.id, secretIds, folderIds, ownerFolderIds };+ return { secretId: secret.id, secretIds, folderIds };};export const indexSecretReads = (reads: TSecretLastRead[]) => groupBy(reads, (read) => read.objectId);@@ -200,10 +196,9 @@ export const classifySecretReads = (trackedSince: Date) => {const activity = new Map<string, TSecretReadResult>();- const record = (objectType: SecretReadObjectType, objectId: string, readers?: Set<string>) => {+ const record = (objectType: SecretReadObjectType, objectId: string) => {(readsByObjectId[objectId] ?? []).forEach((read) => {if (read.objectType !== objectType || read.lastReadAt < trackedSince) return;- if (readers && !readers.has(read.actorId)) return;const current = activity.get(read.actorId) ?? {actorType: read.actorType as TSecretReaderType,actorId: read.actorId,@@ -221,7 +216,6 @@ export const classifySecretReads = (reach.secretIds.forEach((secretId) => record(SecretReadObjectType.Secret, secretId));record(SecretReadObjectType.SecretUnexpanded, reach.secretId);reach.folderIds.forEach((folderId) => record(SecretReadObjectType.Folder, folderId));- reach.ownerFolderIds.forEach((owners, folderId) => record(SecretReadObjectType.Folder, folderId, owners));return activity;};diff --git a/backend/src/ee/services/secret-read-activity/secret-read-activity-service.ts b/backend/src/ee/services/secret-read-activity/secret-read-activity-service.tsindex c48bb44a51..49bbe28c07 100644--- a/backend/src/ee/services/secret-read-activity/secret-read-activity-service.ts+++ b/backend/src/ee/services/secret-read-activity/secret-read-activity-service.ts@@ -252,7 +252,7 @@ export const secretReadActivityServiceFactory = ({? targets.map(({ secret }) => computeSecretReach(secret, graph)): undefined;const objectIds = requestedReaches- ? requestedReaches.flatMap((reach) => [...reach.folderIds, ...reach.ownerFolderIds.keys(), ...reach.secretIds])+ ? requestedReaches.flatMap((reach) => [...reach.folderIds, ...reach.secretIds]): [...folders.map((folder) => folder.id),...loadedSecrets.map((secret) => secret.id),diff --git a/backend/src/ee/services/secret-read-activity/secret-read-activity-types.ts b/backend/src/ee/services/secret-read-activity/secret-read-activity-types.tsindex 193ad16ac0..6ee5bfde96 100644--- a/backend/src/ee/services/secret-read-activity/secret-read-activity-types.ts+++ b/backend/src/ee/services/secret-read-activity/secret-read-activity-types.ts@@ -35,8 +35,6 @@ export type TSecretReach = {secretId: string;secretIds: Set<string>;folderIds: Set<string>;- // Folders that hand it over only through a personal secret referencing it, with the owners whose reads count there.- ownerFolderIds: Map<string, Set<string>>;};// userId is set on a personal secret: its owner's.