@smartcrab/server 0.8.0 → 0.9.0-next-20261203092527

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,96 +0,0 @@
1
- import { describe, expect, test } from "vitest";
2
- import { verifyWebhook } from "./index.js";
3
-
4
- const SECRET = "whsec_docs_vector_fixed_secret";
5
- const TIMESTAMP_SECONDS = 1_700_000_000;
6
- const RAW_BODY =
7
- '{"eventId":"evt_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","occurredAt":"2023-11-14T22:13:20.000Z","payload":{"maskedEmail":"u***@e***","method":"email_otp","userId":"usr_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},"trace":{"requestId":"req_docs_vector"},"type":"user.created","version":1,"workspaceId":"wsp_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}';
8
- const HEADERS = {
9
- "X-Auth-Event-Id": "evt_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
10
- "X-Auth-Timestamp": "1700000000",
11
- "X-Auth-Signature": "v1=8656ce0fa8263cc2c29776b29cff567c276d9d64faa04cd99cb26721e18ded96",
12
- "content-type": "application/json",
13
- };
14
-
15
- const verifyAt = (input: {
16
- readonly headers?: Headers | Readonly<Record<string, string>>;
17
- readonly rawBody?: string;
18
- readonly secret?: string | readonly string[];
19
- readonly nowSeconds?: number;
20
- }) =>
21
- verifyWebhook({
22
- headers: input.headers ?? HEADERS,
23
- rawBody: input.rawBody ?? RAW_BODY,
24
- secret: input.secret ?? SECRET,
25
- clock: () => input.nowSeconds ?? TIMESTAMP_SECONDS,
26
- });
27
-
28
- describe("verifyWebhook", () => {
29
- test("verifies the hard-coded request produced by buildDeliveryRequest", async () => {
30
- const result = await verifyAt({ headers: new Headers(HEADERS) });
31
-
32
- expect(result).toEqual({
33
- type: "Success",
34
- value: {
35
- eventId: "evt_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
36
- timestampSeconds: TIMESTAMP_SECONDS,
37
- },
38
- });
39
- });
40
-
41
- test("accepts case-insensitive header names in a plain record", async () => {
42
- const headers = Object.fromEntries(
43
- Object.entries(HEADERS).map(([key, value]) => [key.toLowerCase(), value]),
44
- );
45
- const result = await verifyAt({ headers });
46
-
47
- expect(result.type).toBe("Success");
48
- });
49
-
50
- test("rejects a changed raw body and a wrong secret", async () => {
51
- const tampered = await verifyAt({ rawBody: `${RAW_BODY} ` });
52
- const wrongSecret = await verifyAt({ secret: "whsec_wrong" });
53
-
54
- expect(tampered).toEqual({ type: "Failure", error: { type: "signature_mismatch" } });
55
- expect(wrongSecret).toEqual({ type: "Failure", error: { type: "signature_mismatch" } });
56
- });
57
-
58
- test("rejects timestamps more than the default tolerance behind or ahead", async () => {
59
- const stale = await verifyAt({ nowSeconds: TIMESTAMP_SECONDS + 301 });
60
- const future = await verifyAt({ nowSeconds: TIMESTAMP_SECONDS - 301 });
61
-
62
- expect(stale).toEqual({
63
- type: "Failure",
64
- error: { type: "timestamp_too_old", ageSeconds: 301, toleranceSeconds: 300 },
65
- });
66
- expect(future).toEqual({
67
- type: "Failure",
68
- error: { type: "timestamp_in_future", skewSeconds: 301, toleranceSeconds: 300 },
69
- });
70
- });
71
-
72
- test("accepts both exact tolerance boundaries", async () => {
73
- const oldestAccepted = await verifyAt({ nowSeconds: TIMESTAMP_SECONDS + 300 });
74
- const furthestFutureAccepted = await verifyAt({ nowSeconds: TIMESTAMP_SECONDS - 300 });
75
-
76
- expect(oldestAccepted.type).toBe("Success");
77
- expect(furthestFutureAccepted.type).toBe("Success");
78
- });
79
-
80
- test("reports each required header when absent", async () => {
81
- for (const header of ["X-Auth-Event-Id", "X-Auth-Timestamp", "X-Auth-Signature"]) {
82
- const headers = Object.fromEntries(Object.entries(HEADERS).filter(([key]) => key !== header));
83
- const result = await verifyAt({ headers });
84
-
85
- expect(result).toEqual({ type: "Failure", error: { type: "missing_header", header } });
86
- }
87
- });
88
-
89
- test("tries each supplied secret during rotation", async () => {
90
- const result = await verifyAt({ secret: ["whsec_previous", SECRET] });
91
- const noMatch = await verifyAt({ secret: ["whsec_previous", "whsec_other"] });
92
-
93
- expect(result.type).toBe("Success");
94
- expect(noMatch).toEqual({ type: "Failure", error: { type: "signature_mismatch" } });
95
- });
96
- });