Skip to content

Commit

Permalink
fix(webhooks): use @noble/hashes instead of crypto for signing
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertCraigie committed Nov 20, 2024
1 parent 2f1568c commit e6575de
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"fix": "./scripts/format"
},
"dependencies": {
"@noble/hashes": "^1.5.0",
"@types/node": "^18.11.18",
"@types/node-fetch": "^2.6.4",
"abort-controller": "^3.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1178,14 +1178,14 @@ export const getHeader = (headers: HeadersLike | Headers, header: string): strin
/**
* Encodes a string to Base64 format.
*/
export const toBase64 = (str: string | null | undefined): string => {
export const toBase64 = (str: string | Uint8Array | null | undefined): string => {
if (!str) return '';
if (typeof Buffer !== 'undefined') {
return Buffer.from(str).toString('base64');
}

if (typeof btoa !== 'undefined') {
return btoa(str);
return btoa(typeof str === 'string' ? str : String(str));
}

throw new FinchError('Cannot generate b64 string; Expected `Buffer` or `btoa` to be defined');
Expand Down
12 changes: 5 additions & 7 deletions src/resources/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import { APIResource } from '../resource';
import * as Shared from './shared';
import * as BenefitsAPI from './hris/benefits/benefits';
import { createHmac } from 'crypto';
import { getRequiredHeader, HeadersLike } from '../core';
import { getRequiredHeader, HeadersLike, toBase64 } from '../core';
import { hmac } from '@noble/hashes/hmac';
import { sha256 } from '@noble/hashes/sha2';

export class Webhooks extends APIResource {
/**
Expand Down Expand Up @@ -40,11 +41,8 @@ export class Webhooks extends APIResource {
) {
const encoder = new TextEncoder();
const toSign = encoder.encode(`${eventId}.${timestamp.getTime() / 1000}.${payload}`);

const hmac = createHmac('sha256', secret);
hmac.update(toSign);

return `v1,${hmac.digest('base64')}`;
const signed = toBase64(hmac(sha256, secret, toSign));
return `v1,${signed}`;
}

/** Make an assertion, if not `true`, then throw. */
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,11 @@
"@jridgewell/resolve-uri" "^3.1.0"
"@jridgewell/sourcemap-codec" "^1.4.14"

"@noble/hashes@^1.5.0":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.5.0.tgz#abadc5ca20332db2b1b2aa3e496e9af1213570b0"
integrity sha512-1j6kQFb7QRru7eKN3ZDvRcP13rugwdxZqCjbiAVZfIJwgj2A65UmT4TgARXGlXgnRkORLTDTrO19ZErt7+QXgA==

"@nodelib/[email protected]":
version "2.1.5"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
Expand Down

0 comments on commit e6575de

Please sign in to comment.