fix(auth): the browser-JWT branch now carries username, email and role - #1231
Open
lilyshen0722 wants to merge 1 commit into
Open
fix(auth): the browser-JWT branch now carries username, email and role#1231lilyshen0722 wants to merge 1 commit into
lilyshen0722 wants to merge 1 commit into
Conversation
`middleware/auth.ts` dispatched on the token prefix and left two different
shapes on `req.user`: the `cm_` API-token branch assigned
`{ id, username, email, role }`, the browser-JWT branch assigned `{ id }`.
Nothing errored — consumers simply read `undefined` for every browser session.
Two live consequences, both confirmed at 799e0d7:
- `github.ts:146` refuses genuine admins with `403 Admin only`; the same
admin holding an API token gets through.
- registry publish/install persist `publisher.name: undefined` (#1211).
Three call sites had already grown a private DB re-read to work around it —
`podController.isGlobalAdminRequest`, `agentProfile.canEditAgentAvatar` and
`marketplace-api.resolveUsername`, the last of which #1211 duplicates rather
than moves. `github.ts` was the site that never got its copy.
The JWT branch already runs one indexed `User.findById` per request for ban
enforcement, so widening that projection makes the two branches shape-identical
at zero extra round-trips. The existing local re-reads stay correct and become
redundant fast-paths; removing them is separate work.
The test drives the real middleware over a real JWT, because the suites
covering the affected routes inject `{ _id, role }` from a fake auth
middleware — i.e. the API-token shape — and so cannot see this defect. Its
`User` mock honours the projection string: with a `select()` that ignores its
argument, narrowing the middleware back to `.select('banned')` leaves all seven
tests green. Both mutations (narrowed projection, restored `{ id }`) redden
four of seven.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
middleware/auth.tsdispatches on the token prefix and leaves two different shapes onreq.user:cm_API token (:51){ id, username, email, role }:81){ id }Nothing errors. Consumers just read
undefinedfor every browser session.Two live consequences, both confirmed at
799e0d7dgithub.ts:146—if (req.user?.role !== 'admin') return res.status(403). A genuine admin in a browser session is refused403 Admin only; the same admin holding an API token gets through. Fails closed, loudly.registrypublish/install —publisher.namepersists asundefined. Fails open, silently. This is fix(registry): publisher.name is undefined for every browser-session publish #1211's defect.Why one fix rather than two
Three call sites had already grown a private DB re-read to work around this, each with its own comment rediscovering the same cause:
podController.isGlobalAdminRequest(:43)agentProfile.canEditAgentAvatar(:250) — "JWT auth populates req.user = { id } WITHOUT role (middleware/auth.ts:81)"marketplace-api.resolveUsername(:11) — which fix(registry): publisher.name is undefined for every browser-session publish #1211 copies intoregistry/helpers.tsunder a comment saying it was movedgithub.tsis the fourth site, and the one that never got its copy. Fixing it locally would make four.The JWT branch already runs one indexed
User.findById(id)per request for ban enforcement (#636). Widening that projection frombannedtobanned username email rolemakes the branches shape-identical at zero extra round-trips, so no consumer has to know which token type it was called with. The three existing local re-reads stay correct and become redundant fast-paths — removing them is separate work, deliberately not in this PR.On the test
It drives the real middleware over a real JWT. The suites covering the affected routes cannot see this defect: their fake auth middleware injects
req.user = { _id, role }directly — the API-token shape — so #809's/statustests pass today against the broken predicate.The
Usermock honours the projection string, and that is load-bearing rather than tidiness. With a naiveselect()that ignores its argument and returns the whole fixture, narrowing the middleware back to.select('banned')leaves all seven tests green — the fields arrive from the fixture, not from the query. Verified by mutation, both ways:.select('banned username email role')→.select('banned')req.user = { id, username, email, role }→req.user = { id }The
CONTROL:case stays green under both, by design — it guards against over-granting (role: 'admin'hardcoded), not against the defect.a user row without a role leaves role undefined, not defaultedpins that absence stays absent: a default here would grant or deny on data the row does not contain.Verification
backend/__tests__/unit/middleware/— 38/38, 7 suitesbackend/__tests__/unit/routes+controllers— 650/650, 99 suitestsc --noEmit— no errors in either touched file (pre-existing errors inscripts/are untouched)npm run lint— the two remaining errors on the new file are the repo-wideimport/no-unresolved+import/extensionspattern that every sibling middleware test carriesNot verified: no runtime exercise of
/api/github/statusor the registry routes against a live browser session — both findings are source-level plus the middleware unit test. I also did not count how many registry rows actually lackpublisher.name; no Mongo read path from this seat.Relates to #1211 (which fixes the registry sites at the consumer) and #809 (which touches
/statusbut leaves:146unchanged).🤖 Generated with Claude Code