Skip to content

Build your personal knowledge base with Trilium Notes

fix(totp): skip time steps already used when matching a code

José M. Requena Plens2f7a388fa2f2
diff --git a/apps/server/src/services/totp.spec.ts b/apps/server/src/services/totp.spec.ts
index e3f2c162bc..f56694b87b 100644
--- a/apps/server/src/services/totp.spec.ts
+++ b/apps/server/src/services/totp.spec.ts
@@ -252,6 +252,26 @@ describe("totp", () => {
}
});
+ it("verifyTOTP only tries steps after the last used one", () => {
+ // Six-digit codes can repeat between steps; a code that matches every step stands in.
+ mockValidate.mockReturnValue(true);
+ vi.useFakeTimers({ toFake: [ "Date" ] });
+ try {
+ cls.init(() => totp.setSecret(SECRET));
+ vi.setSystemTime(new Date("2026-01-01T00:00:40Z"));
+ const step = Math.floor(Date.now() / 1000 / 30);
+ const verify = () => cls.init(() => totp.verifyTOTP("000000"));
+
+ for (const expected of [ step - 1, step, step + 1 ]) {
+ expect(verify()).toBe(true);
+ expect(options.getOption("totpLastUsedStep")).toBe(String(expected));
+ }
+ expect(verify()).toBe(false);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it("enrollment accepts a step either side, the wizard only the current one", async () => {
const codeFor = await useRealCodes();
vi.useFakeTimers({ toFake: [ "Date" ] });
diff --git a/apps/server/src/services/totp.ts b/apps/server/src/services/totp.ts
index 631ee2a0db..146d5ef32d 100644
--- a/apps/server/src/services/totp.ts
+++ b/apps/server/src/services/totp.ts
@@ -60,14 +60,20 @@ const CLOCK_DRIFT_STEPS = 1;
/**
* Returns the time step `submittedPasscode` was generated for, or `null` if it matches none of the
- * steps within `drift` of the current one.
+ * steps within `drift` of the current one. Steps up to `lastUsedStep` are never tried.
*/
-function findTimeStep(secret: string, submittedPasscode: string, drift: number): number | null {
+function findTimeStep(
+ secret: string,
+ submittedPasscode: string,
+ drift: number,
+ lastUsedStep = -Infinity
+): number | null {
const config = generateConfig();
const currentStep = Math.floor(Date.now() / 1000 / config.period);
+ const firstStep = Math.max(currentStep - drift, lastUsedStep + 1);
try {
- for (let step = currentStep - drift; step <= currentStep + drift; step++) {
+ for (let step = firstStep; step <= currentStep + drift; step++) {
const code = { passcode: submittedPasscode, secret: secret.trim(), counter: step };
if (Hotp.validate(code, config)) return step;
}
@@ -107,9 +113,9 @@ function verifyTOTP(submittedPasscode: string): boolean {
const secret = getTotpSecret();
if (!secret) return false;
- const step = findTimeStep(secret, submittedPasscode, CLOCK_DRIFT_STEPS);
- const lastUsedStep = options.getOptionOrNull("totpLastUsedStep");
- if (step === null || (lastUsedStep && step <= Number(lastUsedStep))) return false;
+ const lastUsedStep = Number(options.getOptionOrNull("totpLastUsedStep")) || -Infinity;
+ const step = findTimeStep(secret, submittedPasscode, CLOCK_DRIFT_STEPS, lastUsedStep);
+ if (step === null) return false;
options.setOption("totpLastUsedStep", String(step));
return true;