domainforge-core 0.21.0

Rust core library implementing the SEA DSL primitives and validation engine.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::error::AuthorityError;
use super::policy::*;
use super::types::*;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompatibilityExpression {
    pub expression: String,
    pub location: Option<String>,
}

pub struct PolicyCompiler {
    semantics_version: String,
    compatibility_version: String,
}

impl PolicyCompiler {
    pub fn new(semantics_version: String, compatibility_version: String) -> Self {
        Self {
            semantics_version,
            compatibility_version,
        }
    }

    pub fn compile(
        &self,
        raw_policies: Vec<RawPolicy>,
    ) -> Result<Vec<AuthorityPolicy>, AuthorityError> {
        let mut compiled = Vec::new();
        for raw in raw_policies {
            let policy = self.compile_one(raw)?;
            compiled.push(policy);
        }
        Ok(compiled)
    }

    pub fn semantics_version(&self) -> &str {
        &self.semantics_version
    }

    pub fn compatibility_version(&self) -> &str {
        &self.compatibility_version
    }

    fn compile_one(&self, raw: RawPolicy) -> Result<AuthorityPolicy, AuthorityError> {
        let applies_to = self.parse_structural_predicates(&raw.applies_to)?;
        let when = if raw.when.is_empty() {
            None
        } else {
            Some(self.parse_condition_predicates(&raw.when)?)
        };
        let requires_fact = self.parse_fact_requirements(&raw.requires_fact)?;

        // A3: a `when` path with no matching `requires_fact` entry is
        // satisfied by whatever the caller puts in the request context
        // (ConditionPredicates::evaluate has no source-class filter of its
        // own). Forgetting `requires_fact` silently trusts the caller, so
        // refuse to compile the policy instead of shipping the gap.
        if let Some(ref when) = when {
            for path in when.conditions.keys() {
                if !requires_fact.iter().any(|r| &r.fact_path == path) {
                    return Err(AuthorityError::new(
                        super::error::AuthorityErrorCode::PolicyParseError,
                        format!(
                            "Policy '{}': `when` condition on '{}' has no matching `requires_fact` \
                             entry, so it would be satisfied by unverified caller-supplied context. \
                             Add a `requires_fact` entry for '{}' naming the trusted source classes.",
                            raw.policy_id, path, path
                        ),
                    ));
                }
            }
        }

        // A4: `required_transform` is parsed but never enforced by
        // `FactRequirement::is_satisfied_by`. Silently accepting it would
        // give the policy author a false sense of security. Refuse to
        // compile until enforcement exists.
        for req in &requires_fact {
            if req.required_transform.is_some() {
                return Err(AuthorityError::new(
                    super::error::AuthorityErrorCode::PolicyParseError,
                    format!(
                        "Policy '{}': `required_transform` on fact '{}' is not yet enforced by the \
                         resolver. Remove it rather than rely on an unenforced field.",
                        raw.policy_id, req.fact_path
                    ),
                ));
            }
        }

        Ok(AuthorityPolicy {
            policy_id: raw.policy_id,
            modality: raw.modality,
            priority: raw.priority,
            applies_to,
            when,
            requires_fact,
            semantics_version: self.semantics_version.clone(),
            override_spec: raw.override_spec,
            obligation_spec: raw.obligation_spec,
            description: raw.description,
            evidence_ref: raw.evidence_ref,
        })
    }

