Skip to content

Commit

Permalink
fix: edge case regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Justintime50 committed Aug 14, 2024
1 parent 75f13f2 commit 615361c
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default class Utils {
// Fixes Javascript's float to string conversion. See https://github.com/EasyPost/easypost-node/issues/467
const correctedEventBody = Buffer.from(eventBody)
.toString('utf8')
.replace(/("weight":)(\d+)(?!\.\d)/g, '$1$2.0');
.replace(/("weight":\s*)(\d+)(\s*)(?=,|\})/g, '$1$2.0');

const expectedSignature = crypto
.createHmac('sha256', encodedSecret)
Expand Down
4 changes: 1 addition & 3 deletions test/services/webhook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ describe('Webhook Service', function () {
);

expect(webhookBody.description).to.equal('tracker.updated');
// JS converts this from `136.0` in the response to `136` on the user's end which is unfortunate; however, we
// compare signatures with the correct number prior to JSON parsing it and returning it to the user.
expect(webhookBody.result.weight).to.equal(136.0);
expect(webhookBody.result.weight).to.equal(614.4); // Ensure we convert floats properly
});

it('throws an error when a webhook secret is a differing length', function () {
Expand Down

0 comments on commit 615361c

Please sign in to comment.