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): keep a read of the secret itself when access changed after it
Dimitris Staratzisb118be0841d7
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.mdindex 6c4ba36217..2b74030b7f 100644--- a/backend/CLAUDE.md+++ b/backend/CLAUDE.md@@ -1165,7 +1165,8 @@ owns the two read endpoints and hands `recordRead` / `recordBulkRead` through toexcept an expired folder grant, which can give back access it held back in its folder. Only an actor with asource newer than `judgedFrom` is checked per secret, and each source once per secret across actors. A read frombefore the reader had access to a secret is dropped for that secret (`readsByActorKey`): reads are credited by- folder, and a folder read then could not have returned it. The per-secret sheet shows the plain fact+ folder, and a folder read then could not have returned it. A read recorded against the secret itself+ (`lastSecretReadAt`) always counts, so a role change does not erase it. The per-secret sheet shows the plain factinstead: a read, "No reads", or "No reads yet" with the reason.- **The access sheet.** The graph lives only in the secret's access sheet, served by`GET /insights/:projectId/secrets/read-activity/:secretName` (the fixed segment comes first so a secret nameddiff --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 cbd5c89ebf..8ff6e3a22e 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@@ -105,6 +105,26 @@ describe("classifySecretReads", () => {).toBeUndefined();});+ test("marks only a read recorded against the secret itself as known to have returned it", () => {+ const secretRead = new Date("2026-10-11T00:00:00.000Z");+ expect(+ classify([+ read({ lastReadAt: new Date("2026-10-12T00:00:00.000Z") }),+ read({ objectId: dbPass.id, objectType: SecretReadObjectType.Secret, lastReadAt: secretRead }),+ read({+ objectId: dbUrl.id,+ objectType: SecretReadObjectType.Secret,+ lastReadAt: new Date("2026-10-13T00:00:00.000Z")+ })+ ])+ ).toEqual({+ actorType: ActorType.IDENTITY,+ actorId: "identity-1",+ lastReadAt: new Date("2026-10-13T00:00:00.000Z"),+ lastSecretReadAt: secretRead+ });+ });+test("ignores reads from before the secret was tracked", () => {expect(classify([read({ lastReadAt: new Date("2026-09-30T00:00:00.000Z") })])).toBeUndefined();});@@ -231,6 +251,27 @@ describe("summarizeSecretAccess", () => {);});+ test("keeps a read of the secret itself from before the reader's current access, such as before a role change", () => {+ const folderReadThenSecretRead = new Map([+ [+ toActorKey(ActorType.USER, "direct"),+ {+ actorType: ActorType.USER as const,+ actorId: "direct",+ lastReadAt: new Date("2026-10-02T00:00:00.000Z"),+ lastSecretReadAt: new Date("2026-10-01T00:00:00.000Z")+ }+ ]+ ]);+ const summary = summarizeSecretAccess(+ activity({ reads: folderReadThenSecretRead }),+ access(datedAccess({ direct: new Date("2026-10-05T00:00:00.000Z") }))+ );+ expect(summary.readsByActorKey.get(toActorKey(ActorType.USER, "direct"))?.lastReadAt).toEqual(+ new Date("2026-10-01T00:00:00.000Z")+ );+ });+test("does not count a secret newer than the judged period", () => {const summary = summarizeSecretAccess(activity({ trackedSince: new Date("2026-10-01T00:00:00.000Z") }), access());expect(summary.unread).toEqual([]);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 8eccc91347..7d07a5b991 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@@ -184,14 +184,17 @@ export const classifySecretReads = ((readsByObjectId[objectId] ?? []).forEach((read) => {if (read.objectType !== objectType || read.lastReadAt < trackedSince) return;const key = toActorKey(read.actorType, read.actorId);- const current = activity.get(key);- if (!current || read.lastReadAt > current.lastReadAt) {- activity.set(key, {- actorType: read.actorType as TSecretReaderType,- actorId: read.actorId,- lastReadAt: read.lastReadAt- });+ const current = activity.get(key) ?? {+ actorType: read.actorType as TSecretReaderType,+ actorId: read.actorId,+ lastReadAt: read.lastReadAt+ };+ if (read.lastReadAt > current.lastReadAt) current.lastReadAt = read.lastReadAt;+ const isOfTheSecret = objectId === reach.secretId && objectType !== SecretReadObjectType.Folder;+ if (isOfTheSecret && (!current.lastSecretReadAt || read.lastReadAt > current.lastSecretReadAt)) {+ current.lastSecretReadAt = read.lastReadAt;}+ activity.set(key, current);});};@@ -223,19 +226,18 @@ export const summarizeSecretAccess = (const latest = latestAccessChangeOf(reader);return latest && latest > judgedFrom ? accessSinceOf(reader, activity) : latest;};- // A read is credited by folder, so one from before the reader had access to this secret did not return it. A read- // after their newest source change is after their access to every secret, so only an earlier one is checked.+ // A folder read credits every secret in the folder, so one from before the reader had access to this secret did+ // not return it; a read recorded against the secret itself did, so it always counts. A read after the reader's+ // newest source change is after their access to every secret, so only an earlier one is checked.const readsByActorKey = new Map<string, TSecretReadResult>();entitled.forEach((reader) => {const key = toActorKey(reader.actorType, reader.actorId);const read = reads.get(key);if (!read) return;const latest = latestAccessChangeOf(reader);- if (latest && read.lastReadAt < latest) {- const since = accessSinceOf(reader, activity);- if (since && read.lastReadAt < since) return;- }- readsByActorKey.set(key, read);+ const since = latest && read.lastReadAt < latest ? accessSinceOf(reader, activity) : undefined;+ if (!since || read.lastReadAt >= since) readsByActorKey.set(key, read);+ else if (read.lastSecretReadAt) readsByActorKey.set(key, { ...read, lastReadAt: read.lastSecretReadAt });});const unread = nonAdmins.filter((reader) =>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 2cb6263f3f..357fb7f005 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@@ -25,6 +25,8 @@ export type TSecretReadResult = {actorType: TSecretReaderType;actorId: string;lastReadAt: Date;+ // The latest read recorded against the secret itself, so known to have returned it, unlike a folder read.+ lastSecretReadAt?: Date;};// Everything whose read hands over this secret's value: the secret and every secret that references it,