Repository navigation
fix(auth)!: check client credentials, validate the body, verify real TOTP codes - #223
Open
gjtorikian wants to merge 7 commits into
Open
gjtorikian wants to merge 7 commits into
gjtorikian wants to merge 7 commits into
Conversation
Production guards POST /user_management/authenticate before it validates the body: the client id, PKCE, the client secret, and then whether anything credentialed the request. The emulator minted a session for any client_id and client_secret, so a caller that omits or misspells them passes here and fails there. The emulator now answers as production does. A missing client_id is 400 invalid_request. A client_id not in the client_ form is 400 invalid_client. A client_secret that is not an unexpired API key of the environment, or is missing on a grant that needs one, is 400 invalid_client "Invalid client secret.". A refresh_token or device_code grant needs no secret, and a verified PKCE verifier stands in for one on authorization_code. A wrong or missing verifier for a code bound to a challenge, or a verifier for a code without one, is 400 invalid_grant "Invalid code verifier." before the grant runs, so the code is not spent and no event is recorded. Hosted AuthKit's /x/authkit/users/authenticate alias keeps authenticating with the API key alone, as its session bearer does in production. Source: packages/api/src/userland-sessions/guards/*.guard.ts in workos/workos; live sandbox diffs from the parity scan.
Production validates the authenticate body with the class-validator rules its DTOs declare before any grant runs, and answers 422 invalid_request_parameters with one errors[] entry per failing field, in the DTO's order. The emulator answered 400 invalid_request with one sentence, so the Node SDK raised OauthException here and UnprocessableEntityException there. The emulator now reports the same errors: grant_type_string_required for an absent or empty grant_type, <field>_string_required for a required field that is missing, empty, or not a string, and email_required for an address production's IsEmail refuses, taken as sent. Magic Auth's DTO lists IsEmail first and skips the address once user_id names the user, which the grant now also accepts. The base body is validated before the grant's fields. A missing refresh_token stays the grant's own 400, worded "Missing refresh token." as production words it. Source: packages/api/src/userland-sessions/dto/userland-session-token.dto.ts and common/pipes/validation-with-error-code.pipe.ts in workos/workos; live sandbox diffs from the parity scan.
Production verifies a challenge with the RFC 6238 code otplib computes
from the factor's secret (HMAC-SHA1, six digits, 30-second steps, one step
of drift either way). The emulator only accepted the random six-digit code
it stored on the challenge, so a client that computes the code with
otplib, speakeasy or an authenticator app never completed enrollment, the
factor was never listed, and a password login never stepped up.
The emulator now verifies a code computed from the enrolled secret on
POST /auth/challenges/{id}/verify, on the mfa-totp grant and on the hosted
login page, with node:crypto and no new dependency. The stored code still
verifies, as the convenience for a test that reads it off the store. The
one-time-code rules are unchanged: single use, three misses, expiry.
Source: packages/api-services/src/authentication-challenges/totp-authenticator.ts
in workos/workos; live diff in the parity scan (harness/out/auth-mfa-flow).
The Node compatibility suite signed in with the password grant and no client_id or client_secret, which the guard chain now refuses with 400 invalid_client, as production does. The call now names its client and proves it with the emulator's API key.
Confidence Score: 5/5
[High impact] The PR appears safe to merge; no new actionable issues remain. SummaryThe PR adds client credential checks, field-specific 422 responses, and real TOTP verification. The latest change fixes the earlier rejection of quoted email addresses.
|
…'s rule The alias skipped the whole guard block, and the grant no longer compared the verifier itself, so a challenge-bound code redeemed there without its verifier. PKCE is not a client credential: the alias keeps its exemption from client_id and client_secret only. The 422 email check used the storage bar (one @, no spaces), which let alice@example through where production answers email_required. The check now applies validator.js's isEmail rule, the one production's IsEmail decorator (packages/api/src/common/validators/is-email.validator.ts) applies; each refused and accepted case was confirmed against the live API.
A fixed guess such as 000000 is a real code for the factor's secret once in a million steps, now that computed codes verify. wrongTotpCode picks a six-digit code the secret does not produce in the window, nor one a test stored on the challenge.
validator.js's isEmail, the rule production applies, takes a quoted local
part ("john doe"@example.org) with backslash escapes inside; the check
refused one. Confirmed live: the quoted forms reach the credential check,
an unquoted embedded quote is 422 email_required.
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.
Finding 4 of the 2026-12-04 parity scan:
POST /user_management/authenticateminted a session for any client, answered a missing field with a shape production does not use, and refused a real TOTP code. Three commits, one per part.1. Client credentials (
fix(auth): check client credentials before any grant runs)Production runs four guards before the body is validated or a grant reads it:
UserlandClientIdGuard,UserlandPkceGuard,UserlandClientSecretGuard,UserlandAuthenticateGuard(packages/api/src/userland-sessions/guards/*.guard.ts). The emulator now runs the same checks, in the same order:client_id400 {"error":"invalid_request","error_description":"Missing required parameter: client_id."}client_idnot in theclient_form400 invalid_client"Invalid client id."client_secret, empty, unknown, expired, or an API key in theAuthorizationheader instead400 invalid_client"Invalid client secret."refresh_tokenordevice_codegrant without a secretauthorization_codewith a verified PKCE verifier and no secretcode_verifierfor a code bound to a challenge; a verifier for a code without one, unknown codes includedinvalid_request"code_verifier is required." or a spent code400 invalid_grant"Invalid code verifier.", before the grant runs; the code is not spent and no event is recordedThe emulator has one environment, so any id in the
client_form names it. Aclient_secretis any API key the emulator accepts forAuthorization: Bearer, unexpired. Hosted AuthKit's/x/authkit/users/authenticatealias keeps authenticating with the API key alone, as its session bearer does in production.2. Body validation (
fix(auth): answer 422 for a missing or malformed authenticate field)Production validates the body with the class-validator rules in
packages/api/src/userland-sessions/dto/userland-session-token.dto.tsthroughValidationWithErrorCodePipe, and answers422 invalid_request_parameterswith oneerrors[]entry per failing field, in DTO order. The Node SDK raisesUnprocessableEntityException; the emulator's400 invalid_requestmade it raiseOauthException.grant_type400 invalid_request422grant_type_string_required"Grant Type must be a non-empty string."code,email,password,pending_authentication_token,authentication_challenge_id,organization_id,device_code, …)400 invalid_requestwith one sentence422with<field>_string_required" must be a non-empty string." for each, in DTO orderpassword: 5)422" must be a string."invitation_token: "",ip_address: "")422emailon the password grant400 invalid_requestor 200422email_required"Email must be a valid email address."email400 invalid_request422email_required(its DTO listsIsEmailfirst); no check onceuser_idnames the user, which the grant now also acceptsrefresh_tokenmissing400 invalid_request"Missing refresh token." (optional to the DTO; the grant's own refusal)The base body (
grant_type, the optional context fields) is validated before the grant's fields, so its failures come back alone. An unimplemented grant (email completion, Radar) still validates its DTO first.3. TOTP (
fix(mfa): verify a TOTP code computed from the enrolled secret)Production verifies a challenge with the code otplib computes from the factor's secret (
packages/api-services/src/authentication-challenges/totp-authenticator.ts: HMAC-SHA1, six digits, 30-second steps,window: 1). The emulator accepted only the random code it stored on the challenge, so a client using otplib, speakeasy or an authenticator app never completed enrollment, the factor was never listed, and a password login never stepped up.POST /auth/challenges/{id}/verify, themfa-totpgrant and the hosted login page now verify a code computed from the enrolled secret, withnode:cryptoand no new dependency (totpCode,totpCodeMatches,verifyChallengeCodeinhelpers.ts; the RFC 6238 test vectors pin the implementation). The stored code still verifies, as the convenience for a test that reads it off the store. Production also blocks a code reused across challenges inside its window; the emulator does not, and the doc says so.Verified against the live sandbox
harness/out/prauth-validation/report.mdin the scan's scratchpad: 27 request pairs (every row above plus the success paths) answer the same status and body asapi.workos.comafter normalization. The one remaining difference is by design: a well-formedclient_idthat is not the environment's application isinvalid_clientlive and accepted here, since the emulator registers no AuthKit clients.Migration
A caller that sent
POST /user_management/authenticatewithoutclient_idandclient_secretnow gets400 invalid_client, which is what production would have answered. Sendclient_id(anyclient_…value) andclient_secret(the emulator's API key,sk_test_defaultby default) as the SDKs do. Therefresh_tokenanddevice_codegrants need onlyclient_id. Tests that asserted the old400 invalid_requestfor a missing field now get the 422 shape. Tests that read the MFA code off the store keep working.Tests and docs
bun test(2137 pass),bun run typecheck,bun run lint,bun run fmt:check.src/workos/totp.spec.ts;client credentialsandbody validationblocks inauth.spec.ts. Existing authenticate calls in 20 spec files now send client credentials.docs/behavior.md: new### Client credentialssection; the failure-shapes table and its prose;## Multi-Factor Auth.docs/testing.md: the authenticate examples send credentials.Not in this PR: the
mfa_challenge403 body shape (production returnsuserandauthentication_factors, no challenge),sso_requiredand the other step-up codes,email_verification/sendreturning{user}, and the access token'saudclaim — rows 5 to 8 of the scan's authentication findings.