Skip to content

Commit c88de65

Browse files
committed
Fix #5274 2fa backup codes not validating properly
1 parent ac4efd2 commit c88de65

4 files changed

Lines changed: 17 additions & 14 deletions

File tree

backend/internal/2fa.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,20 +198,23 @@ const internal2fa = {
198198
return false;
199199
}
200200

201-
// Try TOTP code first
202-
const result = await verify({
203-
token,
204-
secret,
205-
});
206-
207-
if (result.valid) {
208-
return true;
201+
// Try TOTP code first, if it's 6 chars. it will throw errors if it's not 6 chars
202+
// and the backup codes are 8 chars.
203+
if (token.length === 6) {
204+
const result = await verify({
205+
token,
206+
secret,
207+
});
208+
209+
if (result.valid) {
210+
return true;
211+
}
209212
}
210213

211214
// Try backup codes
212215
const backupCodes = auth?.meta?.backup_codes || [];
213216
for (let i = 0; i < backupCodes.length; i++) {
214-
const match = await bcrypt.compare(code.toUpperCase(), backupCodes[i]);
217+
const match = await bcrypt.compare(token.toUpperCase(), backupCodes[i]);
215218
if (match) {
216219
// Remove used backup code
217220
const updatedCodes = [...backupCodes];

backend/schema/paths/tokens/2fa/post.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"code": {
1919
"minLength": 6,
20-
"maxLength": 6,
20+
"maxLength": 8,
2121
"type": "string",
2222
"example": "012345"
2323
}

backend/schema/paths/users/userID/2fa/backup-codes/post.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717
],
1818
"requestBody": {
19-
"description": "Verififcation Payload",
19+
"description": "Verification Payload",
2020
"required": true,
2121
"content": {
2222
"application/json": {
@@ -25,7 +25,7 @@
2525
"properties": {
2626
"code": {
2727
"minLength": 6,
28-
"maxLength": 6,
28+
"maxLength": 8,
2929
"type": "string",
3030
"example": "123456"
3131
}

backend/schema/paths/users/userID/2fa/enable/post.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717
],
1818
"requestBody": {
19-
"description": "Verififcation Payload",
19+
"description": "Verification Payload",
2020
"required": true,
2121
"content": {
2222
"application/json": {
@@ -25,7 +25,7 @@
2525
"properties": {
2626
"code": {
2727
"minLength": 6,
28-
"maxLength": 6,
28+
"maxLength": 8,
2929
"type": "string",
3030
"example": "123456"
3131
}

0 commit comments

Comments
 (0)