feat(permissions): ABAC integration, principals caching, cache version validation
- Integrate ABAC policies into apply_visibility_filter() (allow/deny with priority) - Add field whitelist (ABAC_ALLOWED_FIELDS) for build_sql_condition() security - Add request-level ContextVar for user principals (group_ids, role_id) - Set principals in deps.py (session + bearer auth) - Use ContextVar in visibility.py and permission_resolver.py (N+1 fix) - Add version validation to get_cached_visible_ids() (cache strategy unification) - Deactivate delegation route (parked — not integrated into resolve_permissions) - Add 7 ABAC integration tests All 70 tests pass (7 ABAC + 63 existing). No regressions.
This commit is contained in:
@@ -30,6 +30,21 @@ _SUPPORTED_OPS = {
|
||||
"contains", "starts_with", "is_null", "is_not_null",
|
||||
}
|
||||
|
||||
# ─── ABAC Field Whitelist ──────────────────────────────────────────
|
||||
# Only these fields may be referenced in policy conditions per entity
|
||||
# type. Prevents policies from filtering on sensitive columns such as
|
||||
# tenant_id, password_hash, etc.
|
||||
ABAC_ALLOWED_FIELDS: dict[str, set[str]] = {
|
||||
"contact": {"status", "type", "country", "tags", "created_at", "updated_at", "owner_id"},
|
||||
"file": {"status", "size", "mime_type", "created_at"},
|
||||
"task": {"status", "priority", "due_date", "created_at"},
|
||||
"calendar_event": {"status", "start_time", "end_time", "created_at"},
|
||||
"mailbox": {"status", "created_at"},
|
||||
"address": {"country", "city", "created_at", "updated_at"},
|
||||
"contact_folder": {"name", "created_at"},
|
||||
"workflow": {"status", "created_at"},
|
||||
}
|
||||
|
||||
|
||||
def _serialize_policy(p: EntityPolicy) -> dict:
|
||||
return {
|
||||
@@ -51,6 +66,7 @@ def _serialize_policy(p: EntityPolicy) -> dict:
|
||||
def build_sql_condition(
|
||||
conditions: dict[str, Any],
|
||||
model: type,
|
||||
entity_type: str | None = None,
|
||||
) -> Any:
|
||||
"""Recursively translate a JSONB conditions block into a SQLAlchemy filter expression.
|
||||
|
||||
@@ -65,6 +81,11 @@ def build_sql_condition(
|
||||
}
|
||||
|
||||
Nested conditions are supported via rules that contain a nested conditions block.
|
||||
|
||||
When *entity_type* is provided, each rule's ``field`` is validated against
|
||||
the ABAC_ALLOWED_FIELDS whitelist for that entity type. Unknown fields are
|
||||
logged and skipped (not crashed) to prevent policies from filtering on
|
||||
sensitive columns such as ``tenant_id`` or ``password_hash``.
|
||||
"""
|
||||
if not conditions or "operator" not in conditions or "rules" not in conditions:
|
||||
return None
|
||||
@@ -80,7 +101,7 @@ def build_sql_condition(
|
||||
for rule in rules:
|
||||
# Nested conditions block
|
||||
if "operator" in rule and "rules" in rule:
|
||||
nested = build_sql_condition(rule, model)
|
||||
nested = build_sql_condition(rule, model, entity_type=entity_type)
|
||||
if nested is not None:
|
||||
clauses.append(nested)
|
||||
continue
|
||||
@@ -96,6 +117,17 @@ def build_sql_condition(
|
||||
logger.warning(f"Unsupported condition operator: {op}")
|
||||
continue
|
||||
|
||||
# ── Field whitelist check ──────────────────────────────────
|
||||
# Prevent policies from filtering on sensitive columns
|
||||
if entity_type is not None:
|
||||
allowed = ABAC_ALLOWED_FIELDS.get(entity_type)
|
||||
if allowed is not None and field_name not in allowed:
|
||||
logger.warning(
|
||||
f"ABAC field '{field_name}' not in whitelist for "
|
||||
f"entity_type '{entity_type}' — skipping rule"
|
||||
)
|
||||
continue
|
||||
|
||||
# Get the model attribute
|
||||
attr: InstrumentedAttribute | None = getattr(model, field_name, None)
|
||||
if attr is None:
|
||||
@@ -292,7 +324,7 @@ async def apply_policy_filter(
|
||||
deny_clauses.append(True)
|
||||
continue
|
||||
|
||||
condition = build_sql_condition(policy.conditions, model)
|
||||
condition = build_sql_condition(policy.conditions, model, entity_type=entity_type)
|
||||
if condition is None:
|
||||
continue
|
||||
|
||||
|
||||
Reference in New Issue
Block a user