    fn parse_structural_predicates(
        &self,
        raw: &HashMap<String, serde_json::Value>,
    ) -> Result<StructuralPredicates, AuthorityError> {
        // A10: `StructuralPredicates::matches` recognizes exactly five keys
        // ("action", "actor.id", "actor.role", "resource.id",
        // "resource.type") and treats everything else as an open metadata
        // lookup. A typo like "resource.typ" therefore doesn't error — it
        // silently becomes a metadata predicate that (almost certainly)
        // never matches, making the policy permanently inert with no
        // diagnostic. The "resource."/"actor." prefixes are reserved for
        // the known structural fields, so reject unrecognized keys under
        // them at compile time instead of shipping a silently-dead policy.
        const KNOWN_STRUCTURAL_KEYS: [&str; 5] = [
            "action",
            "actor.id",
            "actor.role",
            "resource.id",
            "resource.type",
        ];
        for key in raw.keys() {
            validate_fact_path(key)?;
            if (key.starts_with("resource.") || key.starts_with("actor."))
                && !KNOWN_STRUCTURAL_KEYS.contains(&key.as_str())
            {
                return Err(AuthorityError::new(
                    super::error::AuthorityErrorCode::PolicyParseError,
                    format!(
                        "Unrecognized structural predicate key '{}'. The 'resource.' and 'actor.' \
                         prefixes are reserved for {:?}; did you mean one of those?",
                        key, KNOWN_STRUCTURAL_KEYS
                    ),
                ));
            }
        }
        Ok(StructuralPredicates {
            predicates: raw.clone(),
        })
    }

    fn parse_condition_predicates(
        &self,
        raw: &HashMap<String, serde_json::Value>,
    ) -> Result<ConditionPredicates, AuthorityError> {
        for key in raw.keys() {
            validate_fact_path(key)?;
        }
        Ok(ConditionPredicates {
            conditions: raw.clone(),
        })
    }

    fn parse_fact_requirements(
        &self,
        raw: &[RawFactRequirement],
    ) -> Result<Vec<FactRequirement>, AuthorityError> {
        let mut requirements = Vec::new();
        for req in raw {
            validate_fact_path(&req.fact_path)?;
            requirements.push(FactRequirement {
                fact_path: req.fact_path.clone(),
                allowed_source_classes: req.allowed_source_classes.clone(),
                allowed_source_ids: req.allowed_source_ids.clone().unwrap_or_default(),
                max_age: req
                    .max_age
                    .as_ref()
                    .map(|s| parse_duration(s))
                    .transpose()?,
                evidence_ref_required: req.evidence_ref_required.unwrap_or(false),
                signature_required: req.signature_required.unwrap_or(false),
                minimum_confidence: req.minimum_confidence,
                required_transform: req.required_transform.clone(),
                derived_from_source: req.derived_from_source.clone(),
            });
        }
        Ok(requirements)
    }
}

/// A9: parses a duration string. Fixes three foot-guns in the previous
/// version: a bare number silently meant hours (surprising — now rejected,
/// a unit suffix is required); negative durations were accepted (`"-5h"`
/// made every fact look "fresh" forever — now rejected); and suffix
/// matching checked single-char suffixes first, so `"10ms"` matched the
/// `s` branch and became `"10m"` before parsing, producing a confusing
/// error instead of 10 milliseconds. Longer suffixes (`ms`) are now matched
/// before shorter ones (`s`, `m`, `h`, `d`).
fn parse_duration(s: &str) -> Result<chrono::Duration, AuthorityError> {
    let s = s.trim();
    let invalid = || AuthorityError::invalid_environment(format!("Invalid duration: '{}'", s));

    // Longer suffixes must be checked before shorter ones ("ms" before "s").
    let parsed = if let Some(n) = s.strip_suffix("ms") {
        n.parse::<i64>().ok().map(chrono::Duration::milliseconds)
    } else if let Some(n) = s.strip_suffix('s') {
        n.parse::<i64>().ok().map(chrono::Duration::seconds)
    } else if let Some(n) = s.strip_suffix('m') {
        n.parse::<i64>().ok().map(chrono::Duration::minutes)
    } else if let Some(n) = s.strip_suffix('h') {
        n.parse::<i64>().ok().map(chrono::Duration::hours)
    } else if let Some(n) = s.strip_suffix('d') {
        n.parse::<i64>().ok().map(chrono::Duration::days)
    } else {
        return Err(AuthorityError::invalid_environment(format!(
            "Invalid duration: '{}' — a unit suffix (ms, s, m, h, d) is required",
            s
        )));
    };

    match parsed {
        Some(d) if d >= chrono::Duration::zero() => Ok(d),
        _ => Err(invalid()),
    }
}

