Skip to content

Commit 7b0b07b

Browse files
authored
Merge pull request #524 from unit-finance/yanivs-webhook-pr
Webhook verify to be more secure
2 parents 7161e56 + 02336dc commit 7b0b07b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

resources/webhooks.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ export class Webhooks extends BaseResource {
4242
}
4343

4444
public verify(signature: string, secret: string, payload: any) {
45+
const signatureBuffer = Buffer.from(signature, "base64")
4546
const hmac = crypto.createHmac("sha1", secret)
4647
hmac.update(JSON.stringify(payload))
47-
return hmac.digest("base64") == signature
48+
49+
return crypto.timingSafeEqual(hmac.digest(), signatureBuffer)
4850
}
4951
}
5052

tests/webhooks.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,15 @@ describe("Get Webhook Test", () => {
2222
expect(res.data.type === "webhook").toBeTruthy()
2323
})
2424
})
25+
})
26+
27+
describe("Verify Webhook test", () => {
28+
test("verify webhook signature", () => {
29+
const signature = "UUNz8ch1Ovjg+ijXUEwlAlWEktU="
30+
const secret = "OB2HL5E3B4HJ7IVXRNL4YQKYIQIVJK36ZZLPZEFWZVSDSC7LLFJQ===="
31+
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"}}}}]}
32+
33+
const verifyResult = unit.webhooks.verify(signature, secret, payload)
34+
expect(verifyResult).toBeTruthy()
35+
})
2536
})

0 commit comments

Comments
 (0)