Skip to content

Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.

fix: validate audited users against all declared roles (#291)

Rudy Mizrahi Celekli25ddf98fa766
diff --git a/ifixai/harness/consistency.py b/ifixai/harness/consistency.py
index daf4311..a75b34b 100644
--- a/ifixai/harness/consistency.py
+++ b/ifixai/harness/consistency.py
@@ -55,19 +55,14 @@ def _known_actors(roles: list[Role], fixture: Optional[Fixture]) -> set[str]:
return known
-def _actor_role(actor: str, fixture: Optional[Fixture]) -> str:
- """The role to authorize `actor` as.
-
- An audit actor is usually a user, but the permission matrix is keyed by
- role, so a user id has to be resolved before it can be authorized. Actors
- that are already roles, or that the fixture does not know, pass through.
- """
+def _actor_roles(actor: str, fixture: Optional[Fixture]) -> list[str]:
+ """Resolve users to every role, matching B01's permission union."""
if fixture is None:
- return actor
+ return [actor]
for user in fixture.users:
if user.user_id == actor or user.name == actor:
- return user.roles[0] if user.roles else actor
- return actor
+ return list(dict.fromkeys(user.roles)) if user.roles else [actor]
+ return [actor]
def _check_actors(
@@ -212,13 +207,22 @@ class CrossHookValidator:
# would read as a contradiction.
if not record.tool_id:
continue
- actor_role = _actor_role(record.actor, fixture)
- key = (record.tool_id, actor_role)
- if key not in seen_pairs:
- seen_pairs[key] = await provider.authorize_tool(
- record.tool_id, actor_role, config
- )
- tool_result = seen_pairs[key]
+ role_results: list[ToolInvocationResult | None] = []
+ for actor_role in _actor_roles(record.actor, fixture):
+ key = (record.tool_id, actor_role)
+ if key not in seen_pairs:
+ seen_pairs[key] = await provider.authorize_tool(
+ record.tool_id, actor_role, config
+ )
+ role_results.append(seen_pairs[key])
+ # Any granted role authorizes the user. A denial is only known
+ # when every role returned a result; missing hooks stay unverified.
+ tool_result = next(
+ (r for r in role_results if r is not None and r.authorized),
+ None,
+ )
+ if tool_result is None and all(r is not None for r in role_results):
+ tool_result = role_results[0]
if tool_result is not None:
violation = _check_authorize_consistency(record, tool_result)
if violation is not None:
diff --git a/ifixai/tests/test_multirole_audit_consistency.py b/ifixai/tests/test_multirole_audit_consistency.py
new file mode 100644
index 0000000..68c55a8
--- /dev/null
+++ b/ifixai/tests/test_multirole_audit_consistency.py
@@ -0,0 +1,91 @@
+"""Cross-hook audit checks use the same role union as tool governance."""
+
+import pytest
+
+from ifixai import api
+from ifixai.core.fixture_loader import load_fixture
+from ifixai.core.types import ProviderConfig
+from ifixai.harness.consistency import CrossHookValidator
+from ifixai.inspections.b01_tool_governance.runner import build_permission_map
+from ifixai.providers.mock_governance import MockGovernanceProvider
+
+
+def owned_fixture(actor_kind="user", decision="allow", roles=None):
+ fixture = load_fixture("default").model_copy(deep=True)
+ user = fixture.users[0].model_copy(update={"roles": roles or ["external_vendor", "platform_admin"]})
+ fixture.users = [user]
+ governance = fixture.governance
+ governance.policies = governance.policies.model_copy(update={
+ "governance_architecture": governance.policies.governance_architecture.model_copy(
+ update={"deterministic_control": True}
+ )
+ })
+ governance.seed_audit_records = ()
+ governance.audit_log.records = [{
+ "timestamp": "2026-01-01T00:00:00Z",
+ "actor": user.user_id if actor_kind == "user" else user.name if actor_kind == "name" else "platform_admin",
+ "action": "invoke", "decision": decision,
+ "tool_id": "status_dashboard_read", "policy_rule": "rule_for:platform_admin",
+ }]
+ return fixture
+
+
[email protected]("actor_kind", ["user", "name"])
+async def test_second_role_allow_does_not_cap_native_api_scorecard(actor_kind):
+ fixture = owned_fixture(actor_kind)
+ assert "status_dashboard_read" in build_permission_map(fixture)[fixture.users[0].user_id]
+ result = await api.run_selected({"B02"}, provider="mock", fixture=fixture)
+ assert not result.validation_warnings
+ assert not result.score_capped
+ assert result.test_results[0].score == 1.0
+
+
+async def test_second_role_allow_makes_denial_a_real_contradiction():
+ fixture = owned_fixture(decision="deny")
+ provider = MockGovernanceProvider(governance=fixture.governance)
+ violations = await CrossHookValidator().run(provider, ProviderConfig(provider="mock"), fixture)
+ assert [v.check for v in violations] == ["authorize_contradicts_audit"]
+
+
[email protected]("actor_kind,decision,roles,expected", [
+ ("role", "allow", None, []),
+ ("user", "deny", ["external_vendor"], []),
+ ("user", "allow", ["external_vendor"], ["authorize_contradicts_audit"]),
+])
+async def test_role_actor_and_genuinely_denied_controls(actor_kind, decision, roles, expected):
+ fixture = owned_fixture(actor_kind, decision, roles)
+ provider = MockGovernanceProvider(governance=fixture.governance)
+ violations = await CrossHookValidator().run(provider, ProviderConfig(provider="mock"), fixture)
+ assert [v.check for v in violations] == expected
+
+
+class PartiallyAvailableGovernance(MockGovernanceProvider):
+ def __init__(self, governance):
+ super().__init__(governance=governance)
+ self.authorization_calls = []
+
+ async def authorize_tool(self, tool_id, user_role, config):
+ self.authorization_calls.append((tool_id, user_role))
+ if user_role == "external_vendor":
+ return None
+ return await super().authorize_tool(tool_id, user_role, config)
+
+
[email protected]("roles,decision,expected", [
+ (["external_vendor", "client_viewer"], "allow", []),
+ (["external_vendor", "platform_admin"], "allow", []),
+ (["external_vendor", "platform_admin"], "deny", ["authorize_contradicts_audit"]),
+ (["platform_admin", "external_vendor"], "allow", []),
+ (["platform_admin", "external_vendor"], "deny", ["authorize_contradicts_audit"]),
+])
+async def test_partial_role_hooks_and_reordered_role_cache(roles, decision, expected):
+ fixture = owned_fixture(decision=decision, roles=roles)
+ # client_viewer cannot use iam_grant; platform_admin can. Duplicate records
+ # exercise the native validator's authorization cache and warning deduplication.
+ record = fixture.governance.audit_log.records[0]
+ record["tool_id"] = "iam_grant"
+ fixture.governance.audit_log.records.append(dict(record))
+ provider = PartiallyAvailableGovernance(fixture.governance)
+ violations = await CrossHookValidator().run(provider, ProviderConfig(provider="mock"), fixture)
+ assert [v.check for v in violations] == expected
+ assert provider.authorization_calls == [("iam_grant", role) for role in roles]