pub struct CompatibilityLoweringAuditor {
    version: String,
}

impl CompatibilityLoweringAuditor {
    pub fn new(version: String) -> Result<Self, AuthorityError> {
        if version.trim().is_empty() {
            return Err(AuthorityError::invalid_environment(
                "Compatibility lowering version is required",
            ));
        }

        Ok(Self { version })
    }

    pub fn version(&self) -> &str {
        &self.version
    }

    /// A5: the previous implementation lowercased `expr` and applied the
    /// resulting *byte* offsets back onto the original string. For
    /// non-ASCII input where lowercasing changes byte length (e.g. `İ` →
    /// `i̇`), that could slice at a non-UTF8 boundary and panic. It also
    /// matched " and "/" or " inside quoted string literals, so a value
    /// like `role = "black or white"` was rejected as ambiguous. This
    /// version scans char-by-char, tracks quote state, and never lowercases
    /// the whole string — only individual ASCII keyword characters are
    /// compared case-insensitively.
    pub fn audit_expression(&self, expr: &str) -> Result<Option<LoweredPolicy>, AuthorityError> {
        // Reject all top-level structural OR as ambiguous (spec §14.1.7, §17.7).
        // "Top-level" = outside quoted string literals.
        if contains_top_level(expr, " or ") {
            return Err(AuthorityError::ambiguous_lowering(expr));
        }

        // Handle negated conditions: `not X = "Y"` lowers to when condition.
        if let Some(inner) = strip_prefix_ci(expr, "not ") {
            let inner_trimmed = inner.trim();
            // Reject if the negated expression targets structural keys
            // (spec §14.1.7): negative structural predicates are ambiguous.
            if starts_with_ci(inner_trimmed, "resource.")
                || starts_with_ci(inner_trimmed, "actor.")
                || starts_with_ci(inner_trimmed, "action")
            {
                return Err(AuthorityError::ambiguous_lowering(expr));
            }
            return match parse_equality(inner_trimmed) {
                Some((key, val)) => Ok(Some(LoweredPolicy {
                    original: expr.to_string(),
                    lowered_applies_to: HashMap::new(),
                    lowered_when: {
                        let mut m = HashMap::new();
                        m.insert(key, serde_json::json!({"__neq": val}));
                        m
                    },
                })),
                None => Err(AuthorityError::ambiguous_lowering(expr)),
            };
        }

        if contains_top_level(expr, " and ") {
            let mut applies_to = HashMap::new();
            let mut when = HashMap::new();
            for part in split_top_level(expr, " and ") {
                let trimmed = part.trim();
                let (k, v) = parse_equality(trimmed)
                    .ok_or_else(|| AuthorityError::ambiguous_lowering(expr))?;
                if starts_with_ci(trimmed, "resource.")
                    || starts_with_ci(trimmed, "actor.")
                    || starts_with_ci(trimmed, "action")
                {
                    applies_to.insert(k, v);
                } else {
                    when.insert(k, v);
                }
            }
            return Ok(Some(LoweredPolicy {
                original: expr.to_string(),
                lowered_applies_to: applies_to,
                lowered_when: when,
            }));
        }

        if let Some((k, v)) = parse_equality(expr) {
            let mut applies_to = HashMap::new();
            applies_to.insert(k, v);
            return Ok(Some(LoweredPolicy {
                original: expr.to_string(),
                lowered_applies_to: applies_to,
                lowered_when: HashMap::new(),
            }));
        }

        // Unrecognized syntax (e.g. `!=`, `<`, `>=`, an unparenthesized
        // mixed and/or): previously this returned `Ok(None)`, silently
        // leaving the policy uncompiled. A policy compiler that silently
        // drops a clause is a correctness hole, not a convenience — error
        // instead so the gap is visible at compile time.
        Err(AuthorityError::ambiguous_lowering(expr))
    }
}

/// True if `keyword` (matched case-insensitively, ASCII only) occurs outside
/// any double-quoted region of `s`.
fn contains_top_level(s: &str, keyword: &str) -> bool {
    find_top_level(s, keyword).is_some()
}

