Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

Commit f6ab498

Browse files
authored
fix(codes): drop all codes when one is consumed (#326) r=@rfk
Fixes https://github.com/mozilla/fxa-bugzilla-mirror/issues/440
1 parent c226b07 commit f6ab498

File tree

6 files changed

+52
-5
lines changed

6 files changed

+52
-5
lines changed

db-server/test/backend/db_tests.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,12 +1334,16 @@ module.exports = function (config, DB) {
13341334
})
13351335

13361336
describe('unblockCodes', () => {
1337-
let uid1, code1
1337+
let uid1, code1, code2
13381338
beforeEach(() => {
13391339
uid1 = newUuid()
13401340
code1 = unblockCode()
13411341

1342-
return db.createUnblockCode(uid1, code1)
1342+
code2 = unblockCode()
1343+
return P.all([
1344+
db.createUnblockCode(uid1, code1),
1345+
db.createUnblockCode(uid1, code2)
1346+
])
13431347
})
13441348

13451349
it('should fail to consume unknown code', () => {
@@ -1350,6 +1354,18 @@ module.exports = function (config, DB) {
13501354
})
13511355
})
13521356

1357+
it('should fail to consume old unblock code', () => {
1358+
return db.consumeUnblockCode(uid1, code1)
1359+
.then((code) => {
1360+
assert.ok(code)
1361+
return db.consumeUnblockCode(uid1, code2)
1362+
.then(assert.fail, (err) => {
1363+
assert.equal(err.code, 404, 'err.code')
1364+
assert.equal(err.errno, 116, 'err.errno')
1365+
})
1366+
})
1367+
})
1368+
13531369
it('should consume unblock code', () => {
13541370
return db.consumeUnblockCode(uid1, code1)
13551371
.then((code) => {

lib/db/mem.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,8 @@ module.exports = function (log, error) {
10501050
return P.reject(error.notFound())
10511051
}
10521052
var timestamp = row[code]
1053-
delete row[code]
1053+
// Delete all codes for that uid
1054+
unblockCodes[uid.toString('hex')] = null
10541055

10551056
return P.resolve({ createdAt: timestamp })
10561057
}

lib/db/mysql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ module.exports = function (log, error) {
848848
)
849849
}
850850

851-
var CONSUME_UNBLOCK_CODE = 'CALL consumeUnblockCode_1(?, ?)'
851+
var CONSUME_UNBLOCK_CODE = 'CALL consumeUnblockCode_2(?, ?)'
852852

853853
MySql.prototype.consumeUnblockCode = function (uid, code) {
854854
// hash the code since it's like a password

lib/db/patch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
// The expected patch level of the database. Update if you add a new
66
// patch in the ./schema/ directory.
7-
module.exports.level = 76
7+
module.exports.level = 77

lib/db/schema/patch-076-077.sql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
SET NAMES utf8mb4 COLLATE utf8mb4_bin;
2+
3+
CREATE PROCEDURE `consumeUnblockCode_2` (
4+
inUid BINARY(16),
5+
inCodeHash BINARY(32)
6+
)
7+
BEGIN
8+
DECLARE timestamp BIGINT;
9+
SET @timestamp = (
10+
SELECT createdAt FROM unblockCodes
11+
WHERE
12+
uid = inUid
13+
AND
14+
unblockCodeHash = inCodeHash
15+
);
16+
17+
DELETE FROM unblockCodes
18+
WHERE
19+
uid = inUid;
20+
21+
SELECT @timestamp AS createdAt;
22+
END;
23+
24+
UPDATE dbMetadata SET value = '77' WHERE name = 'schema-patch-level';

lib/db/schema/patch-077-076.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- SET NAMES utf8mb4 COLLATE utf8mb4_bin;
2+
3+
-- DROP PROCEDURE `consumeUnblockCode_2`;
4+
5+
-- UPDATE dbMetadata SET value = '76' WHERE name = 'schema-patch-level';
6+

0 commit comments

Comments
 (0)