# Role Hierarchy Enforced

- **Pattern:** `ab-002250` (`saas-authorization.access-control.role-hierarchy-enforced`)
- **Severity:** high
- **Lifecycle:** active
- **Last modified:** 2026-07-06
- **Canonical URL:** https://auditbuffet.com/patterns/ab-002250
- **License:** CC-BY-4.0 — attribute to AuditBuffet Pattern Catalog (https://auditbuffet.com/patterns/ab-002250)

## Why it matters

When roles are checked with exact string equality (`role === 'member'`), a higher-privilege user fails checks designed for lower-privilege users — they must be explicitly listed in every condition they should satisfy. This creates access gaps that worsen as the role model grows. An Admin denied access to a Member-gated feature is a support burden at best; an Admin who can bypass a Member-gated resource because the hierarchy isn't enforced is a CWE-284 / OWASP A01 violation at worst. NIST 800-53 AC-3 expects access enforcement to reflect the intended privilege model, not accidental exclusion through literal comparisons.

## Severity rationale

High because exact-equality role checks produce both access gaps (Admins locked out) and privilege inversions (lower roles unexpectedly included) depending on how conditions are written.

## Remediation

Replace exact role comparisons with a `hasMinimumRole` helper that encodes the hierarchy numerically. Once the rank table exists, every future role check becomes a single function call.

```typescript
// lib/auth/roles.ts
const ROLE_RANK: Record<string, number> = { viewer: 1, member: 2, admin: 3 };

export function hasMinimumRole(user: { role: string }, required: string): boolean {
  return (ROLE_RANK[user.role] ?? 0) >= (ROLE_RANK[required] ?? Infinity);
}

// Usage: if (!hasMinimumRole(user, 'member')) return forbidden();
```

Review all existing `user.role === 'x'` checks and replace with `hasMinimumRole(user, 'x')` wherever hierarchy applies.

## Detection

**The detection method for this pattern is part of AuditBuffet Pro.**

The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the [Authorization Audit](https://auditbuffet.com/audits/saas-authorization). The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.

## External references

- cwe CWE-284
- cwe CWE-285
- owasp A01
- nist AC-3
- soc2 CC6.3

Taxons: access-control

HTML version: https://auditbuffet.com/patterns/ab-002250