fn find_top_level(s: &str, keyword: &str) -> Option<usize> {
    let chars: Vec<(usize, char)> = s.char_indices().collect();
    let kw: Vec<char> = keyword.chars().collect();
    let mut in_quotes = false;
    let mut i = 0;
    while i < chars.len() {
        let (byte_idx, ch) = chars[i];
        if ch == '"' {
            in_quotes = !in_quotes;
            i += 1;
            continue;
        }
        if !in_quotes
            && i + kw.len() <= chars.len()
            && chars[i..i + kw.len()]
                .iter()
                .zip(kw.iter())
                .all(|((_, c), k)| c.eq_ignore_ascii_case(k))
        {
            return Some(byte_idx);
        }
        i += 1;
    }
    None
}

/// Split `s` on every top-level (outside-quotes) occurrence of `keyword`,
/// matched case-insensitively. Never allocates a lowercased copy of `s`, so
/// byte offsets always land on the original string's char boundaries.
fn split_top_level<'a>(s: &'a str, keyword: &str) -> Vec<&'a str> {
    let chars: Vec<(usize, char)> = s.char_indices().collect();
    let kw: Vec<char> = keyword.chars().collect();
    let mut parts = Vec::new();
    let mut in_quotes = false;
    let mut last = 0usize;
    let mut i = 0;
    while i < chars.len() {
        let (byte_idx, ch) = chars[i];
        if ch == '"' {
            in_quotes = !in_quotes;
            i += 1;
            continue;
        }
        if !in_quotes
            && i + kw.len() <= chars.len()
            && chars[i..i + kw.len()]
                .iter()
                .zip(kw.iter())
                .all(|((_, c), k)| c.eq_ignore_ascii_case(k))
        {
            parts.push(&s[last..byte_idx]);
            let (last_byte, last_ch) = chars[i + kw.len() - 1];
            last = last_byte + last_ch.len_utf8();
            i += kw.len();
            continue;
        }
        i += 1;
    }
    parts.push(&s[last..]);
    parts
}

fn starts_with_ci(s: &str, prefix: &str) -> bool {
    let mut sc = s.chars();
    prefix
        .chars()
        .all(|p| sc.next().is_some_and(|c| c.eq_ignore_ascii_case(&p)))
}

fn strip_prefix_ci<'a>(s: &'a str, prefix: &str) -> Option<&'a str> {
    if starts_with_ci(s, prefix) {
        let byte_len: usize = prefix
            .chars()
            .zip(s.chars())
            .map(|(_, c)| c.len_utf8())
            .sum();
        Some(&s[byte_len..])
    } else {
        None
    }
}

fn parse_equality(s: &str) -> Option<(String, serde_json::Value)> {
    let s = s.trim();
    if let Some(eq_pos) = find_top_level(s, " = ") {
        let key = s[..eq_pos].trim().to_string();
        let val = s[eq_pos + 3..].trim().trim_matches('"').to_string();
        Some((key, serde_json::Value::String(val)))
    } else {
        None
    }
}

#[derive(Debug, Clone)]
pub struct LoweredPolicy {
    pub original: String,
    pub lowered_applies_to: HashMap<String, serde_json::Value>,
    pub lowered_when: HashMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RawPolicy {
    pub policy_id: String,
    pub modality: PolicyModality,
    pub priority: i32,
    #[serde(default)]
    pub applies_to: HashMap<String, serde_json::Value>,
    #[serde(default)]
    pub when: HashMap<String, serde_json::Value>,
    #[serde(default)]
    pub requires_fact: Vec<RawFactRequirement>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub override_spec: Option<OverrideSpec>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub obligation_spec: Option<ObligationSpec>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub evidence_ref: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RawFactRequirement {
    pub fact_path: String,
    pub allowed_source_classes: Vec<SourceClass>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_source_ids: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_age: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub evidence_ref_required: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub signature_required: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub minimum_confidence: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required_transform: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub derived_from_source: Option<Vec<SourceClass>>,
}