@davoodmehraban89 / Aram / commits / 8ed0b86
Fork 0

feat(auth): validate exact signed company/year grant claims, no wildcard or fallback

davoodmehraban89 committed Oct 30, 2026 at 22:24 UTC 8ed0b8614e61e3751a68b9aa45b72f231498f406
1 file changed +41 -1
backend/src/aram_backend/authenticated_identity.py
+41 -1
@@ -20,10 +20,20 @@ from aram_backend.config import Settings, get_settings
20
21 MAX_TOKEN_LENGTH = 8192
22 MAX_LIFETIME_SECONDS = 900
23 +MAX_SIGNED_YEAR_SCOPES = 32
24 TRUSTED_ROLES = frozenset(("mapping_proposer", "mapping_reviewer"))
25 bearer_scheme = HTTPBearer(auto_error=False)
26
27
28 +@dataclass(frozen=True)
29 +class SignedYearScope:
30 + """Exact signed company, fiscal-year and role boundary; no wildcards."""
31 +
32 + company_code: str
33 + fiscal_year: int
34 + role: str
35 +
36 +
37 @dataclass(frozen=True)
38 class VerifiedPrincipal:
39 """Identity from successfully validated RS256 issuer/audience claims only."""
@@ -32,6 +42,9 @@ class VerifiedPrincipal:
42 subject: str
43 roles: tuple[str, ...]
44 expires_at: int
45 + # Older valid tokens with no explicit grants may authenticate, but can
46 + # NEVER authorize year-specific Mapping operations.
47 + year_scopes: tuple[SignedYearScope, ...] = ()
48
49
50 def _fail_invalid() -> None:
@@ -49,6 +62,30 @@ def _is_safe_string(value: object, limit: int) -> bool:
62 )
63
64
65 +def _validated_signed_scopes(raw: object, *, permitted_role: str) -> tuple[SignedYearScope, ...]:
66 + if type(raw) is not list or len(raw) > MAX_SIGNED_YEAR_SCOPES:
67 + _fail_invalid()
68 + seen: set[SignedYearScope] = set()
69 + result: list[SignedYearScope] = []
70 + for item in raw:
71 + if type(item) is not dict or set(item) != {"company_code", "fiscal_year", "role"}:
72 + _fail_invalid()
73 + code, year, role = item["company_code"], item["fiscal_year"], item["role"]
74 + if (
75 + not _is_safe_string(code, 64)
76 + or "*" in code or "?" in code
77 + or type(year) is not int or year <= 0 or year > 9999
78 + or type(role) is not str or role != permitted_role
79 + ):
80 + _fail_invalid()
81 + scope = SignedYearScope(company_code=code, fiscal_year=year, role=role)
82 + if scope in seen:
83 + _fail_invalid()
84 + seen.add(scope)
85 + result.append(scope)
86 + return tuple(result)
87 +
88 +
89 def _trusted_public_key(settings: Settings) -> RSAPublicKey:
90 if (
91 not _is_safe_string(settings.auth_issuer, 512)
@@ -115,9 +152,12 @@ def verify_access_token(token: str, *, settings: Settings) -> VerifiedPrincipal:
152 or len(roles) > 1
153 ):
154 _fail_invalid()
155 + scopes = _validated_signed_scopes(
156 + claims.get("aram_scopes", []), permitted_role=roles[0],
157 + )
158 return VerifiedPrincipal(
159 issuer=claims["iss"], subject=claims["sub"],
120 - roles=tuple(sorted(roles)), expires_at=exp,
160 + roles=tuple(sorted(roles)), expires_at=exp, year_scopes=scopes,
161 )
162
163