SPB Git

Repository Public

webhooks.test.ts 5.9 KB 99 lines typescript

1import assert from "node:assert/strict"2import crypto from "node:crypto"3import { test } from "node:test"4import { EVENT_TYPES, SNIPPET_NODE, SNIPPET_PYTHON, checkWebhookUrl, deliveryTone, eventKey, isEventType, isPrivateV4, isPrivateV6, normalizeHostPort } from "./webhooks"56test("webhooks : catalogue des événements", () => {7  assert.equal(EVENT_TYPES.length, 12)8  assert.equal(new Set(EVENT_TYPES).size, 12)9  assert.ok(isEventType("message.received"))10  assert.ok(!isEventType("webhook.test"))11  assert.equal(eventKey("message.received"), "adm.webhooks.event.message_received")12  assert.equal(deliveryTone("delivered"), "ok")13  assert.equal(deliveryTone("retrying"), "warn")14  assert.equal(deliveryTone("failed"), "danger")15  assert.equal(deliveryTone("pending"), "")16})1718test("webhooks : URL publique https:443 acceptée", () => {19  for (const u of ["https://example.org/hook", "https://example.org:443/hook?x=1", "  https://hooks.example.org/a/b  ", "https://8.8.8.8/h", "https://[2606:4700::1111]/h"]) {20    const c = checkWebhookUrl(u)21    assert.equal(c.ok, true, u)22    assert.equal(c.problem, null, u)23    assert.equal(c.allowlisted, false)24  }25  assert.equal(checkWebhookUrl("https://Example.ORG/hook").hostPort, "example.org:443")26})2728test("webhooks : refus de forme (400 invalid_url côté serveur) → bloquants", () => {29  assert.deepEqual([checkWebhookUrl("").problem, checkWebhookUrl("pas une url").problem, checkWebhookUrl("example.org/hook").problem], ["invalid", "invalid", "invalid"])30  assert.equal(checkWebhookUrl("ftp://example.org/hook").problem, "scheme")31  assert.equal(checkWebhookUrl("javascript:alert(1)").problem, "scheme")32  assert.equal(checkWebhookUrl("https://user:pass@example.org/hook").problem, "credentials")33  assert.equal(checkWebhookUrl("https://example.org/hook?token=spm_abc123").problem, "token")34  for (const u of ["", "ftp://example.org/x", "https://u:p@example.org/", "https://example.org/?k=spx_9"]) {35    const c = checkWebhookUrl(u)36    assert.equal(c.blocking, true, u)37    assert.equal(c.ok, false, u)38  }39})4041test("webhooks : destinations que le serveur refuserait (403) → avertissement, non bloquant", () => {42  const cases: [string, string][] = [43    ["http://example.org/hook", "not_https_443"], ["https://example.org:8443/hook", "not_https_443"], ["http://127.0.0.1:8080/hook", "not_https_443"],44    ["https://10.0.0.1/hook", "private"], ["https://127.0.0.1/hook", "private"], ["https://192.168.2.10/h", "private"], ["https://172.20.1.1/h", "private"],45    ["https://169.254.169.254/latest", "private"], ["https://100.64.0.1/h", "private"], ["https://localhost/h", "private"], ["https://api.localhost/h", "private"],46    ["https://[::1]/h", "private"], ["https://[fe80::1]/h", "private"], ["https://[fd00::1]/h", "private"], ["https://[::ffff:127.0.0.1]/h", "private"],47  ]48  for (const [u, problem] of cases) {49    const c = checkWebhookUrl(u)50    assert.equal(c.problem, problem, u)51    assert.equal(c.blocking, false, u)52    assert.equal(c.ok, true, u)53  }54})5556test("webhooks : la liste des destinations internes est la seule exception (hôte:port exact)", () => {57  const list = ["127.0.0.1:8080", "internal.lan:443"]58  const ok = checkWebhookUrl("http://127.0.0.1:8080/hook", list)59  assert.deepEqual([ok.ok, ok.problem, ok.allowlisted, ok.hostPort], [true, null, true, "127.0.0.1:8080"])60  assert.equal(checkWebhookUrl("https://INTERNAL.lan/hook", list).allowlisted, true)61  // autre port, autre hôte : pas d'exception62  assert.equal(checkWebhookUrl("http://127.0.0.1:8081/hook", list).problem, "not_https_443")63  assert.equal(checkWebhookUrl("https://127.0.0.1/hook", list).problem, "private")64  // les identifiants et les jetons restent refusés même pour un hôte permis65  assert.equal(checkWebhookUrl("http://u:p@127.0.0.1:8080/hook", list).problem, "credentials")66})6768test("webhooks : adresses privées littérales", () => {69  for (const h of ["10.1.2.3", "127.0.0.1", "0.0.0.0", "172.16.0.1", "172.31.255.255", "192.168.0.1", "100.127.0.1", "224.0.0.1", "255.255.255.255"]) assert.ok(isPrivateV4(h), h)70  for (const h of ["8.8.8.8", "172.32.0.1", "100.128.0.1", "51.161.112.61", "example.org", "999.1.1.1"]) assert.ok(!isPrivateV4(h), h)71  for (const h of ["::1", "::", "fe80::1", "fc00::1", "fdab::2", "ff02::1", "::ffff:10.0.0.1", "::ffff:7f00:1"]) assert.ok(isPrivateV6(h), h)72  for (const h of ["2606:4700::1111", "::ffff:8.8.8.8", "example.org"]) assert.ok(!isPrivateV6(h), h)73})7475test("webhooks : entrée hôte:port de la liste", () => {76  assert.equal(normalizeHostPort(" 127.0.0.1:8080 "), "127.0.0.1:8080")77  assert.equal(normalizeHostPort("Internal.LAN:443"), "internal.lan:443")78  assert.equal(normalizeHostPort("[::1]:9000"), "::1:9000")79  for (const bad of ["", "127.0.0.1", "http://127.0.0.1:80", "host:", "host:0", "host:70000", "host:80/path", "ho st:80", "::1:9000"]) assert.equal(normalizeHostPort(bad), null, bad)80})8182test("webhooks : l'exemple Node vérifie bien une signature produite comme le serveur", async () => {83  // exécute l'exemple tel qu'il est affiché (sans la ligne d'import ni les commentaires express)84  const body = SNIPPET_NODE.replace(/^import .*$/m, "")85  const verify = new Function("crypto", "Buffer", `${body}\nreturn verify`)(crypto, Buffer) as (secret: string, header: string | undefined, raw: string, tol?: number) => boolean86  const secret = "whsec_test"87  const raw = JSON.stringify({ id: "evt_1", type: "webhook.test" })88  const t = Math.floor(Date.now() / 1000)89  const sig = (ts: number, s = secret) => `t=${ts},v1=${crypto.createHmac("sha256", s).update(`${ts}.${raw}`).digest("hex")}`90  assert.equal(verify(secret, sig(t), raw), true)91  assert.equal(verify(secret, sig(t), `${raw} `), false)92  assert.equal(verify("whsec_autre", sig(t), raw), false)93  assert.equal(verify(secret, sig(t - 3600), raw), false)94  assert.equal(verify(secret, undefined, raw), false)95  assert.equal(verify(secret, "t=abc,v1=00", raw), false)96  assert.match(SNIPPET_PYTHON, /hmac\.compare_digest/)97  assert.match(SNIPPET_PYTHON, /t=\(\\d\+\),v1=/)98})99
Files main
Menu

Theme