Skip to content

Commit d19f5c1

Browse files
committed
Fix upgrade problem with otplib existing secrets
1 parent 77662b4 commit d19f5c1

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

backend/internal/2fa.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import crypto from "node:crypto";
22
import bcrypt from "bcrypt";
3-
import { generateSecret, generateURI, verify } from "otplib";
3+
import { createGuardrails, generateSecret, generateURI, verify } from "otplib";
44
import errs from "../lib/error.js";
55
import authModel from "../models/auth.js";
66
import internalUser from "./user.js";
@@ -204,6 +204,13 @@ const internal2fa = {
204204
const result = await verify({
205205
token,
206206
secret,
207+
// These guardrails lower the minimum length requirement for secrets.
208+
// In v12 of otplib the default minimum length is 10 and in v13 it is 16.
209+
// Since there are 2fa secrets in the wild generated with v12 we need to allow shorter secrets
210+
// so people won't be locked out when upgrading.
211+
guardrails: createGuardrails({
212+
MIN_SECRET_BYTES: 10,
213+
}),
207214
});
208215

209216
if (result.valid) {
@@ -278,7 +285,11 @@ const internal2fa = {
278285
},
279286

280287
getUserPasswordAuth: async (userId) => {
281-
const auth = await authModel.query().where("user_id", userId).andWhere("type", "password").first();
288+
const auth = await authModel
289+
.query()
290+
.where("user_id", userId)
291+
.andWhere("type", "password")
292+
.first();
282293

283294
if (!auth) {
284295
throw new errs.ItemNotFoundError("Auth not found");

0 commit comments

Comments
 (0)