Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion resources/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ export class Webhooks extends BaseResource {
}

public verify(signature: string, secret: string, payload: any) {
const signatureBuffer = Buffer.from(signature, "base64")
const hmac = crypto.createHmac("sha1", secret)
hmac.update(JSON.stringify(payload))
return hmac.digest("base64") == signature

return crypto.timingSafeEqual(hmac.digest(), signatureBuffer)
Comment on lines +48 to +49
Copy link

Copilot AI Aug 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comparison will fail because hmac.digest() returns a Buffer while signatureBuffer is created from base64. Both should use the same encoding - either both as base64 strings or both as raw Buffers. Consider using hmac.digest('base64') or converting signatureBuffer to raw bytes.

Suggested change
return crypto.timingSafeEqual(hmac.digest(), signatureBuffer)
const computedSignatureBuffer = Buffer.from(hmac.digest('base64'), 'base64')
return crypto.timingSafeEqual(computedSignatureBuffer, signatureBuffer)

Copilot uses AI. Check for mistakes.
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/webhooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@ describe("Get Webhook Test", () => {
expect(res.data.type === "webhook").toBeTruthy()
})
})
})

describe("Verify Webhook test", () => {
test("verify webhook signature", () => {
const signature = "UUNz8ch1Ovjg+ijXUEwlAlWEktU="
const secret = "OB2HL5E3B4HJ7IVXRNL4YQKYIQIVJK36ZZLPZEFWZVSDSC7LLFJQ===="
const payload = {"data":[{"id":"46306092","type":"application.approved","attributes":{"createdAt":"2025-08-05T06:48:38.957Z","tags":{"key":"another-tag","test":"webhook-tag","number":"111"}},"relationships":{"application":{"data":{"id":"3895367","type":"individualApplication"}},"customer":{"data":{"id":"3310133","type":"individualCustomer"}}}}]}

const verifyResult = unit.webhooks.verify(signature, secret, payload)
expect(verifyResult).toBeTruthy()
})
})
Loading