Repository navigation
fix(authz): accept bounded Keyverse claim aliases #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
5ff4474
8bf7df4
773964e
46b9fdb
a93f9ed
103e54b
0b40e77
cb85a35
77e6ecb
47e2215
9348983
80966ae
0ce6d1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,17 @@ | |
| "sdp-security": ["security"], | ||
| } | ||
|
|
||
| _KEYVERSE_ROLE_MAP = { | ||
| "member": ["data-analyst"], | ||
| "data-analyst": ["data-analyst"], | ||
| "data-admin": ["data-admin", "data-analyst"], | ||
| "admin": ["admin", "data-analyst"], | ||
| "platform-admin": ["platform-admin", "admin", "data-analyst"], | ||
| "security": ["security"], | ||
| } | ||
|
|
||
| _SUBJECT_CLAIMS = ("preferred_username", "email", "sub") | ||
| _TENANT_CLAIMS = ("tenant_id", "tid", "organization", "org") | ||
| _ALLOWED_JWT_ALGORITHMS = {"RS256", "RS384", "RS512", "ES256", "ES384", "ES512"} | ||
|
|
||
|
|
||
|
|
@@ -57,6 +67,23 @@ def _claim_values(value: Any) -> list[str]: | |
| return [str(value)] | ||
|
|
||
|
|
||
| def _single_string_claim(value: Any, claim_name: str) -> str | None: | ||
| """Return one optional string claim, rejecting ambiguous claim shapes. | ||
|
|
||
| Signed identity claims that grant application roles must have one explicit | ||
| string value. Accepting arrays or coercing arbitrary values could merge | ||
| multiple authorities or invoke surprising conversions at the authorization | ||
| boundary, so malformed role claims fail closed. | ||
| """ | ||
| if value is None: | ||
| return None | ||
| if not isinstance(value, str): | ||
| raise ValueError(f"{claim_name} claim must be a string") | ||
| if not value.strip(): | ||
| raise ValueError(f"{claim_name} claim must be non-empty") | ||
| return value | ||
|
|
||
|
|
||
| def oidc_role_claims(claims: dict[str, Any]) -> list[str]: | ||
| return _claim_values(claims.get("roles")) | ||
|
|
||
|
|
@@ -75,8 +102,7 @@ def load_oidc_role_map() -> dict[str, list[str]]: | |
| def validate_oidc_claim_shape(claims: dict[str, Any]) -> None: | ||
| if not any(claims.get(key) for key in _SUBJECT_CLAIMS): | ||
| raise ValueError("missing subject claim") | ||
| if not (claims.get("tenant_id") or claims.get("tid") or claims.get("organization")): | ||
| raise ValueError("missing tenant claim") | ||
| _tenant_claim_value(claims) | ||
| if "exp" not in claims: | ||
| raise ValueError("missing exp claim") | ||
|
|
||
|
|
@@ -89,6 +115,23 @@ def validate_oidc_claim_shape(claims: dict[str, Any]) -> None: | |
| raise ValueError("expired token claims") | ||
|
|
||
|
|
||
| def _tenant_claim_value(claims: dict[str, Any]) -> str: | ||
| """Validate all tenant aliases before selecting the first configured one.""" | ||
| tenant_ids: list[str] = [] | ||
| for key in _TENANT_CLAIMS: | ||
| if key not in claims: | ||
| continue | ||
| tenant_id = _single_string_claim(claims[key], key) | ||
| if tenant_id is None: | ||
| raise ValueError("missing tenant claim") | ||
| tenant_ids.append(tenant_id) | ||
| if not tenant_ids: | ||
| raise ValueError("missing tenant claim") | ||
| if len(set(tenant_ids)) != 1: | ||
| raise ValueError("conflicting tenant claims") | ||
| return tenant_ids[0] | ||
|
Comment on lines
+135
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔍 Multiple tenant aliases now reject on any mismatch
Was this helpful? React with 👍 or 👎 to provide feedback. |
||
|
|
||
|
|
||
| def resolve_oidc_actor_context( | ||
| claims: dict[str, Any], | ||
| *, | ||
|
|
@@ -102,12 +145,15 @@ def resolve_oidc_actor_context( | |
| or claims.get("sub") | ||
| or "anonymous" | ||
| ) | ||
| tenant_id = str(claims.get("tenant_id") or claims.get("tid") or claims.get("organization") or "") | ||
| tenant_id = _tenant_claim_value(claims) | ||
| groups = _claim_values(claims.get("groups")) | ||
|
|
||
| roles: set[str] = set() | ||
| for group in groups: | ||
| roles.update(mapping.get(group, [])) | ||
| role = _single_string_claim(claims.get("role"), "role") | ||
| if role: | ||
| roles.update(_KEYVERSE_ROLE_MAP.get(role, [])) | ||
|
|
||
| return ActorContext(subject=str(subject), tenant_id=tenant_id, roles=sorted(roles)) | ||
|
|
||
|
|
@@ -141,6 +187,12 @@ def _select_jwk(jwks: dict[str, Any], kid: str | None) -> dict[str, Any]: | |
| raise ValueError("no matching jwks key") | ||
|
|
||
|
|
||
| def _validate_jwt_header(header: dict[str, Any]) -> None: | ||
| """Reject critical JWT extensions because this verifier supports none.""" | ||
| if "crit" in header: | ||
| raise ValueError("unsupported critical JWT header") | ||
|
|
||
|
|
||
| def verify_oidc_jwks_token( | ||
| token: str, | ||
| *, | ||
|
|
@@ -163,7 +215,13 @@ def verify_oidc_jwks_token( | |
| jwks = _load_jwks_from_url(jwks_url) | ||
|
|
||
| try: | ||
| header = jwt.get_unverified_header(token) | ||
| try: | ||
| header = jwt.get_unverified_header(token) | ||
| except InvalidTokenError as exc: | ||
| if "Unsupported critical extension" in str(exc): | ||
| raise ValueError("unsupported critical JWT header") from exc | ||
| raise | ||
| _validate_jwt_header(header) | ||
| alg = header.get("alg") | ||
| if alg not in _ALLOWED_JWT_ALGORITHMS: | ||
| raise ValueError("unsupported token algorithm") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Info: Keyverse data-admin maps to an inert application role
_KEYVERSE_ROLE_MAPmapsdata-adminto["data-admin", "data-analyst"], butsrc/sdp/policy.pyrecognizes onlyadmin/platform-admin/data-analyst/security;data-adminappears there only as an obligation string. Thedata-adminrole is therefore inert — non-escalating, but it grants no authority beyonddata-analyst.Was this helpful? React with 👍 or 👎 to provide feedback.