Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(automations): preserve request headers when regenerating a webhook secret (#16315)

Steffen Schmitz59ef87adc5b8
diff --git a/web/src/__tests__/server/automations-trpc.servertest.ts b/web/src/__tests__/server/automations-trpc.servertest.ts
index 247dd87acd..06b8330c97 100644
--- a/web/src/__tests__/server/automations-trpc.servertest.ts
+++ b/web/src/__tests__/server/automations-trpc.servertest.ts
@@ -2394,6 +2394,59 @@ describe("automations trpc", () => {
expect(decryptedStoredSecret).toBe(response.webhookSecret);
});
+ it("should preserve custom request headers when regenerating the secret", async () => {
+ const { project, caller } = await prepare();
+
+ const { secretKey, displaySecretKey } = generateWebhookSecret();
+ const encryptedAuthHeader = encrypt("Bearer super-secret-token");
+ const action = await prisma.action.create({
+ data: {
+ id: v4(),
+ projectId: project.id,
+ type: "WEBHOOK",
+ config: {
+ type: "WEBHOOK",
+ url: "https://example.com/webhook",
+ headers: { "X-Legacy": "legacy-value" },
+ requestHeaders: {
+ "Content-Type": { secret: false, value: "application/json" },
+ Authorization: { secret: true, value: encryptedAuthHeader },
+ },
+ displayHeaders: {
+ "X-Legacy": { secret: false, value: "legacy-value" },
+ "Content-Type": { secret: false, value: "application/json" },
+ Authorization: { secret: true, value: "Bear...oken" },
+ },
+ apiVersion: { prompt: "v1" },
+ secretKey: encrypt(secretKey),
+ displaySecretKey,
+ },
+ },
+ });
+
+ await caller.automations.regenerateWebhookSecret({
+ projectId: project.id,
+ actionId: action.id,
+ });
+
+ const updatedAction = await prisma.action.findUnique({
+ where: { id: action.id },
+ });
+ const updatedConfig =
+ updatedAction?.config as WebhookActionConfigWithSecrets;
+
+ // Rotating the signing secret must not drop the custom headers, and
+ // secret header values must stay encrypted at rest.
+ expect(updatedConfig.requestHeaders).toEqual({
+ "Content-Type": { secret: false, value: "application/json" },
+ Authorization: { secret: true, value: encryptedAuthHeader },
+ });
+ expect(updatedConfig.headers).toEqual({ "X-Legacy": "legacy-value" });
+ expect(decrypt(updatedConfig.requestHeaders!.Authorization.value)).toBe(
+ "Bearer super-secret-token",
+ );
+ });
+
it("should throw error when action not found", async () => {
const { project, caller } = await prepare();
diff --git a/web/src/features/automations/server/router.ts b/web/src/features/automations/server/router.ts
index 1ffe9ac4b3..42cb8ecf82 100644
--- a/web/src/features/automations/server/router.ts
+++ b/web/src/features/automations/server/router.ts
@@ -6,7 +6,7 @@ import {
ActionType,
JobConfigState,
singleFilter,
- isSafeWebhookActionConfig,
+ isWebhookActionConfig,
TriggerEventSource,
TriggerEventSourceSchema,
ProjectNotificationEventTypeSchema,
@@ -15,7 +15,6 @@ import { throwIfNoProjectAccess } from "@/src/features/rbac/utils/checkProjectAc
import { v4 } from "uuid";
import {
convertActionToDomain,
- getActionById,
getAutomations,
getAutomationById,
getConsecutiveAutomationFailures,
@@ -85,9 +84,11 @@ export const automationsRouter = createTRPCRouter({
scope: "automations:CUD",
});
- const existingAction = await getActionById({
- projectId: input.projectId,
- actionId: input.actionId,
+ // Read the raw row rather than going through getActionById: that returns
+ // the sanitized config (no `headers`/`requestHeaders`), and writing it
+ // back would silently drop the automation's custom request headers.
+ const existingAction = await ctx.prisma.action.findFirst({
+ where: { id: input.actionId, projectId: input.projectId },
});
if (!existingAction || existingAction.type !== "WEBHOOK") {
@@ -97,13 +98,15 @@ export const automationsRouter = createTRPCRouter({
});
}
- if (!isSafeWebhookActionConfig(existingAction.config)) {
+ if (!isWebhookActionConfig(existingAction.config)) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: `Invalid webhook configuration for action ${input.actionId}`,
});
}
+ const existingConfig = existingAction.config;
+
// Generate new webhook secret
const { secretKey: newSecretKey, displaySecretKey: newDisplaySecretKey } =
generateWebhookSecret();
@@ -114,16 +117,17 @@ export const automationsRouter = createTRPCRouter({
resourceId: input.actionId,
action: "update",
before: {
- displaySecretKey: existingAction.config.displaySecretKey,
+ displaySecretKey: existingConfig.displaySecretKey,
},
after: {
displaySecretKey: newDisplaySecretKey,
},
});
- // Update action config with new secret
+ // Keep the rest of the stored config (custom headers included) and only
+ // rotate the signing secret. Header values stay encrypted as stored.
const updatedConfig = {
- ...existingAction.config,
+ ...existingConfig,
secretKey: encrypt(newSecretKey),
displaySecretKey: newDisplaySecretKey,
};