Skip to content

Commit b57c361

Browse files
committed
updated webhook verify to be more secure
1 parent a2c9333 commit b57c361

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 = "TzmTqUPhGiyHfKcpYoXePi/EVf0="
30+
const secret = "QK89mgP2v9KPGXVRp92IfYtHpbzrLpsjMp6sfWOPasQ="
31+
const payload = {"data":[{"id":"24","type":"application.created","attributes":{"createdAt":"2025-08-04T13:12:33.887Z","tags":{"key":"another-tag","test":"webhook-tag","number":"111"}},"relationships":{"application":{"data":{"id":"10006","type":"individualApplication"}}}}],"included":[{"id":"10006","type":"individualApplication","attributes":{"ssn":"663885441","tags":{"key":"another-tag","test":"webhook-tag","number":"111"},"email":"[email protected]","phone":{"number":"3476042441","countryCode":"1"},"status":"New","address":{"city":"Cedar Falls","state":"IA","street":"26 Cardinal Dr.","country":"US","postalCode":"50613"},"message":"Pre created application","archived":false,"fullName":{"last":"Mercado","first":"Cheryl"},"createdAt":"2025-08-04T13:12:33.887Z","maskedSSN":"*****5441","occupation":"Doctor","dateOfBirth":"1946-04-11","evaluationId":null,"decisionMethod":null,"decisionReason":null,"decisionUserId":null,"evaluationCodes":null,"evaluationScores":null,"evaluationOutcome":null,"evaluationEntityId":null,"soleProprietorship":false},"relationships":{"org":{"data":{"id":"2","type":"org"}}}}]}
32+
33+
const verifyResult = unit.webhooks.verify(signature, secret, payload)
34+
expect(verifyResult).toBeTruthy()
35+
})
2536
})

0 commit comments

Comments
 (0)