Skip to content

Commit 018bde2

Browse files
nipunn1313Convex, Inc.
authored andcommitted
Limit the deployment history in the system-udf (#42278)
Prevent from seeing audit logs before the retention period. Manually tested and confirmed the UDF throws. GitOrigin-RevId: eecc56dd7fba1728f80e8d3d999229e55cc6cd2a
1 parent d336308 commit 018bde2

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

npm-packages/system-udfs/convex/_system/frontend/listDeploymentEventsFromTime.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Doc } from "../../_generated/dataModel";
22
import { queryPrivateSystem } from "../secretSystemTables";
33
import { v } from "convex/values";
4+
import { clampForAuditLogRetention } from "./paginatedDeploymentEvents";
45

56
/**
67
* Get the deployment events on or after the provided timestamp from least recent
@@ -12,6 +13,7 @@ export default queryPrivateSystem({
1213
{ db },
1314
{ fromTimestamp },
1415
): Promise<Doc<"_deployment_audit_log">[]> {
16+
fromTimestamp = await clampForAuditLogRetention(db, fromTimestamp);
1517
return await db
1618
.query("_deployment_audit_log")
1719
.withIndex("by_creation_time", (q) =>

npm-packages/system-udfs/convex/_system/frontend/paginatedDeploymentEvents.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { paginationOptsValidator } from "convex/server";
22
import { queryPrivateSystem } from "../secretSystemTables";
33
import { v } from "convex/values";
44
import { maximumBytesRead, maximumRowsRead } from "../paginationLimits";
5+
import { DatabaseReader } from "../../_generated/server";
56

67
/**
78
* Paginated query for the deployment events from most recent to least recent
@@ -17,6 +18,8 @@ export default queryPrivateSystem({
1718
}),
1819
},
1920
handler: async function ({ db }, { paginationOpts, filters }) {
21+
filters.minDate = await clampForAuditLogRetention(db, filters.minDate);
22+
2023
const paginatedResults = await db
2124
.query("_deployment_audit_log")
2225
.withIndex("by_creation_time", (q) => {
@@ -58,3 +61,21 @@ export default queryPrivateSystem({
5861
return paginatedResults;
5962
},
6063
});
64+
65+
export async function clampForAuditLogRetention(
66+
db: DatabaseReader,
67+
minDate: number,
68+
) {
69+
const backendInfo = await db.query("_backend_info").first();
70+
const auditLogRetentionDays = Number(backendInfo?.auditLogRetentionDays || 0);
71+
// no limit if auditLogRetentionDays is -1
72+
if (auditLogRetentionDays === -1) {
73+
return minDate;
74+
}
75+
const minAllowable =
76+
Date.now() - (auditLogRetentionDays + 1) * 24 * 60 * 60 * 1000;
77+
if (minDate < minAllowable) {
78+
return minAllowable;
79+
}
80+
return minDate;
81+
}

npm-packages/system-udfs/convex/schema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,7 @@ export default defineSchema({
445445
_backend_state: backendStateTable,
446446
_snapshot_imports: snapshotImportsTable,
447447
_aws_lambda_versions: awsLambdaVersionsTable,
448+
_backend_info: defineTable({
449+
auditLogRetentionDays: v.union(v.int64(), v.null()),
450+
}),
448451
});

0 commit comments

Comments
 (0)