Skip to content

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

fix collapse window

Thiago Araujo da Silvacdfd7dfb7edf
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index 3631f76806..1a6067b692 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -1030,7 +1030,8 @@ The collapse itself is generic. `createCollapsedAuditLog` on the audit log servi
`TCreateAuditLogDTO` plus `collapseKeyParts` (the event type is always part of the key) and an
optional `collapseWindowSeconds` (default 60). The first event per key is written at once and
schedules a delayed `AuditLogCollapsedFlush` job; repeats inside the window only bump a keystore
-counter, and the job writes one summary event with `suppressedRepeats`, `suppressedFrom` and
+counter scoped to that window's start (which the window key holds as its value, so consecutive
+windows and a late flush never share a counter), and the job writes one summary event with `suppressedRepeats`, `suppressedFrom` and
`suppressedUntil` in its metadata when the window closes, so a burst that stops is still accounted
for. To collapse another event, call it instead of `createAuditLog` and add
`TAuditLogCollapseSummary` to that event's metadata type so the summary fields are typed.
diff --git a/backend/src/ee/services/audit-log/audit-log-service.test.ts b/backend/src/ee/services/audit-log/audit-log-service.test.ts
index 8de68e2ca3..08a1861c7b 100644
--- a/backend/src/ee/services/audit-log/audit-log-service.test.ts
+++ b/backend/src/ee/services/audit-log/audit-log-service.test.ts
@@ -30,7 +30,6 @@ const denial = (overrides: Partial<TRecordPermissionDeniedDTO> = {}): TRecordPer
permissionAction: "create",
permissionSubject: "secrets",
errorName: "ForbiddenError",
- message: "denied",
route: "/api/v3/secrets/raw/:secretName",
method: "POST"
},
@@ -108,21 +107,51 @@ describe("recordPermissionDenied", () => {
const [queueName, jobName, payload, opts] = queueService.queue.mock.calls[0];
expect(queueName).toBe(QueueName.AuditLogCollapsedFlush);
expect(jobName).toBe(QueueJobs.AuditLogCollapsedFlush);
- expect(opts.delay).toBe(60_000);
+ expect(opts.delay).toBe(62_000);
expect(opts.jobId).toContain(payload.collapseKey);
expect(payload.orgId).toBe("org-1");
expect(new Date(payload.windowEnd).getTime() - new Date(payload.windowStart).getTime()).toBe(60_000);
expect(keyStore.setItemWithExpiryNX.mock.calls[0][0]).toContain(payload.collapseKey);
+ expect(keyStore.setItemWithExpiryNX.mock.calls[0][2]).toBe(payload.windowStart);
});
- test("a repeat inside the window only bumps the counter", async () => {
- const { service, queueService, keyStore, auditLogQueue } = createHarness({ windowAcquired: false });
+ test("a repeat inside the window only bumps that window's counter", async () => {
+ const openWindowStart = "2026-09-30T10:00:00.000Z";
+ const { service, queueService, keyStore, auditLogQueue } = createHarness({
+ windowAcquired: false,
+ storedCount: openWindowStart
+ });
await service.recordPermissionDenied(denial());
expect(auditLogQueue.pushToLog).not.toHaveBeenCalled();
expect(queueService.queue).not.toHaveBeenCalled();
expect(keyStore.incrementByWithExpiry).toHaveBeenCalledTimes(1);
+ expect(keyStore.incrementByWithExpiry.mock.calls[0][0]).toContain(openWindowStart);
+ });
+
+ test("a repeat that finds the window already closed opens the next one", async () => {
+ const { service, queueService, keyStore, auditLogQueue } = createHarness({ windowAcquired: false });
+ keyStore.getItem.mockResolvedValueOnce(null);
+ keyStore.setItemWithExpiryNX.mockResolvedValueOnce(null).mockResolvedValueOnce("OK");
+
+ await service.recordPermissionDenied(denial());
+
+ expect(keyStore.incrementByWithExpiry).not.toHaveBeenCalled();
+ expect(auditLogQueue.pushToLog).toHaveBeenCalledTimes(1);
+ expect(queueService.queue).toHaveBeenCalledTimes(1);
+ });
+
+ test("a repeat that loses the race twice is recorded instead of dropped", async () => {
+ const { service, queueService, keyStore, auditLogQueue } = createHarness({ windowAcquired: false });
+ keyStore.getItem.mockResolvedValue(null);
+
+ await service.recordPermissionDenied(denial());
+
+ expect(keyStore.setItemWithExpiryNX).toHaveBeenCalledTimes(2);
+ expect(keyStore.incrementByWithExpiry).not.toHaveBeenCalled();
+ expect(auditLogQueue.pushToLog).toHaveBeenCalledTimes(1);
+ expect(queueService.queue).not.toHaveBeenCalled();
});
test("the method is part of the collapse key", async () => {
@@ -198,7 +227,7 @@ describe("createCollapsedAuditLog", () => {
await service.createCollapsedAuditLog({ ...collapsed(EventType.GET_SECRETS), collapseWindowSeconds: 300 });
expect(keyStore.setItemWithExpiryNX.mock.calls[0][1]).toBe(300);
- expect(queueService.queue.mock.calls[0][3].delay).toBe(300_000);
+ expect(queueService.queue.mock.calls[0][3].delay).toBe(302_000);
});
});
@@ -221,7 +250,10 @@ describe("collapsed audit log flush job", () => {
await flush({ data: jobData });
- expect(keyStore.deleteItem).toHaveBeenCalledTimes(1);
+ expect(keyStore.getItem).toHaveBeenCalledTimes(1);
+ expect(keyStore.getItem.mock.calls[0][0]).toContain(jobData.collapseKey);
+ expect(keyStore.getItem.mock.calls[0][0]).toContain(jobData.windowStart);
+ expect(keyStore.deleteItem).not.toHaveBeenCalled();
expect(auditLogQueue.pushToLog).toHaveBeenCalledTimes(1);
expect(auditLogQueue.pushToLog.mock.calls[0][0]).toMatchObject({
orgId: "org-1",
@@ -240,11 +272,10 @@ describe("collapsed audit log flush job", () => {
});
test("writes nothing when no repeat was counted", async () => {
- const { flush, keyStore, auditLogQueue } = createHarness({ storedCount: "0" });
+ const { flush, auditLogQueue } = createHarness({ storedCount: "0" });
await flush({ data: jobData });
- expect(keyStore.deleteItem).not.toHaveBeenCalled();
expect(auditLogQueue.pushToLog).not.toHaveBeenCalled();
});
});
diff --git a/backend/src/ee/services/audit-log/audit-log-service.ts b/backend/src/ee/services/audit-log/audit-log-service.ts
index 02787feca1..5d05801294 100644
--- a/backend/src/ee/services/audit-log/audit-log-service.ts
+++ b/backend/src/ee/services/audit-log/audit-log-service.ts
@@ -30,12 +30,16 @@ import {
EventType,
TAuditLogCollapsedFlushJobData,
TAuditLogCollapseSummary,
- TAuditLogServiceFactory
+ TAuditLogServiceFactory,
+ TCreateAuditLogDTO
} from "./audit-log-types";
const AUDIT_LOG_ROW_WARNING_THRESHOLD = 350_000_000;
const AUDIT_LOG_ALERT_ROW_INCREMENT = 10_000_000;
const DEFAULT_COLLAPSE_WINDOW_SECONDS = 60;
+// The flush runs after the window key has expired, so every repeat it counts was made while
+// that window was open.
+const COLLAPSE_FLUSH_GRACE_MS = 2_000;
type TAuditLogServiceFactoryDep = {
auditLogDAL: TAuditLogDALFactory;
@@ -45,10 +49,7 @@ type TAuditLogServiceFactoryDep = {
auditLogSettingsService: Pick<TAuditLogSettingsServiceFactory, "getEffectiveSettings">;
licenseService: Pick<TLicenseServiceFactory, "getPlan">;
queueService: Pick<TQueueServiceFactory, "queue" | "start">;
- keyStore: Pick<
- TKeyStoreFactory,
- "getItem" | "setItemWithExpiry" | "setItemWithExpiryNX" | "incrementByWithExpiry" | "deleteItem"
- >;
+ keyStore: Pick<TKeyStoreFactory, "getItem" | "setItemWithExpiry" | "setItemWithExpiryNX" | "incrementByWithExpiry">;
smtpService: Pick<TSmtpService, "sendMail">;
userDAL: Pick<TUserDALFactory, "getUsersByFilter">;
notificationService: Pick<TNotificationServiceFactory, "createUserNotifications">;
@@ -175,8 +176,7 @@ export const auditLogServiceFactory = ({
...el,
updatedAt: el.createdAt,
expiresAt: el.expiresAt,
- event: { type: logEventType, metadata: eventMetadata },
- eventClass: getAuditLogEventClass(logEventType),
+ event: { type: logEventType, class: getAuditLogEventClass(logEventType), metadata: eventMetadata },
actor: { type: eActor, metadata: actorMetadata }
}));
};
@@ -207,10 +207,9 @@ export const auditLogServiceFactory = ({
windowEnd,
...auditLog
}: TAuditLogCollapsedFlushJobData) => {
- const countKey = KeyStorePrefixes.AuditLogCollapseCount(collapseKey);
- const suppressedRepeats = Number(await keyStore.getItem(countKey)) || 0;
+ const suppressedRepeats =
+ Number(await keyStore.getItem(KeyStorePrefixes.AuditLogCollapseCount(collapseKey, windowStart))) || 0;
if (!suppressedRepeats) return;
- await keyStore.deleteItem(countKey);
const summary: TAuditLogCollapseSummary = {
suppressedRepeats,
@@ -227,25 +226,27 @@ export const auditLogServiceFactory = ({
await flushCollapsedRepeats(job.data);
});
- // First event per key is written and opens a window. Repeats inside it only bump a counter
- // that the flush job turns into one summary event.
- const createCollapsedAuditLog: TAuditLogServiceFactory["createCollapsedAuditLog"] = async ({
- collapseKeyParts,
- collapseWindowSeconds = DEFAULT_COLLAPSE_WINDOW_SECONDS,
- ...data
- }) => {
- const appCfg = getConfig();
- if (appCfg.DISABLE_AUDIT_LOG_GENERATION) return;
-
- const collapseKey = generateCacheKeyFromData([data.event.type, ...collapseKeyParts]);
- const acquired = await keyStore.setItemWithExpiryNX(
- KeyStorePrefixes.AuditLogCollapseWindow(collapseKey),
- collapseWindowSeconds,
- "1"
- );
+ // First event per key is written and opens a window. The window key holds its own start, and
+ // repeats bump a counter scoped to that start, so a late flush never reads the next window's
+ // repeats and consecutive windows never share a counter.
+ const collapseAuditLog = async (
+ data: TCreateAuditLogDTO,
+ collapseKey: string,
+ collapseWindowSeconds: number,
+ isRetry = false
+ ): Promise<void> => {
+ const windowKey = KeyStorePrefixes.AuditLogCollapseWindow(collapseKey);
+ const windowStart = new Date().toISOString();
+ const acquired = await keyStore.setItemWithExpiryNX(windowKey, collapseWindowSeconds, windowStart);
if (!acquired) {
+ const openWindowStart = await keyStore.getItem(windowKey);
+ if (!openWindowStart) {
+ if (!isRetry) return collapseAuditLog(data, collapseKey, collapseWindowSeconds, true);
+ await createAuditLog(data);
+ return;
+ }
await keyStore.incrementByWithExpiry(
- KeyStorePrefixes.AuditLogCollapseCount(collapseKey),
+ KeyStorePrefixes.AuditLogCollapseCount(collapseKey, openWindowStart),
1,
collapseWindowSeconds * 10
);
@@ -254,21 +255,32 @@ export const auditLogServiceFactory = ({
await createAuditLog(data);
- const windowStart = new Date();
- const windowEnd = new Date(windowStart.getTime() + collapseWindowSeconds * 1000);
+ const windowEnd = new Date(Date.parse(windowStart) + collapseWindowSeconds * 1000).toISOString();
await queueService.queue(
QueueName.AuditLogCollapsedFlush,
QueueJobs.AuditLogCollapsedFlush,
- { ...data, collapseKey, windowStart: windowStart.toISOString(), windowEnd: windowEnd.toISOString() },
+ { ...data, collapseKey, windowStart, windowEnd },
{
- jobId: `audit-log-collapse-${collapseKey}-${windowStart.getTime()}`,
- delay: collapseWindowSeconds * 1000,
+ jobId: `audit-log-collapse-${collapseKey}-${Date.parse(windowStart)}`,
+ delay: collapseWindowSeconds * 1000 + COLLAPSE_FLUSH_GRACE_MS,
removeOnComplete: true,
removeOnFail: true
}
);
};
+ const createCollapsedAuditLog: TAuditLogServiceFactory["createCollapsedAuditLog"] = async ({
+ collapseKeyParts,
+ collapseWindowSeconds = DEFAULT_COLLAPSE_WINDOW_SECONDS,
+ ...data
+ }) => {
+ const appCfg = getConfig();
+ if (appCfg.DISABLE_AUDIT_LOG_GENERATION) return;
+
+ const collapseKey = generateCacheKeyFromData([data.event.type, ...collapseKeyParts]);
+ await collapseAuditLog(data, collapseKey, collapseWindowSeconds);
+ };
+
// Called from the onError hook, so it must never throw or slow the response.
const recordPermissionDenied: TAuditLogServiceFactory["recordPermissionDenied"] = async ({
orgId,
diff --git a/backend/src/ee/services/audit-log/audit-log-types.ts b/backend/src/ee/services/audit-log/audit-log-types.ts
index 352e551f9e..76450b15af 100644
--- a/backend/src/ee/services/audit-log/audit-log-types.ts
+++ b/backend/src/ee/services/audit-log/audit-log-types.ts
@@ -173,9 +173,9 @@ export type TAuditLogServiceFactory = {
{
event: {
type: string;
+ class: AuditLogEventClass;
metadata: unknown;
};
- eventClass: AuditLogEventClass;
actor: {
type: string;
metadata: unknown;
@@ -6008,7 +6008,6 @@ interface PermissionDeniedEvent {
permissionSubject?: string;
permissionSubjectDetails?: Record<string, unknown>;
errorName: string;
- message: string;
route?: string;
method: string;
};
diff --git a/backend/src/keystore/keystore.ts b/backend/src/keystore/keystore.ts
index 0e4e38f4b3..f8da2e878b 100644
--- a/backend/src/keystore/keystore.ts
+++ b/backend/src/keystore/keystore.ts
@@ -201,7 +201,8 @@ export const KeyStorePrefixes = {
LicenseCloudPlanLastKnown: (orgId: string) => `infisical-cloud-plan-last-known-${orgId}` as const,
AuditLogOrgSettings: (orgId: string) => `audit-log-settings-${orgId}` as const,
AuditLogCollapseWindow: (key: string) => `audit-log-collapse-window-${key}` as const,
- AuditLogCollapseCount: (key: string) => `audit-log-collapse-count-${key}` as const,
+ AuditLogCollapseCount: (key: string, windowStart: string) =>
+ `audit-log-collapse-count-${key}-${windowStart}` as const,
// Set after a billing mutation to flag the org's plan cache for stale-while-revalidate reads.
LicenseCachePassThrough: (orgId: string) => `license-cache-passthrough-${orgId}` as const,
// Single-flight guard so only one background revalidation runs per org per lock window.
diff --git a/backend/src/server/plugins/audit-log-permission-denied.ts b/backend/src/server/plugins/audit-log-permission-denied.ts
index c9e717eb7d..089c05e0f9 100644
--- a/backend/src/server/plugins/audit-log-permission-denied.ts
+++ b/backend/src/server/plugins/audit-log-permission-denied.ts
@@ -20,6 +20,17 @@ const pickSubjectDetails = (subject: unknown) => {
return Object.keys(details).length ? details : undefined;
};
+const readMissingPermission = (details: unknown) => {
+ if (!details || typeof details !== "object") return undefined;
+ const { missingPermissions } = details as { missingPermissions?: { action?: unknown; subject?: unknown }[] };
+ const first = Array.isArray(missingPermissions) ? missingPermissions[0] : undefined;
+ if (!first) return undefined;
+ return {
+ action: typeof first.action === "string" ? first.action : undefined,
+ subject: typeof first.subject === "string" ? first.subject : undefined
+ };
+};
+
const readProjectId = (source: unknown) => {
if (!source || typeof source !== "object") return undefined;
const { projectId } = source as { projectId?: unknown };
@@ -40,6 +51,7 @@ export const injectPermissionDeniedAuditLog = fp(async (server: FastifyZodProvid
if (!orgId) return;
const caslError = isCaslDenial ? (error as unknown as ForbiddenError<AnyAbility>) : undefined;
+ const missing = isCaslDenial ? undefined : readMissingPermission((error as PermissionBoundaryError).details);
const projectId =
requestContext.get(RequestContextKey.ProjectDetails)?.id ??
readProjectId(req.params) ??
@@ -51,11 +63,10 @@ export const injectPermissionDeniedAuditLog = fp(async (server: FastifyZodProvid
orgId,
projectId,
metadata: {
- permissionAction: caslError?.action ? String(caslError.action) : undefined,
- permissionSubject: caslError?.subjectType ? String(caslError.subjectType) : undefined,
+ permissionAction: caslError?.action ? String(caslError.action) : missing?.action,
+ permissionSubject: caslError?.subjectType ? String(caslError.subjectType) : missing?.subject,
permissionSubjectDetails: pickSubjectDetails(caslError?.subject),
errorName: error.name,
- message: error.message,
route: req.routeOptions.url,
method: req.method
}
diff --git a/backend/src/server/routes/v1/organization-router.ts b/backend/src/server/routes/v1/organization-router.ts
index 623ec0a527..e03ba49273 100644
--- a/backend/src/server/routes/v1/organization-router.ts
+++ b/backend/src/server/routes/v1/organization-router.ts
@@ -308,9 +308,9 @@ export const registerOrgRouter = async (server: FastifyZodProvider) => {
z.object({
event: z.object({
type: z.string(),
+ class: z.nativeEnum(AuditLogEventClass),
metadata: z.any()
}),
- eventClass: z.nativeEnum(AuditLogEventClass),
actor: z.object({
type: z.string(),
metadata: z.any()
diff --git a/docs/documentation/platform/audit-logs.mdx b/docs/documentation/platform/audit-logs.mdx
index a937b65d24..8d428d3a9c 100644
--- a/docs/documentation/platform/audit-logs.mdx
+++ b/docs/documentation/platform/audit-logs.mdx
@@ -142,16 +142,15 @@ Each log contains the following fields:
<ResponseField name="type" type="string">
The event type (for example, `create-secret`). See [Event types](#event-types) for the full catalog.
</ResponseField>
+ <ResponseField name="class" type="string">
+ The [event class](#event-classes) the event type belongs to: `management`, `authentication`, `authorization`, or `data-access`.
+ </ResponseField>
<ResponseField name="metadata" type="object">
Context specific to the event. The shape depends on `event.type`; a `get-secret` event carries `environment`, `secretPath`, `secretKey`, and `secretVersion`, while a batch event like `create-secrets` carries a `secrets` array.
</ResponseField>
</Expandable>
</ResponseField>
-<ResponseField name="eventClass" type="string">
- The [event class](#event-classes) the event type belongs to: `management`, `authentication`, `authorization`, or `data-access`.
-</ResponseField>
-
<ResponseField name="actor" type="object">
Who performed the action.
<Expandable title="properties">
@@ -202,6 +201,7 @@ Each log contains the following fields:
"projectName": "[PROJECT_NAME]",
"event": {
"type": "get-secret",
+ "class": "data-access",
"metadata": {
"environment": "prod",
"secretPath": "/db",
diff --git a/frontend/src/hooks/api/auditLogs/types.tsx b/frontend/src/hooks/api/auditLogs/types.tsx
index dfef666f68..9ef11adaae 100644
--- a/frontend/src/hooks/api/auditLogs/types.tsx
+++ b/frontend/src/hooks/api/auditLogs/types.tsx
@@ -1130,8 +1130,7 @@ export type AuditLog = {
organization: string;
workspace: string;
ipAddress: string;
- event: Event;
- eventClass: AuditLogEventClass;
+ event: Event & { class: AuditLogEventClass };
userAgent: string;
userAgentType: UserAgentType;
createdAt: string;
diff --git a/frontend/src/pages/organization/AuditLogsPage/components/AuditLogDetailsSheet.tsx b/frontend/src/pages/organization/AuditLogsPage/components/AuditLogDetailsSheet.tsx
index 3c1ed037f0..ac8a761ca3 100644
--- a/frontend/src/pages/organization/AuditLogsPage/components/AuditLogDetailsSheet.tsx
+++ b/frontend/src/pages/organization/AuditLogsPage/components/AuditLogDetailsSheet.tsx
@@ -40,7 +40,7 @@ export const AuditLogDetailsSheet = ({ isOpen, onOpenChange, auditLog, timezone
{formatDateTime({ timestamp: displayedLog.createdAt, timezone })}
</SheetDescription>
<div>
- <Badge variant="neutral">{displayedLog.eventClass}</Badge>
+ <Badge variant="neutral">{displayedLog.event.class}</Badge>
</div>
</SheetHeader>
<div className="min-h-0 flex-1 p-4">
diff --git a/frontend/src/pages/organization/AuditLogsPage/components/LogsTableRow.tsx b/frontend/src/pages/organization/AuditLogsPage/components/LogsTableRow.tsx
index 0e9e78d7ad..6a9dddd1e3 100644
--- a/frontend/src/pages/organization/AuditLogsPage/components/LogsTableRow.tsx
+++ b/frontend/src/pages/organization/AuditLogsPage/components/LogsTableRow.tsx
@@ -65,7 +65,6 @@ export const LogsTableRow = ({ auditLog, rowNumber, timezone, onClick }: Props)
<TableCell>
<div className="flex flex-wrap items-center gap-2 text-sm">
<Tag label="event" value={auditLog.event.type} />
- <Tag label="class" value={auditLog.eventClass} />
<Tag label="actor" value={auditLog.actor.type} />
{auditLog.actor.type === ActorType.USER && (
<>