Repository navigation
fix(admin): a custom admin view with a path skips the admin-auth redirect - #1034
Conversation
`Root` guards its own `handleAuthRedirect` with `!isCustomAdminView(...)`, and that helper matches on `view.path` alone — it reads no `public` flag, whatever its docstring claims (@payloadcms/next 3.90.2). So `analytics` and `map`, the two `admin.components.views` entries declaring a path, rendered the admin chrome to anyone. Nothing was exposed: both iframe a public URL and read no collection. The defect is that the next path-bearing view is unauthenticated by default, with no local signal and nothing in the suite to show it. `requireAdminAccess` is that gate, in `admin/helpers/` so the next view adds one call rather than its own copy of the rule. It reproduces `handleAuthRedirect`'s target, which is not exported: the unauthorized view for a signed-in user, the login view otherwise, each carrying the refused route back as `redirect`. It takes only the four reads it makes rather than a whole `InitPageResult`, so its spec calls it with a literal. `/admin` is untouched — the `dashboard` view declares no path, which is also why the gate turns on a config detail with no marker at the component. A rule in `sanitized-config-rules` walks the registrations and fails on a path-bearing view reaching no such call, so the next one cannot forget it quietly. Watched failing with the call removed from `AtlasMapView`, and the guard's own three cases watched failing against an inverted gate and a collapsed login/unauthorized split. `admin-route-config` now pins `login` and `unauthorized`, the two routes the guard composes a refusal from and the unit fixture assumes. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TJRT2BCgcc813AsggkH9YQ
The lane is 227 files and 2907 cases with the guard's spec added, and `perf-figures-pinned` holds PERF.md and its three count-quoting consumers to the tree. The twelfth session row carries the measured lane, and the gate row its warm run on that lane — 112 s where 103 s was measured on 226 files, which the file's own spread note already covers. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TJRT2BCgcc813AsggkH9YQ
…holds it Review-pass fixes, each from a finding verified against the source. The guard dropped the view's query. Payload's `handleAuthRedirect` folds the request's own `searchParams` into the `redirect` value, less an inbound `redirect` key, so a manager refused at `/admin/analytics?range=30d` was returned to the view with the range gone. It now composes the same value, and encodes it with `encodeURIComponent` rather than `URLSearchParams`, which spells a space `+` where Payload's `qs.stringify` spells it `%20`. `AdminAccessContext` takes its two route types from `Pick<SanitizedConfig['admin']['routes'], …>`, the repo's convention for a deliberately narrow parameter, instead of restating Payload's literals. The coverage rule now asserts a *call*, not a mention, so a guard imported and never called reddens where lint was the only thing standing in the way. Two ways it could have passed vacuously are closed: comments come off before the match, and a case asserts the excluded path still names the guard — the exclusion is a literal path, so moving the module would otherwise silence it and leave every importer green. The spec's `redirect` mock throws, as Next's own does, which is the shape `preview-auto-signin.int.spec.ts` already uses; `canAccessAdmin` is now a required key, so each case states its whole precondition. `admin-ui.md` and the spec cited a `dashboard` view that this repo does not register — `#1029` is not merged. The accurate illustration is `loginPlugin`'s pathless `login` view, which also shows why dropping the `path` is no fix: `getRouteData` routes a pathless view only by matching its key against Payload's own `oneSegmentViews`. PERF.md's gate row carries 112 s warm rather than 103 s, since that is now the slowest warm run, and `testing-reqs.md` follows it. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TJRT2BCgcc813AsggkH9YQ
The "one key still unwritten" section reads as though `canAccessAdmin` answers for the whole panel. It does not: Payload skips its auth redirect for a custom view declaring a `path`, so such a route consults the key itself or not at all. One pointer to `admin-ui.md`, where the rule lives. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TJRT2BCgcc813AsggkH9YQ
|
🚅 Deployed to the SahajCloud-pr-1034 environment in sahajcloud
1 service not affected by this PR
|
sydevs-bot
left a comment
There was a problem hiding this comment.
1 question(s) for the reviewer, one per thread.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
The diff matches the ticket. A guard, one call in each of the two path-bearing views, a config walk that fails on the next one, and the docs rule #1030 asked for because nothing local signals the gate. Product code is 61 lines over 3 files, in a helpers/ folder that already exists. Nothing is smuggled in.
The three alternatives the body dismisses are each genuinely closed. AdminViewConfig.Component is false | string | { path }, so no wrapper can exist. src/app/(payload)/admin/[[...segments]]/page.tsx carries Payload's own "DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME" — a harder reason than the selectiveCache one the body gives. And isCustomAdminView does match view.path alone. No contract surface is touched, so no consumer side applies.
Findings
should-fix— the guard'spermissionstype restates one Payload exports, for nothing (requireAdminAccess.ts:8).consider— the registration spec accepts a guard call anywhere in a view's transitive imports (sanitized-config-rules.spec.ts:275).
What was checked
Read both views whole, the guard, both new specs, tests/utils/importGraph.ts and payload.config.ts's views. Upstream: isCustomAdminView, handleAuthRedirect, Root/index.js:125-139. createLocalReq sets req.pathname and req.searchParams from the request URL, so the guard's two reads are sound.
Re-verified the preview anonymously, with /api/managers/me returning {"user":null}. /admin/analytics and /admin/map each carry a 307 to /admin/login?redirect=… in the RSC payload, no iframe, Fathom or SetStepNav, and no more chrome than /admin itself. ?range=30d&redirect=%2Felsewhere came back as redirect=%2Fadmin%2Fanalytics%3Frange%3D30d, so the query carry and the inbound drop both hold live. Both session-bound criteria stay manual.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
The guard's `permissions` restated `{ canAccessAdmin?: boolean }` inline,
so an upstream rename or retype left both call sites compiling while the
guard read `undefined` and refused every manager. `SanitizedPermissions`
is all-optional, so `Pick<InitPageResult, 'permissions'>` costs the spec
literal nothing and makes `pnpm typecheck` name the drift — verified by
renaming the upstream key, which reports TS2551 here.
The narrowing on `req` stays: that one earns its keep.
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ew3B4bUMmHxGyLospfaXeX
`reachableFiles` walked a view's whole transitive graph, so any module below it that called the guard satisfied a view that never did. The docblock's reason is the barrel case, and the entry plus its direct imports covers that, since a registration naming a folder re-exports the component one hop away. Watched it bite: with the call removed from `AnalyticsView` and a guard call planted two hops down, the old walk passed and this one fails. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Ew3B4bUMmHxGyLospfaXeX
COVERAGE.md listed neither the registration rule nor the guard's own spec. The admin-ui rule said a view "reaches no such call", which read as the whole import graph — it is now the view and its direct imports. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Ew3B4bUMmHxGyLospfaXeX
|
Both review findings adopted. The branch was already on the current
Gate ✓: the lean gate's five steps, 227 unit files / 2909 cases, and The 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
Three doc conflicts, all one root cause: main (#1034) and this branch each re-measured the unit lane and each bumped the counts PERF.md pins. Both session rows are kept — the file says to record a row, never replace one — so #1034's is twelfth, this branch's thirteenth, and this merge's own measurement is fourteenth. The pinned `Files / tests` cell, tests/AGENTS.md and docs/rules/testing-reqs.md all take the merged tree's figures. testing-reqs.md took one line from each side: this branch's wording, which says the 78 s is the slowest median recorded rather than a plain figure, and main's re-measured 112 s warm gate, which supersedes the 103 s both the prose and the Tier 2 row carried. Verified: the lean gate green on the merged tree (227 files, 2911 tests), plus admin-route-config.int.spec.ts, the int spec main added. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Y7SN4RkU4aopyud3HXNHMw
Three Markdown conflicts, all the same figures both sides re-pinned. `main` re-measured the warm lean gate at 112 s on 2026-10-25, so that row supersedes this branch's 103 s. The session table keeps both sides' rows — this branch's sixteenth and `main`'s own 227 / 2909 from #1034. The counts belonged to neither side: #1034 adds a unit spec, so the merged tree carries 228 files and 2918 cases, measured here inside the gate. `perf-figures-pinned.spec.ts` is what refuses the stale pair, and why the four consumer docs move with it. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01QLYiYBabKtcYdEPm1qE81C
Three conflicts, all the unit-lane counts again. #1034 added a unit spec and re-measured, so neither side's figures described the merged tree. The counts are re-measured here rather than picked: 228 files and 2924 tests, from two runs of the lane on the merged tree. PERF.md is the single writer, so AGENTS.md, tests/AGENTS.md and docs/rules/testing-reqs.md all take the same pair. main's new session row is kept under the PR labelling this branch introduced, and the numbering follows the dates. The gate's warm figure takes main's 112 s over this branch's 103 s — the target is held to the slowest measurement. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BqU6prAi33yWgUTScUkwLV
Five conflicts, four of them the pinned unit-lane counts and one an import list. Both sides' additions are kept everywhere. - `src/plugins/eventImport/importCollection.ts`: both sides added an import. This branch classifies `imports.locale` as `'unrelated'` (#912); main's #1013 declares the collection's retention windows. Both are used. - `src/migrations/index.ts`: both sides registered a migration. Ordered by timestamp, so #1013's `20261024_161437` precedes this branch's `20261025_195340`. - `tests/PERF.md`: both sides' session rows are kept, ordered by date, under the PR labelling this branch introduced, so main's #1013 and #1034 rows take it too. Ordinals follow the dates. - `docs/rules/testing-reqs.md`, `tests/AGENTS.md`: the counts differed only because each side added a spec file. Measured on the merged tree instead: 229 files / 2938 tests, 59.58 s on 4 cores. A sixth change the merge could not report. #1013's `20261021_152406_retention_registry` lands behind this branch's newer `20261025_195340`, so the newest snapshot still named the three replaced task slugs and `migrate:create` re-emitted both `task_slug` enum rebuilds — without the `DELETE`s that migration runs before it casts, which would abort the boot migration on the first legacy row. `20261026_204230_merge_1013_main_snapshot` keeps only its snapshot, the third time this branch has healed the chain that way. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01437mP62cow1952SftEcyXL
Three doc conflicts, all one root cause: main (#1034) and this branch each re-measured the unit lane and each bumped the counts PERF.md pins. Both session rows are kept — the file says to record a row, never replace one — so #1034's is twelfth, this branch's thirteenth, and this merge's own measurement is fourteenth. The pinned `Files / tests` cell, tests/AGENTS.md and docs/rules/testing-reqs.md all take the merged tree's figures. testing-reqs.md took one line from each side: this branch's wording, which says the 78 s is the slowest median recorded rather than a plain figure, and main's re-measured 112 s warm gate, which supersedes the 103 s both the prose and the Tier 2 row carried. Verified: the lean gate green on the merged tree (227 files, 2911 tests), plus admin-route-config.int.spec.ts, the int spec main added. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Y7SN4RkU4aopyud3HXNHMw
Summary
pathrendered withoutcanAccessAdmin. Both now refuse first.Change outline
Flow — what answers an anonymous
GET /admin/analyticsPayload's own gate is skipped for exactly these routes, so the refusal has to live in the view.
RootPage(/admin/analytics) @payloadcms/next/dist/views/Root/index.js initReq → { permissions, req } !permissions.canAccessAdmin && !isPublicAdminRoute && !isCustomAdminView isCustomAdminView: view.path === '/analytics' → true # matches on path alone → redirect(handleAuthRedirect(…)) never reached render AnalyticsView(initPageResult) src/components/admin/AnalyticsView.tsx + requireAdminAccess(initPageResult) src/components/admin/helpers/requireAdminAccess.ts + permissions.canAccessAdmin → return + refused = pathname + the view's own query, less an inbound `redirect` + req.user → /admin/unauthorized?redirect=<refused> + no user → /admin/login?redirect=<refused> DefaultTemplate → SetStepNav → iframeThe same for
AtlasMapViewat/admin/map./adminis untouched: the only other registered view isloginPlugin'slogin, which declares no path.Files
src/components/admin/ ├── AnalyticsView.tsx # gate, then Fathom's public share iframe ├── AtlasMapView.tsx # gate, then the public atlas iframe └── helpers/ + └── requireAdminAccess.ts # the one gate, and where a refusal goesBehaviour — the rule that holds it
Preview
Verified anonymously on this preview at 21:21Z, with
GET /api/managers/mereturning{"user":null}on the same host to confirm the requests carried no session:/admin/login?redirect=%2Fadmin%2Fanalytics· 307/admin/login?redirect=%2Fadmin%2Fmap· 307/admin/login· 307 — unchanged, Payload's own gateNeither view rendered: zero occurrences of
iframe,FathomorSetStepNavin either body.⚠ The status line is not what the ticket's checklist expected, and that is Next, not this change.
curl -w '%{http_code}'reports 200 with an emptyredirect_urlon all three routes, because the admin shell streams and aredirect()thrown after the first flush ships as a307directive inside the RSC payload rather than as a response status./admin, which this PR does not touch, behaves identically — so compare against that row, not against a bare 3xx. Follow the redirect (curl -L) or read the payload to see the target.Neither view reads a collection — both iframe a public URL — so none of these needed seeded data.
previewUrl.seedis unset in this repo, and nothing here would use it.Test results
tests/int/admin-route-config.int.spec.ts✓ 1 passedEach new assertion was watched failing against the real defect: the call removed from
AtlasMapView, the call commented out, the gate inverted, the login/unauthorized split collapsed, the query carry dropped, andADMIN_ACCESS_GUARDpointed at a moved file.Manual verification
Acceptance criteria 1 and 4 are verified above. The two that need a session are yours — this session has no
PREVIEW_ADMIN_EMAILin its environment, so it cannot sign in to the preview.canAccessAdminlands on/admin/unauthorized?redirect=…on both, not on the view (criterion 2). The unit spec covers the branch; only a real such manager exercises the permission that selects it.Notes for reviewer
AnalyticsViewiframes Fathom's public share URL andAtlasMapViewiframesSAHAJATLAS_URL; neither reads a collection. The fix is about the next path-bearing view, which would be unauthenticated by default with no local signal.handleAuthRedirectrather than calling it. That function is in none of@payloadcms/next's ten export subpaths, and the package declares no wildcard, so a deep import is refused by Node's exports gating.admin-route-config.int.spec.tsnow pins the two routes it composes from, andgetSafeRedirectinsrc/components/admin/SignIn/index.tsxis already the counterpart that sanitises the parameter it writes.tests/unit/admin-view-auth-guard.spec.tsassumes/admin,/loginand/unauthorizedare Payload's defaults. Checkedsrc/payload.config.ts, which sets neitherroutes.adminnoradmin.routes; both pins live inadmin-route-config.int.spec.ts.Findings raised in review and not acted on, each with its reason:
Six dismissed findings
@payloadcms/nextsoisCustomAdminViewhonours thepublicflag its own docstring claims. This is the true root cause, and the repo does patch (patches/[email protected]). Declined:publicis not a key ofAdminViewConfig, so declaring it needs a cast or module augmentation; unlike the drizzle-kit patch, which is a build-time CLI only, this one would sit in the request path of every admin render; and it needs re-verifying against nine exactly-pinned@payloadcms/*packages on every bump. See the question thread for what to do upstream instead.gatedView()wrapper at the registration, so the gate cannot be forgotten. Impossible:AdminViewConfig.Componentisfalse | string | { path, exportName? }— always a module path, never a function — so a wrapper inpayload.config.tscan only decoratepath/meta/exact.Root, insrc/app/(payload)/admin/[[...segments]]/page.tsx. One notch deeper and genuinely available, butselectiveCachekeys itsreqmemo on the overrides object by reference, so the wrapper cannot hit that cache andgetAccessResultswould run twice on every admin page load, not just these two. It would also have to re-implementisPublicAdminRouteand the create-first-user ordering, or lock out/admin/login— a fork ofRoot's redirect precedence against an exactly-pinned dependency.canAccessAdminneeds Payload andpg, and middleware is Edge. A cookie-presence check is Edge-safe but strictly weaker — a signed-in manager with no admin access passes it.NEXT_TRAILING_SLASHin theredirectvalue, as upstream does.PayloadRequest['pathname']isstringandformatAdminURL'spathis'' | `/${string}` | null, so this needs a cast in product code for a variable this repo does not set.custom-cell-drawer-select-coverage.spec.tsontotests/utils/importGraph.ts. Its resolver triesindex.tsbut notindex.tsx, and its walk follows relative imports only, so a cell reachingSelectableCellthrough an@/import is a false negative. Real, and left out of this PR: it changes an unrelated guard's strength and belongs in its own review. Both halves are bounded today by that spec's ownfinds the registrationscase.Why the gate is a call in each view rather than a type
Nothing type-level can replace the spec. Because
Componentis a path string, the type system never sees the component function, so it cannot demand a branded return; branding the config entry would only prove someone called a helper, not that the component calls the guard. The spec asserts the actual property, and is the documented house idiom for this class of defect —custom-cell-drawer-select-coverage.spec.tswalksCell:registrations the same way.Dropping the
pathis not an alternative either:isPathMatchingRoutereturns false for a pathless view, andgetRouteDatathen routes one only by matching its key against Payload's ownoneSegmentViews. That is whyloginPlugin's pathlessloginview works and why a pathlessanalyticswould be reachable at no URL at all.Closes #1030
🤖 Generated with Claude Code
https://claude.ai/code/session_01TJRT2BCgcc813AsggkH9YQ