Security rules
NGB022: Authorization decision from user-controllable state
Reports Angular route guards (CanActivateFn, CanMatchFn, CanActivateChildFn, CanLoadFn, and guard classes) whose decision is traced to a role, permission or admin flag read from state the user controls: URL parameters, Web Storage, cookies or window.name.
| Property | Value |
|---|---|
| Rule ID | NGB022 |
| Category | Navigation & authentication |
| Default severity | medium |
| CWE | CWE-602, CWE-807 |
| Built-in presets | recommended: warn, strict: error |
Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.
What it checks
Reports Angular route guards (CanActivateFn, CanMatchFn, CanActivateChildFn, CanLoadFn, and guard classes) whose decision is traced to a role, permission or admin flag read from state the user controls: URL parameters, Web Storage, cookies or window.name. Decisions based on server responses or in-memory state are not reported.
Why it matters
Route guards only decide what the browser navigates to. A role read from localStorage or a query parameter can be changed by the user in seconds, so the guard is a UI convenience, not access control. This is harmless if every API behind the guarded area enforces authorization on the server, and a privilege-escalation bug if it does not. The finding marks the places where that assumption must be verified.
Detection behavior and supported patterns
- Guards declared as
const g: CanActivateFn = …, functions and guard classes listed incanActivate/canMatch/canActivateChild/canLoadroute arrays, classes implementingCanActivateetc. - Decision expressions: returned values,
ifconditions and ternaries inside the guard; values are traced through services and helpers (taint engine) - Sources: ActivatedRoute /
locationURL values,localStorage/sessionStoragereads,document.cookie,window.name - Only values named like authorization data are reported:
role,roles,isAdmin,permissions,privileges,entitlements,superuser, …
Example
export const adminGuard: CanActivateFn = () => localStorage.getItem('role') === 'admin';Safer alternative
export const adminGuard: CanActivateFn = () => inject(SessionService).me().pipe(map((me) => me.roles.includes('admin')));// …and the /api/admin/** endpoints check the role on the server.Remediation
Keep guards for navigation UX, but derive roles from a server response (e.g. /api/me) held in memory, and make every API endpoint behind the guarded area enforce the same authorization on the server. Never accept roles or feature flags from URL parameters.
False-positive considerations
- Applications whose APIs enforce authorization server-side use guards purely for UX; suppress the finding with a justification referencing the server check.
Known limitations
- Server-side authorization is not visible. A finding does not mean the protected data is exposed; it means the client-side check alone proves nothing.
- Roles decoded from a JWT read from storage are reported (the user can edit the stored token; the signature is only checked by the server).
- Guards built by factories (
roleGuard("admin")) are analyzed only when the returned function is visible in the factory body.
References
Run it
ngbastion explain NGB022