Make verifyTotp total so a malformed 2FA POST cannot kill the gateway - #345
Merged
Conversation
A POST to /dashboard/login/2fa with the `totp_code` field simply absent reached verifyTotp(undefined, secret). otpauth reads `.length` off the token, so the call raised `TypeError: Cannot read properties of undefined` inside an async route handler — an unhandled rejection, which is fatal, so one malformed request took the whole gateway process down. Same class as the t()-not-imported defect in #343: a bad request must render an error, never end the process. The secret argument is attacker-shaped too. /dashboard/login/2fa/setup and the settings enable_2fa action both pass a `secret` straight from the request body, and Secret.fromBase32(undefined) throws the same way, as does a string that is not base32 at all. So verifyTotp is now total on both arguments: a non-string or blank token, a non-string or blank secret, and a secret that will not parse as base32 each answer false. Nothing else changes — a number, an object or a junk string already answered false via otpauth's own null return, and a real code for the current period still verifies. Found on the R4 instance while building a client for the perch-interactive routes; one malformed POST restarted its gateway during #343's own verification.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The defect
A POST to
/dashboard/login/2fawith thetotp_codefield simply absent reachesverifyTotp(undefined, secret). otpauth reads.lengthoff the token, so it raisesTypeError: Cannot read properties of undefined (reading 'length')inside an async route handler. That is an unhandled rejection, which is fatal, so one malformed request takes the whole gateway process down.Same class as the
t()-not-imported defect in #343: a bad request must render an error, never end the process. This one was found the same evening, on the same instance, and offered rather than fixed then.Reproduced directly against the pinned otpauth:
The secret argument is attacker-shaped too
Three call sites, and the code is not the only field that comes out of a request body:
dashboard/index.js:240—/dashboard/login/2fa, the codedashboard/index.js:350—/dashboard/login/2fa/setup, the codedashboard/settings/sections/two-factor.js:124—enable_2fa, code andsecretSecret.fromBase32(undefined)throwsCannot read properties of undefined (reading 'replace'), and a string that is not base32 throws on parse.The fix
verifyTotpis now total on both arguments. A non-string or blank token, a non-string or blank secret, and a secret that will not parse as base32 each answerfalse. Guarding in the one function covers all three call sites.Nothing else changes. A number, an object or a junk string already answered
falsethrough otpauth's ownnullreturn, so the observable behaviour for every previously-working input is identical. A real code for the current period still verifies.Tests
Four cases appended to
tests/dashboard-2fa-login.test.js, the file #343 added:undefined,null) answers false instead of throwingThe first and third fail on
mainwith aTypeError. All ten in the file pass with the fix, as docsrf-middleware(14) andi18n-global-parity(7).