Skip to content

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

fix(secrets): drop the eventId default and resolve honey token folders properly

Calvinbe9e99c1f6e6
diff --git a/backend/CLAUDE.md b/backend/CLAUDE.md
index cf0fd9bef4..148abad6c4 100644
--- a/backend/CLAUDE.md
+++ b/backend/CLAUDE.md
@@ -811,6 +811,8 @@ await someDAL.transaction(async (tx) => {
- `emitMany(events, tx)` writes a batch in one go (eg one event per secret in a bulk write), in chunks
of 1000 rows to stay under Postgres's bind parameter limit. Every event gets one `eventId` (UUID),
shared by all its consumers' rows; `TEvent.id` is the consumer's row, `TEvent.eventId` the occurrence.
+ `eventId` is nullable, because a pod on the previous release writes rows without it during a rolling
+ deploy.
**How delivery works:**
diff --git a/backend/e2e-test/secret-folder-dal.spec.ts b/backend/e2e-test/secret-folder-dal.spec.ts
new file mode 100644
index 0000000000..00c8879286
--- /dev/null
+++ b/backend/e2e-test/secret-folder-dal.spec.ts
@@ -0,0 +1,43 @@
+import { Knex } from "knex";
+
+import { TableName } from "@app/db/schemas";
+import { secretFolderDALFactory } from "@app/services/secret-folder/secret-folder-dal";
+
+import { createIsolatedOrgAndProject } from "./testUtils/fixtures";
+import { createFolder } from "./testUtils/folders";
+
+declare const testDb: Knex;
+
+const folderDAL = secretFolderDALFactory(testDb as never);
+
+describe("secret folder dal (postgres)", () => {
+ // No API puts an environment into its deletion grace period and still reads its folders, so the
+ // environment is soft-deleted directly.
+ test("findSecretPathByFolderIds hides an environment pending deletion unless asked to include it", async () => {
+ const { projectId, authToken, cleanup } = await createIsolatedOrgAndProject("folder-dal");
+ try {
+ const folder = await createFolder({
+ workspaceId: projectId,
+ environmentSlug: "dev",
+ secretPath: "/",
+ name: "app",
+ authToken
+ });
+ await testDb(TableName.Environment).where({ projectId, slug: "dev" }).update({ deleteAfter: new Date() });
+
+ const [hidden] = await folderDAL.findSecretPathByFolderIds(projectId, [folder.id], undefined, {
+ readFromPrimary: true
+ });
+ expect(hidden).toBeUndefined();
+
+ const [found] = await folderDAL.findSecretPathByFolderIds(projectId, [folder.id], undefined, {
+ readFromPrimary: true,
+ includePendingDeletion: true
+ });
+ expect(found).toMatchObject({ id: folder.id, path: "/app", environmentSlug: "dev" });
+ } finally {
+ await testDb(TableName.Environment).where({ projectId }).update({ deleteAfter: null });
+ await cleanup();
+ }
+ });
+});
diff --git a/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts b/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts
index 11f05eb91e..148a8206d7 100644
--- a/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts
+++ b/backend/src/db/migrations/20261019120000_event-outbox-event-id.ts
@@ -4,13 +4,14 @@ import { TableName } from "../schemas";
// One id per event, shared by every consumer's row, so the same occurrence can be recognised across
// consumers and handed out as the CloudEvents `id`. The row `id` is per consumer and can't serve.
-// The default only fills rows written before this column existed and rows from a pod that doesn't set
-// it yet; the outbox is pruned within a day, so the rewrite the volatile default causes is small.
+// Nullable with no default: a pod still running the previous release inserts without it during a rolling
+// deploy, and NOT NULL would fail that insert along with the write it belongs to. Those rows are pruned
+// within a day.
export async function up(knex: Knex): Promise<void> {
if (await knex.schema.hasColumn(TableName.EventOutbox, "eventId")) return;
await knex.schema.alterTable(TableName.EventOutbox, (t) => {
- t.uuid("eventId").notNullable().defaultTo(knex.fn.uuid());
+ t.uuid("eventId");
});
}
diff --git a/backend/src/db/schemas/event-outbox.ts b/backend/src/db/schemas/event-outbox.ts
index c4b718f551..c356f280b6 100644
--- a/backend/src/db/schemas/event-outbox.ts
+++ b/backend/src/db/schemas/event-outbox.ts
@@ -22,7 +22,7 @@ export const EventOutboxSchema = z.object({
lastError: z.string().nullable().optional(),
createdAt: z.date(),
updatedAt: z.date(),
- eventId: z.string().uuid()
+ eventId: z.string().uuid().nullable().optional()
});
export type TEventOutbox = z.infer<typeof EventOutboxSchema>;
diff --git a/backend/src/ee/services/honey-token/honey-token-service.ts b/backend/src/ee/services/honey-token/honey-token-service.ts
index 9f962b5efe..161ddf2ffd 100644
--- a/backend/src/ee/services/honey-token/honey-token-service.ts
+++ b/backend/src/ee/services/honey-token/honey-token-service.ts
@@ -443,14 +443,16 @@ export const honeyTokenServiceFactory = ({
}
const { projectId } = honeyToken;
- const [folderWithEnv] = await folderDAL.findSecretPathByFolderIds(projectId, [honeyToken.folderId]);
- // No folder resolves while the environment is pending deletion, and update and revoke must still work
- // then. The empty scope fails the event schema, so that event is dropped rather than misrouted.
- const eventScope = {
- projectId,
- environment: folderWithEnv?.environmentSlug ?? "",
- secretPath: folderWithEnv?.path ?? "/"
- };
+ // From the primary and including an environment pending deletion: update and revoke must still work
+ // during the grace period, and the permission check needs the token's real environment and path.
+ const [folderWithEnv] = await folderDAL.findSecretPathByFolderIds(projectId, [honeyToken.folderId], undefined, {
+ readFromPrimary: true,
+ includePendingDeletion: true
+ });
+ if (!folderWithEnv) {
+ throw new NotFoundError({ message: `Folder of honey token with ID "${honeyTokenId}" not found` });
+ }
+ const eventScope = { projectId, environment: folderWithEnv.environmentSlug, secretPath: folderWithEnv.path };
const { permission: updatePermission } = await permissionService.getProjectPermission({
actor: actor.type,
actorId: actor.id,
@@ -462,8 +464,8 @@ export const honeyTokenServiceFactory = ({
ForbiddenError.from(updatePermission).throwUnlessCan(
ProjectPermissionHoneyTokenActions.Edit,
subject(ProjectPermissionSub.HoneyTokens, {
- environment: folderWithEnv?.environmentSlug ?? "",
- secretPath: folderWithEnv?.path ?? "/"
+ environment: folderWithEnv.environmentSlug,
+ secretPath: folderWithEnv.path
})
);
@@ -645,14 +647,16 @@ export const honeyTokenServiceFactory = ({
}
const { projectId } = honeyToken;
- const [folderWithEnv] = await folderDAL.findSecretPathByFolderIds(projectId, [honeyToken.folderId]);
- // No folder resolves while the environment is pending deletion, and update and revoke must still work
- // then. The empty scope fails the event schema, so that event is dropped rather than misrouted.
- const eventScope = {
- projectId,
- environment: folderWithEnv?.environmentSlug ?? "",
- secretPath: folderWithEnv?.path ?? "/"
- };
+ // From the primary and including an environment pending deletion: update and revoke must still work
+ // during the grace period, and the permission check needs the token's real environment and path.
+ const [folderWithEnv] = await folderDAL.findSecretPathByFolderIds(projectId, [honeyToken.folderId], undefined, {
+ readFromPrimary: true,
+ includePendingDeletion: true
+ });
+ if (!folderWithEnv) {
+ throw new NotFoundError({ message: `Folder of honey token with ID "${honeyTokenId}" not found` });
+ }
+ const eventScope = { projectId, environment: folderWithEnv.environmentSlug, secretPath: folderWithEnv.path };
const { permission: revokePermission } = await permissionService.getProjectPermission({
actor: actor.type,
actorId: actor.id,
@@ -664,8 +668,8 @@ export const honeyTokenServiceFactory = ({
ForbiddenError.from(revokePermission).throwUnlessCan(
ProjectPermissionHoneyTokenActions.Revoke,
subject(ProjectPermissionSub.HoneyTokens, {
- environment: folderWithEnv?.environmentSlug ?? "",
- secretPath: folderWithEnv?.path ?? "/"
+ environment: folderWithEnv.environmentSlug,
+ secretPath: folderWithEnv.path
})
);
diff --git a/backend/src/services/event-outbox/event-definition.test.ts b/backend/src/services/event-outbox/event-definition.test.ts
index d995c25bb6..1e2995537c 100644
--- a/backend/src/services/event-outbox/event-definition.test.ts
+++ b/backend/src/services/event-outbox/event-definition.test.ts
@@ -73,6 +73,10 @@ describe("toCloudEvent", () => {
});
});
+ test("refuses an event recorded without an eventId", () => {
+ expect(() => toCloudEvent(ExampleEvent, { ...event, eventId: null })).toThrow("without an eventId");
+ });
+
test("refuses an event of another type", () => {
expect(() =>
toCloudEvent(ExampleEvent, { ...event, eventType: "com.infisical.secrets.secret.modified.v1" })
diff --git a/backend/src/services/event-outbox/event-definition.ts b/backend/src/services/event-outbox/event-definition.ts
index be503cecc6..c90e509505 100644
--- a/backend/src/services/event-outbox/event-definition.ts
+++ b/backend/src/services/event-outbox/event-definition.ts
@@ -63,6 +63,9 @@ export const toCloudEvent = <TData extends z.AnyZodObject>(
if (event.eventType !== definition.type) {
throw new Error(`Event '${event.eventType}' cannot be read as '${definition.type}'`);
}
+ if (!event.eventId) {
+ throw new Error(`Event '${event.id}' was recorded without an eventId, by a release that predates it`);
+ }
return {
specversion: "1.0",
diff --git a/backend/src/services/secret-folder/secret-folder-dal.ts b/backend/src/services/secret-folder/secret-folder-dal.ts
index 842998b456..47d6ff5b37 100644
--- a/backend/src/services/secret-folder/secret-folder-dal.ts
+++ b/backend/src/services/secret-folder/secret-folder-dal.ts
@@ -217,15 +217,23 @@ export const secretFolderDALFactory = (db: TDbClient) => {
projectId: string,
folderIds: string[],
tx?: Knex,
- { readFromPrimary = false }: { readFromPrimary?: boolean } = {}
+ {
+ readFromPrimary = false,
+ includePendingDeletion = false
+ }: { readFromPrimary?: boolean; includePendingDeletion?: boolean } = {}
) => {
const conn = tx || (readFromPrimary ? db : db.replicaNode());
+ // An environment pending deletion is hidden from every read, but a caller still acting on a resource
+ // inside it (eg revoking a honey token during the grace period) needs its real location.
+ const excludePendingDeletion = (qb: Knex.QueryBuilder) => {
+ if (!includePendingDeletion) void qb.whereNull(`${TableName.Environment}.deleteAfter`);
+ };
try {
const targetFolders = await conn(TableName.SecretFolder)
.join(TableName.Environment, `${TableName.SecretFolder}.envId`, `${TableName.Environment}.id`)
.whereIn(`${TableName.SecretFolder}.id`, folderIds)
.where(`${TableName.Environment}.projectId`, projectId)
- .whereNull(`${TableName.Environment}.deleteAfter`)
+ .where(excludePendingDeletion)
.select(
selectAllTableCols(TableName.SecretFolder),
db.ref("slug").withSchema(TableName.Environment).as("environmentSlug"),
@@ -243,7 +251,7 @@ export const secretFolderDALFactory = (db: TDbClient) => {
.join(TableName.Environment, `${TableName.SecretFolder}.envId`, `${TableName.Environment}.id`)
.whereIn(`${TableName.SecretFolder}.id`, folderIds)
.where(`${TableName.Environment}.projectId`, projectId)
- .whereNull(`${TableName.Environment}.deleteAfter`)
+ .where(excludePendingDeletion)
);
const idMap = buildFolderIdMap(allEnvFolders);