@aarivbuildz/hookverify 0.0.0-stage → 1.0.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/CONTRIBUTING.md +64 -0
- package/GOOD_FIRST_ISSUES.md +86 -0
- package/LICENSE +21 -0
- package/README.md +197 -2
- package/SECURITY.md +45 -0
- package/cjs/index.cjs +4 -0
- package/cjs/internal.cjs +916 -0
- package/package.json +55 -4
- package/src/index.mjs +3 -0
- package/src/internal.mjs +33 -0
- package/src/providers/discord.mjs +3 -0
- package/src/providers/github.mjs +3 -0
- package/src/providers/linear.mjs +3 -0
- package/src/providers/shopify.mjs +3 -0
- package/src/providers/slack.mjs +3 -0
- package/src/providers/standard-webhooks.mjs +3 -0
- package/src/providers/stripe.mjs +3 -0
- package/src/providers/svix.mjs +3 -0
- package/types/index.d.ts +268 -0
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aarivbuildz/hookverify",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Zero-dependency webhook signature verification with constant-time checks, replay protection, and pluggable provider adapters",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"webhook",
|
|
7
|
+
"signature",
|
|
8
|
+
"verification",
|
|
9
|
+
"hmac",
|
|
10
|
+
"ed25519",
|
|
11
|
+
"replay-protection",
|
|
12
|
+
"security",
|
|
13
|
+
"github",
|
|
14
|
+
"stripe",
|
|
15
|
+
"slack",
|
|
16
|
+
"shopify",
|
|
17
|
+
"linear",
|
|
18
|
+
"svix",
|
|
19
|
+
"discord"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "./cjs/index.cjs",
|
|
24
|
+
"module": "./src/index.mjs",
|
|
25
|
+
"types": "./types/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./types/index.d.ts",
|
|
29
|
+
"import": "./src/index.mjs",
|
|
30
|
+
"require": "./cjs/index.cjs"
|
|
31
|
+
},
|
|
32
|
+
"./providers/*": {
|
|
33
|
+
"types": "./types/index.d.ts",
|
|
34
|
+
"import": "./src/providers/*.mjs"
|
|
35
|
+
},
|
|
36
|
+
"./package.json": "./package.json"
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"src",
|
|
40
|
+
"cjs",
|
|
41
|
+
"types",
|
|
42
|
+
"README.md",
|
|
43
|
+
"SECURITY.md",
|
|
44
|
+
"CONTRIBUTING.md",
|
|
45
|
+
"GOOD_FIRST_ISSUES.md",
|
|
46
|
+
"LICENSE"
|
|
47
|
+
],
|
|
48
|
+
"scripts": {
|
|
49
|
+
"test": "node --test tests/*.test.mjs",
|
|
50
|
+
"test:all": "node --test tests/*.test.mjs"
|
|
51
|
+
},
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=18.0.0"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {},
|
|
56
|
+
"devDependencies": {}
|
|
57
|
+
}
|
package/src/index.mjs
ADDED
package/src/internal.mjs
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// ESM facade. Node's createRequire keeps this facade and the CommonJS engine
|
|
2
|
+
// on exactly the same implementation, so both module systems cannot diverge.
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const engine = require('../cjs/internal.cjs');
|
|
8
|
+
|
|
9
|
+
export const {
|
|
10
|
+
HookVerifyError,
|
|
11
|
+
ERROR_CODES,
|
|
12
|
+
MemoryReplayStore,
|
|
13
|
+
defineProvider,
|
|
14
|
+
registerProvider,
|
|
15
|
+
getProvider,
|
|
16
|
+
listProviders,
|
|
17
|
+
verify,
|
|
18
|
+
verifyAsync,
|
|
19
|
+
isValid,
|
|
20
|
+
createVerifier,
|
|
21
|
+
providers,
|
|
22
|
+
github,
|
|
23
|
+
stripe,
|
|
24
|
+
slack,
|
|
25
|
+
shopify,
|
|
26
|
+
linear,
|
|
27
|
+
svix,
|
|
28
|
+
standardWebhooks,
|
|
29
|
+
discord,
|
|
30
|
+
helpers,
|
|
31
|
+
} = engine;
|
|
32
|
+
|
|
33
|
+
export default engine;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for hookverify. No external type dependencies.
|
|
3
|
+
* The runtime uses only node:crypto from the Node standard library.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** Error codes exposed as `error.code` on every HookVerifyError. */
|
|
7
|
+
export type ErrorCode =
|
|
8
|
+
| 'INVALID_ARGUMENT'
|
|
9
|
+
| 'INVALID_BODY'
|
|
10
|
+
| 'MISSING_HEADER'
|
|
11
|
+
| 'INVALID_HEADER'
|
|
12
|
+
| 'INVALID_SECRET'
|
|
13
|
+
| 'INVALID_SIGNATURE'
|
|
14
|
+
| 'SIGNATURE_MISMATCH'
|
|
15
|
+
| 'TIMESTAMP_MISSING'
|
|
16
|
+
| 'TIMESTAMP_INVALID'
|
|
17
|
+
| 'TIMESTAMP_OUT_OF_TOLERANCE'
|
|
18
|
+
| 'REPLAYED_DELIVERY'
|
|
19
|
+
| 'UNKNOWN_PROVIDER'
|
|
20
|
+
| 'PROVIDER_ERROR';
|
|
21
|
+
|
|
22
|
+
export declare const ERROR_CODES: Readonly<Record<ErrorCode, ErrorCode>>;
|
|
23
|
+
|
|
24
|
+
/** Every rejection thrown by this library is a HookVerifyError with a stable code. */
|
|
25
|
+
export declare class HookVerifyError extends Error {
|
|
26
|
+
readonly name: 'HookVerifyError';
|
|
27
|
+
readonly code: ErrorCode;
|
|
28
|
+
readonly details?: Readonly<Record<string, unknown>>;
|
|
29
|
+
constructor(code: ErrorCode, message: string, details?: Record<string, unknown>);
|
|
30
|
+
toJSON(): Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Byte-exact raw payload. Parsed objects are rejected at runtime. */
|
|
34
|
+
export type RawBody = Buffer | Uint8Array | string;
|
|
35
|
+
|
|
36
|
+
/** Header value: a single string or a homogeneous array of strings. */
|
|
37
|
+
export type HeaderValue = string | ReadonlyArray<string> | null;
|
|
38
|
+
export type HeaderMap =
|
|
39
|
+
| Readonly<Record<string, HeaderValue>>
|
|
40
|
+
| Map<string, HeaderValue>
|
|
41
|
+
| { get(name: string): unknown; forEach(callback: (value: unknown, key: string) => void): void };
|
|
42
|
+
|
|
43
|
+
/** Milliseconds since the Unix epoch. */
|
|
44
|
+
export type Milliseconds = number;
|
|
45
|
+
|
|
46
|
+
/** A synchronous or asynchronous replay store. Keys are opaque provider-scoped strings. */
|
|
47
|
+
export interface ReplayStore {
|
|
48
|
+
checkAndRecord(key: string, nowMs: Milliseconds): boolean | Promise<boolean>;
|
|
49
|
+
has?(key: string, nowMs?: Milliseconds): boolean;
|
|
50
|
+
prune?(nowMs?: Milliseconds): number;
|
|
51
|
+
clear?(): void;
|
|
52
|
+
readonly size?: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ProviderContext {
|
|
56
|
+
readonly provider: string;
|
|
57
|
+
readonly body: Buffer;
|
|
58
|
+
readonly headers: ReadonlyMap<string, ReadonlyArray<string | null>>;
|
|
59
|
+
/** Header names that were supplied as arrays and must be rejected when read. */
|
|
60
|
+
readonly headerArrays: ReadonlySet<string>;
|
|
61
|
+
readonly secrets: readonly string[];
|
|
62
|
+
readonly toleranceSeconds: number;
|
|
63
|
+
readonly nowMs: Milliseconds;
|
|
64
|
+
readonly options: Readonly<Record<string, unknown>>;
|
|
65
|
+
readonly helpers: {
|
|
66
|
+
decodeHex(value: string, label?: string): Buffer;
|
|
67
|
+
decodeBase64(value: string, label?: string): Buffer;
|
|
68
|
+
decodeHexOfLength(value: string, bytes: number, label?: string): Buffer;
|
|
69
|
+
constantTimeEqual(a: Buffer, b: Buffer): boolean;
|
|
70
|
+
hmac(secret: string | Buffer, payload: Buffer, algorithm?: string): Buffer;
|
|
71
|
+
deliveryKey(provider: string, identifier: string): string;
|
|
72
|
+
signatureKey(provider: string, secretIndex: number, signature: Buffer | Uint8Array): string;
|
|
73
|
+
getHeader(ctx: ProviderContext, name: string): string;
|
|
74
|
+
getOptionalHeader(ctx: ProviderContext, name: string): string | null;
|
|
75
|
+
parseTimestampSeconds(value: string): Milliseconds;
|
|
76
|
+
verifyHmac(ctx: ProviderContext, provided: Buffer | Buffer[], payload?: Buffer): VerificationResultInternal;
|
|
77
|
+
verifyHmacWithSecrets(secrets: ReadonlyArray<string | Buffer>, provided: Buffer | Buffer[], payload: Buffer): VerificationResultInternal;
|
|
78
|
+
fail(code: ErrorCode, message: string, details?: Record<string, unknown>): never;
|
|
79
|
+
HookVerifyError: typeof HookVerifyError;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Internal result contract for provider authors. */
|
|
84
|
+
export interface VerificationResultInternal {
|
|
85
|
+
timestampMs?: Milliseconds | null;
|
|
86
|
+
secretIndex?: number | null;
|
|
87
|
+
signature?: Buffer | Uint8Array | string;
|
|
88
|
+
replayKey?: string | null;
|
|
89
|
+
/**
|
|
90
|
+
* Every signature that verified, one per matching secret. The engine records
|
|
91
|
+
* a replay key for each entry. `verifyHmacWithSecrets` fills this in.
|
|
92
|
+
*/
|
|
93
|
+
matchedSignatures?: ReadonlyArray<{
|
|
94
|
+
secretIndex: number;
|
|
95
|
+
signature: Buffer | Uint8Array | string;
|
|
96
|
+
}>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Public, frozen result returned by successful verification. */
|
|
100
|
+
export interface VerificationResult {
|
|
101
|
+
readonly provider: string;
|
|
102
|
+
readonly valid: true;
|
|
103
|
+
readonly timestamp: Date | null;
|
|
104
|
+
readonly secretIndex: number | null;
|
|
105
|
+
readonly replayKey: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface VerifyOptions {
|
|
109
|
+
/** Primary shared secret. Ignored when `secrets` is provided. */
|
|
110
|
+
secret?: string;
|
|
111
|
+
/** Ordered secrets for zero-downtime rotation. */
|
|
112
|
+
secrets?: readonly string[];
|
|
113
|
+
/** Replay window in seconds, default 300. Applies to both stale and future timestamps. */
|
|
114
|
+
toleranceSeconds?: number;
|
|
115
|
+
/** Injectable clock for deterministic tests; returns milliseconds. */
|
|
116
|
+
now?: () => Milliseconds;
|
|
117
|
+
/** Optional replay store; callers can supply a Redis-backed implementation. */
|
|
118
|
+
replayStore?: ReplayStore;
|
|
119
|
+
/** Provider-specific options, for example `publicKey` for Discord. */
|
|
120
|
+
[key: string]: unknown;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface ProviderDefinition {
|
|
124
|
+
readonly name: string;
|
|
125
|
+
readonly description?: string;
|
|
126
|
+
readonly requiredHeaders?: readonly string[];
|
|
127
|
+
/**
|
|
128
|
+
* Set to true only for adapters that authenticate without a shared secret,
|
|
129
|
+
* for example Discord when the Ed25519 public key is supplied through
|
|
130
|
+
* `publicKey`. Other adapters still require `secret` or `secrets`.
|
|
131
|
+
*/
|
|
132
|
+
readonly secretOptional?: boolean;
|
|
133
|
+
/** Must be synchronous. Returning a promise is reported as PROVIDER_ERROR. */
|
|
134
|
+
verify(ctx: ProviderContext): VerificationResultInternal;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface Provider extends ProviderDefinition {
|
|
138
|
+
readonly name: string;
|
|
139
|
+
readonly description: string;
|
|
140
|
+
readonly requiredHeaders: ReadonlyArray<string>;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Bounded LRU + TTL in-memory replay store. */
|
|
144
|
+
export declare class MemoryReplayStore implements ReplayStore {
|
|
145
|
+
constructor(options?: { maxEntries?: number; ttlSeconds?: number });
|
|
146
|
+
readonly maxEntries: number;
|
|
147
|
+
readonly ttlSeconds: number;
|
|
148
|
+
readonly size: number;
|
|
149
|
+
checkAndRecord(key: string, nowMs?: Milliseconds): boolean;
|
|
150
|
+
has(key: string, nowMs?: Milliseconds): boolean;
|
|
151
|
+
prune(nowMs?: Milliseconds): number;
|
|
152
|
+
clear(): void;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export declare function defineProvider(definition: ProviderDefinition): Provider;
|
|
156
|
+
export declare function registerProvider(provider: Provider): Provider;
|
|
157
|
+
export declare function getProvider(name: string | Provider): Provider;
|
|
158
|
+
export declare function listProviders(): ReadonlyArray<string>;
|
|
159
|
+
|
|
160
|
+
export declare function verify(
|
|
161
|
+
provider: string | Provider,
|
|
162
|
+
body: RawBody,
|
|
163
|
+
headers: HeaderMap,
|
|
164
|
+
options?: VerifyOptions
|
|
165
|
+
): VerificationResult;
|
|
166
|
+
|
|
167
|
+
export declare function verify(
|
|
168
|
+
options: VerifyOptions & {
|
|
169
|
+
provider: string | Provider;
|
|
170
|
+
body: RawBody;
|
|
171
|
+
headers: HeaderMap;
|
|
172
|
+
}
|
|
173
|
+
): VerificationResult;
|
|
174
|
+
|
|
175
|
+
export declare function verifyAsync(
|
|
176
|
+
provider: string | Provider,
|
|
177
|
+
body: RawBody,
|
|
178
|
+
headers: HeaderMap,
|
|
179
|
+
options?: VerifyOptions
|
|
180
|
+
): Promise<VerificationResult>;
|
|
181
|
+
|
|
182
|
+
export declare function verifyAsync(
|
|
183
|
+
options: VerifyOptions & {
|
|
184
|
+
provider: string | Provider;
|
|
185
|
+
body: RawBody;
|
|
186
|
+
headers: HeaderMap;
|
|
187
|
+
}
|
|
188
|
+
): Promise<VerificationResult>;
|
|
189
|
+
|
|
190
|
+
export declare function isValid(
|
|
191
|
+
provider: string | Provider,
|
|
192
|
+
body: RawBody,
|
|
193
|
+
headers: HeaderMap,
|
|
194
|
+
options?: VerifyOptions
|
|
195
|
+
): boolean;
|
|
196
|
+
|
|
197
|
+
export declare function isValid(
|
|
198
|
+
options: VerifyOptions & {
|
|
199
|
+
provider: string | Provider;
|
|
200
|
+
body: RawBody;
|
|
201
|
+
headers: HeaderMap;
|
|
202
|
+
}
|
|
203
|
+
): boolean;
|
|
204
|
+
|
|
205
|
+
export interface Verifier {
|
|
206
|
+
verify(provider: string | Provider, body: RawBody, headers: HeaderMap, overrides?: VerifyOptions): VerificationResult;
|
|
207
|
+
verifyAsync(provider: string | Provider, body: RawBody, headers: HeaderMap, overrides?: VerifyOptions): Promise<VerificationResult>;
|
|
208
|
+
isValid(provider: string | Provider, body: RawBody, headers: HeaderMap, overrides?: VerifyOptions): boolean;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export declare function createVerifier(defaults?: VerifyOptions): Verifier;
|
|
212
|
+
|
|
213
|
+
export declare const providers: Readonly<{
|
|
214
|
+
github: Provider;
|
|
215
|
+
stripe: Provider;
|
|
216
|
+
slack: Provider;
|
|
217
|
+
shopify: Provider;
|
|
218
|
+
linear: Provider;
|
|
219
|
+
svix: Provider;
|
|
220
|
+
standardWebhooks: Provider;
|
|
221
|
+
'standard-webhooks': Provider;
|
|
222
|
+
discord: Provider;
|
|
223
|
+
}>;
|
|
224
|
+
|
|
225
|
+
export declare const github: Provider;
|
|
226
|
+
export declare const stripe: Provider;
|
|
227
|
+
export declare const slack: Provider;
|
|
228
|
+
export declare const shopify: Provider;
|
|
229
|
+
export declare const linear: Provider;
|
|
230
|
+
export declare const svix: Provider;
|
|
231
|
+
export declare const standardWebhooks: Provider;
|
|
232
|
+
export declare const discord: Provider;
|
|
233
|
+
|
|
234
|
+
export declare const helpers: Readonly<{
|
|
235
|
+
decodeHex(value: string, label?: string): Buffer;
|
|
236
|
+
decodeBase64(value: string, label?: string): Buffer;
|
|
237
|
+
constantTimeEqual(a: Buffer, b: Buffer): boolean;
|
|
238
|
+
deliveryKey(provider: string, identifier: string): string;
|
|
239
|
+
normalizeBody(body: unknown): Buffer;
|
|
240
|
+
normalizeHeaders(headers: unknown): Map<string, Array<string | null>>;
|
|
241
|
+
parseTimestampSeconds(value: string): Milliseconds;
|
|
242
|
+
}>;
|
|
243
|
+
|
|
244
|
+
declare const hookverify: {
|
|
245
|
+
HookVerifyError: typeof HookVerifyError;
|
|
246
|
+
ERROR_CODES: typeof ERROR_CODES;
|
|
247
|
+
MemoryReplayStore: typeof MemoryReplayStore;
|
|
248
|
+
defineProvider: typeof defineProvider;
|
|
249
|
+
registerProvider: typeof registerProvider;
|
|
250
|
+
getProvider: typeof getProvider;
|
|
251
|
+
listProviders: typeof listProviders;
|
|
252
|
+
verify: typeof verify;
|
|
253
|
+
verifyAsync: typeof verifyAsync;
|
|
254
|
+
isValid: typeof isValid;
|
|
255
|
+
createVerifier: typeof createVerifier;
|
|
256
|
+
providers: typeof providers;
|
|
257
|
+
github: typeof github;
|
|
258
|
+
stripe: typeof stripe;
|
|
259
|
+
slack: typeof slack;
|
|
260
|
+
shopify: typeof shopify;
|
|
261
|
+
linear: typeof linear;
|
|
262
|
+
svix: typeof svix;
|
|
263
|
+
standardWebhooks: typeof standardWebhooks;
|
|
264
|
+
discord: typeof discord;
|
|
265
|
+
helpers: typeof helpers;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
export default hookverify;
|