|
| 1 | +import { isNode, isReactNative } from '../constants'; |
1 | 2 | import { SearchResult, Webhook, ApiEvent, ApiEventType } from '../types'; |
2 | 3 | import { WrapperClient } from '../wrapper'; |
3 | 4 |
|
@@ -65,11 +66,73 @@ export default class Webhooks { |
65 | 66 | * @param payload - Received event object to validate |
66 | 67 | * @returns When the signature is valid, it returns the event object |
67 | 68 | */ |
68 | | - async validateSignature<T extends ApiEventType | '' = ''>(data: { |
| 69 | + async validateSignature<T extends ApiEventType = any>(data: { |
69 | 70 | secret: string; |
70 | 71 | signature: string; |
71 | | - payload: ApiEvent<T>; |
| 72 | + payload: string | Buffer | ApiEvent<T>; |
72 | 73 | }): Promise<ApiEvent<T>> { |
73 | | - return this.client.post('/webhooks/validate-signature', { body: data }); |
| 74 | + // Validated locally |
| 75 | + const { secret, signature, payload } = data; |
| 76 | + let payloadString: string; |
| 77 | + if (typeof payload === 'string') { |
| 78 | + payloadString = payload; |
| 79 | + } else if (Buffer.isBuffer(payload)) { |
| 80 | + payloadString = payload.toString('utf8'); |
| 81 | + } else if (typeof payload === 'object') { |
| 82 | + payloadString = JSON.stringify(payload); |
| 83 | + } else { |
| 84 | + throw new Error('Invalid payload type'); |
| 85 | + } |
| 86 | + |
| 87 | + if (isReactNative) { |
| 88 | + // Call the API to validate signature in React Native |
| 89 | + return this.client.post('/webhooks/validate-signature', { |
| 90 | + body: { |
| 91 | + secret, |
| 92 | + signature, |
| 93 | + payload: payloadString, |
| 94 | + }, |
| 95 | + }); |
| 96 | + } else if (isNode) { |
| 97 | + const crypto = await import('crypto'); |
| 98 | + const hmac = crypto.createHmac('sha256', secret); |
| 99 | + const digestBuffer = hmac |
| 100 | + .update(payloadString) |
| 101 | + .digest(); |
| 102 | + // Compare the digest with the signature and prevent timing attacks |
| 103 | + // by using a constant-time comparison |
| 104 | + const signatureBuffer = Buffer.from(signature, 'hex'); |
| 105 | + if (digestBuffer.length !== signatureBuffer.length) { |
| 106 | + throw new Error('Invalid signature'); |
| 107 | + } |
| 108 | + const isValid = crypto.timingSafeEqual(digestBuffer, signatureBuffer); |
| 109 | + if (!isValid) { |
| 110 | + throw new Error('Invalid signature'); |
| 111 | + } |
| 112 | + return JSON.parse(payloadString) as ApiEvent<T>; |
| 113 | + } else { // Web browsers |
| 114 | + const encoder = new TextEncoder(); |
| 115 | + const encodedData = encoder.encode(payloadString); |
| 116 | + const encodedSecret = encoder.encode(secret); |
| 117 | + const digest = await crypto.subtle.sign( |
| 118 | + 'HMAC', |
| 119 | + await crypto.subtle.importKey( |
| 120 | + 'raw', |
| 121 | + encodedSecret, |
| 122 | + { name: 'HMAC', hash: 'SHA-256' }, |
| 123 | + false, |
| 124 | + ['sign'], |
| 125 | + ), |
| 126 | + encodedData, |
| 127 | + ); |
| 128 | + const hexDigest = Array.from(new Uint8Array(digest)) |
| 129 | + .map((b) => b.toString(16).padStart(2, '0')) |
| 130 | + .join('') |
| 131 | + .toLowerCase(); |
| 132 | + if (signature !== hexDigest) { |
| 133 | + throw new Error('Invalid signature'); |
| 134 | + } |
| 135 | + } |
| 136 | + return JSON.parse(payloadString) as ApiEvent<T>; |
74 | 137 | } |
75 | 138 | } |
0 commit comments