@cstar.help/js 0.14.0 → 0.15.0
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.
- package/dist/auth/index.cjs +73 -11
- package/dist/auth/index.d.cts +121 -1
- package/dist/auth/index.d.ts +121 -1
- package/dist/auth/index.js +59 -10
- package/dist/chat/index.cjs +30 -8
- package/dist/chat/index.d.cts +18 -15
- package/dist/chat/index.d.ts +18 -15
- package/dist/chat/index.js +30 -8
- package/dist/community/index.cjs +73 -11
- package/dist/community/index.d.cts +121 -1
- package/dist/community/index.d.ts +121 -1
- package/dist/community/index.js +59 -10
- package/dist/index.cjs +35 -11
- package/dist/index.d.cts +58 -26
- package/dist/index.d.ts +58 -26
- package/dist/index.js +35 -11
- package/dist/library/index.cjs +73 -11
- package/dist/library/index.d.cts +121 -1
- package/dist/library/index.d.ts +121 -1
- package/dist/library/index.js +59 -10
- package/dist/quickhelp/index.cjs +134 -11
- package/dist/quickhelp/index.d.cts +121 -1
- package/dist/quickhelp/index.d.ts +121 -1
- package/dist/quickhelp/index.js +120 -10
- package/dist/webhook/index.cjs +64 -8
- package/dist/webhook/index.d.cts +27 -3
- package/dist/webhook/index.d.ts +27 -3
- package/dist/webhook/index.js +62 -8
- package/dist/webhook/node.cjs +53 -5
- package/dist/webhook/node.d.cts +27 -4
- package/dist/webhook/node.d.ts +27 -4
- package/dist/webhook/node.js +51 -5
- package/package.json +1 -1
package/dist/webhook/node.d.ts
CHANGED
|
@@ -412,11 +412,28 @@ interface WebhookEvent<T extends WebhookEventType = WebhookEventType> {
|
|
|
412
412
|
data: WebhookEventDataMap[T];
|
|
413
413
|
}
|
|
414
414
|
|
|
415
|
+
/** Default replay window for Stripe-style signatures: 5 minutes (300s). */
|
|
416
|
+
declare const DEFAULT_TOLERANCE_SECONDS = 300;
|
|
417
|
+
/** Options for signature verification — accepted by both verify and construct calls. */
|
|
418
|
+
interface VerifyOptions {
|
|
419
|
+
/**
|
|
420
|
+
* Maximum age (in seconds) of a Stripe-style timestamp before the signature
|
|
421
|
+
* is rejected as expired. Defaults to {@link DEFAULT_TOLERANCE_SECONDS}.
|
|
422
|
+
* Ignored for legacy `sha256=<hex>` signatures (which carry no timestamp).
|
|
423
|
+
*/
|
|
424
|
+
tolerance?: number;
|
|
425
|
+
}
|
|
415
426
|
/** Thrown when webhook signature verification fails. */
|
|
416
427
|
declare class WebhookSignatureVerificationError extends Error {
|
|
417
428
|
constructor(message: string);
|
|
418
429
|
}
|
|
419
430
|
|
|
431
|
+
/**
|
|
432
|
+
* Sign a payload in Stripe-style format `t=<unix>,v1=<hex>` (Node-only,
|
|
433
|
+
* synchronous). Used by the cStar server-side webhook dispatcher and
|
|
434
|
+
* `cstar listen --forward-to`. The HMAC is computed over `${ts}.${rawBody}`.
|
|
435
|
+
*/
|
|
436
|
+
declare function signPayloadStripe(rawBody: string, secret: string): string;
|
|
420
437
|
/**
|
|
421
438
|
* Verify a webhook signature and parse the event (synchronous, Node.js only).
|
|
422
439
|
*
|
|
@@ -427,14 +444,20 @@ declare class WebhookSignatureVerificationError extends Error {
|
|
|
427
444
|
* @param rawBody - Raw request body as a string (NOT parsed JSON)
|
|
428
445
|
* @param signature - Value of the `X-Signature` header
|
|
429
446
|
* @param secret - Your webhook endpoint's secret (`whsec_*`)
|
|
447
|
+
* @param options - Optional verifier options (e.g. replay tolerance)
|
|
430
448
|
* @returns Parsed and verified webhook event
|
|
431
449
|
* @throws {WebhookSignatureVerificationError} If verification fails
|
|
432
450
|
*/
|
|
433
|
-
declare function constructEvent(rawBody: string, signature: string, secret: string): WebhookEvent;
|
|
451
|
+
declare function constructEvent(rawBody: string, signature: string, secret: string, options?: VerifyOptions): WebhookEvent;
|
|
434
452
|
/**
|
|
435
453
|
* Verify an HMAC-SHA256 signature (synchronous, Node.js only).
|
|
436
|
-
*
|
|
454
|
+
*
|
|
455
|
+
* Accepts both the canonical Stripe-style format `t=<unix>,v1=<hex>` (signed
|
|
456
|
+
* over `${ts}.${rawBody}`, with replay-window enforcement) and the legacy
|
|
457
|
+
* `sha256=<hex>` format (signed over `rawBody`, no timestamp). Use
|
|
458
|
+
* `options.tolerance` to override the default 300s replay window. Uses
|
|
459
|
+
* timing-safe comparison.
|
|
437
460
|
*/
|
|
438
|
-
declare function verifySignature(rawBody: string, signature: string, secret: string): boolean;
|
|
461
|
+
declare function verifySignature(rawBody: string, signature: string, secret: string, options?: VerifyOptions): boolean;
|
|
439
462
|
|
|
440
|
-
export { type WebhookEvent, WebhookSignatureVerificationError, constructEvent, verifySignature };
|
|
463
|
+
export { DEFAULT_TOLERANCE_SECONDS, type VerifyOptions, type WebhookEvent, WebhookSignatureVerificationError, constructEvent, signPayloadStripe, verifySignature };
|
package/dist/webhook/node.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { createHmac, timingSafeEqual } from "crypto";
|
|
3
3
|
|
|
4
4
|
// src/webhook/index.ts
|
|
5
|
+
var DEFAULT_TOLERANCE_SECONDS = 300;
|
|
5
6
|
var WebhookSignatureVerificationError = class extends Error {
|
|
6
7
|
constructor(message) {
|
|
7
8
|
super(message);
|
|
@@ -10,21 +11,66 @@ var WebhookSignatureVerificationError = class extends Error {
|
|
|
10
11
|
};
|
|
11
12
|
|
|
12
13
|
// src/webhook/node.ts
|
|
13
|
-
function
|
|
14
|
-
if (!
|
|
14
|
+
function parseStripeHeader(header) {
|
|
15
|
+
if (!header || header.startsWith("sha256=")) return null;
|
|
16
|
+
let timestamp = null;
|
|
17
|
+
const v1 = [];
|
|
18
|
+
for (const part of header.split(",")) {
|
|
19
|
+
const eq = part.indexOf("=");
|
|
20
|
+
if (eq === -1) continue;
|
|
21
|
+
const key = part.slice(0, eq).trim();
|
|
22
|
+
const value = part.slice(eq + 1).trim();
|
|
23
|
+
if (key === "t") {
|
|
24
|
+
const parsed = Number.parseInt(value, 10);
|
|
25
|
+
if (Number.isFinite(parsed)) timestamp = parsed;
|
|
26
|
+
} else if (key === "v1") {
|
|
27
|
+
v1.push(value);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (timestamp === null || v1.length === 0) return null;
|
|
31
|
+
return { timestamp, v1 };
|
|
32
|
+
}
|
|
33
|
+
function constantTimeEqualHex(a, b) {
|
|
34
|
+
if (a.length !== b.length) return false;
|
|
35
|
+
try {
|
|
36
|
+
return timingSafeEqual(Buffer.from(a), Buffer.from(b));
|
|
37
|
+
} catch {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function signPayloadStripe(rawBody, secret) {
|
|
42
|
+
const ts = Math.floor(Date.now() / 1e3);
|
|
43
|
+
const sig = createHmac("sha256", secret).update(`${ts}.${rawBody}`).digest("hex");
|
|
44
|
+
return `t=${ts},v1=${sig}`;
|
|
45
|
+
}
|
|
46
|
+
function constructEvent(rawBody, signature, secret, options) {
|
|
47
|
+
if (!verifySignature(rawBody, signature, secret, options)) {
|
|
15
48
|
throw new WebhookSignatureVerificationError(
|
|
16
49
|
"Webhook signature verification failed. Ensure you are using the raw request body and the correct endpoint secret."
|
|
17
50
|
);
|
|
18
51
|
}
|
|
19
52
|
return JSON.parse(rawBody);
|
|
20
53
|
}
|
|
21
|
-
function verifySignature(rawBody, signature, secret) {
|
|
54
|
+
function verifySignature(rawBody, signature, secret, options) {
|
|
55
|
+
if (typeof signature !== "string" || signature.length === 0) return false;
|
|
56
|
+
const stripe = parseStripeHeader(signature);
|
|
57
|
+
if (stripe) {
|
|
58
|
+
const tolerance = options?.tolerance ?? DEFAULT_TOLERANCE_SECONDS;
|
|
59
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
60
|
+
if (Math.abs(now - stripe.timestamp) > tolerance) return false;
|
|
61
|
+
const expected2 = createHmac("sha256", secret).update(`${stripe.timestamp}.${rawBody}`).digest("hex");
|
|
62
|
+
for (const candidate of stripe.v1) {
|
|
63
|
+
if (constantTimeEqualHex(candidate, expected2)) return true;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
22
67
|
const expected = "sha256=" + createHmac("sha256", secret).update(rawBody).digest("hex");
|
|
23
|
-
|
|
24
|
-
return timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
|
|
68
|
+
return constantTimeEqualHex(signature, expected);
|
|
25
69
|
}
|
|
26
70
|
export {
|
|
71
|
+
DEFAULT_TOLERANCE_SECONDS,
|
|
27
72
|
WebhookSignatureVerificationError,
|
|
28
73
|
constructEvent,
|
|
74
|
+
signPayloadStripe,
|
|
29
75
|
verifySignature
|
|
30
76
|
};
|