use crypto.timingSafeEqual in hmac and ownerToken authentication
This commit is contained in:
parent
67b55d1477
commit
ebbb1d05d2
3 changed files with 201 additions and 25 deletions
|
@ -1,3 +1,4 @@
|
|||
const assert = require('assert');
|
||||
const crypto = require('crypto');
|
||||
const storage = require('../storage');
|
||||
const fxa = require('../fxa');
|
||||
|
@ -19,7 +20,7 @@ module.exports = {
|
|||
);
|
||||
hmac.update(Buffer.from(meta.nonce, 'base64'));
|
||||
const verifyHash = hmac.digest();
|
||||
if (verifyHash.equals(Buffer.from(auth, 'base64'))) {
|
||||
if (crypto.timingSafeEqual(verifyHash, Buffer.from(auth, 'base64'))) {
|
||||
req.nonce = crypto.randomBytes(16).toString('base64');
|
||||
storage.setField(id, 'nonce', req.nonce);
|
||||
res.set('WWW-Authenticate', `send-v1 ${req.nonce}`);
|
||||
|
@ -48,7 +49,11 @@ module.exports = {
|
|||
if (!req.meta) {
|
||||
return res.sendStatus(404);
|
||||
}
|
||||
req.authorized = req.meta.owner === ownerToken;
|
||||
const metaOwner = Buffer.from(req.meta.owner, 'utf8');
|
||||
const owner = Buffer.from(ownerToken, 'utf8');
|
||||
assert(metaOwner.length > 0);
|
||||
assert(metaOwner.length === owner.length);
|
||||
req.authorized = crypto.timingSafeEqual(metaOwner, owner);
|
||||
} catch (e) {
|
||||
req.authorized = false;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue