Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,22 @@ module.exports = {
h1.update(hmac.digest());
h1 = h1.digest();
var h2 = crypto.createHmac(hashAlg, secret);
h2.update(new Buffer(parsedSignature.params.signature, 'base64'));

var signatureBase64 = parsedSignature.params.signature;
var signatureBuffer;
if (Buffer.from && Buffer.from !== Uint8Array.from) {
// Node.js 4.5.0 and newer
signatureBuffer = Buffer.from(signatureBase64, 'base64');
} else {
// Node.js <4.5.0 || >=5.0.0 <5.10.0
if (typeof signatureBase64 === 'number') {
Copy link

@jacobq jacobq Aug 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to guard against invalid input, wouldn't it make more sense move this check higher? Perhaps it should also be formulated as an assert since that's what's being done elsewhere in this code e.g.

// ...
var signatureBase64 = parsedSignature.params.signature;
assert.string(signatureBase64, 'signatureBase64');
var signatureBuffer;
// ...

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion is made by Buffer.from internally. Although I agree assert.string can be used for the fallback rather than the type checking.

A TypeError will be thrown if string is not a string.

Source: https://nodejs.org/docs/latest-v10.x/api/buffer.html#buffer_class_method_buffer_from_string_encoding

Copy link

@jacobq jacobq Aug 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so, IIUC, the rationale for pushing the check down into the fall-back code is that it makes it more obvious that it can be removed when the fall-back code is removed. I guess that makes sense. It just seemed a little odd to me that there are ~10 lines of code just to use Buffer.from if it's available...maybe I'm naïve, but it seems like it ought to be a one-liner.

// type-guard against uninitentional uninitialized Buffer allocation
throw new Error('Unexpected .signature type: number, string expected');
}
signatureBuffer = new Buffer(signatureBase64, 'base64');
}
h2.update(signatureBuffer);

h2 = h2.digest();

/* Node 0.8 returns strings from .digest(). */
Expand Down