Skip to content

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

fix(audit-log): reject out-of-range project retention instead of returning 500

Scott Wilson3a3bc770fd58
diff --git a/backend/src/ee/services/audit-log/audit-log-retention-fns.ts b/backend/src/ee/services/audit-log/audit-log-retention-fns.ts
index d1195ba2e0..db3e2eddae 100644
--- a/backend/src/ee/services/audit-log/audit-log-retention-fns.ts
+++ b/backend/src/ee/services/audit-log/audit-log-retention-fns.ts
@@ -7,6 +7,10 @@ const MS_IN_DAY = 24 * 60 * 60 * 1000;
// capped here so no retention value can reach that overflow.
export const MAX_AUDIT_LOG_EXPIRES_AT = new Date("2100-01-01T00:00:00.000Z");
+// Bounds what a project can request: past the expiry cap, so it never limits an unlimited plan, and
+// inside the integer column, so an oversized value is a 400 rather than a failed write.
+export const MAX_PROJECT_AUDIT_LOG_RETENTION_DAYS = 36_500;
+
type TAuditLogRetentionPlan = Pick<TFeatureSet, "auditLogsRetentionDays">;
// A null retention is the License Server's "No limit"; 0 means the plan keeps no audit logs.
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 ea4a0fc7ac..b46ff94562 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
@@ -36,7 +36,11 @@ const denial = (overrides: Partial<TRecordPermissionDeniedDTO> = {}): TRecordPer
...overrides
});
-const createHarness = ({ windowAcquired = true, storedCount = "0", retentionDays = 30 } = {}) => {
+const createHarness = ({
+ windowAcquired = true,
+ storedCount = "0",
+ retentionDays = 30 as number | null
+}: { windowAcquired?: boolean; storedCount?: string; retentionDays?: number | null } = {}) => {
const startHandlers = new Map<string, (job: { data: unknown }) => Promise<void>>();
const queueService = {
start: vi.fn((name: string, fn: (job: { data: unknown }) => Promise<void>) => {
@@ -194,6 +198,17 @@ describe("recordPermissionDenied", () => {
expect(auditLogQueue.pushToLog).not.toHaveBeenCalled();
});
+ test("an unlimited (null) retention records the denial and schedules the flush job", async () => {
+ const { service, queueService, auditLogQueue } = createHarness({ retentionDays: null });
+
+ await service.recordPermissionDenied(denial());
+
+ expect(auditLogQueue.pushToLog).toHaveBeenCalledTimes(1);
+ expect(auditLogQueue.pushToLog.mock.calls[0][0]).toMatchObject({ event: { type: EventType.PERMISSION_DENIED } });
+ expect(queueService.queue).toHaveBeenCalledTimes(1);
+ expect(queueService.queue.mock.calls[0][0]).toBe(QueueName.AuditLogCollapsedFlush);
+ });
+
test("a keystore failure is swallowed", async () => {
const { service, keyStore, auditLogQueue } = createHarness();
keyStore.setItemWithExpiryNX.mockRejectedValueOnce(new Error("redis down"));
diff --git a/backend/src/server/routes/v1/deprecated-project-router.ts b/backend/src/server/routes/v1/deprecated-project-router.ts
index ab6324b521..91f53f556b 100644
--- a/backend/src/server/routes/v1/deprecated-project-router.ts
+++ b/backend/src/server/routes/v1/deprecated-project-router.ts
@@ -8,6 +8,7 @@ import {
SortDirection
} from "@app/db/schemas";
import { ProjectMicrosoftTeamsConfigsSchema } from "@app/db/schemas/project-microsoft-teams-configs";
+import { MAX_PROJECT_AUDIT_LOG_RETENTION_DAYS } from "@app/ee/services/audit-log/audit-log-retention-fns";
import { EventType } from "@app/ee/services/audit-log/audit-log-types";
import { ApiDocsTags, PROJECTS } from "@app/lib/api-docs";
import { CharacterType, characterValidator } from "@app/lib/validator/validate-string";
@@ -267,7 +268,7 @@ export const registerDeprecatedProjectRouter = async (server: FastifyZodProvider
workspaceSlug: z.string().trim()
}),
body: z.object({
- auditLogsRetentionDays: z.number().min(0)
+ auditLogsRetentionDays: z.number().int().min(0).max(MAX_PROJECT_AUDIT_LOG_RETENTION_DAYS)
}),
response: {
200: z.object({
diff --git a/backend/src/server/routes/v1/project-router.ts b/backend/src/server/routes/v1/project-router.ts
index 3b6c3ce065..b1cd61e148 100644
--- a/backend/src/server/routes/v1/project-router.ts
+++ b/backend/src/server/routes/v1/project-router.ts
@@ -14,6 +14,7 @@ import {
SortDirection
} from "@app/db/schemas";
import { ProjectMicrosoftTeamsConfigsSchema } from "@app/db/schemas/project-microsoft-teams-configs";
+import { MAX_PROJECT_AUDIT_LOG_RETENTION_DAYS } from "@app/ee/services/audit-log/audit-log-retention-fns";
import {
AuditLogSettingsResponseSchema,
updateAuditLogSettingsBodySchema
@@ -546,7 +547,13 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
.optional()
.describe(PROJECTS.UPDATE.secretDetectionIgnoreValues),
pitVersionLimit: z.number().min(1).max(100).optional(),
- auditLogsRetentionDays: z.number().min(1).max(365).optional().describe(PROJECTS.UPDATE.auditLogsRetentionDays)
+ auditLogsRetentionDays: z
+ .number()
+ .int()
+ .min(1)
+ .max(365)
+ .optional()
+ .describe(PROJECTS.UPDATE.auditLogsRetentionDays)
}),
response: {
200: z.object({
@@ -700,7 +707,7 @@ export const registerProjectRouter = async (server: FastifyZodProvider) => {
projectId: z.string().trim()
}),
body: z.object({
- auditLogsRetentionDays: z.number().min(0)
+ auditLogsRetentionDays: z.number().int().min(0).max(MAX_PROJECT_AUDIT_LOG_RETENTION_DAYS)
}),
response: {
200: z.object({
diff --git a/frontend/src/pages/project/AuditLogsPage/components/AuditLogsRetentionSection/AuditLogsRetentionSection.tsx b/frontend/src/pages/project/AuditLogsPage/components/AuditLogsRetentionSection/AuditLogsRetentionSection.tsx
index a4c2016115..44d245af08 100644
--- a/frontend/src/pages/project/AuditLogsPage/components/AuditLogsRetentionSection/AuditLogsRetentionSection.tsx
+++ b/frontend/src/pages/project/AuditLogsPage/components/AuditLogsRetentionSection/AuditLogsRetentionSection.tsx
@@ -31,7 +31,7 @@ import { usePopUp } from "@app/hooks";
import { useUpdateWorkspaceAuditLogsRetention } from "@app/hooks/api/projects/queries";
const formSchema = z.object({
- auditLogsRetentionDays: z.coerce.number().min(0)
+ auditLogsRetentionDays: z.coerce.number().int().min(0).max(36_500)
});
type TForm = z.infer<typeof formSchema>;