diff --git a/plugins/webhooks/server/tasks/DeliverWebhookTask.test.ts b/plugins/webhooks/server/tasks/DeliverWebhookTask.test.ts index 1c95657913..0dc042a881 100644 --- a/plugins/webhooks/server/tasks/DeliverWebhookTask.test.ts +++ b/plugins/webhooks/server/tasks/DeliverWebhookTask.test.ts @@ -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", diff --git a/plugins/webhooks/server/tasks/DeliverWebhookTask.ts b/plugins/webhooks/server/tasks/DeliverWebhookTask.ts index 007c32466c..f0d1674c92 100644 --- a/plugins/webhooks/server/tasks/DeliverWebhookTask.ts +++ b/plugins/webhooks/server/tasks/DeliverWebhookTask.ts @@ -795,9 +795,12 @@ export default class DeliverWebhookTask extends BaseTask { }); } - /** 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 { 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 { 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) {