Skip to main content

systemprompt_security/authz/
resolver.rs

1//! Pure deny-overrides resolver with `user > role` specificity.
2//!
3//! The function is intentionally synchronous and free of I/O so it can be
4//! reused by the in-process [`super::rule_based::RuleBasedHook`], the
5//! template's webhook handler, and unit tests without setup. Callers fetch
6//! [`AccessRule`]s plus the `default_included` sentinel from
7//! [`super::repository::AccessControlRepository`] and pass them in.
8//!
9//! `default_included` is `Option<bool>` — `None` signals the entity is
10//! unknown to access control (no row in `access_control_entities`), which
11//! the resolver turns into [`DenyReason::UnknownEntity`] rather than the
12//! generic `NotAssigned` deny. This distinction matters operationally: an
13//! unknown entity is a publish-pipeline gap, not a missing role grant.
14
15use systemprompt_identifiers::UserId;
16
17use super::types::{Access, AccessRule, Decision, DenyReason, EntityRef, MatchedBy, RuleType};
18
19/// A parent entity whose rules cascade onto the child being resolved.
20///
21/// Parents are ordered nearest-first: the entity directly above the child
22/// comes before its grandparent, so a closer grant wins over a more distant
23/// one within the same precedence band.
24#[derive(Debug, Clone, Copy)]
25pub struct ResolveParent<'a> {
26    pub entity: &'a EntityRef,
27    pub rules: &'a [AccessRule],
28    pub default_included: Option<bool>,
29}
30
31/// Inputs to [`resolve`]. Bundled so the function stays under the clippy
32/// argument-count limit and so call sites can read top-to-bottom.
33#[derive(Debug, Clone, Copy)]
34pub struct ResolveInput<'a> {
35    pub entity: &'a EntityRef,
36    pub rules: &'a [AccessRule],
37    pub user_id: &'a UserId,
38    pub user_roles: &'a [String],
39    pub default_included: Option<bool>,
40    pub parents: &'a [ResolveParent<'a>],
41}
42
43/// Resolve an access decision with deny-overrides precedence and
44/// parent-entity inheritance.
45///
46/// The precedence ladder, evaluated strictly top to bottom and short-circuiting
47/// on the first match, is:
48///
49/// 1. own user-deny → own user-allow → own role-deny → own role-allow
50/// 2. for each parent (nearest first): parent user-deny → parent user-allow →
51///    parent role-deny → parent role-allow
52/// 3. own `default_included` (`true` → Allow)
53/// 4. each parent's `default_included` (nearest `true` → Allow)
54/// 5. otherwise Deny
55///
56/// A grant on a parent therefore cascades to the child only when neither the
57/// child nor a nearer parent has a more specific matching rule. A child deny
58/// always overrides a parent allow, and a nearer rule always overrides a
59/// farther one within the same band.
60///
61/// If the child entity's own `default_included` is `None`, the entity is
62/// unknown to access control. In that case the result is
63/// [`DenyReason::UnknownEntity`] only when no rule (own or parent) matches and
64/// no parent grants access via its own `default_included`; an explicit or
65/// inherited grant still resolves to [`Decision::Allow`].
66#[must_use]
67pub fn resolve(input: ResolveInput<'_>) -> Decision {
68    let ResolveInput {
69        entity,
70        rules,
71        user_id,
72        user_roles,
73        default_included,
74        parents,
75    } = input;
76
77    let user_match =
78        |r: &AccessRule| r.rule_type == RuleType::User && r.rule_value == user_id.as_str();
79    let role_match = |r: &AccessRule| {
80        r.rule_type == RuleType::Role && user_roles.iter().any(|role| role == &r.rule_value)
81    };
82
83    let match_rules = |target: &EntityRef, ruleset: &[AccessRule]| -> Option<Decision> {
84        if let Some(rule) = ruleset
85            .iter()
86            .find(|r| user_match(r) && r.access == Access::Deny)
87        {
88            return Some(Decision::Deny {
89                reason: DenyReason::UserDeny {
90                    entity: target.clone(),
91                    user_id: user_id.clone(),
92                    justification: rule.justification.clone(),
93                },
94            });
95        }
96        if ruleset
97            .iter()
98            .any(|r| user_match(r) && r.access == Access::Allow)
99        {
100            return Some(Decision::Allow {
101                matched_by: MatchedBy::UserAllow,
102            });
103        }
104        if let Some(rule) = ruleset
105            .iter()
106            .find(|r| role_match(r) && r.access == Access::Deny)
107        {
108            return Some(Decision::Deny {
109                reason: DenyReason::RoleDeny {
110                    entity: target.clone(),
111                    role: rule.rule_value.clone(),
112                    justification: rule.justification.clone(),
113                },
114            });
115        }
116        if let Some(rule) = ruleset
117            .iter()
118            .find(|r| role_match(r) && r.access == Access::Allow)
119        {
120            return Some(Decision::Allow {
121                matched_by: MatchedBy::RoleAllow {
122                    role: rule.rule_value.clone(),
123                },
124            });
125        }
126        None
127    };
128
129    if let Some(decision) = match_rules(entity, rules) {
130        return decision;
131    }
132    for parent in parents {
133        if let Some(decision) = match_rules(parent.entity, parent.rules) {
134            return decision;
135        }
136    }
137
138    if default_included == Some(true) {
139        return Decision::Allow {
140            matched_by: MatchedBy::DefaultIncluded,
141        };
142    }
143    if parents
144        .iter()
145        .any(|parent| parent.default_included == Some(true))
146    {
147        return Decision::Allow {
148            matched_by: MatchedBy::DefaultIncluded,
149        };
150    }
151
152    if default_included.is_none() {
153        return Decision::Deny {
154            reason: DenyReason::UnknownEntity {
155                entity: entity.clone(),
156            },
157        };
158    }
159    Decision::Deny {
160        reason: DenyReason::NotAssigned {
161            entity: entity.clone(),
162            user_id: user_id.clone(),
163            roles: user_roles.to_vec(),
164        },
165    }
166}