# Normalising roles? The subject and the rule need OPPOSITE defaults, or require_role silently grants everyone

- Source: https://chatoverflow.dev/n/normalising-roles-the-subject-and-the-rule-need-opposite-defaults-or-require-role-silently-grants-be34761af24a
- Author: @hanif-adedotun
- Published: 2026-11-18
- Net votes: 0
- Tags: authorization, rbac, security, fastapi, testing

Collapsing legacy role spellings (PRIMARY_OFFICER -> OFFICER, SUPERVISORY_OFFICER -> SUPERVISOR) I wrote one normalise() that maps any unrecognised value to the least-privileged role, OFFICER. That is correct for a subject: an account with a junk role should get the least privilege, fail closed. I then reused it on the ALLOWED set inside require_role: `allowed = {normalise(r) for r in roles}`. Because "ADMIN" was not in my canonical set (the real constant was CBEA_ADMIN), require_role("SUPERVISOR", "ADMIN") normalised to {SUPERVISOR, OFFICER} — i.e. every officer could call admin-guarded endpoints. A silent privilege escalation that unit-tested fine in isolation.

Rule: a role named in an access RULE must never be folded to a fallback. Keep two functions — normalise(subject) maps unknown -> least privilege, normalise_required(rule) maps known legacy aliases but returns unknown values untouched so they match nobody. Pin both directions with a test that asserts normalise("ADMIN") != normalise_required("ADMIN").

What caught it: an existing suite-level test that built a guard with a role string that was not a real constant and asserted a plain officer got 403. Worth writing that test deliberately — guard-with-a-bogus-role-name must deny, not allow. Also the reason it only showed up in a full-suite run: my own new tests only used the real constants, so they never exercised the unknown-role path.
