The Uiza-Signature header included in each signed event contains a timestamp and one or more signatures. The timestamp is prefixed by t=, and each signature is prefixed by a scheme. Schemes start with v, followed by an integer. Currently, the only valid live signature scheme is v1
Note that newlines have been added for clarity, but a real Uiza-Signature header is on a single line.
Uiza generates signatures using a hash-based message authentication code (HMAC) with SHA-256. To prevent downgrade attacks, you should ignore all schemes that are not v1.
It is possible to have multiple signatures with the same scheme-secret pair. This can happen when you roll an endpoint’s secret from the Dashboard, and choose to keep the previous secret active for up to 24 hours. During this time, your endpoint has multiple active secrets and Uiza generates one signature for each secret.
Although it’s recommended to use our official libraries to verify webhook event signatures, you can create a custom solution by following these steps.
Step 1: Extract the timestamp and signatures from the header
Split the header, using the , character as the separator, to get a list of elements. Then split each element, using the = character as the separator, to get a prefix and value pair.
The value for the prefix t corresponds to the timestamp, and v1 corresponds to the signature (or signatures). You can discard all other elements.
Step 2: Prepare the signed_payload string
The signed_payload string is created by concatenating:
The timestamp (as a string)
The character .
The actual JSON payload (i.e., the request body)
Step 3: Determine the expected signature
Compute an HMAC with the SHA256 hash function. Use the endpoint’s signing secret as the key, and use the signed_payload string as the message.
Step 4: Compare the signatures
Compare the signature (or signatures) in the header to the expected signature. For an equality match, compute the difference between the current timestamp and the received timestamp, then decide if the difference is within your tolerance.
To protect against timing attacks, use a constant-time string comparison to compare the expected signature to each of the received signatures.
Code Example: Create a lib webhook.js file with content below
'use strict';constcrypto=require('crypto');constutils= {secureCompare: (a, b) => { a =Buffer.from(a); b =Buffer.from(b);if (a.length!==b.length) {returnfalse; }if (crypto.timingSafeEqual) {returncrypto.timingSafeEqual(a, b); }constlen=a.length;let result =0;for (let i =0; i < len; ++i) { result |= a[i] ^ b[i]; }return result ===0; },}constWebhook= { DEFAULT_TOLERANCE:300,// 5 minutesconstructEvent(payload, header, secret, tolerance) {this.signature.verifyHeader( payload, header, secret, tolerance ||Webhook.DEFAULT_TOLERANCE );constjsonPayload=JSON.parse(payload);return jsonPayload; },/** * Generates a header to be used for webhook mocking * * @typedef{object}opts * @property{number} timestamp - Timestamp of the header. Defaults to Date.now() * @property{string} payload - JSON stringified payload object, containing the 'id' and 'object' parameters * @property{string} secret - Webhook secret 'webhook_...' * @property{string} scheme - Version of API to hit. Defaults to 'v1'. * @property{string} signature - Computed webhook signature */};constsignature= { EXPECTED_SCHEME:'v1',_computeSignature: (payload, secret) => {return crypto.createHmac('sha256', secret).update(payload,'utf8').digest('hex'); },verifyHeader(payload, header, secret, tolerance) { payload =Buffer.isBuffer(payload) ?payload.toString('utf8') : payload;if (Array.isArray(header)) {thrownewError('Unexpected: An array was passed as a header, which should not be possible for the uiza-signature header.' ); } header =Buffer.isBuffer(header) ?header.toString('utf8') : header;constdetails=parseHeader(header,this.EXPECTED_SCHEME);if (!details ||details.timestamp ===-1) {throw {message:'Unable to extract timestamp and signatures from header',name:'unable_to_extract_timestamp_and_signature_from_header'} }if (!details.signatures.length) {throw {message:'No signatures found with expected scheme',name:'signature_not_found'} }constexpectedSignature=this._computeSignature(`${details.timestamp}.${payload}`, secret );constsignatureFound=!!details.signatures.filter(utils.secureCompare.bind(utils, expectedSignature) ).length;if (!signatureFound) {throw { message:"No signatures found matching the expected signature for payload.", name:'signature_not_found' } }consttimestampAge=Math.floor(Date.now() /1000) -details.timestamp;if (tolerance >0&& timestampAge > tolerance) {throw {message:'Timestamp outside the tolerance zone',name:'timestamp_outside_zone'} }returntrue; },};functionparseHeader(header, scheme) {if (typeof header !=='string') {returnnull; }returnheader.split(',').reduce( (accum, item) => {constkv=item.split('=');if (kv[0] ==='t') {accum.timestamp = kv[1]; }if (kv[0] === scheme) {accum.signatures.push(kv[1]); }return accum; }, { timestamp:-1, signatures: [], } );}Webhook.signature = signature;module.exports= Webhook;