chore: replace scmp with crypto.timingSafeEqual#1184
chore: replace scmp with crypto.timingSafeEqual#1184Methuselah96 wants to merge 1 commit intotwilio:mainfrom
Conversation
|
Investigating failing tests |
|
Fixed |
| if (a.length !== b.length) return false; | ||
| return crypto.timingSafeEqual(a, b); |
There was a problem hiding this comment.
Returning earlier would be unsafe because it would leak timing info. Suggest doing the HMAC beforehand:
const salt = crypto.randomBytes(42);
return crypto.timingSafeEqual(
crypto.createHmac("sha256", salt).update(a).digest(),
crypto.createHmac("sha256", salt).update(b).digest(),
);There was a problem hiding this comment.
Hmm, I'm out of my depth here, but scmp was returning earlier based on the length, so seems like this might be a pre-existing issue
There was a problem hiding this comment.
According to this comment, it's okay to check the length first and return early if they're not equal
There was a problem hiding this comment.
However, according to this article, you should not return early and instead still compare two values. That seems like a more understandable solution to me, so I've updated the PR to that solution.
There was a problem hiding this comment.
Both negate when lengths differ or HMAC works.
The reason I suggested a later approach is that it is not always clear whether the input is from a or b, thus making it error-prone without context.
btw: I wrote that code snippet on Cloudflare docs.
There was a problem hiding this comment.
👍 I've updated the variable names to more clearly indicate which input is the user value and which one is the secret value and verified that we are passing the arguments in the correct order that matches the parameter order
There was a problem hiding this comment.
Given that it has only been used internally, I think it is good enough to make a cut. LGTM
Thanks.
|
+1 from a downstream consumer. We hit If maintainers want a small validation harness, the existing webhook/auth-token signature validators that route through |
Fixes #1168
scmp is now deprecated and is equivalent to
crypto.timingSafeEqual(which is supported by Node.js >=6.6.0)Checklist