Initial import of streamline
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
import { test, expect, describe } from "bun:test";
|
||||
import { createHmac } from "node:crypto";
|
||||
import {
|
||||
RETRY_DELAYS_MS, SIGNATURE_TOLERANCE_S, WEBHOOK_SECRET_PREFIX, buildStatusEvent, eventMatches, isForbiddenAddress,
|
||||
newWebhookSecret, nextAttemptAt, normalizeEvents, signatureHeader, validateWebhookUrl, verifySignatureHeader,
|
||||
} from "../src/openplatform_webhooks_core";
|
||||
|
||||
describe("signature", () => {
|
||||
const secret = newWebhookSecret();
|
||||
const body = JSON.stringify({ id: "bug:a:fixing:1", event: "task.status_changed" });
|
||||
const t = 1_800_000_000;
|
||||
|
||||
test("secret shape", () => {
|
||||
expect(secret.startsWith(WEBHOOK_SECRET_PREFIX)).toBe(true);
|
||||
expect(newWebhookSecret()).not.toBe(secret);
|
||||
});
|
||||
|
||||
test("header is t=<sec>,v1=<hex hmac of '<t>.<body>'> — the contract the docs publish", () => {
|
||||
const want = createHmac("sha256", secret).update(`${t}.${body}`).digest("hex");
|
||||
expect(signatureHeader(secret, body, t)).toBe(`t=${t},v1=${want}`);
|
||||
});
|
||||
|
||||
test("round-trips", () => {
|
||||
expect(verifySignatureHeader(secret, body, signatureHeader(secret, body, t), t)).toBe(true);
|
||||
});
|
||||
|
||||
test("rejects tampered body, wrong secret, and stale timestamp", () => {
|
||||
const h = signatureHeader(secret, body, t);
|
||||
expect(verifySignatureHeader(secret, body + " ", h, t)).toBe(false);
|
||||
expect(verifySignatureHeader(newWebhookSecret(), body, h, t)).toBe(false);
|
||||
expect(verifySignatureHeader(secret, body, h, t + SIGNATURE_TOLERANCE_S + 1)).toBe(false);
|
||||
expect(verifySignatureHeader(secret, body, h, t + SIGNATURE_TOLERANCE_S)).toBe(true);
|
||||
});
|
||||
|
||||
test("accepts any of several v1 (rotation window) and rejects garbage", () => {
|
||||
const good = signatureHeader(secret, body, t).split(",")[1];
|
||||
expect(verifySignatureHeader(secret, body, `t=${t},v1=${"0".repeat(64)},${good}`, t)).toBe(true);
|
||||
expect(verifySignatureHeader(secret, body, "", t)).toBe(false);
|
||||
expect(verifySignatureHeader(secret, body, `v1=${good.slice(3)}`, t)).toBe(false);
|
||||
expect(verifySignatureHeader(secret, body, `t=${t},v1=zz`, t)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("retry schedule", () => {
|
||||
test("one delay per failure, then give up", () => {
|
||||
const now = 1000;
|
||||
RETRY_DELAYS_MS.forEach((d, i) => expect(nextAttemptAt(i + 1, now)).toBe(now + d));
|
||||
expect(nextAttemptAt(RETRY_DELAYS_MS.length + 1, now)).toBeNull();
|
||||
});
|
||||
test("whole schedule spans roughly 20 hours (the auto-disable threshold the docs quote)", () => {
|
||||
const total = RETRY_DELAYS_MS.reduce((a, b) => a + b, 0);
|
||||
expect(total / 3_600_000).toBeGreaterThan(20);
|
||||
expect(total / 3_600_000).toBeLessThan(21);
|
||||
});
|
||||
});
|
||||
|
||||
describe("events filter", () => {
|
||||
test("absent / empty means all", () => {
|
||||
expect(normalizeEvents(undefined)).toEqual({ ok: true, events: null });
|
||||
expect(normalizeEvents(null)).toEqual({ ok: true, events: null });
|
||||
expect(normalizeEvents([])).toEqual({ ok: true, events: null });
|
||||
});
|
||||
test("dedupes, rejects unknown and non-string", () => {
|
||||
expect(normalizeEvents(["fix_succeeded", "fix_succeeded", " fix_failed "])).toEqual({ ok: true, events: ["fix_succeeded", "fix_failed"] });
|
||||
expect(normalizeEvents(["implementing"]).ok).toBe(false); // feature status — not subscribable in v1
|
||||
expect(normalizeEvents([1]).ok).toBe(false);
|
||||
expect(normalizeEvents("fixing").ok).toBe(false);
|
||||
});
|
||||
test("matching", () => {
|
||||
expect(eventMatches(null, "fixing")).toBe(true);
|
||||
expect(eventMatches(["fix_succeeded"], "fixing")).toBe(false);
|
||||
expect(eventMatches(["fix_succeeded"], "fix_succeeded")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("url + address guard", () => {
|
||||
test("url shape", () => {
|
||||
expect(validateWebhookUrl("https://example.com/hook").ok).toBe(true);
|
||||
expect(validateWebhookUrl("http://10.251.240.103:8000/webhook").ok).toBe(true);
|
||||
expect(validateWebhookUrl("ftp://x/").ok).toBe(false);
|
||||
expect(validateWebhookUrl("https://u:p@example.com/").ok).toBe(false);
|
||||
expect(validateWebhookUrl("").ok).toBe(false);
|
||||
expect(validateWebhookUrl("not a url").ok).toBe(false);
|
||||
});
|
||||
test("blocks loopback, unspecified, link-local, metadata, multicast — v4, v6, and v4-mapped", () => {
|
||||
for (const ip of ["127.0.0.1", "127.9.9.9", "0.0.0.0", "169.254.169.254", "100.100.100.200", "224.0.0.1", "255.255.255.255",
|
||||
"::1", "::", "fe80::1", "ff02::1", "::ffff:127.0.0.1", "::ffff:169.254.169.254", "not-an-ip"]) {
|
||||
expect(isForbiddenAddress(ip)).toBe(true);
|
||||
}
|
||||
});
|
||||
test("leaves corporate private ranges alone — legitimate receivers live there", () => {
|
||||
for (const ip of ["10.251.240.103", "172.16.0.1", "192.168.1.1", "8.8.8.8", "fdbd:dc01:ff:31a::1", "2001:db8::1", "::ffff:10.0.0.1"]) {
|
||||
expect(isForbiddenAddress(ip)).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("status event", () => {
|
||||
test("shape", () => {
|
||||
const e = buildStatusEvent({
|
||||
eventId: "bug:t1:fixing:42",
|
||||
task: { id: "t1", title: "支付回调偶发超时", externalId: "CRM-1", meegoId: null, createdViaSource: "crm" },
|
||||
prev: "awaiting_fix", next: "fixing", at: 42,
|
||||
labels: { awaiting_fix: "待修复", fixing: "修复中" },
|
||||
appBaseUrl: "https://streamline.bytedance.net",
|
||||
});
|
||||
expect(e).toEqual({
|
||||
id: "bug:t1:fixing:42", event: "task.status_changed", kind: "bug", taskId: "t1", externalId: "CRM-1",
|
||||
meegoId: null, source: "crm", title: "支付回调偶发超时", prevStatus: "awaiting_fix", prevStatusLabel: "待修复",
|
||||
status: "fixing", statusLabel: "修复中", at: 42, panelUrl: "https://streamline.bytedance.net?task_id=t1",
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user