fix: Improve webhook delivery response handling (#14541)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-12 09:50:35 -05:00
1 parent f8446a3c78
commit b3555dddfe
2 files changed
+66 -2

No files matched your search

@@ -137,6 +137,67 @@ describe("DeliverWebhookTask", () => {
expect(delivery.responseBody).toEqual("SUCCESS");
});
it("should truncate stored response bodies", async () => {
const subscription = await buildWebhookSubscription({
url: "http://example.com",
events: ["*"],
});
const signedInUser = await buildUser({ teamId: subscription.teamId });
captureWebhook(
"http://example.com",
() => new HttpResponse("a".repeat(5000), { status: 200 })
);
await new DeliverWebhookTask().perform({
subscriptionId: subscription.id,
event: {
name: "users.signin",
userId: signedInUser.id,
teamId: subscription.teamId,
actorId: signedInUser.id,
ip,
},
});
const delivery = await WebhookDelivery.findOne({
where: { webhookSubscriptionId: subscription.id },
rejectOnEmpty: true,
});
expect(delivery.status).toBe("success");
expect(delivery.responseBody).toEqual("a".repeat(1024));
});
it("should not read response bodies over the size limit", async () => {
const subscription = await buildWebhookSubscription({
url: "http://example.com",
events: ["*"],
});
const signedInUser = await buildUser({ teamId: subscription.teamId });
captureWebhook(
"http://example.com",
() => new HttpResponse("a".repeat(128 * 1024), { status: 200 })
);
await new DeliverWebhookTask().perform({
subscriptionId: subscription.id,
event: {
name: "users.signin",
userId: signedInUser.id,
teamId: subscription.teamId,
actorId: signedInUser.id,
ip,
},
});
const delivery = await WebhookDelivery.findOne({
where: { webhookSubscriptionId: subscription.id },
rejectOnEmpty: true,
});
expect(delivery.status).toBe("success");
expect(delivery.statusCode).toBe(200);
expect(delivery.responseBody).toEqual("");
});
it("should hit the subscription url with signature header", async () => {
const subscription = await buildWebhookSubscription({
url: "http://example.com",
@@ -795,9 +795,12 @@ export default class DeliverWebhookTask extends BaseTask<Props> {
});
}
/** Maximum number of bytes to read from webhook response bodies. */
/** Maximum number of bytes to store from webhook response bodies. */
private static readonly MAX_RESPONSE_BODY_SIZE = 1024;
/** Maximum number of bytes to read from webhook response bodies. */
private static readonly MAX_RESPONSE_READ_SIZE = 64 * 1024;
private async sendWebhook({
event,
subscription,
@@ -838,6 +841,7 @@ export default class DeliverWebhookTask extends BaseTask<Props> {
body: JSON.stringify(requestBody),
redirect: "error",
timeout: 5000,
size: DeliverWebhookTask.MAX_RESPONSE_READ_SIZE,
});
status = response.ok ? "success" : "failed";
} catch (err) {
@@ -858,7 +862,6 @@ export default class DeliverWebhookTask extends BaseTask<Props> {
if (response) {
try {
// TODO: Use stream to avoid buffering large responses in memory.
const text = await response.text();
responseBody = text.slice(0, DeliverWebhookTask.MAX_RESPONSE_BODY_SIZE);
} catch (err) {