Skip to content

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

fix(reminders): refuse unreachable reminder recipients before the secret is written

Calvin2944b639929f
diff --git a/backend/e2e-test/routes/v1/secret-reminder.spec.ts b/backend/e2e-test/routes/v1/secret-reminder.spec.ts
index 6afbefcdc9..e6d2d51b86 100644
--- a/backend/e2e-test/routes/v1/secret-reminder.spec.ts
+++ b/backend/e2e-test/routes/v1/secret-reminder.spec.ts
@@ -14,7 +14,7 @@ import {
setSecretReminder,
waitForReminderEmails
} from "../../testUtils/reminders";
-import { createSecretV2, getSecretByNameV2 } from "../../testUtils/secrets";
+import { createSecretV2, getSecretByNameV2, updateSecretV2 } from "../../testUtils/secrets";
import { addUserMembership, createUser, deleteUsers } from "../../testUtils/users";
const ENVIRONMENT = "dev";
@@ -238,6 +238,51 @@ describe("Secret reminders delivered through alerts", () => {
await expectAlertFullyRemoved(alert);
});
+ test("a secret update sets the reminder and its recipients", async () => {
+ const secretId = await createSecret("WITH_UPDATE");
+ await updateSecretV2({
+ workspaceId: projectId,
+ environmentSlug: ENVIRONMENT,
+ secretPath: "/",
+ key: "WITH_UPDATE",
+ value: "rotated",
+ reminder: { repeatDays: 14, note: "rotate it", recipients: [member.userId] },
+ authToken
+ });
+
+ const reminder = await getSecretReminder({ secretId, authToken });
+ expect(reminder?.repeatDays).toBe(14);
+ expect(reminder?.recipients).toEqual([member.userId]);
+ });
+
+ test("a secret update with recipients outside the project is refused before the secret changes", async () => {
+ const secretId = await createSecret("REFUSED");
+ const outsider = await newUser("outsider");
+
+ await updateSecretV2({
+ workspaceId: projectId,
+ environmentSlug: ENVIRONMENT,
+ secretPath: "/",
+ key: "REFUSED",
+ value: "rotated",
+ reminder: { repeatDays: 14, recipients: [outsider.userId] },
+ authToken
+ }).expect((res) => {
+ expect(res.statusCode).toBe(400);
+ expect(res.json<{ message: string }>().message).toMatch(/None of the selected reminder recipients/);
+ });
+
+ const secret = await getSecretByNameV2({
+ workspaceId: projectId,
+ environmentSlug: ENVIRONMENT,
+ secretPath: "/",
+ key: "REFUSED",
+ authToken
+ });
+ expect(secret.secretValue).toBe("value");
+ expect(await getSecretReminder({ secretId, authToken })).toBeNull();
+ });
+
test("deleting a reminder removes its alert, channels and recipients", async () => {
const secretId = await createSecret("DELETE_ME");
await setSecretReminder({ secretId, authToken, repeatDays: 30, recipients: [member.userId] });
diff --git a/backend/e2e-test/testUtils/secrets.ts b/backend/e2e-test/testUtils/secrets.ts
index 0b19edae12..40f80d1144 100644
--- a/backend/e2e-test/testUtils/secrets.ts
+++ b/backend/e2e-test/testUtils/secrets.ts
@@ -59,6 +59,7 @@ type TUpdateSecretV2Dto = {
value?: string;
newKey?: string;
comment?: string;
+ reminder?: { repeatDays: number; note?: string; recipients?: string[] };
authToken: string;
type?: SecretType;
};
@@ -78,7 +79,10 @@ export const updateSecretV2 = (dto: TUpdateSecretV2Dto) =>
secretPath: dto.secretPath,
secretValue: dto.value,
newSecretName: dto.newKey,
- secretComment: dto.comment
+ secretComment: dto.comment,
+ secretReminderRepeatDays: dto.reminder?.repeatDays,
+ secretReminderNote: dto.reminder?.note,
+ secretReminderRecipients: dto.reminder?.recipients
}
},
parseSecret
diff --git a/backend/src/services/reminder/reminder-service.test.ts b/backend/src/services/reminder/reminder-service.test.ts
index f8d54a3d86..9a46dab38e 100644
--- a/backend/src/services/reminder/reminder-service.test.ts
+++ b/backend/src/services/reminder/reminder-service.test.ts
@@ -278,7 +278,7 @@ const caller = { actor: "user", actorId: "user-1", actorOrgId: "org-1", actorAut
const saveReminder = (
service: ReturnType<typeof buildWriteService>["service"],
- fields: { recipients?: string[] | null; channels?: unknown[] }
+ fields: { recipients?: string[] | null; resolvedRecipients?: unknown[]; channels?: unknown[] }
) =>
service.createReminder({
...caller,
@@ -342,6 +342,21 @@ describe("reminder alert sync", () => {
expect(calls).toEqual([]);
});
+ test("recipients a caller already resolved are used as given, without a second membership check", async () => {
+ const { service, created } = buildWriteService({ projectUserIds: [] });
+ await saveReminder(service, { resolvedRecipients: [{ principalType: "user", principalId: "user-1" }] });
+ expect((created[0] as { channels: { recipients: unknown }[] }).channels[0].recipients).toEqual([
+ { principalType: "user", principalId: "user-1" }
+ ]);
+ });
+
+ test("resolving recipients refuses a list with nobody left in the project", async () => {
+ const { service } = buildWriteService({ projectUserIds: [] });
+ await expect(
+ service.resolveReminderRecipients({ actorOrgId: "org-1", projectId: "proj-1", recipients: ["user-gone"] })
+ ).rejects.toThrow(/None of the selected reminder recipients/);
+ });
+
test("given channels, saves them as the alert's complete channel list", async () => {
const channels = [
{
diff --git a/backend/src/services/reminder/reminder-service.ts b/backend/src/services/reminder/reminder-service.ts
index 2f6eb26c20..ea49099439 100644
--- a/backend/src/services/reminder/reminder-service.ts
+++ b/backend/src/services/reminder/reminder-service.ts
@@ -10,7 +10,7 @@ import { BadRequestError, NotFoundError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { TGenericPermission } from "@app/lib/types";
-import { TAlertChannelInput } from "../alert/alert-channel-service-types";
+import { TAlertChannelInput, TChannelRecipientInput } from "../alert/alert-channel-service-types";
import { AlertChannelType } from "../alert/alert-channel-types";
import { TAlertServiceFactory } from "../alert/alert-service";
import { AlertPrincipalType, MAX_RECIPIENTS_PER_CHANNEL } from "../alert/alert-types";
@@ -78,23 +78,46 @@ export const reminderServiceFactory = ({
return bySecret;
};
+ // Who a reminder's email goes to. An empty list keeps the reminder's original meaning: everyone in the
+ // project. Exposed so a caller that writes something else first (a secret update) can refuse the
+ // request before that write instead of after it has committed.
+ const resolveReminderRecipients: TReminderServiceFactory["resolveReminderRecipients"] = async ({
+ actorOrgId,
+ projectId,
+ recipients
+ }) => {
+ // Reminder recipients were never pruned when someone left the project, so drop them rather than fail.
+ const requested = [...new Set(recipients ?? [])];
+ const inScope = await alertService.filterRecipientsInScope(
+ { orgId: actorOrgId, projectId },
+ requested.map((principalId) => ({ principalType: AlertPrincipalType.USER, principalId }))
+ );
+ // Falling back to the whole project here would send the secret's key, path and note to people nobody
+ // chose, so a list with nobody left in it is refused instead.
+ if (requested.length && !inScope.length) {
+ throw new BadRequestError({
+ message: "None of the selected reminder recipients are members of this project. Choose recipients again."
+ });
+ }
+ return inScope.length ? inScope : [{ principalType: AlertPrincipalType.PROJECT_MEMBERS, principalId: projectId }];
+ };
+
// Given `channels`, they are the alert's complete channel list. Otherwise the reminder API only knows
// user ids, so it owns the alert's email channels and leaves every other channel (Slack, webhook,
- // PagerDuty) as the user configured it. An empty list keeps the reminder's original meaning: everyone
- // in the project. The alert is written as the caller, so the reminder provider checks they can edit
- // the secret.
+ // PagerDuty) as the user configured it. The alert is written as the caller, so the reminder provider
+ // checks they can edit the secret.
const $syncReminderAlert = async ({
secretId,
secretKey,
projectId,
- recipients,
+ emailRecipients,
channels,
actor
}: {
secretId: string;
secretKey: string;
projectId: string;
- recipients?: string[] | null;
+ emailRecipients: TChannelRecipientInput[];
channels?: TAlertChannelInput[];
actor: TGenericPermission;
}) => {
@@ -130,22 +153,6 @@ export const reminderServiceFactory = ({
return;
}
- // Reminder recipients were never pruned when someone left the project, so drop them rather than fail.
- const requested = [...new Set(recipients ?? [])];
- const inScope = await alertService.filterRecipientsInScope(
- { orgId: actor.actorOrgId, projectId },
- requested.map((principalId) => ({ principalType: AlertPrincipalType.USER, principalId }))
- );
- // Falling back to the whole project here would send the secret's key, path and note to people nobody
- // chose, so a list with nobody left in it is refused instead.
- if (requested.length && !inScope.length) {
- throw new BadRequestError({
- message: "None of the selected reminder recipients are members of this project. Choose recipients again."
- });
- }
- const emailRecipients = inScope.length
- ? inScope
- : [{ principalType: AlertPrincipalType.PROJECT_MEMBERS, principalId: projectId }];
const existingEmailChannels =
existing?.channels.filter((channel) => channel.channelType === AlertChannelType.EMAIL) ?? [];
@@ -203,6 +210,7 @@ export const reminderServiceFactory = ({
repeatDays,
nextReminderDate: nextReminderDateInput,
recipients,
+ resolvedRecipients,
channels,
projectId,
fromDate: fromDateInput,
@@ -214,6 +222,7 @@ export const reminderServiceFactory = ({
repeatDays?: number | null;
nextReminderDate?: string | null;
recipients?: string[] | null;
+ resolvedRecipients?: TChannelRecipientInput[];
channels?: TAlertChannelInput[];
fromDate?: string | null;
projectId: string;
@@ -238,9 +247,12 @@ export const reminderServiceFactory = ({
throw new BadRequestError({ message: "repeatDays must be a positive number" });
}
+ const emailRecipients =
+ resolvedRecipients ?? (await resolveReminderRecipients({ actorOrgId: actor.actorOrgId, projectId, recipients }));
+
// The alert goes first, outside any transaction because encrypting channel config can call out to
// KMS. If the reminder write then fails, an alert with no reminder never fires and is reused next time.
- await $syncReminderAlert({ secretId, secretKey, projectId, recipients, channels, actor });
+ await $syncReminderAlert({ secretId, secretKey, projectId, emailRecipients, channels, actor });
const existingReminder = await reminderDAL.findOne({ secretId });
let reminderId: string;
@@ -556,6 +568,7 @@ export const reminderServiceFactory = ({
};
return {
+ resolveReminderRecipients,
createReminder,
getReminder,
dispatchDueReminders,
diff --git a/backend/src/services/reminder/reminder-types.ts b/backend/src/services/reminder/reminder-types.ts
index 2a2459b90b..1cbb3a2588 100644
--- a/backend/src/services/reminder/reminder-types.ts
+++ b/backend/src/services/reminder/reminder-types.ts
@@ -1,6 +1,6 @@
import { Knex } from "knex";
-import { TAlertChannelInput } from "../alert/alert-channel-service-types";
+import { TAlertChannelInput, TChannelRecipientInput } from "../alert/alert-channel-service-types";
import { ActorAuthMethod, ActorType } from "../auth/auth-type";
export type TReminder = {
@@ -26,6 +26,8 @@ export type TCreateReminderDTO = {
fromDate?: string | null;
nextReminderDate?: string | null;
recipients?: string[] | null;
+ // From resolveReminderRecipients, for a caller that checked them before its own write.
+ resolvedRecipients?: TChannelRecipientInput[];
channels?: TAlertChannelInput[];
};
};
@@ -40,6 +42,12 @@ export type TBatchCreateReminderDTO = {
}[];
export interface TReminderServiceFactory {
+ resolveReminderRecipients: (input: {
+ actorOrgId: string;
+ projectId: string;
+ recipients?: string[] | null;
+ }) => Promise<TChannelRecipientInput[]>;
+
createReminder: ({ actor, actorId, actorOrgId, actorAuthMethod, reminder }: TCreateReminderDTO) => Promise<{
id: string;
created: boolean;
diff --git a/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts b/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts
index 19f856f6a4..3537c39f4f 100644
--- a/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts
+++ b/backend/src/services/secret-v2-bridge/secret-v2-bridge-service.ts
@@ -186,7 +186,7 @@ type TSecretV2BridgeServiceFactoryDep = {
>;
reminderService: Pick<
TReminderServiceFactory,
- "createReminder" | "getReminder" | "batchCreateReminders" | "moveReminderAlerts"
+ "resolveReminderRecipients" | "createReminder" | "getReminder" | "batchCreateReminders" | "moveReminderAlerts"
>;
reminderDAL: Pick<TReminderDALFactory, "findSecretReminders" | "delete">;
secretValidationRuleService: Pick<TSecretValidationRuleServiceFactory, "validateSecrets">;
@@ -792,6 +792,15 @@ export const secretV2BridgeServiceFactory = ({
await $validateSecretReferences(projectId, permission, allSecretReferences);
}
+ // A recipient nobody can notify has to fail here, before the secret changes, not after it has committed.
+ const reminderRecipients = inputSecret.secretReminderRepeatDays
+ ? await reminderService.resolveReminderRecipients({
+ actorOrgId,
+ projectId,
+ recipients: inputSecret.secretReminderRecipients
+ })
+ : undefined;
+
const updatedSecret = await secretDAL.transaction(async (tx) => {
const modifiedSecretsInDB = await fnSecretBulkUpdate({
folderId,
@@ -866,7 +875,7 @@ export const secretV2BridgeServiceFactory = ({
secretId: secret.id,
message: inputSecret.secretReminderNote,
repeatDays: inputSecret.secretReminderRepeatDays,
- recipients: inputSecret.secretReminderRecipients
+ resolvedRecipients: reminderRecipients
}
});
}
diff --git a/backend/src/services/secret/secret-service.ts b/backend/src/services/secret/secret-service.ts
index c4a0e96769..690cec8481 100644
--- a/backend/src/services/secret/secret-service.ts
+++ b/backend/src/services/secret/secret-service.ts
@@ -1985,23 +1985,11 @@ export const secretServiceFactory = ({
newSecretName,
metadata,
secretValue,
- secretMetadata
+ secretMetadata,
+ secretReminderNote,
+ secretReminderRepeatDays,
+ secretReminderRecipients
});
-
- if (secretReminderRepeatDays) {
- await reminderService.createReminder({
- actor,
- actorId,
- actorOrgId,
- actorAuthMethod,
- reminder: {
- secretId: secret.id,
- message: secretReminderNote,
- repeatDays: secretReminderRepeatDays,
- recipients: secretReminderRecipients
- }
- });
- }
return { type: SecretProtectionType.Direct as const, secret };
}