|
1 | | -import { createHmac, generateKeyPairSync, sign as cryptoSign, type KeyObject } from "node:crypto"; |
| 1 | +import { |
| 2 | + createHash, |
| 3 | + createHmac, |
| 4 | + generateKeyPairSync, |
| 5 | + sign as cryptoSign, |
| 6 | + type KeyObject, |
| 7 | +} from "node:crypto"; |
2 | 8 | import { WebhookVerifierConfig } from "@trigger.dev/core/v3"; |
3 | 9 | import { webhookProviderConfigs } from "@trigger.dev/core/webhooks"; |
4 | 10 | import { describe, expect, it } from "vitest"; |
@@ -471,6 +477,77 @@ describe("asymmetric verifier (config-driven)", () => { |
471 | 477 | ); |
472 | 478 | expect(result.ok).toBe(true); |
473 | 479 | }); |
| 480 | + |
| 481 | + it("verifies a Bridge-style RSA signature over a SHA-256 digest with prehash", () => { |
| 482 | + const { publicKey, privateKey } = generateKeyPairSync("rsa", { modulusLength: 2048 }); |
| 483 | + const pem = publicKey.export({ type: "spki", format: "pem" }).toString(); |
| 484 | + const ts = String(NOW_MS); |
| 485 | + const body = JSON.stringify({ event_type: "customer.updated" }); |
| 486 | + const digest = createHash("sha256").update(`${ts}.${body}`).digest(); |
| 487 | + const sig = cryptoSign("sha256", digest, privateKey).toString("base64"); |
| 488 | + const config = { |
| 489 | + scheme: "asymmetric", |
| 490 | + algorithm: "rsa-sha256", |
| 491 | + encoding: "base64", |
| 492 | + signatureHeader: "x-webhook-signature", |
| 493 | + signature: { itemSeparator: ",", fieldSeparator: "=", field: "v0" }, |
| 494 | + timestamp: { |
| 495 | + source: { from: "signatureField", field: "t" }, |
| 496 | + unit: "milliseconds", |
| 497 | + toleranceSeconds: 600, |
| 498 | + }, |
| 499 | + signingString: { template: "{timestamp}.{body}" }, |
| 500 | + prehash: "sha256", |
| 501 | + }; |
| 502 | + const request = (cfgOverrides: Record<string, unknown>, rawBody: string) => |
| 503 | + verify( |
| 504 | + cfg({ ...config, ...cfgOverrides }), |
| 505 | + input({ |
| 506 | + rawBytes: bytes(rawBody), |
| 507 | + secret: pem, |
| 508 | + headers: { "x-webhook-signature": `t=${ts},v0=${sig}` }, |
| 509 | + }) |
| 510 | + ); |
| 511 | + |
| 512 | + expect(request({}, body).ok).toBe(true); |
| 513 | + expect(request({ prehash: undefined }, body).ok).toBe(false); |
| 514 | + expect(request({}, '{"event_type":"customer.deleted"}').ok).toBe(false); |
| 515 | + }); |
| 516 | + |
| 517 | + it("verifies an ECDSA P-384 / SHA-384 signature in DER and raw r||s form", () => { |
| 518 | + const { publicKey, privateKey } = generateKeyPairSync("ec", { namedCurve: "P-384" }); |
| 519 | + const pem = publicKey.export({ type: "spki", format: "pem" }).toString(); |
| 520 | + const body = JSON.stringify({ id: "evt_1" }); |
| 521 | + const config = cfg({ |
| 522 | + scheme: "asymmetric", |
| 523 | + algorithm: "ecdsa-p384", |
| 524 | + encoding: "base64", |
| 525 | + signatureHeader: "webhook-signature", |
| 526 | + signature: {}, |
| 527 | + signingString: "raw", |
| 528 | + }); |
| 529 | + const withSig = (sig: Buffer, rawBody = body) => |
| 530 | + verify( |
| 531 | + config, |
| 532 | + input({ |
| 533 | + rawBytes: bytes(rawBody), |
| 534 | + secret: pem, |
| 535 | + headers: { "webhook-signature": sig.toString("base64") }, |
| 536 | + }) |
| 537 | + ); |
| 538 | + |
| 539 | + const der = cryptoSign("sha384", Buffer.from(body), privateKey); |
| 540 | + const p1363 = cryptoSign("sha384", Buffer.from(body), { |
| 541 | + key: privateKey, |
| 542 | + dsaEncoding: "ieee-p1363", |
| 543 | + }); |
| 544 | + expect(withSig(der).ok).toBe(true); |
| 545 | + expect(withSig(p1363).ok).toBe(true); |
| 546 | + expect(withSig(der, '{"id":"evt_2"}').ok).toBe(false); |
| 547 | + |
| 548 | + const sha256Sig = cryptoSign("sha256", Buffer.from(body), privateKey); |
| 549 | + expect(withSig(sha256Sig).ok).toBe(false); |
| 550 | + }); |
474 | 551 | }); |
475 | 552 |
|
476 | 553 | describe("adversarial / fail-closed", () => { |
|
0 commit comments