Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

feat(event-streaming): encrypted custom headers and a test endpoint that reports the result (#15873)

Othman Abu Ajamiehdf455323569c
diff --git a/brain/knowledge/eventing-webhooks/event-destinations.md b/brain/knowledge/eventing-webhooks/event-destinations.md
index 215e9b7ace5..5a11044945c 100644
--- a/brain/knowledge/eventing-webhooks/event-destinations.md
+++ b/brain/knowledge/eventing-webhooks/event-destinations.md
@@ -15,13 +15,20 @@ Streams platform/project activity events to webhook URLs in real time. Internal
### How it works
- `eventDestinationService.trigger()` finds matching destinations and classifies each URL as internal vs external.
- **Internal** (URL origin == instance's public API origin, path under `/v1/webhooks/<flowId>`, any route suffix): dispatched directly via `webhookService.handleWebhook` — no outbound HTTP, so the SSRF filter never sees a self-referential private-IP call (GIT-1539). A `/draft` URL runs the latest version; a `/test` URL only saves sample data and does not run the flow.
-- **External**: queued as ONE_TIME jobs, POSTed via `safeHttp` (SSRF-protected); delivery failures logged at error level.
+- **External**: queued as ONE_TIME jobs, POSTed via `safeHttp.postForStatus` (SSRF-protected, reads only the status); delivery failures logged at error level.
- Endpoints: `POST/GET/DELETE /v1/event-destinations` (+ `POST /:id` and `PATCH /:id`), and `POST /test` sends a mock event (defaults to `FLOW_CREATED`, format `RAW`) built by `buildMockEvent()`.
### Gotchas
+- **A header value is decrypted in exactly one function, `decryptBoundHeaders`, and only on the way out.** Two callers: `resolveDeliveryHeaders`, the method the worker calls over RPC just before an external POST, and the internal dispatch to a handler flow. The `headers` column stores the URL the values were saved for and a *per-key* map of ciphertexts (`{ url, values: { [name]: {iv,data} } }`), not one blob over the whole map, so the key names are readable without the key. That is what lets `maskHeaders` be pure and synchronous, lets `update()` keep a value by copying its ciphertext, and stops one unreadable row from 500-ing the whole list endpoint. `grep decryptString packages/server/api/src/app/event-destinations` should return one line. An `EVENT_DESTINATION` job carries the destination id, never the headers — copying `resolveAiProvider`, because the worker has no database and no `AP_ENCRYPTION_KEY`.
+- **A stored header value is bound to the URL it was saved against, on write and on send.** `update()` answers 400 if the request changes `url` while carrying any stored value over (a `null` for a stored key, or an omitted `headers` field entirely — both branches, or it is bypassed by omission). Without that rule "write-only" is decorative: anyone who can edit a destination points it at their own endpoint and the next audit event delivers the secret for them. `update()` reads the row `FOR UPDATE` inside a transaction, so two overlapping edits cannot combine into a new URL with headers typed for the old one (and an omitted `format` is read fresh, so an events-only edit cannot undo a format switch — do not "fix" that by dropping `format` from the UPDATE, that reopens the webhook URL + `OTLP_PROTOBUF` pair). On send, the worker passes the job's `webhookUrl` to `resolveEventDestinationHeaders`, which matches on `url` too: a job queued before the URL changed (a retry, say) gets `null` and is dropped, instead of carrying the new secret to the old URL. The stored `url` makes the binding hold outside `update()` too: `decryptBoundHeaders` sends nothing when it differs from the row's URL, so a URL that changed without the values (an older replica during a rolling deploy, a rollback, a manual SQL edit) drops the event instead of sending the secret to the new URL, and keeping a value needs the stored `url`, not only the row's. A `null` key matches a stored name in any letter case, in both the keep lookup (`toStoredHeaders`) and the binding check (`assertUrlChangeRebindsStoredHeaders`): make one case-sensitive without the other and a case change either loses the value or carries it to the new URL. For the same reason `POST /test` takes no `destinationId` (an unknown key is stripped, as it always was, so old callers keep working) and rejects a `null` header — it can only send what the caller typed.
+- **Delivery and `POST /test` read only the status of the response.** `safeHttp.postForStatus` takes the body as a stream without decompression, reads at most 16 KB of it to the end so the keep-alive socket goes back to the pool, and destroys it past that or at an `AbortSignal` deadline. The axios defaults buffered and inflated the whole body (a 200 MB answer added ~690 MB to the API), and axios `timeout` only fires on an idle socket, so a byte-a-time drip held the request open. Do not go back to `safeHttp.axios.request` here. `POST /test` answers an `errorCode` (`EventDestinationTestError`), never the raw error text, which held the resolved private IP and the `AP_SSRF_ALLOW_LIST` hint; it allows 20 calls a minute per user.
+- **An update is a partial patch: an omitted field keeps its stored value.** `url` and `events` are optional on `POST /:id` and `PATCH /:id`, like `enabled`, `format` and `headers`, so the listing's Enabled switch sends only `{ enabled }`. It used to send the whole cached row, and a stale row wrote back an old URL and events plus `headers: null`, which deleted a header added in another tab without an error. Both checks run on the resolved URL (`url ?? stored.url`, read under the row lock), never on `rest.url`: compare `rest.url` and every toggle on a destination with headers answers the rebinding 400 (`undefined !== stored.url`), while a `format`-only switch to `OTLP_PROTOBUF` on a webhook URL slips through (`new URL(undefined)` reads as "not a webhook"). `headers: null` still means "clear all", so a client that echoes a cached row's headers deletes values it never saw. An unreadable stored map counts as no headers: a toggle leaves it alone, and a save that sends `headers` replaces it.
+- **The delivery owns the framing headers.** `Content-Type`, `Content-Length`, `Content-Encoding`, `Transfer-Encoding`, `Host` and `Connection` are refused as custom header names in any letter case (`formErrors.reservedHeaderName`), and so are two names that differ only in case (`formErrors.duplicateHeaderName`). The format decides the body encoding, so a custom `Content-Type` could only mislabel it. Every send path also spreads the custom headers first and sets its own content type last.
+- **An internal handler flow gets the custom headers, with lowercase names, exactly as an HTTP delivery would.** A handler flow whose Catch Webhook trigger uses HEADER or BASIC auth reads `payload.headers`, and the same flow reached through the embed-domain URL already gets them over HTTP. The consequence: the values enter the webhook job and the flow run's trigger output, which project members can read, the same as any HTTP request to that webhook. `POST /test` forwards the typed headers the same way.
- **An OTLP destination URL is used exactly as pasted.** The OTLP spec says a signal-specific endpoint "MUST be used as-is", and vendor paths differ (`/v1/logs`, Loki `/otlp/v1/logs`, PostHog `/i/v1/logs`), so nothing appends `/v1/logs`.
- **The worker encodes protobuf, not the API**, because BullMQ stores job data as JSON and raw bytes do not survive the queue. The queued payload is always the OTLP/JSON object; `contentType: 'application/x-protobuf'` on the job tells the worker to encode it. A job without `contentType` posts JSON, which is exactly the pre-format job shape.
-- **The API refuses `OTLP_PROTOBUF` for any webhook URL** (400, `EVENT_DESTINATION_FORMAT_NOT_SUPPORTED_BY_WEBHOOK`), on create and on update. The webhook routes have no protobuf parser, so an HTTP POST of protobuf gets a 415, and the internal dispatch would hand the flow JSON anyway. The check uses `extractWebhookFlowIdCandidate`, host-agnostic like the cycle guard, so an embed-host URL is refused too. `OTLP_JSON` stays allowed: both paths deliver it as JSON.
+- **The API refuses `OTLP_PROTOBUF` for any webhook URL** (400, `EVENT_DESTINATION_FORMAT_NOT_SUPPORTED_BY_WEBHOOK`), on create, on update and on `POST /test`, so a test cannot pass where delivery would fail. The webhook routes have no protobuf parser, so an HTTP POST of protobuf gets a 415, and the internal dispatch would hand the flow JSON anyway. The check uses `extractWebhookFlowIdCandidate`, host-agnostic like the cycle guard, so an embed-host URL is refused too. `OTLP_JSON` stays allowed: both paths deliver it as JSON.
+- **The worker calls `resolveEventDestinationHeaders` only for a job with `hasHeaders: true`.** A destination with no stored headers (every pre-format destination) posts exactly as before, with no RPC to the API and no new way to fail.
- **The OTLP attribute keys are a customer contract.** Envelope fields keep their RAW names, and `data` is flattened to dotted keys (`data.flowRun.status`); the body is the raw event JSON, not evlog's wide event. `packages/server/utils/test/otlp-logs.test.ts` pins the whole request, so an evlog upgrade that changes `toOTLPLogRecord` fails a test instead of silently changing every customer's dashboards.
- The old `assertUrlIsExternal` check was **removed** to support internal handler flows; recursion is instead prevented by a server-side **cycle guard** keyed on the target flow of any webhook URL, regardless of route suffix (so a self-targeting `/draft` destination is still dropped on its own run events).
- **The cycle guard is host-agnostic on purpose — do not "tighten" it back to an origin check.** It matches any destination whose path contains `/v1/webhooks/<flowId>`, percent-decoding the id segment. It used to require `destination.origin === getPublicApiUrl().origin`, which is `AP_FRONTEND_URL`: platforms with an embed subdomain get `WEBHOOK_URL_PREFIX` rewritten to `https://<embed-hostname>/api/v1/webhooks`, so the origin never matched, the guard failed open, and a handler flow re-triggered on its own runs — one webhook POST produced 838 runs in ~3 min (GIT-1641, Pylon 5378). `domainHelper.getPublicApiUrl()` is called with no `platformId` here, so it can never resolve to the embed host. The path is the stable part (`/v1/webhooks` is fixed by the route registration and every URL builder); the host is not.
diff --git a/bun.lock b/bun.lock
index 5d687e483ef..ee381839394 100644
--- a/bun.lock
+++ b/bun.lock
@@ -120,7 +120,7 @@
},
"packages/core/execution": {
"name": "@activepieces/core-execution",
- "version": "0.25.0",
+ "version": "0.26.0",
"dependencies": {
"@activepieces/core-piece-types": "workspace:*",
"@activepieces/core-utils": "workspace:*",
@@ -165,7 +165,7 @@
},
"packages/core/shared": {
"name": "@activepieces/shared",
- "version": "0.193.0",
+ "version": "0.194.0",
"dependencies": {
"@activepieces/core-execution": "workspace:*",
"@activepieces/core-formula": "workspace:*",
diff --git a/packages/core/execution/package.json b/packages/core/execution/package.json
index d14093bb61d..bb1a0317885 100644
--- a/packages/core/execution/package.json
+++ b/packages/core/execution/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/core-execution",
- "version": "0.25.0",
+ "version": "0.26.0",
"type": "commonjs",
"main": "./dist/src/index.js",
"scripts": {
diff --git a/packages/core/execution/src/lib/workers/job-data.ts b/packages/core/execution/src/lib/workers/job-data.ts
index 05ef831f1ed..d2950f86a32 100644
--- a/packages/core/execution/src/lib/workers/job-data.ts
+++ b/packages/core/execution/src/lib/workers/job-data.ts
@@ -366,6 +366,7 @@ export const EventDestinationJobData = z.object({
webhookUrl: z.string(),
payload: z.unknown(), // EE audit-event schema stays in @activepieces/shared; engine does not need it
contentType: z.enum(['application/json', 'application/x-protobuf']).optional(),
+ hasHeaders: z.boolean().optional(),
jobType: z.literal(WorkerJobType.EVENT_DESTINATION),
})
diff --git a/packages/core/execution/src/lib/workers/worker-contract.ts b/packages/core/execution/src/lib/workers/worker-contract.ts
index a2f4beac78f..4efc1920953 100644
--- a/packages/core/execution/src/lib/workers/worker-contract.ts
+++ b/packages/core/execution/src/lib/workers/worker-contract.ts
@@ -100,6 +100,7 @@ export type WorkerToApiContract = {
resolveAiProvider(input: ResolveAiProviderRequest): Promise<ResolveAiProviderResponse>
resolveAiModelCandidates(input: ResolveAiModelCandidatesRequest): Promise<ResolveAiModelCandidatesResponse>
reportAiKeyOutcome(input: ReportAiKeyOutcomeRequest): Promise<void>
+ resolveEventDestinationHeaders(input: ResolveEventDestinationHeadersRequest): Promise<ResolveEventDestinationHeadersResponse>
saveFlowStepFile(input: SaveFlowStepFileRequest): Promise<SaveFlowStepFileResponse>
readFlowStepFile(input: ReadFlowStepFileRequest): Promise<ReadFlowStepFileResponse>
reportAiUsage(input: ReportAiUsageRequest): Promise<void>
@@ -467,6 +468,16 @@ export type ReportAiKeyOutcomeRequest = {
signal: ProviderOutcomeSignal
}
+export type ResolveEventDestinationHeadersRequest = {
+ platformId: string
+ destinationId: string
+ destinationUrl: string
+}
+
+export type ResolveEventDestinationHeadersResponse = {
+ headers: Record<string, string>
+} | null
+
export type SaveFlowStepFileRequest = {
projectId: string
platformId: string
diff --git a/packages/core/shared/package.json b/packages/core/shared/package.json
index 173477c5ab2..21514f7212e 100644
--- a/packages/core/shared/package.json
+++ b/packages/core/shared/package.json
@@ -1,6 +1,6 @@
{
"name": "@activepieces/shared",
- "version": "0.193.0",
+ "version": "0.194.0",
"type": "commonjs",
"sideEffects": false,
"main": "./dist/src/index.js",
diff --git a/packages/core/shared/src/lib/ee/event-destinations/dto.ts b/packages/core/shared/src/lib/ee/event-destinations/dto.ts
index 06caea233fa..5426e4baed4 100644
--- a/packages/core/shared/src/lib/ee/event-destinations/dto.ts
+++ b/packages/core/shared/src/lib/ee/event-destinations/dto.ts
@@ -1,6 +1,31 @@
import { z } from 'zod'
+import { formErrors } from '../../form-errors'
import { ApplicationEventName } from '../audit-events'
+const HEADER_NAME_PATTERN = /^[A-Za-z0-9!#$%&'*+\-.^_`|~]+$/
+
+const HEADER_VALUE_PATTERN = /^[\t\x20-\x7e\x80-\xff]*$/
+
+const DELIVERY_OWNED_HEADER_NAMES: ReadonlySet<string> = new Set([
+ 'content-type',
+ 'content-length',
+ 'content-encoding',
+ 'transfer-encoding',
+ 'host',
+ 'connection',
+])
+
+const HeaderName = z.string()
+ .regex(HEADER_NAME_PATTERN, formErrors.invalidHeaderName)
+ .refine((name) => !DELIVERY_OWNED_HEADER_NAMES.has(name.toLowerCase()), formErrors.reservedHeaderName)
+
+const HeaderValue = z.string().regex(HEADER_VALUE_PATTERN, formErrors.invalidHeaderValue)
+
+const hasUniqueHeaderNames = (headers: Record<string, unknown>): boolean => {
+ const names = Object.keys(headers).map((name) => name.toLowerCase())
+ return new Set(names).size === names.length
+}
+
export enum EventDestinationScope {
PLATFORM = 'PLATFORM',
PROJECT = 'PROJECT',
@@ -12,6 +37,24 @@ export enum EventDestinationFormat {
OTLP_PROTOBUF = 'OTLP_PROTOBUF',
}
+export enum EventDestinationTestError {
+ BLOCKED = 'BLOCKED',
+ TIMEOUT = 'TIMEOUT',
+ TLS = 'TLS',
+ CONNECTION_FAILED = 'CONNECTION_FAILED',
+ HANDLER_FLOW_FAILED = 'HANDLER_FLOW_FAILED',
+}
+
+export const EventDestinationHeaders = z.record(HeaderName, HeaderValue)
+ .refine(hasUniqueHeaderNames, formErrors.duplicateHeaderName)
+
+export type EventDestinationHeaders = z.infer<typeof EventDestinationHeaders>
+
+export const EventDestinationHeadersRequest = z.record(HeaderName, HeaderValue.nullable())
+ .refine(hasUniqueHeaderNames, formErrors.duplicateHeaderName)
+
+export type EventDestinationHeadersRequest = z.infer<typeof EventDestinationHeadersRequest>
+
export const ListPlatformEventDestinationsRequestBody = z.object({
cursor: z.string().optional(),
limit: z.coerce.number().optional(),
@@ -23,18 +66,30 @@ export const CreatePlatformEventDestinationRequestBody = z.object({
events: z.array(z.enum(ApplicationEventName)),
url: z.url(),
enabled: z.boolean().optional(),
+ headers: EventDestinationHeadersRequest.nullish(),
format: z.enum(EventDestinationFormat).optional(),
})
export type CreatePlatformEventDestinationRequestBody = z.infer<typeof CreatePlatformEventDestinationRequestBody>
-export const UpdatePlatformEventDestinationRequestBody = CreatePlatformEventDestinationRequestBody
+export const UpdatePlatformEventDestinationRequestBody = CreatePlatformEventDestinationRequestBody.partial()
export type UpdatePlatformEventDestinationRequestBody = z.infer<typeof UpdatePlatformEventDestinationRequestBody>
export const TestPlatformEventDestinationRequestBody = z.object({
url: z.url(),
event: z.enum(ApplicationEventName).optional(),
+ headers: EventDestinationHeaders.nullish(),
+ format: z.enum(EventDestinationFormat).optional(),
})
export type TestPlatformEventDestinationRequestBody = z.infer<typeof TestPlatformEventDestinationRequestBody>
+
+export const TestPlatformEventDestinationResponse = z.object({
+ renderedBody: z.unknown(),
+ status: z.number().optional(),
+ durationMs: z.number(),
+ errorCode: z.enum(EventDestinationTestError).optional(),
+})
+
+export type TestPlatformEventDestinationResponse = z.infer<typeof TestPlatformEventDestinationResponse>
diff --git a/packages/core/shared/src/lib/ee/event-destinations/index.ts b/packages/core/shared/src/lib/ee/event-destinations/index.ts
index 3a042a6d583..e91dd68978b 100644
--- a/packages/core/shared/src/lib/ee/event-destinations/index.ts
+++ b/packages/core/shared/src/lib/ee/event-destinations/index.ts
@@ -1,7 +1,7 @@
import { BaseModelSchema } from '@activepieces/core-utils'
import { z } from 'zod'
import { ApplicationEventName } from '../audit-events/index'
-import { EventDestinationFormat, EventDestinationScope } from './dto'
+import { EventDestinationFormat, EventDestinationHeadersRequest, EventDestinationScope } from './dto'
const EventDestinationBase = {
...BaseModelSchema,
@@ -9,6 +9,7 @@ const EventDestinationBase = {
events: z.array(z.enum(ApplicationEventName)),
url: z.url(),
enabled: z.boolean(),
+ headers: EventDestinationHeadersRequest.nullable(),
format: z.enum(EventDestinationFormat),
}
diff --git a/packages/core/utils/src/lib/activepieces-error.ts b/packages/core/utils/src/lib/activepieces-error.ts
index c6e05ae2224..fff7791bb7d 100644
--- a/packages/core/utils/src/lib/activepieces-error.ts
+++ b/packages/core/utils/src/lib/activepieces-error.ts
@@ -78,6 +78,7 @@ export type ApErrorParams =
| SandboxExecutionTimeoutParams
| SandboxInternalErrorParams
| InvalidCustomDomainErrorParams
+ | EventDestinationUrlChangeRequiresHeadersParams
| McpPieceRequiresConnectionParams
| McpPieceConnectionMismatchParams
| ErrorUpdatingSubscriptionParams
@@ -483,6 +484,10 @@ export type FlowExternalIdAlreadyExistsParams = BaseErrorParams<ErrorCode.FLOW_E
externalId: string
}>
+export type EventDestinationUrlChangeRequiresHeadersParams = BaseErrorParams<ErrorCode.EVENT_DESTINATION_URL_CHANGE_REQUIRES_HEADERS, {
+ headerNames: string[]
+}>
+
export type McpPieceRequiresConnectionParams = BaseErrorParams<ErrorCode.MCP_PIECE_REQUIRES_CONNECTION, {
pieceName: string
}>
@@ -625,6 +630,7 @@ export enum ErrorCode {
INVALID_SMTP_CREDENTIALS = 'INVALID_SMTP_CREDENTIALS',
INVALID_GIT_CREDENTIALS = 'INVALID_GIT_CREDENTIALS',
INVALID_RELEASE_TYPE = 'INVALID_RELEASE_TYPE',
+ EVENT_DESTINATION_URL_CHANGE_REQUIRES_HEADERS = 'EVENT_DESTINATION_URL_CHANGE_REQUIRES_HEADERS',
MCP_PIECE_REQUIRES_CONNECTION = 'MCP_PIECE_REQUIRES_CONNECTION',
MCP_PIECE_CONNECTION_MISMATCH = 'MCP_PIECE_CONNECTION_MISMATCH',
SUBFLOW_FAILED = 'SUBFLOW_FAILED',
diff --git a/packages/core/utils/src/lib/form-errors.ts b/packages/core/utils/src/lib/form-errors.ts
index 0494e2d4d14..4c2ed31e164 100644
--- a/packages/core/utils/src/lib/form-errors.ts
+++ b/packages/core/utils/src/lib/form-errors.ts
@@ -18,6 +18,10 @@ export const formErrors = {
tierTooManyModels: 'tierTooManyModels',
tierDuplicateModel: 'tierDuplicateModel',
tierThinkingBudgetInvalid: 'tierThinkingBudgetInvalid',
+ invalidHeaderName: 'invalidHeaderName',
+ reservedHeaderName: 'reservedHeaderName',
+ duplicateHeaderName: 'duplicateHeaderName',
+ invalidHeaderValue: 'invalidHeaderValue',
} as const
export const SAFE_EXTERNAL_ID_PATTERN = /^(?!\.{1,2}$)[A-Za-z0-9._-]{1,128}$/
diff --git a/packages/server/api/src/app/core/security/rate-limit.ts b/packages/server/api/src/app/core/security/rate-limit.ts
index af6cf1ac616..3edb39ff145 100644
--- a/packages/server/api/src/app/core/security/rate-limit.ts
+++ b/packages/server/api/src/app/core/security/rate-limit.ts
@@ -37,3 +37,10 @@ export const emailCodeRateLimit: RateLimitOptions = {
),
timeWindow: system.getOrThrow(AppSystemProp.API_RATE_LIMIT_AUTHN_WINDOW),
}
+
+export const eventDestinationTestRateLimit: RateLimitOptions = {
+ max: 20,
+ timeWindow: '1 minute',
+ hook: 'preHandler',
+ keyGenerator: (req) => `event-destination-test:${req.principal.id}`,
+}
diff --git a/packages/server/api/src/app/database/migration/postgres/1863000000000-AddEventDestinationHeaders.ts b/packages/server/api/src/app/database/migration/postgres/1863000000000-AddEventDestinationHeaders.ts
new file mode 100644
index 00000000000..3826f6a9490
--- /dev/null
+++ b/packages/server/api/src/app/database/migration/postgres/1863000000000-AddEventDestinationHeaders.ts
@@ -0,0 +1,22 @@
+import { QueryRunner } from 'typeorm'
+import { Migration } from '../../migration'
+
+export class AddEventDestinationHeaders1863000000000 implements Migration {
+ name = 'AddEventDestinationHeaders1863000000000'
+ breaking = false
+ release = '0.93.0'
+
+ public async up(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(`
+ ALTER TABLE "event_destination"
+ ADD COLUMN IF NOT EXISTS "headers" jsonb
+ `)
+ }
+
+ public async down(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(`
+ ALTER TABLE "event_destination"
+ DROP COLUMN IF EXISTS "headers"
+ `)
+ }
+}
diff --git a/packages/server/api/src/app/database/postgres-connection.ts b/packages/server/api/src/app/database/postgres-connection.ts
index b96020601d1..65bce6bb27d 100644
--- a/packages/server/api/src/app/database/postgres-connection.ts
+++ b/packages/server/api/src/app/database/postgres-connection.ts
@@ -450,6 +450,7 @@ import { AddAgentFolderId1858000000000 } from './migration/postgres/185800000000
import { AddPlatformModelTier1859000000000 } from './migration/postgres/1859000000000-AddPlatformModelTier'
import { AddPieceSetRequiredActions1860000000000 } from './migration/postgres/1860000000000-AddPieceSetRequiredActions'
import { AddEventDestinationEnabledAndFormat1862000000000 } from './migration/postgres/1862000000000-AddEventDestinationEnabledAndFormat'
+import { AddEventDestinationHeaders1863000000000 } from './migration/postgres/1863000000000-AddEventDestinationHeaders'
const getSslConfig = (): boolean | TlsOptions => {
const useSsl = system.get(AppSystemProp.POSTGRES_USE_SSL)
@@ -915,6 +916,7 @@ export const getMigrations = (): (new () => Migration)[] => {
AddPlatformModelTier1859000000000,
AddPieceSetRequiredActions1860000000000,
AddEventDestinationEnabledAndFormat1862000000000,
+ AddEventDestinationHeaders1863000000000,
]
return migrations
}
diff --git a/packages/server/api/src/app/ee/platform-webhooks/platform-webhooks.controller.ts b/packages/server/api/src/app/ee/platform-webhooks/platform-webhooks.controller.ts
index 0becb493936..37551b9b7b7 100644
--- a/packages/server/api/src/app/ee/platform-webhooks/platform-webhooks.controller.ts
+++ b/packages/server/api/src/app/ee/platform-webhooks/platform-webhooks.controller.ts
@@ -1,9 +1,10 @@
import { SeekPage } from '@activepieces/core-utils'
-import { CreatePlatformEventDestinationRequestBody, EventDestination, ListPlatformEventDestinationsRequestBody, PrincipalType, TestPlatformEventDestinationRequestBody, UpdatePlatformEventDestinationRequestBody } from '@activepieces/shared'
+import { CreatePlatformEventDestinationRequestBody, EventDestination, ListPlatformEventDestinationsRequestBody, PrincipalType, TestPlatformEventDestinationRequestBody, TestPlatformEventDestinationResponse, UpdatePlatformEventDestinationRequestBody } from '@activepieces/shared'
import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod'
import { StatusCodes } from 'http-status-codes'
import { z } from 'zod'
import { securityAccess } from '../../core/security/authorization/fastify-security'
+import { eventDestinationTestRateLimit } from '../../core/security/rate-limit'
import { eventDestinationService } from '../../event-destinations/event-destinations.service'
export const platformWebhooksController: FastifyPluginAsyncZod = async (app) => {
@@ -49,6 +50,8 @@ export const platformWebhooksController: FastifyPluginAsyncZod = async (app) =>
projectId: undefined,
url: req.body.url,
event: req.body.event,
+ format: req.body.format,
+ headers: req.body.headers,
})
})
}
@@ -105,9 +108,13 @@ export const DeleteEventDestinationRequest = {
export const TestPlatformEventDestinationRequest = {
schema: {
body: TestPlatformEventDestinationRequestBody,
+ response: {
+ [StatusCodes.OK]: TestPlatformEventDestinationResponse,
+ },
},
config: {
security: securityAccess.platformAdminOnly([PrincipalType.USER]),
+ rateLimit: eventDestinationTestRateLimit,
},
}
diff --git a/packages/server/api/src/app/event-destinations/event-destinations-rpc-handlers.ts b/packages/server/api/src/app/event-destinations/event-destinations-rpc-handlers.ts
new file mode 100644
index 00000000000..edf363ef095
--- /dev/null
+++ b/packages/server/api/src/app/event-destinations/event-destinations-rpc-handlers.ts
@@ -0,0 +1,14 @@
+import { isNil } from '@activepieces/core-utils'
+import { ResolveEventDestinationHeadersRequest, ResolveEventDestinationHeadersResponse } from '@activepieces/shared'
+import { FastifyBaseLogger } from 'fastify'
+import { eventDestinationService } from './event-destinations.service'
+
+export const eventDestinationRpcHandlers = (log: FastifyBaseLogger) => ({
+ async resolveEventDestinationHeaders({ platformId, destinationId, destinationUrl }: ResolveEventDestinationHeadersRequest): Promise<ResolveEventDestinationHeadersResponse> {
+ const headers = await eventDestinationService(log).resolveDeliveryHeaders({ platformId, destinationId, destinationUrl })
+ if (isNil(headers)) {
+ return null
+ }
+ return { headers }
+ },
+})
diff --git a/packages/server/api/src/app/event-destinations/event-destinations.entity.ts b/packages/server/api/src/app/event-destinations/event-destinations.entity.ts
index 969ccd1888d..d208c9e0e23 100644
--- a/packages/server/api/src/app/event-destinations/event-destinations.entity.ts
+++ b/packages/server/api/src/app/event-destinations/event-destinations.entity.ts
@@ -1,8 +1,23 @@
import { EventDestination, EventDestinationFormat, EventDestinationScope, Platform, Project } from '@activepieces/shared'
import { EntitySchema } from 'typeorm'
+import { z } from 'zod'
import { ApIdSchema, BaseColumnSchemaPart } from '../database/database-common'
+import { EncryptedObject } from '../helper/encryption'
-export type EventDestinationSchema = EventDestination & {
+type WithoutHeaders<T> = T extends unknown ? Omit<T, 'headers'> : never
+
+export const StoredEventDestinationHeaders = z.object({
+ url: z.string(),
+ values: z.record(z.string(), EncryptedObject),
+})
+
+export type StoredEventDestinationHeaders = z.infer<typeof StoredEventDestinationHeaders>
+
+export type EventDestinationRow = WithoutHeaders<EventDestination> & {
+ headers: StoredEventDestinationHeaders | null
+}
+
+export type EventDestinationSchema = EventDestinationRow & {
platform: Platform
project: Project
}
@@ -37,6 +52,10 @@ export const EventDestinationEntity = new EntitySchema<EventDestinationSchema>({
nullable: false,
default: true,
},
+ headers: {
+ type: 'jsonb',
+ nullable: true,
+ },
format: {
type: String,
nullable: false,
diff --git a/packages/server/api/src/app/event-destinations/event-destinations.service.ts b/packages/server/api/src/app/event-destinations/event-destinations.service.ts
index 5d63ca86cf7..eab521846ce 100644
--- a/packages/server/api/src/app/event-destinations/event-destinations.service.ts
+++ b/packages/server/api/src/app/event-destinations/event-destinations.service.ts
@@ -1,13 +1,15 @@
-import { ActivepiecesError, apId, Cursor, ErrorCode, isNil, partition, PlatformId, ProjectId, SeekPage, spreadIfDefined, tryCatch, tryCatchSync } from '@activepieces/core-utils'
-import { OtlpExportLogsRequest, otlpLogs } from '@activepieces/server-utils'
-import { AgentRunSource, ApplicationEvent, ApplicationEventName, buildMockEvent, CreatePlatformEventDestinationRequestBody, EventDestination, EventDestinationFormat, EventDestinationJobData, EventDestinationScope, EventPayload, FlowRunEvent, LATEST_JOB_DATA_SCHEMA_VERSION, UpdatePlatformEventDestinationRequestBody, WorkerJobType } from '@activepieces/shared'
+import { ActivepiecesError, apId, Cursor, ErrorCode, isNil, partition, PlatformId, ProjectId, SeekPage, spreadIfDefined, spreadIfNotUndefined, tryCatch, tryCatchSync } from '@activepieces/core-utils'
+import { OtlpExportLogsRequest, otlpLogs, PostForStatusFailure, safeHttp } from '@activepieces/server-utils'
+import { AgentRunSource, ApplicationEvent, ApplicationEventName, buildMockEvent, CreatePlatformEventDestinationRequestBody, EventDestination, EventDestinationFormat, EventDestinationHeaders, EventDestinationHeadersRequest, EventDestinationJobData, EventDestinationScope, EventDestinationTestError, EventPayload, FlowRunEvent, LATEST_JOB_DATA_SCHEMA_VERSION, TestPlatformEventDestinationResponse, UpdatePlatformEventDestinationRequestBody, WorkerJobType } from '@activepieces/shared'
import { FastifyBaseLogger } from 'fastify'
import { StatusCodes } from 'http-status-codes'
import { ArrayContains, FindOptionsWhere } from 'typeorm'
import { repoFactory } from '../core/db/repo-factory'
+import { transaction } from '../core/db/transaction'
import { flowVersionService } from '../flows/flow-version/flow-version.service'
import { applicationEvents } from '../helper/application-events'
import { domainHelper } from '../helper/domain-helper'
+import { EncryptedObject, encryptUtils } from '../helper/encryption'
import { buildPaginator } from '../helper/pagination/build-paginator'
import { paginationHelper } from '../helper/pagination/pagination-utils'
import { system } from '../helper/system/system'
@@ -19,7 +21,9 @@ import { jobQueue, JobType } from '../workers/job-queue/job-queue'
import { eventDestinationHooks } from './event-destinations-hooks'
import {
EventDestinationEntity,
+ EventDestinationRow,
EventDestinationSchema,
+ StoredEventDestinationHeaders,
} from './event-destinations.entity'
const eventDestinationRepo = repoFactory<EventDestinationSchema>(
@@ -37,8 +41,17 @@ const FLOW_RUN_EVENT_ACTIONS: ReadonlySet<ApplicationEventName> = new Set([
const WEBHOOK_PATH_MARKER = '/v1/webhooks/'
+const MILLISECONDS_PER_SECOND = 1000
+
const PROTOBUF_CONTENT_TYPE = 'application/x-protobuf'
+const TEST_ERROR_BY_POST_FAILURE: Record<PostForStatusFailure, EventDestinationTestError> = {
+ [PostForStatusFailure.BLOCKED]: EventDestinationTestError.BLOCKED,
+ [PostForStatusFailure.TIMEOUT]: EventDestinationTestError.TIMEOUT,
+ [PostForStatusFailure.TLS]: EventDestinationTestError.TLS,
+ [PostForStatusFailure.CONNECTION_FAILED]: EventDestinationTestError.CONNECTION_FAILED,
+}
+
export const eventDestinationService = (log: FastifyBaseLogger) => ({
setup(): void {
applicationEvents(log).registerListeners(log, {
@@ -59,7 +72,7 @@ export const eventDestinationService = (log: FastifyBaseLogger) => ({
create: async ({ request, platformId }: CreateParams): Promise<EventDestination> => {
const format = request.format ?? EventDestinationFormat.RAW
assertWebhookUrlSupportsFormat({ url: request.url, format })
- const entity: EventDestination = {
+ const entity: EventDestinationRow = {
id: apId(),
created: new Date().toISOString(),
updated: new Date().toISOString(),
@@ -69,15 +82,46 @@ export const eventDestinationService = (log: FastifyBaseLogger) => ({
url: request.url,
enabled: request.enabled ?? true,
format,
+ headers: await toStoredHeaders({ requested: request.headers, stored: null, url: request.url }),
}
- return eventDestinationRepo().save(entity)
+ const saved = await eventDestinationRepo().save(entity)
+ return maskHeaders({ row: saved, log })
},
update: async ({ id, platformId, request }: UpdateParams): Promise<EventDestination> => {
- const existing = await eventDestinationRepo().findOneByOrFail({ id, platformId })
- const format = request.format ?? existing.format
- assertWebhookUrlSupportsFormat({ url: request.url, format })
- await eventDestinationRepo().update({ id, platformId }, { ...request, format })
- return eventDestinationRepo().findOneByOrFail({ id, platformId })
+ const updated = await transaction(async (entityManager) => {
+ const repo = eventDestinationRepo(entityManager)
+ const stored = await repo.findOneOrFail({
+ where: { id, platformId },
+ lock: { mode: 'pessimistic_write' },
+ })
+ const { headers: requestedHeaders, ...rest } = request
+ const url = rest.url ?? stored.url
+ const format = rest.format ?? stored.format
+ assertWebhookUrlSupportsFormat({ url, format })
+ const storedHeaders = parseStoredHeaders({ headers: stored.headers, destinationId: stored.id, log })
+ assertUrlChangeRebindsStoredHeaders({
+ requested: requestedHeaders,
+ stored: storedHeaders,
+ url,
+ rowUrl: stored.url,
+ })
+ const headers = requestedHeaders === undefined
+ ? undefined
+ : await toStoredHeaders({
+ requested: requestedHeaders,
+ stored: storedHeaders,
+ url,
+ })
+ await repo.update({ id, platformId }, {
+ ...rest,
+ url,
+ format,
+ ...spreadIfNotUndefined('headers', headers),
+ updated: new Date().toISOString(),
+ })
+ return repo.findOneByOrFail({ id, platformId })
+ })
+ return maskHeaders({ row: updated, log })
},
delete: async ({ id, platformId }: DeleteParams): Promise<void> => {
await eventDestinationRepo().delete({
@@ -107,8 +151,9 @@ export const eventDestinationService = (log: FastifyBaseLogger) => ({
})
const { data, cursor } = await paginator.paginate(queryBuilder)
+ const masked = data.map((row) => maskHeaders({ row, log }))
- return paginationHelper.createPage<EventDestination>(data, cursor)
+ return paginationHelper.createPage<EventDestination>(masked, cursor)
},
trigger: async ({ projectId, event }: TriggerParams): Promise<void> => {
const platformId = event.platformId
@@ -164,33 +209,140 @@ export const eventDestinationService = (log: FastifyBaseLogger) => ({
destinationUrl: destination.url,
internalFlowId,
format: destination.format,
+ storedHeaders: destination.headers,
body: buildDeliveryBody({ format: destination.format, event: enrichedEvent }),
}),
))
},
- test: async ({ platformId, projectId, url, event }: TestParams): Promise<void> => {
+ resolveDeliveryHeaders: async ({ platformId, destinationId, destinationUrl }: ResolveDeliveryHeadersParams): Promise<EventDestinationHeaders | null> => {
+ const destination = await eventDestinationRepo().findOneBy({ id: destinationId, platformId, url: destinationUrl })
+ if (isNil(destination)) {
+ return null
+ }
+ return decryptBoundHeaders({ headers: destination.headers, destinationId: destination.id, destinationUrl: destination.url, log })
+ },
+ test: async ({ platformId, projectId, url, event, format, headers }: TestParams): Promise<TestPlatformEventDestinationResponse> => {
const eventToTest = event ?? ApplicationEventName.FLOW_CREATED
+ const formatToTest = format ?? EventDestinationFormat.RAW
+ assertWebhookUrlSupportsFormat({ url, format: formatToTest })
const mockEvent = buildMockEvent({ event: eventToTest, platformId, projectId })
+ const renderedBody = buildDeliveryBody({ format: formatToTest, event: mockEvent })
+ const resolvedHeaders = headers ?? {}
const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
path: 'v1/webhooks',
})
- await dispatchEventToDestination({
- log,
- platformId,
- projectId,
- destinationId: apId(),
+ const internalFlowId = matchInternalWebhookFlowId({
destinationUrl: url,
- internalFlowId: matchInternalWebhookFlowId({
- destinationUrl: url,
- webhookUrlPrefix,
- }),
- format: EventDestinationFormat.RAW,
- body: mockEvent,
+ webhookUrlPrefix,
})
+ const startedAt = Date.now()
+ const outcome = isNil(internalFlowId)
+ ? await postToDestination({ log, url, body: renderedBody, headers: resolvedHeaders, format: formatToTest })
+ : await dispatchToInternalFlow({
+ log,
+ destinationId: apId(),
+ destinationUrl: url,
+ flowId: internalFlowId,
+ body: renderedBody,
+ headers: resolvedHeaders,
+ })
+ return {
+ renderedBody,
+ durationMs: Date.now() - startedAt,
+ ...outcome,
+ }
},
})
+function parseStoredHeaders({ headers, destinationId, log }: ParseStoredHeadersParams): StoredEventDestinationHeaders | null {
+ if (isNil(headers)) {
+ return null
+ }
+ const parsed = StoredEventDestinationHeaders.safeParse(headers)
+ if (!parsed.success) {
+ log.warn({ destination: { id: destinationId } }, '[eventDestinationService#parseStoredHeaders] Stored headers are unreadable and are treated as empty')
+ return null
+ }
+ return parsed.data
+}
+
+function assertUrlChangeRebindsStoredHeaders({ requested, stored, url, rowUrl }: AssertUrlChangeRebindsStoredHeadersParams): void {
+ if (isNil(stored)) {
+ return
+ }
+ const carriedOverNames = carriedOverHeaderNames({ requested, stored, url, rowUrl })
+ if (carriedOverNames.length === 0) {
+ return
+ }
+ throw new ActivepiecesError({
+ code: ErrorCode.EVENT_DESTINATION_URL_CHANGE_REQUIRES_HEADERS,
+ params: { headerNames: carriedOverNames },
+ })
+}
+
+function carriedOverHeaderNames({ requested, stored, url, rowUrl }: CarriedOverHeaderNamesParams): string[] {
+ const storedNames = Object.keys(stored.values)
+ if (requested === undefined) {
+ return url === rowUrl ? [] : storedNames
+ }
+ if (requested === null || url === stored.url) {
+ return []
+ }
+ const requestedByLowerName = byLowerCaseName(requested)
+ return storedNames.filter((name) => {
+ const lowerName = name.toLowerCase()
+ return requestedByLowerName.has(lowerName) && isNil(requestedByLowerName.get(lowerName))
+ })
+}
+
+async function toStoredHeaders({ requested, stored, url }: ToStoredHeadersParams): Promise<StoredEventDestinationHeaders | null> {
+ if (isNil(requested)) {
+ return null
+ }
+ const storedByLowerName = byLowerCaseName(stored?.values ?? {})
+ const entries = await Promise.all(
+ Object.entries(requested).map(async ([key, value]): Promise<[string, EncryptedObject] | null> => {
+ if (!isNil(value)) {
+ return [key, await encryptUtils.encryptString(value)]
+ }
+ const keptCiphertext = storedByLowerName.get(key.toLowerCase())
+ return isNil(keptCiphertext) ? null : [key, keptCiphertext]
+ }),
+ )
+ const resolved = entries.filter((entry): entry is [string, EncryptedObject] => !isNil(entry))
+ return resolved.length === 0 ? null : { url, values: Object.fromEntries(resolved) }
+}
+
+function byLowerCaseName<T>(headers: Record<string, T>): Map<string, T> {
+ return new Map(Object.entries(headers).map(([name, value]) => [name.toLowerCase(), value]))
+}
+
+async function decryptBoundHeaders({ headers, destinationId, destinationUrl, log }: DecryptBoundHeadersParams): Promise<EventDestinationHeaders | null> {
+ const stored = parseStoredHeaders({ headers, destinationId, log })
+ if (isNil(stored)) {
+ return {}
+ }
+ if (stored.url !== destinationUrl) {
+ log.warn({ destination: { id: destinationId } }, '[eventDestinationService#decryptBoundHeaders] Stored headers belong to another URL and are not sent, so the event is dropped until the values are saved again')
+ return null
+ }
+ const entries = await Promise.all(
+ Object.entries(stored.values).map(async ([key, value]): Promise<[string, string]> => [key, await encryptUtils.decryptString(value)]),
+ )
+ return Object.fromEntries(entries)
+}
+
+function maskHeaders({ row, log }: MaskHeadersParams): EventDestination {
+ const keys = Object.keys(parseStoredHeaders({ headers: row.headers, destinationId: row.id, log })?.values ?? {})
+ return {
+ ...row,
+ headers: keys.length === 0
+ ? null
+ : Object.fromEntries(keys.map((key) => [key, null])),
+ }
+}
+
function buildDeliveryBody({ format, event }: BuildDeliveryBodyParams): DeliveryBody {
if (format === EventDestinationFormat.RAW) {
return event
@@ -211,8 +363,11 @@ function assertWebhookUrlSupportsFormat({ url, format }: AssertWebhookUrlSupport
}
}
-function deliveryJobFields({ format }: DeliveryJobFieldsParams): Pick<EventDestinationJobData, 'contentType'> {
- return format === EventDestinationFormat.OTLP_PROTOBUF ? { contentType: PROTOBUF_CONTENT_TYPE } : {}
+function deliveryJobFields({ format, hasHeaders }: DeliveryJobFieldsParams): Pick<EventDestinationJobData, 'contentType' | 'hasHeaders'> {
+ return {
+ ...(format === EventDestinationFormat.OTLP_PROTOBUF ? { contentType: PROTOBUF_CONTENT_TYPE } : {}),
+ ...(hasHeaders ? { hasHeaders } : {}),
+ }
}
const dispatchEventToDestination = async ({
@@ -223,15 +378,29 @@ const dispatchEventToDestination = async ({
destinationUrl,
internalFlowId,
format,
+ storedHeaders,
body,
}: DispatchEventParams): Promise<void> => {
if (!isNil(internalFlowId)) {
+ const { data: headers, error } = await tryCatch(() => decryptBoundHeaders({ headers: storedHeaders, destinationId, destinationUrl, log }))
+ if (error !== null) {
+ log.error({
+ destination: { id: destinationId },
+ flow: { id: internalFlowId },
+ error: error.message,
+ }, '[eventDestinationService#dispatchEventToDestination] Stored headers could not be decrypted, dropping the event for the internal handler flow')
+ return
+ }
+ if (isNil(headers)) {
+ return
+ }
await dispatchToInternalFlow({
log,
destinationId,
destinationUrl,
flowId: internalFlowId,
body,
+ headers,
})
return
}
@@ -245,19 +414,40 @@ const dispatchEventToDestination = async ({
webhookId: destinationId,
webhookUrl: destinationUrl,
payload: body,
- ...deliveryJobFields({ format }),
+ ...deliveryJobFields({ format, hasHeaders: !isNil(storedHeaders) }),
jobType: WorkerJobType.EVENT_DESTINATION,
},
})
}
+const postToDestination = async ({ log, url, body, headers, format }: PostToDestinationParams): Promise<DeliveryOutcome> => {
+ const timeoutInSeconds = system.getNumberOrThrow(AppSystemProp.EVENT_DESTINATION_TIMEOUT_SECONDS)
+ const isProtobuf = format === EventDestinationFormat.OTLP_PROTOBUF
+ const result = await safeHttp.postForStatus({
+ url,
+ headers: { ...headers, 'Content-Type': isProtobuf ? PROTOBUF_CONTENT_TYPE : 'application/json' },
+ body: isProtobuf ? Buffer.from(otlpLogs.encodeExportRequest(body)) : body,
+ timeoutMs: timeoutInSeconds * MILLISECONDS_PER_SECOND,
+ })
+ if (!result.responded) {
+ log.info({
+ webhookUrl: url,
+ webhook: { deliveryFailure: result.failure },
+ error: result.error.message,
+ }, '[eventDestinationService#test] The test delivery did not reach the destination')
+ return { errorCode: TEST_ERROR_BY_POST_FAILURE[result.failure] }
+ }
+ return { status: result.status }
+}
+
const dispatchToInternalFlow = async ({
log,
destinationId,
destinationUrl,
flowId,
body,
-}: DispatchToInternalFlowParams): Promise<void> => {
+ headers,
+}: DispatchToInternalFlowParams): Promise<DeliveryOutcome> => {
const routeSuffix = webhookRouteSuffix({ destinationUrl, flowId })
const isDraftOrTest = routeSuffix.startsWith('/draft') || routeSuffix === '/test'
const { data: response, error } = await tryCatch(async () => webhookService.handleWebhook({
@@ -273,7 +463,7 @@ const dispatchToInternalFlow = async ({
flowVersionToRun: isDraftOrTest
? WebhookFlowVersionToRun.LATEST
: WebhookFlowVersionToRun.LOCKED_FALL_BACK_TO_LATEST,
- data: () => Promise.resolve(buildInternalWebhookPayload({ destinationUrl, body })),
+ data: () => Promise.resolve(buildInternalWebhookPayload({ destinationUrl, body, headers })),
execute: routeSuffix !== '/test',
failParentOnFailure: false,
}))
@@ -283,7 +473,7 @@ const dispatchToInternalFlow = async ({
flow: { id: flowId },
error: error.message,
}, '[eventDestinationService#dispatchToInternalFlow] Failed to dispatch the event to the internal handler flow')
- return
+ return { errorCode: EventDestinationTestError.HANDLER_FLOW_FAILED }
}
if (response.status >= StatusCodes.BAD_REQUEST) {
log.error({
@@ -292,13 +482,17 @@ const dispatchToInternalFlow = async ({
response: { status: response.status },
}, '[eventDestinationService#dispatchToInternalFlow] Internal handler flow did not accept the event — the flow may be deleted or disabled')
}
+ return { status: response.status }
}
-const buildInternalWebhookPayload = ({ destinationUrl, body }: BuildInternalWebhookPayloadParams): EventPayload => {
+const buildInternalWebhookPayload = ({ destinationUrl, body, headers }: BuildInternalWebhookPayloadParams): EventPayload => {
const { data: url } = tryCatchSync(() => new URL(destinationUrl))
return {
method: 'POST',
- headers: { 'content-type': 'application/json' },
+ headers: {
+ ...Object.fromEntries(Object.entries(headers).map(([name, value]) => [name.toLowerCase(), value])),
+ 'content-type': 'application/json',
+ },
body,
queryParams: isNil(url) ? {} : Object.fromEntries(url.searchParams),
}
@@ -438,6 +632,38 @@ const webhookRouteSuffix = ({ destinationUrl, flowId }: WebhookRouteSuffixParams
}
+type ParseStoredHeadersParams = {
+ headers: StoredEventDestinationHeaders | null
+ destinationId: string
+ log: FastifyBaseLogger
+}
+
+type AssertUrlChangeRebindsStoredHeadersParams = {
+ requested: EventDestinationHeadersRequest | null | undefined
+ stored: StoredEventDestinationHeaders | null
+ url: string
+ rowUrl: string
+}
+
+type CarriedOverHeaderNamesParams = AssertUrlChangeRebindsStoredHeadersParams & {
+ stored: StoredEventDestinationHeaders
+}
+
+type ToStoredHeadersParams = {
+ requested: EventDestinationHeadersRequest | null | undefined
+ stored: StoredEventDestinationHeaders | null
+ url: string
+}
+
+type DecryptBoundHeadersParams = ParseStoredHeadersParams & {
+ destinationUrl: string
+}
+
+type MaskHeadersParams = {
+ row: EventDestinationRow
+ log: FastifyBaseLogger
+}
+
type DeleteParams = {
id: string
platformId: string
@@ -470,6 +696,21 @@ type TestParams = {
projectId?: ProjectId
url: string
event?: ApplicationEventName
+ format?: EventDestinationFormat
+ headers?: EventDestinationHeaders | null
+}
+
+type DeliveryOutcome = {
+ status?: number
+ errorCode?: EventDestinationTestError
+}
+
+type PostToDestinationParams = {
+ log: FastifyBaseLogger
+ url: string
+ body: DeliveryBody
+ headers: EventDestinationHeaders
+ format: EventDestinationFormat
}
type DeliveryBody = ApplicationEvent | OtlpExportLogsRequest
@@ -486,6 +727,7 @@ type AssertWebhookUrlSupportsFormatParams = {
type DeliveryJobFieldsParams = {
format: EventDestinationFormat
+ hasHeaders: boolean
}
type EnrichFlowRunEventParams = {
@@ -522,20 +764,29 @@ type DispatchEventParams = {
destinationUrl: string
internalFlowId: string | null
format: EventDestinationFormat
+ storedHeaders: StoredEventDestinationHeaders | null
body: unknown
}
+type ResolveDeliveryHeadersParams = {
+ platformId: PlatformId
+ destinationId: string
+ destinationUrl: string
+}
+
type DispatchToInternalFlowParams = {
log: FastifyBaseLogger
destinationId: string
destinationUrl: string
flowId: string
body: unknown
+ headers: EventDestinationHeaders
}
type BuildInternalWebhookPayloadParams = {
destinationUrl: string
body: unknown
+ headers: EventDestinationHeaders
}
type ExtractWebhookFlowIdCandidateParams = {
diff --git a/packages/server/api/src/app/helper/error-handler.ts b/packages/server/api/src/app/helper/error-handler.ts
index 1ee9d9d2663..24acd9648a7 100644
--- a/packages/server/api/src/app/helper/error-handler.ts
+++ b/packages/server/api/src/app/helper/error-handler.ts
@@ -117,6 +117,7 @@ const statusCodeMap: Partial<Record<ErrorCode, StatusCodes>> = {
[ErrorCode.AUTHENTICATION]: StatusCodes.UNAUTHORIZED,
[ErrorCode.INVALID_LICENSE_KEY]: StatusCodes.BAD_REQUEST,
[ErrorCode.EMAIL_ALREADY_HAS_ACTIVATION_KEY]: StatusCodes.CONFLICT,
+ [ErrorCode.EVENT_DESTINATION_URL_CHANGE_REQUIRES_HEADERS]: StatusCodes.BAD_REQUEST,
[ErrorCode.MCP_PIECE_REQUIRES_CONNECTION]: StatusCodes.BAD_REQUEST,
[ErrorCode.MCP_PIECE_CONNECTION_MISMATCH]: StatusCodes.BAD_REQUEST,
[ErrorCode.DOES_NOT_MEET_BUSINESS_REQUIREMENTS]: StatusCodes.UNPROCESSABLE_ENTITY,
diff --git a/packages/server/api/src/app/workers/rpc/worker-rpc-service.ts b/packages/server/api/src/app/workers/rpc/worker-rpc-service.ts
index baace04372a..826d71023d5 100644
--- a/packages/server/api/src/app/workers/rpc/worker-rpc-service.ts
+++ b/packages/server/api/src/app/workers/rpc/worker-rpc-service.ts
@@ -7,6 +7,7 @@ import { websocketService } from '../../core/websockets.service'
import { distributedStore, redisConnections } from '../../database/redis-connections'
import { agentRpcHandlers } from '../../ee/agent/agent-rpc-handlers'
import { chatPersonalizationService } from '../../ee/agent/personalization/chat-personalization-service'
+import { eventDestinationRpcHandlers } from '../../event-destinations/event-destinations-rpc-handlers'
import { fileService, getLocationForFile } from '../../file/file.service'
import { s3Helper } from '../../file/s3-helper'
import { signedFileTransport } from '../../file/signed-file-transport'
@@ -410,6 +411,9 @@ export function createHandlers(log: FastifyBaseLogger, assignment: WorkerGroupAs
async reportAiKeyOutcome(input) {
return aiRpcHandlers(log).reportAiKeyOutcome(input)
},
+ async resolveEventDestinationHeaders(input) {
+ return eventDestinationRpcHandlers(log).resolveEventDestinationHeaders(input)
+ },
async saveFlowStepFile(input) {
return aiRpcHandlers(log).saveFlowStepFile(input)
},
diff --git a/packages/server/api/test/helpers/mocks/index.ts b/packages/server/api/test/helpers/mocks/index.ts
index 79f5af792d5..30df2758714 100644
--- a/packages/server/api/test/helpers/mocks/index.ts
+++ b/packages/server/api/test/helpers/mocks/index.ts
@@ -10,6 +10,7 @@ import { databaseConnection } from '../../../src/app/database/database-connectio
import { generateApiKey } from '../../../src/app/ee/api-keys/api-key-service'
import { OAuthAppWithEncryptedSecret } from '../../../src/app/ee/oauth-apps/oauth-app.entity'
import { PlatformPlanEntity } from '../../../src/app/ee/platform/platform-plan/platform-plan.entity'
+import { StoredEventDestinationHeaders } from '../../../src/app/event-destinations/event-destinations.entity'
import { encryptUtils } from '../../../src/app/helper/encryption'
import { PieceMetadataSchema } from '../../../src/app/pieces/metadata/piece-metadata-entity'
import { pieceMetadataService } from '../../../src/app/pieces/metadata/piece-metadata-service'
@@ -775,6 +776,7 @@ export const createMockEventDestination = (eventDestination?: Partial<MockEventD
url: eventDestination?.url ?? faker.internet.url(),
scope: eventDestination?.scope ?? EventDestinationScope.PLATFORM,
enabled: eventDestination?.enabled ?? true,
+ headers: eventDestination?.headers ?? null,
format: eventDestination?.format ?? EventDestinationFormat.RAW,
}
}
@@ -789,6 +791,7 @@ type MockEventDestination = {
url: string
scope: EventDestinationScope
enabled: boolean
+ headers: StoredEventDestinationHeaders | null
format: EventDestinationFormat
}
diff --git a/packages/server/api/test/integration/cloud/event-destinations/event-destination-trigger.test.ts b/packages/server/api/test/integration/cloud/event-destinations/event-destination-trigger.test.ts
index 8e9b34bafe1..d56340fe2f8 100644
--- a/packages/server/api/test/integration/cloud/event-destinations/event-destination-trigger.test.ts
+++ b/packages/server/api/test/integration/cloud/event-destinations/event-destination-trigger.test.ts
@@ -4,6 +4,7 @@ import { FastifyInstance } from 'fastify'
import { eventDestinationService } from '../../../../src/app/event-destinations/event-destinations.service'
import { applicationEvents } from '../../../../src/app/helper/application-events'
import { domainHelper } from '../../../../src/app/helper/domain-helper'
+import { encryptUtils } from '../../../../src/app/helper/encryption'
import { WebhookFlowVersionToRun, webhookService } from '../../../../src/app/webhooks/webhook.service'
import * as jobQueueModule from '../../../../src/app/workers/job-queue/job-queue'
import { db } from '../../../helpers/db'
@@ -79,6 +80,12 @@ const buildFlowRunEvent = (params: { platformId: string, projectId?: string, flo
const ENTITLED_PLAN = { plan: { eventStreamingEnabled: true } }
+const INTERNAL_PATH_SECRET = 'Bearer internal-path-secret'
+
+const QUEUED_JOB_SECRET = 'Bearer queued-job-secret'
+
+const DESTINATION_URL = 'https://collector.example.com/v1/logs'
+
let app: FastifyInstance
beforeAll(async () => {
@@ -115,6 +122,103 @@ describe('Event Destination Trigger', () => {
vi.restoreAllMocks()
})
+ it('should never put a stored header, plaintext or ciphertext, into the queued job', async () => {
+ const ctx = await createTestContext(app, ENTITLED_PLAN)
+ const storedHeader = await encryptUtils.encryptString(QUEUED_JOB_SECRET)
+ const destination = createMockEventDestination({
+ platformId: ctx.platform.id,
+ events: [ApplicationEventName.FLOW_CREATED],
+ scope: EventDestinationScope.PLATFORM,
+ url: DESTINATION_URL,
+ headers: { url: DESTINATION_URL, values: { Authorization: storedHeader } },
+ })
+ await db.save('event_destination', destination)
+
+ await eventDestinationService(app.log).trigger({
+ event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
+ })
+
+ const jobData = addSpy.mock.calls[0][0].data
+ expect(jobData).not.toHaveProperty('headers')
+ expect(JSON.stringify(jobData)).not.toContain(QUEUED_JOB_SECRET)
+ expect(JSON.stringify(jobData)).not.toContain(storedHeader.data)
+ })
+
+ it('should hand the decrypted headers to the worker only through the delivery resolver', async () => {
+ const ctx = await createTestContext(app, ENTITLED_PLAN)
+ const otherCtx = await createTestContext(app, ENTITLED_PLAN)
+ const destination = createMockEventDestination({
+ platformId: ctx.platform.id,
+ events: [ApplicationEventName.FLOW_CREATED],
+ scope: EventDestinationScope.PLATFORM,
+ url: DESTINATION_URL,
+ headers: { url: DESTINATION_URL, values: { Authorization: await encryptUtils.encryptString(QUEUED_JOB_SECRET) } },
+ })
+ await db.save('event_destination', destination)
+
+ const resolved = await eventDestinationService(app.log).resolveDeliveryHeaders({
+ platformId: ctx.platform.id,
+ destinationId: destination.id,
+ destinationUrl: destination.url,
+ })
+ expect(resolved).toEqual({ Authorization: QUEUED_JOB_SECRET })
+
+ const crossTenant = await eventDestinationService(app.log).resolveDeliveryHeaders({
+ platformId: otherCtx.platform.id,
+ destinationId: destination.id,
+ destinationUrl: destination.url,
+ })
+ expect(crossTenant).toBeNull()
+ })
+
+ it('should refuse the stored headers to a job queued for a URL the destination no longer has', async () => {
+ const ctx = await createTestContext(app, ENTITLED_PLAN)
+ const destination = createMockEventDestination({
+ platformId: ctx.platform.id,
+ events: [ApplicationEventName.FLOW_CREATED],
+ scope: EventDestinationScope.PLATFORM,
+ url: 'https://old.example.com/collect',
+ headers: { url: 'https://old.example.com/collect', values: { Authorization: await encryptUtils.encryptString(QUEUED_JOB_SECRET) } },
+ })
+ await db.save('event_destination', destination)
+ await eventDestinationService(app.log).trigger({
+ event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
+ })
+ const queuedJob = addSpy.mock.calls[0][0].data
+
+ await db.update('event_destination', destination.id, {
+ url: 'https://new.example.com/collect',
+ headers: { url: 'https://new.example.com/collect', values: { Authorization: await encryptUtils.encryptString('Bearer bound-to-the-new-url') } },
+ })
+
+ const resolved = await eventDestinationService(app.log).resolveDeliveryHeaders({
+ platformId: queuedJob.platformId,
+ destinationId: queuedJob.webhookId,
+ destinationUrl: queuedJob.webhookUrl,
+ })
+ expect(resolved).toBeNull()
+ })
+
+ it('should refuse the stored headers when the destination URL changed without them, as an older build would do', async () => {
+ const ctx = await createTestContext(app, ENTITLED_PLAN)
+ const destination = createMockEventDestination({
+ platformId: ctx.platform.id,
+ events: [ApplicationEventName.FLOW_CREATED],
+ scope: EventDestinationScope.PLATFORM,
+ url: 'https://moved-by-an-older-build.example/collect',
+ headers: { url: DESTINATION_URL, values: { Authorization: await encryptUtils.encryptString(QUEUED_JOB_SECRET) } },
+ })
+ await db.save('event_destination', destination)
+
+ const resolved = await eventDestinationService(app.log).resolveDeliveryHeaders({
+ platformId: ctx.platform.id,
+ destinationId: destination.id,
+ destinationUrl: destination.url,
+ })
+
+ expect(resolved).toBeNull()
+ })
+
it('should drop the event when the platform is not entitled to event streaming', async () => {
const ctx = await createTestContext(app, { plan: { eventStreamingEnabled: false } })
const destination = createMockEventDestination({
@@ -620,7 +724,7 @@ describe('Event Destination Trigger', () => {
)
})
- it('should queue a RAW destination with the same job fields as before formats existed', async () => {
+ it('should queue a RAW destination without headers with the same job fields as before formats existed', async () => {
const ctx = await createTestContext(app, ENTITLED_PLAN)
const destination = createMockEventDestination({
platformId: ctx.platform.id,
@@ -634,6 +738,7 @@ describe('Event Destination Trigger', () => {
const jobData = addSpy.mock.calls[0][0].data
expect(jobData).not.toHaveProperty('contentType')
+ expect(jobData).not.toHaveProperty('hasHeaders')
expect(jobData.payload).toEqual(event)
})
@@ -676,6 +781,32 @@ describe('Event Destination Trigger', () => {
expect(jobData.payload.resourceLogs).toHaveLength(1)
})
+ it('should tell the worker to resolve headers only for a destination that stores them', async () => {
+ const ctx = await createTestContext(app, ENTITLED_PLAN)
+ const withHeaders = createMockEventDestination({
+ platformId: ctx.platform.id,
+ events: [ApplicationEventName.FLOW_CREATED],
+ scope: EventDestinationScope.PLATFORM,
+ url: 'https://example.com/with-headers',
+ headers: { url: 'https://example.com/with-headers', values: { Authorization: await encryptUtils.encryptString(QUEUED_JOB_SECRET) } },
+ })
+ const withoutHeaders = createMockEventDestination({
+ platformId: ctx.platform.id,
+ events: [ApplicationEventName.FLOW_CREATED],
+ scope: EventDestinationScope.PLATFORM,
+ url: 'https://example.com/without-headers',
+ })
+ await db.save('event_destination', [withHeaders, withoutHeaders])
+
+ await eventDestinationService(app.log).trigger({
+ event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
+ })
+
+ const jobsByUrl = new Map(addSpy.mock.calls.map(([job]) => [job.data.webhookUrl, job.data]))
+ expect(jobsByUrl.get(withHeaders.url)).toMatchObject({ hasHeaders: true })
+ expect(jobsByUrl.get(withoutHeaders.url)).not.toHaveProperty('hasHeaders')
+ })
+
it('regression: ensure that we have setup the event streaming listeners', async () => {
const ctx = await createTestContext(app, ENTITLED_PLAN)
const workerDestination = createMockEventDestination({
@@ -906,6 +1037,57 @@ describe('Event Destination Trigger', () => {
expect(payload.body.resourceLogs[0].scopeLogs[0].logRecords[0].eventName).toBe(ApplicationEventName.FLOW_CREATED)
})
+ it('should forward the stored headers to the internal handler flow with lowercase names, as an HTTP delivery would', async () => {
+ const ctx = await createTestContext(app, ENTITLED_PLAN)
+ const flowId = apId()
+ const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
+ path: 'v1/webhooks',
+ })
+ const handlerFlowUrl = `${webhookUrlPrefix}/${flowId}`
+ const destination = createMockEventDestination({
+ platformId: ctx.platform.id,
+ events: [ApplicationEventName.FLOW_CREATED],
+ scope: EventDestinationScope.PLATFORM,
+ url: handlerFlowUrl,
+ headers: { url: handlerFlowUrl, values: { Authorization: await encryptUtils.encryptString(INTERNAL_PATH_SECRET) } },
+ })
+ await db.save('event_destination', destination)
+
+ await eventDestinationService(app.log).trigger({
+ event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
+ })
+
+ const payload = await handleWebhookSpy.mock.calls[0][0].data(ctx.project.id)
+ expect(payload.headers).toEqual({
+ authorization: INTERNAL_PATH_SECRET,
+ 'content-type': 'application/json',
+ })
+ expect(addSpy).not.toHaveBeenCalled()
+ })
+
+ it('should not hand the stored headers to an internal handler flow when they belong to another URL', async () => {
+ const ctx = await createTestContext(app, ENTITLED_PLAN)
+ const flowId = apId()
+ const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
+ path: 'v1/webhooks',
+ })
+ const destination = createMockEventDestination({
+ platformId: ctx.platform.id,
+ events: [ApplicationEventName.FLOW_CREATED],
+ scope: EventDestinationScope.PLATFORM,
+ url: `${webhookUrlPrefix}/${flowId}`,
+ headers: { url: DESTINATION_URL, values: { Authorization: await encryptUtils.encryptString(INTERNAL_PATH_SECRET) } },
+ })
+ await db.save('event_destination', destination)
+
+ await eventDestinationService(app.log).trigger({
+ event: buildFlowEvent(ApplicationEventName.FLOW_CREATED, { platformId: ctx.platform.id }),
+ })
+
+ expect(handleWebhookSpy).not.toHaveBeenCalled()
+ expect(addSpy).not.toHaveBeenCalled()
+ })
+
it('should dispatch internally when the internal URL carries a path suffix (e.g. /sync)', async () => {
const ctx = await createTestContext(app, ENTITLED_PLAN)
const flowId = apId()
@@ -1104,14 +1286,14 @@ describe('Event Destination Trigger', () => {
expect(addSpy).not.toHaveBeenCalled()
})
- it('test() should dispatch internally for an internal URL and queue an outbound job for an external URL', async () => {
+ it('test() should dispatch internally for an internal URL and deliver synchronously for an external URL', async () => {
const ctx = await createTestContext(app, ENTITLED_PLAN)
const flowId = apId()
const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
path: 'v1/webhooks',
})
- await eventDestinationService(app.log).test({
+ const internalResult = await eventDestinationService(app.log).test({
platformId: ctx.platform.id,
url: `${webhookUrlPrefix}/${flowId}`,
})
@@ -1119,21 +1301,37 @@ describe('Event Destination Trigger', () => {
expect(handleWebhookSpy).toHaveBeenCalledWith(
expect.objectContaining({ flowId, async: true }),
)
+ expect(internalResult.renderedBody).toMatchObject({ action: ApplicationEventName.FLOW_CREATED })
expect(addSpy).not.toHaveBeenCalled()
+ const externalResult = await eventDestinationService(app.log).test({
+ platformId: ctx.platform.id,
+ url: 'http://127.0.0.1:1/external-hook',
+ })
+ expect(addSpy).not.toHaveBeenCalled()
+ expect(externalResult.errorCode).toBeDefined()
+ expect(externalResult.status).toBeUndefined()
+ expect(externalResult.renderedBody).toMatchObject({ action: ApplicationEventName.FLOW_CREATED })
+ })
+
+ it('test() should forward the typed headers to an internal handler flow with lowercase names', async () => {
+ const ctx = await createTestContext(app, ENTITLED_PLAN)
+ const flowId = apId()
+ const webhookUrlPrefix = await domainHelper.getPublicApiUrl({
+ path: 'v1/webhooks',
+ })
+
await eventDestinationService(app.log).test({
platformId: ctx.platform.id,
- url: 'https://example.com/external-hook',
+ url: `${webhookUrlPrefix}/${flowId}`,
+ headers: { 'X-Handler-Key': 'typed-by-the-caller' },
+ })
+
+ const payload = await handleWebhookSpy.mock.calls[0][0].data(ctx.project.id)
+ expect(payload.headers).toEqual({
+ 'x-handler-key': 'typed-by-the-caller',
+ 'content-type': 'application/json',
})
- expect(addSpy).toHaveBeenCalledTimes(1)
- expect(addSpy).toHaveBeenCalledWith(
- expect.objectContaining({
- data: expect.objectContaining({
- webhookUrl: 'https://example.com/external-hook',
- jobType: WorkerJobType.EVENT_DESTINATION,
- }),
- }),
- )
})
})
})
diff --git a/packages/server/api/test/integration/cloud/event-destinations/event-destination.test.ts b/packages/server/api/test/integration/cloud/event-destinations/event-destination.test.ts
index 42ff179fee6..1c473012c74 100644
--- a/packages/server/api/test/integration/cloud/event-destinations/event-destination.test.ts
+++ b/packages/server/api/test/integration/cloud/event-destinations/event-destination.test.ts
@@ -1,13 +1,19 @@
import { apId, ErrorCode } from '@activepieces/core-utils'
-import { ApplicationEventName, EventDestinationFormat, PlatformRole, PrincipalType } from '@activepieces/shared'
+import { safeHttp } from '@activepieces/server-utils'
+import { ApplicationEventName, EventDestinationFormat, EventDestinationTestError, PlatformRole, PrincipalType } from '@activepieces/shared'
import { FastifyInstance } from 'fastify'
import { StatusCodes } from 'http-status-codes'
+import { eventDestinationService } from '../../../../src/app/event-destinations/event-destinations.service'
import { domainHelper } from '../../../../src/app/helper/domain-helper'
import { generateMockToken } from '../../../helpers/auth'
+import { db } from '../../../helpers/db'
import { mockBasicUser } from '../../../helpers/mocks'
import { createTestContext, TestContext } from '../../../helpers/test-context'
import { setupTestEnvironment, teardownTestEnvironment } from '../../../helpers/test-setup'
+const UNREACHABLE_URL = 'http://127.0.0.1:1/webhook'
+const TEST_DELIVERY_LIMIT_PER_MINUTE = 20
+
let app: FastifyInstance | null = null
const createEnabledContext = async (): Promise<TestContext> => createTestContext(app!, {
@@ -180,6 +186,54 @@ describe('Event Destinations API', () => {
expect(response?.json().format).toBe(EventDestinationFormat.OTLP_JSON)
})
+ it('should change only the enabled flag when the update sends nothing else, keeping the URL, events, format and stored header value', async () => {
+ const ctx = await createEnabledContext()
+ const url = 'https://otlp.example.com/v1/logs'
+ const created = await ctx.post('/v1/event-destinations', {
+ url,
+ events: [ApplicationEventName.FLOW_CREATED, ApplicationEventName.FLOW_DELETED],
+ format: EventDestinationFormat.OTLP_JSON,
+ headers: { Authorization: 'Bearer secret' },
+ })
+
+ const response = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ enabled: false,
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.OK)
+ expect(response?.json()).toMatchObject({
+ enabled: false,
+ url,
+ events: [ApplicationEventName.FLOW_CREATED, ApplicationEventName.FLOW_DELETED],
+ format: EventDestinationFormat.OTLP_JSON,
+ headers: { Authorization: null },
+ })
+ const resolved = await eventDestinationService(app!.log).resolveDeliveryHeaders({
+ platformId: ctx.platform.id,
+ destinationId: created?.json().id,
+ destinationUrl: url,
+ })
+ expect(resolved).toEqual({ Authorization: 'Bearer secret' })
+ })
+
+ it('should refuse a format-only update that sets OTLP_PROTOBUF on a webhook URL, and keep the stored format', async () => {
+ const ctx = await createEnabledContext()
+ const webhookUrl = `${await domainHelper.getPublicApiUrl({ path: 'v1/webhooks' })}/${apId()}`
+ const created = await ctx.post('/v1/event-destinations', {
+ url: webhookUrl,
+ events: [ApplicationEventName.FLOW_CREATED],
+ })
+
+ const response = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ format: EventDestinationFormat.OTLP_PROTOBUF,
+ })
+ const listed = await ctx.get('/v1/event-destinations')
+
+ expect(response?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+ expect(response?.json().code).toBe(ErrorCode.EVENT_DESTINATION_FORMAT_NOT_SUPPORTED_BY_WEBHOOK)
+ expect(listed?.json().data[0].format).toBe(EventDestinationFormat.RAW)
+ })
+
it('should refuse an update that sets OTLP_PROTOBUF on a webhook URL, and keep the stored format', async () => {
const ctx = await createEnabledContext()
const webhookUrl = `${await domainHelper.getPublicApiUrl({ path: 'v1/webhooks' })}/${apId()}`
@@ -243,6 +297,29 @@ describe('Event Destinations API', () => {
expect(response?.json().url).toBe('https://example.com/patched')
})
+ it('should accept a PATCH that sends only the enabled flag', async () => {
+ const ctx = await createEnabledContext()
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/original',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+
+ const response = await ctx.inject({
+ method: 'PATCH',
+ url: `/api/v1/event-destinations/${created?.json().id}`,
+ body: { enabled: false },
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.OK)
+ expect(response?.json()).toMatchObject({
+ enabled: false,
+ url: 'https://example.com/original',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: null },
+ })
+ })
+
it('should return error for non-existent destination', async () => {
const ctx = await createEnabledContext()
const nonExistentId = apId()
@@ -287,25 +364,538 @@ describe('Event Destinations API', () => {
})
describe('POST /v1/event-destinations/test', () => {
- it('should accept a test request with a webhook URL and an event name', async () => {
+ it('should return the raw event as the rendered body for the RAW format', async () => {
const ctx = await createEnabledContext()
const response = await ctx.post('/v1/event-destinations/test', {
- url: 'https://example.com/webhook',
+ url: UNREACHABLE_URL,
event: ApplicationEventName.FLOW_CREATED,
})
expect(response?.statusCode).toBe(StatusCodes.OK)
+ const body = response?.json()
+ expect(body.renderedBody.action).toBe(ApplicationEventName.FLOW_CREATED)
+ expect(body.renderedBody.platformId).toBe(ctx.platform.id)
+ expect(typeof body.durationMs).toBe('number')
})
- it('should accept a test request with no event (defaults to flow.created)', async () => {
+ it('should default to flow.created when no event is given', async () => {
const ctx = await createEnabledContext()
+ const response = await ctx.post('/v1/event-destinations/test', {
+ url: UNREACHABLE_URL,
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.OK)
+ expect(response?.json().renderedBody.action).toBe(ApplicationEventName.FLOW_CREATED)
+ })
+
+ it('should echo the OTLP/JSON request for an OTLP format', async () => {
+ const ctx = await createEnabledContext()
+
+ const response = await ctx.post('/v1/event-destinations/test', {
+ url: UNREACHABLE_URL,
+ event: ApplicationEventName.FLOW_RUN_FINISHED,
+ format: EventDestinationFormat.OTLP_JSON,
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.OK)
+ const record = response?.json().renderedBody.resourceLogs[0].scopeLogs[0].logRecords[0]
+ expect(record.eventName).toBe(ApplicationEventName.FLOW_RUN_FINISHED)
+ expect(JSON.parse(record.body.stringValue).action).toBe(ApplicationEventName.FLOW_RUN_FINISHED)
+ expect(record.attributes).toContainEqual({ key: 'platformId', value: { stringValue: ctx.platform.id } })
+ })
+
+ it('should post protobuf bytes for OTLP_PROTOBUF and still echo the OTLP/JSON form', async () => {
+ const ctx = await createEnabledContext()
+ const postSpy = vi.spyOn(safeHttp, 'postForStatus').mockResolvedValue({ responded: true, status: 200 })
+
+ const response = await ctx.post('/v1/event-destinations/test', {
+ url: 'https://otlp.example.com/v1/logs',
+ format: EventDestinationFormat.OTLP_PROTOBUF,
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.OK)
+ expect(response?.json().renderedBody.resourceLogs).toHaveLength(1)
+ const sent = postSpy.mock.calls[0][0]
+ expect(sent.headers).toMatchObject({ 'Content-Type': 'application/x-protobuf' })
+ expect(Buffer.isBuffer(sent.body)).toBe(true)
+ postSpy.mockRestore()
+ })
+
+ it('should refuse OTLP_PROTOBUF for a webhook URL, so a test cannot pass where delivery fails', async () => {
+ const ctx = await createEnabledContext()
+ const postSpy = vi.spyOn(safeHttp, 'postForStatus')
+
+ const response = await ctx.post('/v1/event-destinations/test', {
+ url: `https://automations.customer.example/api/v1/webhooks/${apId()}`,
+ format: EventDestinationFormat.OTLP_PROTOBUF,
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+ expect(response?.json().code).toBe(ErrorCode.EVENT_DESTINATION_FORMAT_NOT_SUPPORTED_BY_WEBHOOK)
+ expect(postSpy).not.toHaveBeenCalled()
+ postSpy.mockRestore()
+ })
+
+ it('should report the failure instead of throwing when the destination is unreachable', async () => {
+ const ctx = await createEnabledContext()
+
+ const response = await ctx.post('/v1/event-destinations/test', {
+ url: UNREACHABLE_URL,
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.OK)
+ const body = response?.json()
+ expect(body.errorCode).toBeDefined()
+ expect(body).not.toHaveProperty('error')
+ expect(body.status).toBeUndefined()
+ })
+
+ it('should report a blocked address as a fixed code, without the resolved address or the server setting', async () => {
+ const ctx = await createEnabledContext()
+
+ const response = await ctx.post('/v1/event-destinations/test', {
+ url: 'http://10.0.0.1/collect',
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.OK)
+ expect(response?.json().errorCode).toBe(EventDestinationTestError.BLOCKED)
+ expect(response?.body).not.toContain('not allowed')
+ expect(response?.body).not.toContain('AP_SSRF_ALLOW_LIST')
+ })
+
+ it('should limit how often one admin can send a test delivery', async () => {
+ const ctx = await createEnabledContext()
+ const otherCtx = await createEnabledContext()
+ const postSpy = vi.spyOn(safeHttp, 'postForStatus').mockResolvedValue({ responded: true, status: 200 })
+ const statusCodes: (number | undefined)[] = []
+
+ for (let attempt = 0; attempt <= TEST_DELIVERY_LIMIT_PER_MINUTE; attempt++) {
+ const response = await ctx.post('/v1/event-destinations/test', { url: 'https://example.com/webhook' })
+ statusCodes.push(response?.statusCode)
+ }
+ const otherAdmin = await otherCtx.post('/v1/event-destinations/test', { url: 'https://example.com/webhook' })
+
+ expect(statusCodes.slice(0, TEST_DELIVERY_LIMIT_PER_MINUTE).every((statusCode) => statusCode === StatusCodes.OK)).toBe(true)
+ expect(statusCodes[TEST_DELIVERY_LIMIT_PER_MINUTE]).toBe(StatusCodes.TOO_MANY_REQUESTS)
+ expect(otherAdmin?.statusCode).toBe(StatusCodes.OK)
+ expect(postSpy).toHaveBeenCalledTimes(TEST_DELIVERY_LIMIT_PER_MINUTE + 1)
+ postSpy.mockRestore()
+ })
+
+ it('should refuse to look up a stored header value', async () => {
+ const ctx = await createEnabledContext()
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+
+ const nullHeader = await ctx.post('/v1/event-destinations/test', {
+ url: UNREACHABLE_URL,
+ headers: { Authorization: null },
+ })
+ expect(nullHeader?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+
+ const postSpy = vi.spyOn(safeHttp, 'postForStatus').mockResolvedValue({ responded: true, status: 200 })
+ const byDestinationId = await ctx.post('/v1/event-destinations/test', {
+ url: 'https://example.com/webhook',
+ destinationId: created?.json().id,
+ })
+ expect(byDestinationId?.statusCode).toBe(StatusCodes.OK)
+ expect(postSpy.mock.calls[0][0].headers).not.toHaveProperty('Authorization')
+ postSpy.mockRestore()
+ })
+
+ it('should send only the headers the caller supplied in the request', async () => {
+ const ctx = await createEnabledContext()
+ await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer stored-secret' },
+ })
+ const postSpy = vi.spyOn(safeHttp, 'postForStatus').mockResolvedValue({ responded: true, status: 200 })
+
const response = await ctx.post('/v1/event-destinations/test', {
url: 'https://example.com/webhook',
+ headers: { 'X-Tenant': 'typed-by-the-caller' },
})
expect(response?.statusCode).toBe(StatusCodes.OK)
+ const sentHeaders = postSpy.mock.calls[0][0].headers
+ expect(sentHeaders).not.toHaveProperty('Authorization')
+ expect(JSON.stringify(sentHeaders)).not.toContain('stored-secret')
+ postSpy.mockRestore()
+ })
+ })
+
+ describe('Headers', () => {
+ it('should never return a header value, only its key', async () => {
+ const ctx = await createEnabledContext()
+
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+
+ expect(created?.json().headers).toEqual({ Authorization: null })
+
+ const listed = await ctx.get('/v1/event-destinations')
+ const found = listed?.json().data.find((destination: { id: string }) => destination.id === created?.json().id)
+ expect(found.headers).toEqual({ Authorization: null })
+ })
+
+ it('should keep a stored header value when the update sends null for its key', async () => {
+ const ctx = await createEnabledContext()
+
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+
+ const updated = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_DELETED],
+ headers: { Authorization: null },
+ })
+
+ expect(updated?.statusCode).toBe(StatusCodes.OK)
+ expect(updated?.json().events).toContain(ApplicationEventName.FLOW_DELETED)
+ expect(updated?.json().headers).toEqual({ Authorization: null })
+ })
+
+ it('should keep a stored header value when the update sends null for its key in another letter case', async () => {
+ const ctx = await createEnabledContext()
+
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { 'X-Api-Key': 'secret' },
+ })
+
+ const updated = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { 'x-api-key': null },
+ })
+
+ expect(updated?.statusCode).toBe(StatusCodes.OK)
+ expect(updated?.json().headers).toEqual({ 'x-api-key': null })
+ })
+
+ it('should refuse to carry a stored header value over to a different URL when the null key differs in letter case', async () => {
+ const ctx = await createEnabledContext()
+
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { 'X-Api-Key': 'secret' },
+ })
+
+ const moved = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ url: 'https://attacker.example/collect',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { 'x-api-key': null },
+ })
+
+ expect(moved?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+ expect(moved?.json().code).toBe(ErrorCode.EVENT_DESTINATION_URL_CHANGE_REQUIRES_HEADERS)
+ })
+
+ it('should refuse to carry a stored header value over to a different URL', async () => {
+ const ctx = await createEnabledContext()
+
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+
+ const kept = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ url: 'https://attacker.example/collect',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: null },
+ })
+ expect(kept?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+
+ const omitted = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ url: 'https://attacker.example/collect',
+ events: [ApplicationEventName.FLOW_CREATED],
+ })
+ expect(omitted?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+
+ const unchanged = await ctx.get('/v1/event-destinations')
+ const found = unchanged?.json().data.find((destination: { id: string }) => destination.id === created?.json().id)
+ expect(found.url).toBe('https://example.com/webhook')
+ })
+
+ it('should refuse a URL-only update on a destination with stored headers', async () => {
+ const ctx = await createEnabledContext()
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+
+ const response = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ url: 'https://attacker.example/collect',
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+ expect(response?.json().code).toBe(ErrorCode.EVENT_DESTINATION_URL_CHANGE_REQUIRES_HEADERS)
+ const listed = await ctx.get('/v1/event-destinations')
+ const found = listed?.json().data.find((destination: { id: string }) => destination.id === created?.json().id)
+ expect(found.url).toBe('https://example.com/webhook')
+ })
+
+ it('should allow a URL change that re-enters every stored header value', async () => {
+ const ctx = await createEnabledContext()
+
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+
+ const retyped = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ url: 'https://example.com/webhook-renamed',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer retyped' },
+ })
+ expect(retyped?.statusCode).toBe(StatusCodes.OK)
+ expect(retyped?.json().url).toBe('https://example.com/webhook-renamed')
+
+ const dropped = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ url: 'https://example.com/webhook-again',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: {},
+ })
+ expect(dropped?.statusCode).toBe(StatusCodes.OK)
+ expect(dropped?.json().headers).toBeNull()
+ })
+
+ it('should mask every stored key when the update replaces one value and keeps another', async () => {
+ const ctx = await createEnabledContext()
+
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer kept', 'X-Tenant': 'old' },
+ })
+
+ const updated = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: null, 'X-Tenant': 'new' },
+ })
+
+ expect(updated?.statusCode).toBe(StatusCodes.OK)
+ expect(updated?.json().headers).toEqual({ Authorization: null, 'X-Tenant': null })
+ })
+
+ it('should treat a header whose key was renamed as absent, without failing the update', async () => {
+ const ctx = await createEnabledContext()
+
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+
+ const updated = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorisation: null },
+ })
+
+ expect(updated?.statusCode).toBe(StatusCodes.OK)
+ expect(updated?.json().headers).toBeNull()
+ })
+
+ it('should reject a header name that is not a valid HTTP field name', async () => {
+ const ctx = await createEnabledContext()
+
+ const emptyName = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { '': 'value' },
+ })
+ expect(emptyName?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+
+ const nulByte = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { 'X-Tenant\u0000': 'value' },
+ })
+ expect(nulByte?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+ })
+
+ it('should reject a header name that the delivery sets itself, in any letter case', async () => {
+ const ctx = await createEnabledContext()
+ const postSpy = vi.spyOn(safeHttp, 'postForStatus').mockResolvedValue({ responded: true, status: 200 })
+
+ for (const name of ['Content-Type', 'content-type', 'CONTENT-LENGTH', 'Content-Encoding', 'Transfer-Encoding', 'Host', 'connection']) {
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { [name]: 'text/plain' },
+ })
+ expect(created?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+
+ const tested = await ctx.post('/v1/event-destinations/test', {
+ url: 'https://example.com/webhook',
+ headers: { [name]: 'text/plain' },
+ })
+ expect(tested?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+ }
+ expect(postSpy).not.toHaveBeenCalled()
+ postSpy.mockRestore()
+ })
+
+ it('should reject a header value with a line break, a control character or a hidden character', async () => {
+ const ctx = await createEnabledContext()
+ const postSpy = vi.spyOn(safeHttp, 'postForStatus').mockResolvedValue({ responded: true, status: 200 })
+
+ for (const value of ['Bearer secret\u200b', 'Bearer secret\r\nX-Injected: yes', 'Bearer\u0000secret']) {
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: value },
+ })
+ expect(created?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+
+ const tested = await ctx.post('/v1/event-destinations/test', {
+ url: 'https://example.com/webhook',
+ headers: { Authorization: value },
+ })
+ expect(tested?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+ }
+ expect(postSpy).not.toHaveBeenCalled()
+
+ const accepted = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer abc\tdéf' },
+ })
+ expect(accepted?.statusCode).toBe(StatusCodes.OK)
+ postSpy.mockRestore()
+ })
+
+ it('should refuse to keep a stored value whose URL changed outside of an update, until the value is typed again', async () => {
+ const ctx = await createEnabledContext()
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+ const id = created?.json().id
+ await db.update('event_destination', id, { url: 'https://moved-by-an-older-build.example/collect' })
+
+ const toggled = await ctx.post(`/v1/event-destinations/${id}`, { enabled: false })
+ expect(toggled?.statusCode).toBe(StatusCodes.OK)
+
+ const kept = await ctx.post(`/v1/event-destinations/${id}`, {
+ url: 'https://moved-by-an-older-build.example/collect',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: null },
+ })
+ expect(kept?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+ expect(kept?.json().code).toBe(ErrorCode.EVENT_DESTINATION_URL_CHANGE_REQUIRES_HEADERS)
+
+ const retyped = await ctx.post(`/v1/event-destinations/${id}`, {
+ url: 'https://moved-by-an-older-build.example/collect',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer retyped' },
+ })
+ expect(retyped?.statusCode).toBe(StatusCodes.OK)
+ const stored = await db.findOneByOrFail<{ headers: { url: string } }>('event_destination', { id })
+ expect(stored.headers.url).toBe('https://moved-by-an-older-build.example/collect')
+ })
+
+ it('should reject two header names that differ only in letter case', async () => {
+ const ctx = await createEnabledContext()
+
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { 'X-Api-Key': 'one', 'x-api-key': 'two' },
+ })
+ expect(created?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+
+ const tested = await ctx.post('/v1/event-destinations/test', {
+ url: 'https://example.com/webhook',
+ headers: { 'X-Api-Key': 'one', 'x-api-key': 'two' },
+ })
+ expect(tested?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+ })
+
+ it('should mask an unreadable stored header instead of failing the whole list', async () => {
+ const ctx = await createEnabledContext()
+
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+ await db.update('event_destination', created?.json().id, {
+ headers: { iv: 'not-a-per-key-map', data: 'written-by-an-older-build' },
+ })
+
+ const listed = await ctx.get('/v1/event-destinations')
+
+ expect(listed?.statusCode).toBe(StatusCodes.OK)
+ const found = listed?.json().data.find((destination: { id: string }) => destination.id === created?.json().id)
+ expect(found.headers).toBeNull()
+ })
+
+ it('should leave an unreadable stored header map alone on a toggle and replace it on a save that sends headers', async () => {
+ const ctx = await createEnabledContext()
+ const unreadable = { iv: 'not-a-per-key-map', data: 'written-by-an-older-build' }
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+ const id = created?.json().id
+ await db.update('event_destination', id, { headers: unreadable })
+
+ const toggled = await ctx.post(`/v1/event-destinations/${id}`, { enabled: false })
+
+ expect(toggled?.statusCode).toBe(StatusCodes.OK)
+ const afterToggle = await db.findOneByOrFail<{ headers: unknown }>('event_destination', { id })
+ expect(afterToggle.headers).toEqual(unreadable)
+
+ const saved = await ctx.post(`/v1/event-destinations/${id}`, {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: {},
+ })
+
+ expect(saved?.statusCode).toBe(StatusCodes.OK)
+ expect(saved?.json().headers).toBeNull()
+ const afterSave = await db.findOneByOrFail<{ headers: unknown }>('event_destination', { id })
+ expect(afterSave.headers).toBeNull()
+ })
+
+ it('should drop a header whose key is absent from the update', async () => {
+ const ctx = await createEnabledContext()
+
+ const created = await ctx.post('/v1/event-destinations', {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: { Authorization: 'Bearer secret' },
+ })
+
+ const updated = await ctx.post(`/v1/event-destinations/${created?.json().id}`, {
+ url: 'https://example.com/webhook',
+ events: [ApplicationEventName.FLOW_CREATED],
+ headers: {},
+ })
+
+ expect(updated?.json().headers).toBeNull()
})
})
diff --git a/packages/server/worker/src/lib/execute/jobs/event-destination.ts b/packages/server/worker/src/lib/execute/jobs/event-destination.ts
index 2eaeb68238b..463ab90b95f 100644
--- a/packages/server/worker/src/lib/execute/jobs/event-destination.ts
+++ b/packages/server/worker/src/lib/execute/jobs/event-destination.ts
@@ -1,4 +1,4 @@
-import { isObject } from '@activepieces/core-utils'
+import { isNil, isObject } from '@activepieces/core-utils'
import { otlpLogs, safeHttp } from '@activepieces/server-utils'
import { EngineResponseStatus, EventDestinationJobData, WorkerJobType } from '@activepieces/shared'
import { workerSettings } from '../../config/worker-settings'
@@ -9,9 +9,18 @@ export const eventDestinationJob: JobHandler<EventDestinationJobData, FireAndFor
async execute(ctx: JobContext, data: EventDestinationJobData): Promise<FireAndForgetJobResult> {
const timeoutInSeconds = workerSettings.getSettings().EVENT_DESTINATION_TIMEOUT_SECONDS
+ const headers = data.hasHeaders === true ? await resolveHeaders({ ctx, data }) : {}
+ if (isNil(headers)) {
+ ctx.log.warn({
+ webhook: { id: data.webhookId },
+ platform: { id: data.platformId },
+ }, 'Event destination was deleted or moved to another URL before delivery, dropping the event')
+ return { kind: JobResultKind.FIRE_AND_FORGET, status: EngineResponseStatus.OK }
+ }
+
const result = await safeHttp.postForStatus({
url: data.webhookUrl,
- headers: { 'Content-Type': data.contentType ?? 'application/json' },
+ headers: { ...headers, 'Content-Type': data.contentType ?? 'application/json' },
body: toRequestBody(data),
timeoutMs: timeoutInSeconds * 1000,
})
@@ -35,6 +44,15 @@ export const eventDestinationJob: JobHandler<EventDestinationJobData, FireAndFor
},
}
+async function resolveHeaders({ ctx, data }: ResolveHeadersParams): Promise<Record<string, string> | null> {
+ const resolved = await ctx.apiClient.resolveEventDestinationHeaders({
+ platformId: data.platformId,
+ destinationId: data.webhookId,
+ destinationUrl: data.webhookUrl,
+ })
+ return isNil(resolved) ? null : resolved.headers
+}
+
function toRequestBody(data: EventDestinationJobData): unknown {
if (data.contentType !== 'application/x-protobuf' || !isObject(data.payload)) {
return data.payload
@@ -43,3 +61,8 @@ function toRequestBody(data: EventDestinationJobData): unknown {
}
const MIN_FAILURE_HTTP_STATUS = 400
+
+type ResolveHeadersParams = {
+ ctx: JobContext
+ data: EventDestinationJobData
+}
diff --git a/packages/server/worker/test/lib/execute/jobs/event-destination.test.ts b/packages/server/worker/test/lib/execute/jobs/event-destination.test.ts
index 8524bff7e4c..9655889211d 100644
--- a/packages/server/worker/test/lib/execute/jobs/event-destination.test.ts
+++ b/packages/server/worker/test/lib/execute/jobs/event-destination.test.ts
@@ -22,20 +22,43 @@ describe('eventDestinationJob', () => {
postSpy.mockRestore()
})
- it('posts a job without a content type as JSON', async () => {
+ it('posts a job without hasHeaders as JSON and never calls the API for headers', async () => {
+ const { ctx, resolveHeaders } = makeContext({ headers: { Authorization: 'Bearer unused' } })
const payload = { action: 'flow.created', platformId: 'platform-1' }
- await eventDestinationJob.execute(makeContext(), makeJobData({ payload }))
+ await eventDestinationJob.execute(ctx, makeJobData({ payload }))
- expect(postSpy).toHaveBeenCalledWith({
+ expect(resolveHeaders).not.toHaveBeenCalled()
+ expect(postSpy).toHaveBeenCalledWith(expect.objectContaining({
url: 'https://example.com/webhook',
headers: { 'Content-Type': 'application/json' },
body: payload,
- timeoutMs: 10000,
+ }))
+ })
+
+ it('resolves and sends the stored headers when the job says the destination has them', async () => {
+ const { ctx, resolveHeaders } = makeContext({ headers: { Authorization: 'Bearer secret' } })
+
+ await eventDestinationJob.execute(ctx, makeJobData({ hasHeaders: true }))
+
+ expect(resolveHeaders).toHaveBeenCalledWith({
+ platformId: 'platform-1',
+ destinationId: 'destination-1',
+ destinationUrl: 'https://example.com/webhook',
})
+ expect(postSpy.mock.calls[0][0].headers).toEqual({ 'Content-Type': 'application/json', Authorization: 'Bearer secret' })
+ })
+
+ it('drops the event when the destination was deleted or moved to another URL before delivery', async () => {
+ const { ctx } = makeContext({ headers: null })
+
+ await eventDestinationJob.execute(ctx, makeJobData({ hasHeaders: true }))
+
+ expect(postSpy).not.toHaveBeenCalled()
})
it('encodes the queued OTLP/JSON request to protobuf bytes for a protobuf job', async () => {
+ const { ctx } = makeContext({ headers: null })
const payload = {
resourceLogs: [{
scopeLogs: [{
@@ -44,7 +67,7 @@ describe('eventDestinationJob', () => {
}],
}
- await eventDestinationJob.execute(makeContext(), makeJobData({ payload, contentType: 'application/x-protobuf' }))
+ await eventDestinationJob.execute(ctx, makeJobData({ payload, contentType: 'application/x-protobuf' }))
const sent = postSpy.mock.calls[0][0]
expect(sent.headers).toEqual({ 'Content-Type': 'application/x-protobuf' })
@@ -54,7 +77,7 @@ describe('eventDestinationJob', () => {
it('logs why a delivery did not reach the destination', async () => {
postSpy.mockResolvedValue({ responded: false, failure: PostForStatusFailure.TIMEOUT, error: new Error('the destination did not answer') })
- const ctx = makeContext()
+ const { ctx } = makeContext({ headers: null })
await eventDestinationJob.execute(ctx, makeJobData({}))
@@ -65,10 +88,13 @@ describe('eventDestinationJob', () => {
})
})
-function makeContext(): JobContext {
- return {
+function makeContext({ headers }: { headers: Record<string, string> | null }) {
+ const resolveHeaders = vi.fn().mockResolvedValue(headers === null ? null : { headers })
+ const ctx = {
+ apiClient: { resolveEventDestinationHeaders: resolveHeaders },
log: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
} as unknown as JobContext
+ return { ctx, resolveHeaders }
}
function makeJobData(overrides: Partial<EventDestinationJobData>): EventDestinationJobData {
diff --git a/packages/web/public/locales/en/translation.json b/packages/web/public/locales/en/translation.json
index 884b733c891..c13640bc268 100644
--- a/packages/web/public/locales/en/translation.json
+++ b/packages/web/public/locales/en/translation.json
@@ -2957,6 +2957,10 @@
"The complete JSON key file for a service account with the Vertex AI User role.": "The complete JSON key file for a service account with the Vertex AI User role.",
"Connect a Google Cloud service account to call Vertex AI models inside your own GCP project.\n\n1. Open the [Google Cloud Console](https://console.cloud.google.com) and select the project you want to bill.\n2. Enable the **Vertex AI API** under *APIs & Services > Enabled APIs*.\n3. Go to **IAM & Admin > Service Accounts** and create a service account, granting it the **Vertex AI User** role. Avoid broader roles — follow least-privilege so a leaked key has limited blast radius.\n4. Open the service account, go to **Keys > Add Key > Create new key**, choose **JSON**, and download it.\n5. Paste the entire contents of that JSON file below, then set the project ID and the region your models are served from.\n6. Add the model ids you want to expose — Vertex model availability depends on your project, region and Model Garden access, so we cannot list them for you.": "Connect a Google Cloud service account to call Vertex AI models inside your own GCP project.\n\n1. Open the [Google Cloud Console](https://console.cloud.google.com) and select the project you want to bill.\n2. Enable the **Vertex AI API** under *APIs & Services > Enabled APIs*.\n3. Go to **IAM & Admin > Service Accounts** and create a service account, granting it the **Vertex AI User** role. Avoid broader roles — follow least-privilege so a leaked key has limited blast radius.\n4. Open the service account, go to **Keys > Add Key > Create new key**, choose **JSON**, and download it.\n5. Paste the entire contents of that JSON file below, then set the project ID and the region your models are served from.\n6. Add the model ids you want to expose — Vertex model availability depends on your project, region and Model Garden access, so we cannot list them for you.",
"invalidGcpResourceId": "Use lowercase letters, numbers and hyphens only",
+ "invalidHeaderName": "A header name may only contain letters, numbers and the characters ! # $ % & ' * + - . ^ _ ` | ~",
+ "reservedHeaderName": "Content-Type, Content-Length, Content-Encoding, Transfer-Encoding, Host and Connection are set by the delivery and cannot be added as custom headers",
+ "duplicateHeaderName": "A header name may appear only once, whatever its letter case",
+ "invalidHeaderValue": "A header value can contain only letters, numbers, spaces, tabs and common symbols. Remove any line break or hidden character, such as a zero-width space",
"API style": "API style",
"Chat completions": "Chat completions",
"Responses": "Responses",
diff --git a/packages/web/src/app/routes/platform/sample-data.ts b/packages/web/src/app/routes/platform/sample-data.ts
index c054ea3350f..4da0ee0b909 100644
--- a/packages/web/src/app/routes/platform/sample-data.ts
+++ b/packages/web/src/app/routes/platform/sample-data.ts
@@ -127,6 +127,7 @@ function eventDestinations(): EventDestination[] {
url,
events: SAMPLE_EVENT_NAMES.slice(0, 4),
enabled: true,
+ headers: null,
format,
}));
}
diff --git a/packages/web/test/app/routes/platform/infra/event-destinations/components/event-destination-mutations.test.tsx b/packages/web/test/app/routes/platform/infra/event-destinations/components/event-destination-mutations.test.tsx
index 5064301fe7a..ec885c43777 100644
--- a/packages/web/test/app/routes/platform/infra/event-destinations/components/event-destination-mutations.test.tsx
+++ b/packages/web/test/app/routes/platform/infra/event-destinations/components/event-destination-mutations.test.tsx
@@ -170,6 +170,7 @@ const destination: EventDestination = {
events: [ApplicationEventName.FLOW_CREATED],
url: 'https://old.example.com/hook',
enabled: true,
+ headers: {},
format: EventDestinationFormat.RAW,
};
diff --git a/packages/web/test/app/routes/platform/infra/event-destinations/lib/event-destinations-collection.test.ts b/packages/web/test/app/routes/platform/infra/event-destinations/lib/event-destinations-collection.test.ts
index 2e85f58ecf3..2ebac6e2b24 100644
--- a/packages/web/test/app/routes/platform/infra/event-destinations/lib/event-destinations-collection.test.ts
+++ b/packages/web/test/app/routes/platform/infra/event-destinations/lib/event-destinations-collection.test.ts
@@ -39,6 +39,7 @@ const destination: EventDestination = {
events: [ApplicationEventName.FLOW_CREATED],
url: 'https://old.example.com/hook',
enabled: true,
+ headers: {},
format: EventDestinationFormat.RAW,
};