#30: stop enrolment from killing the auth service, and from enabling MFA without recovery codes

Confirming a second factor returned "Upstream service is unreachable" - the
Next proxy's message for a backend it cannot reach. The backend was not
unreachable, it was dead: the process had just been OOM-killed.

generateRecoveryCodes hashed all ten codes with Promise.all. Argon2id here is
configured for 64 MiB per hash, so ten concurrently reserve 640 MiB against a
container limited to 512 MiB. Hashing is now sequential, peaking at 64 MiB and
taking a few seconds - the right trade for something that happens once per
enrolment. A timing-based test covers it, against the real hash rather than a
mock, because the cost being guarded is memory held concurrently and only the
real implementation reserves it.

The worse half is what the crash left behind. The enrolment row was marked
CONFIRMED before the codes were generated, so the account came out of this with
a second factor enabled, no recovery codes, and nobody having seen any: locked
behind an authenticator entry with no way back. That is how an account ends up
"assuming" it has a factor its owner cannot use.

The order is now verify, store the codes, then confirm. Anything that fails in
between leaves the enrolment pending and retryable, which is the recoverable
end of that failure.

Both bugs sat behind the one fixed in #28 - the code check failed first, so the
crash was never reached.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Opus 5 committed 2026-08-03 11:49:00 -03:00
1 parent c7251211b5
commit 6935f9feeb
4 files changed
+72 -7

No files matched your search

+31
View File
@@ -212,3 +212,34 @@ describe("TOTP code parsing", () => {
}
});
});
describe("recovery code generation stays inside the container's memory", () => {
// Argon2id here is configured for 64 MiB per hash. Hashing the ten codes in
// parallel reserved 640 MiB, over the auth container's 512 MiB limit, and the
// process was OOM-killed in the middle of enrolment - which reached the
// browser as "upstream service is unreachable" and left the account with a
// confirmed second factor and no recovery codes.
//
// Timed against the real hash rather than a mock: the cost here is memory
// held concurrently, and only the real implementation reserves it. Four codes
// sequentially must take roughly four times one hash; in parallel they would
// take about one.
it("hashes the codes one at a time, not all at once", { timeout: 60000 }, async () => {
const { hashPassword } = await import("./crypto.js");
const { generateRecoveryCodes } = await import("./mfaHelpers.js");
const singleStart = Date.now();
await hashPassword("baseline");
const single = Date.now() - singleStart;
const batchStart = Date.now();
const out = await generateRecoveryCodes(4);
const batch = Date.now() - batchStart;
expect(out.rawCodes).toHaveLength(4);
expect(out.hashedCodes).toHaveLength(4);
expect(out.hashedCodes.every((h) => h.startsWith("$argon2id$"))).toBe(true);
// Generous lower bound: parallel would land near 1x, sequential near 4x.
expect(batch).toBeGreaterThan(single * 2.5);
});
});