⚙️ The Policy Attached to the Group, Not Actually to Anything Inside It
Calling RequireAuthorization() on a Minimal API RouteGroupBuilder looks like it should protect every endpoint mapped under that group, and in current versions it generally does – but the metadata is attached to the group’s own convention list, and an endpoint mapped through a NESTED group, through a separately-constructed MapGroup() call that isn’t chained directly off the protected group, or through an extension method that maps routes onto a plain IEndpointRouteBuilder rather than the group itself can end up outside that convention’s reach, inheriting none of the authorization requirement while still being reachable at a URL that visually looks like it’s under the protected prefix.
🔎 The Problem
var admin = app.MapGroup("/admin").RequireAuthorization("AdminOnly");
admin.MapGet("/dashboard", GetDashboard); // protected correctly
// A nested group built from the ORIGINAL app builder instead of
// from `admin` - it shares the /admin URL prefix by coincidence of
// naming, but was never chained off the group that actually carries
// the RequireAuthorization() convention:
var reports = app.MapGroup("/admin/reports");
reports.MapGet("/export", ExportReport); // reachable with NO auth
// check at all, despite
// sitting under /admin/
✅ Fix: Chain Every Sub-Group Off the Protected Group, Not Off the Root Builder
- Build nested route groups by calling MapGroup() on the already-protected group variable (admin.MapGroup(“/reports”) rather than app.MapGroup(“/admin/reports”)) – chaining this way means the nested group genuinely inherits the parent’s RequireAuthorization() convention instead of merely sharing its URL prefix by coincidence.
- After adding or restructuring any route group hierarchy, write an integration test that specifically calls each endpoint without an auth token and asserts a 401/403 – a test asserting the HAPPY path (a valid token gets a 200) will not catch this bug, since the broken endpoint still works fine for an authenticated caller too.
- Where the route structure genuinely needs flexibility that makes consistent group-chaining awkward, apply RequireAuthorization() as an explicit call on every individually mapped endpoint instead of relying on inherited group metadata – more verbose, but immune to this specific inheritance gap entirely.
🚨 Why This Is Dangerous
- This is close to the worst possible failure mode for an authorization bug – it fails open, not closed, meaning an endpoint that was SUPPOSED to require authorization is simply reachable by anyone, with every normal request (including ones with no credentials at all) returning a completely normal 200 response instead of an error that would draw attention to the problem.
- The URL structure itself actively hides the bug during a casual review – /admin/reports/export visually reads as obviously nested under the protected /admin prefix, so a developer scanning the route table for anything ‘outside’ the admin group has no reason to suspect this specific endpoint, since it isn’t outside the group by URL, only by code structure.
A route group’s URL prefix and its authorization convention are two separate things that happen to usually travel together – the moment a sub-group is built from the wrong parent, the URL keeps the appearance of protection while the actual policy quietly stops applying.
