EasyPost / easypost-node

EasyPost Shipping API Client Library for Node
https://easypost.com/docs/api
MIT License
139 stars 54 forks source link

fix: webhook validation when float weight field is present (closes #467) #471

Closed Justintime50 closed 3 months ago

Justintime50 commented 3 months ago

Description

When the weight field is present on a webhook (eg: tracking update), it gets converted but JS drops the decimal if .0 which often is the case. This breaks webhook validation because HMAC signatures won't match if it's altered at all. This properly puts back the .0 if necessary and missing to ensure that validation will continue to work. Tests validate that we properly compare the signatures

Testing

Pull Request Type

Please select the option(s) that are relevant to this PR.

footcarts commented 3 months ago

Hi,

I just noticed that this regex is missing an edge case. When I initially wrote it, I only saw examples of weight with values of null and whole numbers formatted with .0. After running the regex for a while, I discovered that weight can also have other decimal values, like 614.4. In this case, the old regex does not ignore these values and incorrectly inserts .0, resulting in 614.4 being transformed into 61.04.4. I have now updated the regex to support these cases.

.replace(/("weight":\s*)(\d+)(\s*)(?=,|\})/g,"$1$2.0")

We have been running this updated version and have had 0 webhook validation errors.

Justintime50 commented 3 months ago

Fantastic, thanks for the updated info! We've incorporated that.