Skip to content

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

fix assert on test

Thiago Araujo da Silva8bad60f6f1a8
diff --git a/backend/src/server/routes/index.ts b/backend/src/server/routes/index.ts
index e2030f6a74..467ed736f5 100644
--- a/backend/src/server/routes/index.ts
+++ b/backend/src/server/routes/index.ts
@@ -1071,6 +1071,7 @@ export const registerRoutes = async (
});
const alertChannelTestService = alertChannelTestServiceFactory({
alertChannelDAL,
+ alertDAL,
alertRecipientResolver,
alertProviderRegistry,
kmsService,
diff --git a/backend/src/services/alert/alert-channel-test-service.test.ts b/backend/src/services/alert/alert-channel-test-service.test.ts
index bc2dc1cdd7..53593e5151 100644
--- a/backend/src/services/alert/alert-channel-test-service.test.ts
+++ b/backend/src/services/alert/alert-channel-test-service.test.ts
@@ -70,9 +70,18 @@ const buildKeyStore = () => {
};
};
+const OWNING_ALERT = {
+ id: "alert-1",
+ orgId: ORG_ID,
+ projectId: null,
+ resourceType: RESOURCE_TYPE,
+ resourceId: "resource-1"
+};
+
const buildDeps = (overrides?: {
registry?: ReturnType<typeof alertProviderRegistryFactory>;
channel?: Record<string, unknown> | null;
+ channelOwners?: Record<string, unknown>[];
recipients?: { email: string }[];
keyStore?: ReturnType<typeof buildKeyStore>;
}) => {
@@ -83,6 +92,9 @@ const buildDeps = (overrides?: {
alertChannelDAL: {
findById: async () => overrides?.channel ?? null
},
+ alertDAL: {
+ findByChannelId: async () => overrides?.channelOwners ?? [OWNING_ALERT]
+ },
alertRecipientResolver: {
resolveMany: async (rowsByChannel: Map<string, unknown[]>) =>
new Map([...rowsByChannel.keys()].map((channelId) => [channelId, overrides?.recipients ?? []]))
@@ -133,7 +145,7 @@ describe("alertChannelTestService", () => {
expect(sent[0].config).toEqual({ webhookUrl: "https://hooks.slack.com/services/T/B/x" });
// A test must never page an on-call rotation at the severity a real firing would carry.
expect(sent[0].payload.severity).toBe("info");
- expect(sent[0].payload.summary).toContain("Test notification");
+ expect(sent[0].payload.summary).toContain("Test alert from Infisical");
} finally {
restore();
}
@@ -196,6 +208,65 @@ describe("alertChannelTestService", () => {
).rejects.toThrow(/not found in this scope/);
});
+ test("authorizes a saved channel against the alert that owns it, not the requested resource", async () => {
+ const checked: { action: string; resourceId?: string | null }[] = [];
+ const registry = buildProvider({
+ assertPermission: async (input) => {
+ checked.push({ action: input.action, resourceId: input.resourceId });
+ // The caller can edit their own resource but not the one the channel's alert watches.
+ if (input.resourceId === OWNING_ALERT.resourceId) throw new Error("forbidden");
+ }
+ });
+ const { deps } = buildDeps({
+ registry,
+ channel: {
+ id: "channel-1",
+ orgId: ORG_ID,
+ projectId: null,
+ channelType: "slack",
+ encryptedConfig: Buffer.from(JSON.stringify({ webhookUrl: "https://hooks.slack.com/services/T/B/stored" }))
+ }
+ });
+ const service = alertChannelTestServiceFactory(deps);
+
+ await expect(
+ service.testChannel({
+ ...actor,
+ resourceType: RESOURCE_TYPE,
+ resourceId: "resource-the-caller-owns",
+ channelId: "channel-1",
+ channelType: "slack" as never,
+ config: {}
+ })
+ ).rejects.toThrow("forbidden");
+
+ expect(checked).toContainEqual({ action: "edit", resourceId: OWNING_ALERT.resourceId });
+ });
+
+ test("rejects a saved channel that no alert owns", async () => {
+ const { deps } = buildDeps({
+ channelOwners: [],
+ channel: {
+ id: "channel-1",
+ orgId: ORG_ID,
+ projectId: null,
+ channelType: "slack",
+ encryptedConfig: Buffer.from(JSON.stringify({ webhookUrl: "https://hooks.slack.com/services/T/B/stored" }))
+ }
+ });
+ const service = alertChannelTestServiceFactory(deps);
+
+ await expect(
+ service.testChannel({
+ ...actor,
+ resourceType: RESOURCE_TYPE,
+ channelId: "channel-1",
+ channelType: "slack" as never,
+ config: {}
+ })
+ ).rejects.toThrow(/not found in this scope/);
+ });
+
test("rejects an invalid config before any cooldown is spent", async () => {
const { deps, keyStore } = buildDeps();
const service = alertChannelTestServiceFactory(deps);
diff --git a/backend/src/services/alert/alert-channel-test-service.ts b/backend/src/services/alert/alert-channel-test-service.ts
index 2b53dd2283..b9b3ae4c95 100644
--- a/backend/src/services/alert/alert-channel-test-service.ts
+++ b/backend/src/services/alert/alert-channel-test-service.ts
@@ -14,6 +14,7 @@ import {
import { decryptChannelConfig, getAlertChannelCipher } from "./alert-channel-crypto-fns";
import { TAlertChannelDALFactory } from "./alert-channel-dal";
import { TAlertChannelDeps, TAlertPayload, TAlertRecipient } from "./alert-channel-types";
+import { TAlertDALFactory } from "./alert-dal";
import { TAlertProviderRegistry } from "./alert-provider-registry";
import { TAlertRecipientResolver } from "./alert-recipient-resolver";
import { TTestAlertChannelDTO, TTestAlertChannelResponse } from "./alert-service-types";
@@ -21,6 +22,7 @@ import { AlertPermissionAction, IResourceAlertProvider, TAlertContext } from "./
export type TAlertChannelTestServiceFactoryDep = {
alertChannelDAL: Pick<TAlertChannelDALFactory, "findById">;
+ alertDAL: Pick<TAlertDALFactory, "findByChannelId">;
alertRecipientResolver: Pick<TAlertRecipientResolver, "resolveMany">;
alertProviderRegistry: TAlertProviderRegistry;
kmsService: Pick<TKmsServiceFactory, "createCipherPairWithDataKey">;
@@ -36,6 +38,7 @@ const MAX_ERROR_LENGTH = 300;
export const alertChannelTestServiceFactory = ({
alertChannelDAL,
+ alertDAL,
alertRecipientResolver,
alertProviderRegistry,
kmsService,
@@ -54,6 +57,40 @@ export const alertChannelTestServiceFactory = ({
});
};
+ const $assertChannelOwnersPermit = async (channelId: string, dto: TTestAlertChannelDTO) => {
+ const owners = await alertDAL.findByChannelId(channelId);
+ if (owners.length === 0) {
+ throw new NotFoundError({ message: `Channel with ID '${channelId}' was not found in this scope` });
+ }
+
+ for (const owner of owners) {
+ if (owner.orgId !== dto.actorOrgId) {
+ throw new NotFoundError({ message: `Channel with ID '${channelId}' was not found in this scope` });
+ }
+
+ const ownerProvider = alertProviderRegistry.get(owner.resourceType);
+ if (!ownerProvider) {
+ throw new BadRequestError({
+ message: `No alert provider is registered for resource type '${owner.resourceType}'`
+ });
+ }
+
+ // eslint-disable-next-line no-await-in-loop
+ await ownerProvider.assertPermission({
+ action: AlertPermissionAction.Edit,
+ orgId: owner.orgId,
+ projectId: owner.projectId,
+ resourceId: owner.resourceId,
+ actor: {
+ actor: dto.actor,
+ actorId: dto.actorId,
+ actorAuthMethod: dto.actorAuthMethod,
+ actorOrgId: dto.actorOrgId
+ }
+ });
+ }
+ };
+
const $resolveConfig = async (dto: TTestAlertChannelDTO, projectId: string | null) => {
const definition = getChannelDefinition(dto.channelType);
const incoming = dto.config ?? {};
@@ -73,6 +110,8 @@ export const alertChannelTestServiceFactory = ({
});
}
+ await $assertChannelOwnersPermit(channel.id, dto);
+
const { decryptor } = await getAlertChannelCipher(kmsService, {
orgId: channel.orgId,
projectId: channel.projectId
diff --git a/backend/src/services/alert/alert-dal.ts b/backend/src/services/alert/alert-dal.ts
index 64fc688cc6..3e5e52a650 100644
--- a/backend/src/services/alert/alert-dal.ts
+++ b/backend/src/services/alert/alert-dal.ts
@@ -90,6 +90,20 @@ export const alertDALFactory = (db: TDbClient) => {
}
};
+ const findByChannelId = async (channelId: string, tx?: Knex): Promise<TAlerts[]> => {
+ try {
+ const alerts = await (tx || db.replicaNode())(TableName.Alert)
+ .join(TableName.AlertChannelMembership, `${TableName.Alert}.id`, `${TableName.AlertChannelMembership}.alertId`)
+ .where(`${TableName.AlertChannelMembership}.channelId`, channelId)
+ .select(selectAllTableCols(TableName.Alert))
+ .orderBy(`${TableName.Alert}.createdAt`, "asc");
+
+ return alerts as TAlerts[];
+ } catch (error) {
+ throw new DatabaseError({ error, name: "FindByChannelId" });
+ }
+ };
+
const findScopedDuplicate = async (
filter: {
orgId: string;
@@ -123,6 +137,7 @@ export const alertDALFactory = (db: TDbClient) => {
findEnabledByResourceType,
findActiveById,
findActiveByScope,
+ findByChannelId,
findScopedDuplicate
};
};