@samva/webhooks 0.36.0 → 0.37.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/effect.js +1 -1
- package/dist/errors.d.ts +5 -5
- package/dist/promise.js +1 -1
- package/dist/{protocol-Cqf7JfeH.js → protocol-IJ8lllqW.js} +11 -60
- package/dist/protocol.d.ts +0 -29
- package/dist/protocol.js +1 -1
- package/package.json +1 -1
package/dist/effect.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as generateWebhookSecret, c as verifyWebhookAt, d as MalformedWebhookPayloadError, f as WebhookCryptoError, g as isKnownEvent, h as WebhookVerificationError, i as WEBHOOK_TIMESTAMP_HEADER, l as InvalidWebhookHeadersError, m as WebhookTimestampError, n as WEBHOOK_ID_HEADER, p as WebhookSignatureError, r as WEBHOOK_SIGNATURE_HEADER, s as signWebhook, t as DEFAULT_TOLERANCE_SECONDS, u as InvalidWebhookSecretError } from "./protocol-IJ8lllqW.js";
|
|
2
2
|
import { t as WebhookEventJsonSchemas } from "./json-schemas.gen-C5nX_b5f.js";
|
|
3
3
|
import { ContactConsentChangedEventSchema, ContactCreatedEventSchema, ContactDeletedEventSchema, ContactResubscribedEventSchema, ContactSubscribedEventSchema, ContactUnsubscribedEventSchema, ContactUpdatedEventSchema, ConversationArchivedEventSchema, ConversationCreatedEventSchema, ConversationUpdatedEventSchema, DomainUpdatedEventSchema, DomainWarmingUpdatedEventSchema, EmailSendingPausedEventSchema, EmailSendingResumedEventSchema, FlowRegistrationUpdatedEventSchema, JourneyWebhookEventSchema, MailboxActionPendingApprovalEventSchema, MailboxActionResolvedEventSchema, MailboxMessageDeliveredEventSchema, MailboxMessageDiscardedEventSchema, MailboxMessageFailedEventSchema, MailboxMessageQuarantinedEventSchema, MailboxMessageReadEventSchema, MailboxMessageReceivedEventSchema, MailboxMessageReleasedEventSchema, MailboxMessageSentEventSchema, MessageBouncedEventSchema, MessageClickedEventSchema, MessageComplainedEventSchema, MessageDeliveredEventSchema, MessageFailedEventSchema, MessageOpenedEventSchema, MessageReadEventSchema, MessageReceivedEventSchema, MessageSentEventSchema, SamvaWebhookEventSchema, TemplateRegistrationUpdatedEventSchema, WebhookCreatedEventSchema, WebhookDeletedEventSchema, WebhookTestEventSchema, WebhookUpdatedEventSchema, WhatsappNumberConnectedEventSchema } from "./schemas.gen.js";
|
|
4
4
|
import { Clock, Effect } from "effect";
|
package/dist/errors.d.ts
CHANGED
|
@@ -49,8 +49,8 @@ export declare class WebhookTimestampError extends WebhookVerificationError<"Web
|
|
|
49
49
|
constructor(options: WebhookVerificationFailureOptions);
|
|
50
50
|
}
|
|
51
51
|
/**
|
|
52
|
-
* No signature in the `webhook-signature` header matches the delivery
|
|
53
|
-
*
|
|
52
|
+
* No signature in the `webhook-signature` header matches the delivery. Treat the request as forged
|
|
53
|
+
* and reject it. When genuine deliveries fail, the usual
|
|
54
54
|
* causes are the wrong endpoint secret or a body that was parsed and re-serialized before
|
|
55
55
|
* verification instead of passed through as the exact bytes received.
|
|
56
56
|
*/
|
|
@@ -65,9 +65,9 @@ export declare class MalformedWebhookPayloadError extends WebhookVerificationErr
|
|
|
65
65
|
constructor(options: WebhookVerificationFailureOptions);
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
68
|
-
* The runtime's Web Crypto implementation could not import a key or compute a signature,
|
|
69
|
-
*
|
|
70
|
-
*
|
|
68
|
+
* The runtime's Web Crypto implementation could not import a key or compute a signature, or the
|
|
69
|
+
* Effect entrypoint could not read the request body. Points at the runtime or its configuration
|
|
70
|
+
* rather than at the delivery.
|
|
71
71
|
*/
|
|
72
72
|
export declare class WebhookCryptoError extends WebhookVerificationError<"WebhookCryptoError"> {
|
|
73
73
|
constructor(options: WebhookVerificationFailureOptions);
|
package/dist/promise.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as generateWebhookSecret, c as verifyWebhookAt, d as MalformedWebhookPayloadError, f as WebhookCryptoError, g as isKnownEvent, h as WebhookVerificationError, i as WEBHOOK_TIMESTAMP_HEADER, l as InvalidWebhookHeadersError, m as WebhookTimestampError, n as WEBHOOK_ID_HEADER, o as readWebhookHeaders, p as WebhookSignatureError, r as WEBHOOK_SIGNATURE_HEADER, s as signWebhook, t as DEFAULT_TOLERANCE_SECONDS, u as InvalidWebhookSecretError } from "./protocol-IJ8lllqW.js";
|
|
2
2
|
import { t as WebhookEventJsonSchemas } from "./json-schemas.gen-C5nX_b5f.js";
|
|
3
3
|
// src/promise.ts
|
|
4
4
|
/**
|
|
@@ -502,8 +502,8 @@ var WebhookTimestampError = class extends WebhookVerificationError {
|
|
|
502
502
|
}
|
|
503
503
|
};
|
|
504
504
|
/**
|
|
505
|
-
* No signature in the `webhook-signature` header matches the delivery
|
|
506
|
-
*
|
|
505
|
+
* No signature in the `webhook-signature` header matches the delivery. Treat the request as forged
|
|
506
|
+
* and reject it. When genuine deliveries fail, the usual
|
|
507
507
|
* causes are the wrong endpoint secret or a body that was parsed and re-serialized before
|
|
508
508
|
* verification instead of passed through as the exact bytes received.
|
|
509
509
|
*/
|
|
@@ -522,9 +522,9 @@ var MalformedWebhookPayloadError = class extends WebhookVerificationError {
|
|
|
522
522
|
}
|
|
523
523
|
};
|
|
524
524
|
/**
|
|
525
|
-
* The runtime's Web Crypto implementation could not import a key or compute a signature,
|
|
526
|
-
*
|
|
527
|
-
*
|
|
525
|
+
* The runtime's Web Crypto implementation could not import a key or compute a signature, or the
|
|
526
|
+
* Effect entrypoint could not read the request body. Points at the runtime or its configuration
|
|
527
|
+
* rather than at the delivery.
|
|
528
528
|
*/
|
|
529
529
|
var WebhookCryptoError = class extends WebhookVerificationError {
|
|
530
530
|
constructor(options) {
|
|
@@ -552,10 +552,6 @@ const WEBHOOK_SIGNATURE_HEADER = "webhook-signature";
|
|
|
552
552
|
*/
|
|
553
553
|
const DEFAULT_TOLERANCE_SECONDS = 300;
|
|
554
554
|
const SECRET_PREFIX = "whsec_";
|
|
555
|
-
const ASYMMETRIC_ALGORITHM = {
|
|
556
|
-
name: "RSASSA-PKCS1-v1_5",
|
|
557
|
-
hash: "SHA-256"
|
|
558
|
-
};
|
|
559
555
|
const MINIMUM_SECRET_BYTES = 24;
|
|
560
556
|
const MAXIMUM_SECRET_BYTES = 64;
|
|
561
557
|
const GENERATED_SECRET_BYTES = 32;
|
|
@@ -594,13 +590,6 @@ const importHmacKey = async (secret) => {
|
|
|
594
590
|
throw new WebhookCryptoError({ detail: `Unable to import webhook signing key: ${cause instanceof Error ? cause.message : String(cause)}` });
|
|
595
591
|
}
|
|
596
592
|
};
|
|
597
|
-
const importVerificationKey = async (publicKey) => {
|
|
598
|
-
try {
|
|
599
|
-
return await crypto.subtle.importKey("spki", toCryptoBytes(base64ToBytes(publicKey.trim())), ASYMMETRIC_ALGORITHM, false, ["verify"]);
|
|
600
|
-
} catch {
|
|
601
|
-
return;
|
|
602
|
-
}
|
|
603
|
-
};
|
|
604
593
|
const signedBytes = (id, timestamp, body) => {
|
|
605
594
|
const prefix = encoder.encode(`${id}.${timestamp}.`);
|
|
606
595
|
const material = new Uint8Array(prefix.byteLength + body.byteLength);
|
|
@@ -616,21 +605,14 @@ const hmac = async (secret, id, timestamp, body) => {
|
|
|
616
605
|
throw new WebhookCryptoError({ detail: `Unable to sign webhook: ${cause instanceof Error ? cause.message : String(cause)}` });
|
|
617
606
|
}
|
|
618
607
|
};
|
|
619
|
-
const asymmetricMatch = async (key, candidate, content) => {
|
|
620
|
-
try {
|
|
621
|
-
return await crypto.subtle.verify(ASYMMETRIC_ALGORITHM, key, toCryptoBytes(candidate), content);
|
|
622
|
-
} catch {
|
|
623
|
-
return false;
|
|
624
|
-
}
|
|
625
|
-
};
|
|
626
608
|
const timingSafeEqual = (left, right) => {
|
|
627
609
|
if (left.byteLength !== right.byteLength) return false;
|
|
628
610
|
let difference = 0;
|
|
629
611
|
for (let index = 0; index < left.byteLength; index += 1) difference |= (left[index] ?? 0) ^ (right[index] ?? 0);
|
|
630
612
|
return difference === 0;
|
|
631
613
|
};
|
|
632
|
-
const signatureCandidates = (header
|
|
633
|
-
if (version !==
|
|
614
|
+
const signatureCandidates = (header) => header.split(" ").map((candidate) => candidate.split(",", 2)).flatMap(([version, encoded]) => {
|
|
615
|
+
if (version !== "v1" || encoded === void 0) return [];
|
|
634
616
|
try {
|
|
635
617
|
return [base64ToBytes(encoded)];
|
|
636
618
|
} catch {
|
|
@@ -659,14 +641,6 @@ const generateWebhookSecret = () => {
|
|
|
659
641
|
return `${SECRET_PREFIX}${bytesToBase64(bytes)}`;
|
|
660
642
|
};
|
|
661
643
|
/**
|
|
662
|
-
* Builds the bytes every signature version covers: `<id>.<timestamp>.<body>`. Use it to verify a
|
|
663
|
-
* signature with your own crypto code; {@link verifyWebhookAt} and
|
|
664
|
-
* {@link verifyWebhookAtWithPublicKeys} build it for you.
|
|
665
|
-
*/
|
|
666
|
-
const webhookSignedContent = (input) => signedBytes(input.id, input.timestamp, toBytes(input.body));
|
|
667
|
-
/** Formats raw signature bytes as one `<version>,<base64>` entry of the `webhook-signature` header. */
|
|
668
|
-
const encodeWebhookSignature = (version, signature) => `${version},${bytesToBase64(signature)}`;
|
|
669
|
-
/**
|
|
670
644
|
* Computes the `v1` HMAC-SHA256 signature Samva sends, formatted as one `webhook-signature`
|
|
671
645
|
* entry. Use it to produce signed test deliveries for your handler.
|
|
672
646
|
*
|
|
@@ -677,7 +651,7 @@ const encodeWebhookSignature = (version, signature) => `${version},${bytesToBase
|
|
|
677
651
|
const signWebhook = async (input) => {
|
|
678
652
|
const timestamp = String(input.timestamp);
|
|
679
653
|
const signature = await hmac(input.secret, input.id, timestamp, toBytes(input.body));
|
|
680
|
-
return
|
|
654
|
+
return `v1,${bytesToBase64(signature)}`;
|
|
681
655
|
};
|
|
682
656
|
const requireHeader = (header, value) => {
|
|
683
657
|
if (value === null || value === void 0) throw new InvalidWebhookHeadersError({
|
|
@@ -727,30 +701,7 @@ const verifyWebhookAt = async (input) => {
|
|
|
727
701
|
const { id, timestamp, signature } = freshHeaders(input);
|
|
728
702
|
const body = toBytes(input.body);
|
|
729
703
|
const expected = await hmac(input.secret, id, timestamp, body);
|
|
730
|
-
if (!signatureCandidates(signature
|
|
731
|
-
return {
|
|
732
|
-
id,
|
|
733
|
-
event: parseEvent(body)
|
|
734
|
-
};
|
|
735
|
-
};
|
|
736
|
-
/**
|
|
737
|
-
* Verifies a `v2` (RSASSA-PKCS1-v1_5, SHA-256) delivery against one or more public keys, each a
|
|
738
|
-
* Base64-encoded SPKI DER key. Pass several keys during a key rotation; the delivery verifies when
|
|
739
|
-
* any `v2` signature entry matches any key.
|
|
740
|
-
*
|
|
741
|
-
* Header, timestamp, and envelope checks match {@link verifyWebhookAt}. Keys that fail to import
|
|
742
|
-
* are skipped. Rejects with {@link WebhookSignatureError} when `publicKeys` is empty or nothing
|
|
743
|
-
* matches, and {@link WebhookCryptoError} when none of the keys imports.
|
|
744
|
-
*/
|
|
745
|
-
const verifyWebhookAtWithPublicKeys = async (input) => {
|
|
746
|
-
const { id, timestamp, signature } = freshHeaders(input);
|
|
747
|
-
if (input.publicKeys.length === 0) throw new WebhookSignatureError({ detail: "No webhook verification key is available" });
|
|
748
|
-
const keys = (await Promise.all(input.publicKeys.map(importVerificationKey))).filter((key) => key !== void 0);
|
|
749
|
-
if (keys.length === 0) throw new WebhookCryptoError({ detail: "Unable to import any webhook verification key: keys must be Base64 SPKI DER" });
|
|
750
|
-
const body = toBytes(input.body);
|
|
751
|
-
const content = toCryptoBytes(signedBytes(id, timestamp, body));
|
|
752
|
-
const candidates = signatureCandidates(signature, "v2");
|
|
753
|
-
if (!(await Promise.all(keys.flatMap((key) => candidates.map((candidate) => asymmetricMatch(key, candidate, content))))).includes(true)) throw new WebhookSignatureError({ detail: "Webhook signature verification failed" });
|
|
704
|
+
if (!signatureCandidates(signature).some((candidate) => timingSafeEqual(expected, candidate))) throw new WebhookSignatureError({ detail: "Webhook signature verification failed" });
|
|
754
705
|
return {
|
|
755
706
|
id,
|
|
756
707
|
event: parseEvent(body)
|
|
@@ -767,6 +718,6 @@ const readWebhookHeaders = (headers) => requireHeaders({
|
|
|
767
718
|
timestamp: headers.get(WEBHOOK_TIMESTAMP_HEADER),
|
|
768
719
|
signature: headers.get(WEBHOOK_SIGNATURE_HEADER)
|
|
769
720
|
});
|
|
770
|
-
export {
|
|
721
|
+
export { generateWebhookSecret as a, verifyWebhookAt as c, MalformedWebhookPayloadError as d, WebhookCryptoError as f, isKnownEvent as g, WebhookVerificationError as h, WEBHOOK_TIMESTAMP_HEADER as i, InvalidWebhookHeadersError as l, WebhookTimestampError as m, WEBHOOK_ID_HEADER as n, readWebhookHeaders as o, WebhookSignatureError as p, WEBHOOK_SIGNATURE_HEADER as r, signWebhook as s, DEFAULT_TOLERANCE_SECONDS as t, InvalidWebhookSecretError as u };
|
|
771
722
|
|
|
772
|
-
//# sourceMappingURL=protocol-
|
|
723
|
+
//# sourceMappingURL=protocol-IJ8lllqW.js.map
|
package/dist/protocol.d.ts
CHANGED
|
@@ -67,29 +67,12 @@ export interface VerifyWebhookInput {
|
|
|
67
67
|
/** The current time in Unix milliseconds, as `Date.now()` returns it. */
|
|
68
68
|
readonly nowMilliseconds: number;
|
|
69
69
|
}
|
|
70
|
-
/**
|
|
71
|
-
* Signature scheme version inside a `webhook-signature` entry: `v1` is HMAC-SHA256 with the
|
|
72
|
-
* endpoint's shared secret; `v2` is RSASSA-PKCS1-v1_5 with SHA-256, checked against a public key.
|
|
73
|
-
*/
|
|
74
|
-
export type WebhookSignatureVersion = "v1" | "v2";
|
|
75
70
|
/**
|
|
76
71
|
* Creates a random `whsec_`-prefixed signing secret from the runtime's secure random source.
|
|
77
72
|
* Samva issues each endpoint's secret for you; use this to sign test deliveries with
|
|
78
73
|
* {@link signWebhook}.
|
|
79
74
|
*/
|
|
80
75
|
export declare const generateWebhookSecret: () => string;
|
|
81
|
-
/**
|
|
82
|
-
* Builds the bytes every signature version covers: `<id>.<timestamp>.<body>`. Use it to verify a
|
|
83
|
-
* signature with your own crypto code; {@link verifyWebhookAt} and
|
|
84
|
-
* {@link verifyWebhookAtWithPublicKeys} build it for you.
|
|
85
|
-
*/
|
|
86
|
-
export declare const webhookSignedContent: (input: {
|
|
87
|
-
readonly id: string;
|
|
88
|
-
readonly timestamp: string;
|
|
89
|
-
readonly body: string | Uint8Array;
|
|
90
|
-
}) => Uint8Array;
|
|
91
|
-
/** Formats raw signature bytes as one `<version>,<base64>` entry of the `webhook-signature` header. */
|
|
92
|
-
export declare const encodeWebhookSignature: (version: WebhookSignatureVersion, signature: Uint8Array) => string;
|
|
93
76
|
/**
|
|
94
77
|
* Computes the `v1` HMAC-SHA256 signature Samva sends, formatted as one `webhook-signature`
|
|
95
78
|
* entry. Use it to produce signed test deliveries for your handler.
|
|
@@ -120,18 +103,6 @@ export declare const signWebhook: (input: {
|
|
|
120
103
|
* {@link MalformedWebhookPayloadError}, or {@link WebhookCryptoError}.
|
|
121
104
|
*/
|
|
122
105
|
export declare const verifyWebhookAt: (input: VerifyWebhookInput) => Promise<VerifiedWebhook>;
|
|
123
|
-
/**
|
|
124
|
-
* Verifies a `v2` (RSASSA-PKCS1-v1_5, SHA-256) delivery against one or more public keys, each a
|
|
125
|
-
* Base64-encoded SPKI DER key. Pass several keys during a key rotation; the delivery verifies when
|
|
126
|
-
* any `v2` signature entry matches any key.
|
|
127
|
-
*
|
|
128
|
-
* Header, timestamp, and envelope checks match {@link verifyWebhookAt}. Keys that fail to import
|
|
129
|
-
* are skipped. Rejects with {@link WebhookSignatureError} when `publicKeys` is empty or nothing
|
|
130
|
-
* matches, and {@link WebhookCryptoError} when none of the keys imports.
|
|
131
|
-
*/
|
|
132
|
-
export declare const verifyWebhookAtWithPublicKeys: (input: Omit<VerifyWebhookInput, "secret"> & {
|
|
133
|
-
readonly publicKeys: ReadonlyArray<string>;
|
|
134
|
-
}) => Promise<VerifiedWebhook>;
|
|
135
106
|
/**
|
|
136
107
|
* Reads the three signing headers from a Fetch API `Headers` object, for code that needs a header
|
|
137
108
|
* value, such as the timestamp, before or beside verification. Throws
|
package/dist/protocol.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as DEFAULT_TOLERANCE_SECONDS,
|
|
1
|
+
export { t as DEFAULT_TOLERANCE_SECONDS, l as InvalidWebhookHeadersError, u as InvalidWebhookSecretError, d as MalformedWebhookPayloadError, n as WEBHOOK_ID_HEADER, r as WEBHOOK_SIGNATURE_HEADER, i as WEBHOOK_TIMESTAMP_HEADER, f as WebhookCryptoError, p as WebhookSignatureError, m as WebhookTimestampError, h as WebhookVerificationError, a as generateWebhookSecret, g as isKnownEvent, o as readWebhookHeaders, s as signWebhook, c as verifyWebhookAt } from "./protocol-IJ8lllqW.js";
|