|
| 1 | +import { createHmac, timingSafeEqual } from 'node:crypto' |
| 2 | + |
| 3 | +// ─── Provider Callback Authentication ─── |
| 4 | +// |
| 5 | +// External provider callbacks must be cryptographically authenticated |
| 6 | +// before they can update execution state. The verifier abstraction |
| 7 | +// keeps provider-specific signature formats behind the interface so |
| 8 | +// canonical execution types stay clean. |
| 9 | +// |
| 10 | +// HMAC-SHA-256 is the temporary minimum for providers without a native |
| 11 | +// asymmetric webhook-signature scheme. |
| 12 | + |
| 13 | +export interface VerifiedProviderEvent { |
| 14 | + providerId: string |
| 15 | + providerEventId: string |
| 16 | + timestamp: Date |
| 17 | + nonce?: string |
| 18 | + payloadHash: string |
| 19 | +} |
| 20 | + |
| 21 | +export interface ProviderCallbackVerifier { |
| 22 | + providerId: string |
| 23 | + verify(input: { |
| 24 | + rawBody: Uint8Array |
| 25 | + headers: Headers |
| 26 | + receivedAt: Date |
| 27 | + }): Promise<VerifiedProviderEvent> |
| 28 | +} |
| 29 | + |
| 30 | +// ─── HMAC-SHA-256 Verifier ─── |
| 31 | + |
| 32 | +export interface HmacCredential { |
| 33 | + keyId: string |
| 34 | + secret: string |
| 35 | +} |
| 36 | + |
| 37 | +const TIMESTAMP_WINDOW_MS = 5 * 60 * 1000 // 5 minutes |
| 38 | + |
| 39 | +/** |
| 40 | + * Compute the HMAC-SHA-256 signature over the exact raw body. |
| 41 | + * |
| 42 | + * The signed material is: |
| 43 | + * provider_event_id.timestamp.rawBody |
| 44 | + * |
| 45 | + * This binds the event ID, timestamp, and body together so that |
| 46 | + * replaying the same signature with a different body, event ID, |
| 47 | + * or timestamp fails. |
| 48 | + */ |
| 49 | +function computeSignature(secret: string, providerEventId: string, timestamp: string, rawBody: Uint8Array): Buffer { |
| 50 | + const signedMaterial = Buffer.concat([ |
| 51 | + Buffer.from(`${providerEventId}.${timestamp}.`, 'utf8'), |
| 52 | + Buffer.from(rawBody), |
| 53 | + ]) |
| 54 | + return createHmac('sha256', secret).update(signedMaterial).digest() |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Constant-time comparison of two buffers. |
| 59 | + * Returns true if they are equal in length and content. |
| 60 | + */ |
| 61 | +function safeEqual(a: Buffer, b: Buffer): boolean { |
| 62 | + if (a.length !== b.length) return false |
| 63 | + return timingSafeEqual(a, b) |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Parse a signature header in the format: |
| 68 | + * t=<unix_ms>,v1=<hex_sig>,key_id=<keyId> |
| 69 | + */ |
| 70 | +function parseSignatureHeader(header: string | null): { |
| 71 | + timestamp: string | null |
| 72 | + signature: string | null |
| 73 | + keyId: string | null |
| 74 | +} { |
| 75 | + if (!header) return { timestamp: null, signature: null, keyId: null } |
| 76 | + const parts = header.split(',') |
| 77 | + const map: Record<string, string> = {} |
| 78 | + for (const part of parts) { |
| 79 | + const eq = part.indexOf('=') |
| 80 | + if (eq > 0) map[part.slice(0, eq).trim()] = part.slice(eq + 1).trim() |
| 81 | + } |
| 82 | + return { |
| 83 | + timestamp: map['t'] ?? null, |
| 84 | + signature: map['v1'] ?? null, |
| 85 | + keyId: map['key_id'] ?? null, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Create an HMAC-SHA-256 callback verifier for a specific provider. |
| 91 | + * |
| 92 | + * Credentials are scoped to one provider and one environment. |
| 93 | + * Supports key rotation by accepting both current and previous credentials. |
| 94 | + */ |
| 95 | +export function createHmacVerifier( |
| 96 | + providerId: string, |
| 97 | + credentials: HmacCredential[], |
| 98 | +): ProviderCallbackVerifier { |
| 99 | + if (credentials.length === 0) { |
| 100 | + throw new Error(`HMAC verifier for provider '${providerId}' requires at least one credential`) |
| 101 | + } |
| 102 | + |
| 103 | + return { |
| 104 | + providerId, |
| 105 | + |
| 106 | + async verify(input: { |
| 107 | + rawBody: Uint8Array |
| 108 | + headers: Headers |
| 109 | + receivedAt: Date |
| 110 | + }): Promise<VerifiedProviderEvent> { |
| 111 | + const { rawBody, headers, receivedAt } = input |
| 112 | + |
| 113 | + const sigHeader = headers.get('x-provider-signature') |
| 114 | + const { timestamp, signature, keyId } = parseSignatureHeader(sigHeader) |
| 115 | + |
| 116 | + if (!timestamp || !signature || !keyId) { |
| 117 | + throw new VerificationError('missing_signature', 'Signature header is missing or malformed') |
| 118 | + } |
| 119 | + |
| 120 | + // Find the credential by key ID |
| 121 | + const credential = credentials.find((c) => c.keyId === keyId) |
| 122 | + if (!credential) { |
| 123 | + throw new VerificationError('unknown_key', `Unknown key ID: ${keyId}`) |
| 124 | + } |
| 125 | + |
| 126 | + // Check timestamp window |
| 127 | + const eventTime = new Date(Number(timestamp)) |
| 128 | + if (isNaN(eventTime.getTime())) { |
| 129 | + throw new VerificationError('invalid_timestamp', 'Timestamp is not a valid number') |
| 130 | + } |
| 131 | + const ageMs = Math.abs(receivedAt.getTime() - eventTime.getTime()) |
| 132 | + if (ageMs > TIMESTAMP_WINDOW_MS) { |
| 133 | + throw new VerificationError('expired', `Callback timestamp is outside the ${TIMESTAMP_WINDOW_MS}ms window`) |
| 134 | + } |
| 135 | + |
| 136 | + // Get provider event ID from header |
| 137 | + const providerEventId = headers.get('x-provider-event-id') |
| 138 | + if (!providerEventId) { |
| 139 | + throw new VerificationError('missing_event_id', 'Provider event ID header is missing') |
| 140 | + } |
| 141 | + |
| 142 | + // Compute expected signature over exact raw body |
| 143 | + const expectedSig = computeSignature(credential.secret, providerEventId, timestamp, rawBody) |
| 144 | + const providedSig = Buffer.from(signature, 'hex') |
| 145 | + |
| 146 | + if (!safeEqual(expectedSig, providedSig)) { |
| 147 | + throw new VerificationError('invalid_signature', 'Signature does not match the raw body') |
| 148 | + } |
| 149 | + |
| 150 | + // Get optional nonce |
| 151 | + const nonce = headers.get('x-provider-nonce') ?? undefined |
| 152 | + |
| 153 | + // Compute payload hash for idempotency |
| 154 | + const payloadHash = createHashSha256(rawBody) |
| 155 | + |
| 156 | + return { |
| 157 | + providerId, |
| 158 | + providerEventId, |
| 159 | + timestamp: eventTime, |
| 160 | + nonce, |
| 161 | + payloadHash, |
| 162 | + } |
| 163 | + }, |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * Custom error class for verification failures. |
| 169 | + * Contains a safe code that can be logged without exposing secrets. |
| 170 | + */ |
| 171 | +export class VerificationError extends Error { |
| 172 | + constructor( |
| 173 | + public readonly code: string, |
| 174 | + message: string, |
| 175 | + ) { |
| 176 | + super(message) |
| 177 | + this.name = 'VerificationError' |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +/** |
| 182 | + * Compute SHA-256 hash of a buffer, return hex string. |
| 183 | + */ |
| 184 | +function createHashSha256(data: Uint8Array): string { |
| 185 | + const { createHash } = require('node:crypto') |
| 186 | + return createHash('sha256').update(Buffer.from(data)).digest('hex') |
| 187 | +} |
| 188 | + |
| 189 | +// ─── Verifier Registry ─── |
| 190 | + |
| 191 | +const verifiers = new Map<string, ProviderCallbackVerifier>() |
| 192 | + |
| 193 | +/** |
| 194 | + * Register a callback verifier for a provider. |
| 195 | + */ |
| 196 | +export function registerVerifier(verifier: ProviderCallbackVerifier): void { |
| 197 | + verifiers.set(verifier.providerId, verifier) |
| 198 | +} |
| 199 | + |
| 200 | +/** |
| 201 | + * Get a registered verifier by provider ID. |
| 202 | + */ |
| 203 | +export function getVerifier(providerId: string): ProviderCallbackVerifier | undefined { |
| 204 | + return verifiers.get(providerId) |
| 205 | +} |
| 206 | + |
| 207 | +/** |
| 208 | + * Check if a verifier is registered for a provider. |
| 209 | + */ |
| 210 | +export function hasVerifier(providerId: string): boolean { |
| 211 | + return verifiers.has(providerId) |
| 212 | +} |
0 commit comments