feat(core): clock-free check and named-zone ban for deterministic eval
public
feat(core): clock-free check and named-zone ban for deterministic eval Add the two seams a host needs to expose an IMMUTABLE-safe FEEL surface (feel-pg's upcoming feel_eval_immutable, for expression indexes): - CompiledExpr::is_clock_free(): a static AST check that the expression calls neither now() nor today() anywhere (the only clock-reading builtins, ADR-18), including inside nested function literals, list/context entries and loop bodies. now()/today() vary over time, so they cannot be IMMUTABLE. - EvalEnv::forbidding_named_zones(): a runtime policy turning any temporal value with a *named* IANA zone (@Europe/Berlin) into an error-null, while fixed offsets (+02:00) and Z pass through. A named zone's offset is resolved against the updatable IANA tzdb, so it is not tzdb-independent. Applied at the only three sites that can attach a named zone — the @"…" literal and the time()/ date and time() string constructors — so a named zone can never come into existence (even from runtime context data), closing the comparison/arithmetic leak a result-only check would miss. Default behaviour is unchanged (policy off; both are opt-in). See RD-10. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
| Changeset | f464c3ca07fbf7aa999f4ff9ccd29beea8b079ce |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | 0b52cfd6f512 |
| Children | |
| Pushlog | by timo via ssh |
modified crates/feel-core/src/ast.rs +60 −0
| @@ -155,3 +155,63 @@ | ||
| 155 | 155 | pub end: Box<Expr>, |
| 156 | 156 | pub end_open: bool, |
| 157 | 157 | } |
| 158 | + | |
| 159 | +impl Expr { | |
| 160 | + /// `true` if evaluating this expression can never read the wall clock — i.e. | |
| 161 | + /// it calls neither `now()` nor `today()` anywhere in the tree, including | |
| 162 | + /// nested function literals, list/context entries, and loop sources/bodies. | |
| 163 | + /// | |
| 164 | + /// This is the *static* half of IMMUTABLE eligibility (exposed as | |
| 165 | + /// [`crate::CompiledExpr::is_clock_free`]). It deliberately does **not** | |
| 166 | + /// consider named time zones (a runtime/data property, vetted via | |
| 167 | + /// [`crate::EvalEnv::forbidding_named_zones`]) nor host functions (the | |
| 168 | + /// embedder's concern — feel-pg has no host-function seam). The match is | |
| 169 | + /// exhaustive so a new `Expr` variant forces this to be reconsidered. | |
| 170 | + pub(crate) fn is_clock_free(&self) -> bool { | |
| 171 | + match self { | |
| 172 | + Expr::Number(_) | |
| 173 | + | Expr::Str(_) | |
| 174 | + | Expr::TemporalLit(_) | |
| 175 | + | Expr::Bool(_) | |
| 176 | + | Expr::Null | |
| 177 | + | Expr::Var(_) => true, | |
| 178 | + Expr::List(items) => items.iter().all(Expr::is_clock_free), | |
| 179 | + Expr::ContextLit(entries) => entries.iter().all(|(_, e)| e.is_clock_free()), | |
| 180 | + Expr::Path(target, _) => target.is_clock_free(), | |
| 181 | + // `now`/`today` are the only clock-reading built-ins (ADR-18). Any | |
| 182 | + // call by those names is treated as a clock read regardless of args. | |
| 183 | + Expr::FnCall(name, args) => { | |
| 184 | + name != "now" | |
| 185 | + && name != "today" | |
| 186 | + && args.positional.iter().all(Expr::is_clock_free) | |
| 187 | + && args.named.iter().all(|(_, e)| e.is_clock_free()) | |
| 188 | + } | |
| 189 | + Expr::FnDef { body, .. } => body.is_clock_free(), | |
| 190 | + Expr::Neg(e) | Expr::Not(e) => e.is_clock_free(), | |
| 191 | + Expr::Arith(_, a, b) | Expr::Compare(_, a, b) | Expr::And(a, b) | Expr::Or(a, b) => { | |
| 192 | + a.is_clock_free() && b.is_clock_free() | |
| 193 | + } | |
| 194 | + Expr::Between(a, b, c) | Expr::If(a, b, c) => { | |
| 195 | + a.is_clock_free() && b.is_clock_free() && c.is_clock_free() | |
| 196 | + } | |
| 197 | + Expr::In(value, interval) => { | |
| 198 | + value.is_clock_free() | |
| 199 | + && interval.start.is_clock_free() | |
| 200 | + && interval.end.is_clock_free() | |
| 201 | + } | |
| 202 | + Expr::For { source, body, .. } => source.is_clock_free() && body.is_clock_free(), | |
| 203 | + Expr::Quantified { | |
| 204 | + source, predicate, .. | |
| 205 | + } => source.is_clock_free() && predicate.is_clock_free(), | |
| 206 | + } | |
| 207 | + } | |
| 208 | +} | |
| 209 | + | |
| 210 | +impl IterSource { | |
| 211 | + fn is_clock_free(&self) -> bool { | |
| 212 | + match self { | |
| 213 | + IterSource::Expr(e) => e.is_clock_free(), | |
| 214 | + IterSource::Range(a, b) => a.is_clock_free() && b.is_clock_free(), | |
| 215 | + } | |
| 216 | + } | |
| 217 | +} | |
modified crates/feel-core/src/builtins.rs +5 −2
| @@ -140,9 +140,12 @@ | ||
| 140 | 140 | "today" => nullary(args, || crate::temporal::today_value(&env.now)), |
| 141 | 141 | |
| 142 | 142 | // --- temporal constructors & component functions (ADR-18) --- |
| 143 | + // `time`/`date and time` are the only constructors that can attach a named | |
| 144 | + // zone (via string parsing); vet it away under the IMMUTABLE policy (RD-10). | |
| 145 | + // `date` carries no zone, so it needs no vetting. | |
| 143 | 146 | "date" => date_fn(args), |
| 144 | - "time" => time_fn(args), | |
| 145 | - "date and time" => date_and_time_fn(args), | |
| 147 | + "time" => env.vet_named_zone(time_fn(args)), | |
| 148 | + "date and time" => env.vet_named_zone(date_and_time_fn(args)), | |
| 146 | 149 | "duration" => duration_fn(args), |
| 147 | 150 | "years and months duration" => years_and_months_duration_fn(args), |
| 148 | 151 | "day of week" => component_str_fn(args, crate::temporal::day_of_week_name), |
modified crates/feel-core/src/eval.rs +3 −1
| @@ -56,8 +56,10 @@ | ||
| 56 | 56 | Expr::Number(n) => Ok(Value::Number(*n)), |
| 57 | 57 | Expr::Str(s) => Ok(Value::String(s.clone())), |
| 58 | 58 | // A malformed temporal literal is a type/domain error -> error-null. |
| 59 | + // A named-zone literal is vetted away under the IMMUTABLE policy (RD-10). | |
| 59 | 60 | Expr::TemporalLit(s) => { |
| 60 | - Ok(crate::temporal::parse_at_literal(s).unwrap_or(Value::Error)) | |
| 61 | + let v = crate::temporal::parse_at_literal(s).unwrap_or(Value::Error); | |
| 62 | + Ok(self.env.vet_named_zone(v)) | |
| 61 | 63 | } |
| 62 | 64 | Expr::Bool(b) => Ok(Value::Boolean(*b)), |
| 63 | 65 | Expr::Null => Ok(Value::Null), |
modified crates/feel-core/src/lib.rs +105 −0
| @@ -121,6 +121,20 @@ | ||
| 121 | 121 | pub fn evaluate_with_env(&self, context: &Context, env: &EvalEnv) -> Result<Value, FeelError> { |
| 122 | 122 | eval::Evaluator::new(env).eval(&self.0, context) |
| 123 | 123 | } |
| 124 | + | |
| 125 | + /// `true` if this expression never reads the wall clock — it calls neither | |
| 126 | + /// `now()` nor `today()` anywhere (ADR-18). Wrappers exposing a deterministic, | |
| 127 | + /// IMMUTABLE evaluation surface (e.g. feel-pg's `feel_eval_immutable`, for | |
| 128 | + /// expression indexes; RD-10) gate on this: a non-clock-free expression cannot | |
| 129 | + /// be IMMUTABLE because `now()`/`today()` vary over time. | |
| 130 | + /// | |
| 131 | + /// This is the *static* half. The runtime half — forbidding *named* time zones, | |
| 132 | + /// whose offsets depend on the updatable IANA tzdb — is | |
| 133 | + /// [`EvalEnv::forbidding_named_zones`]. Host functions/objects are out of scope | |
| 134 | + /// here (the embedder's concern; feel-pg has no host-function seam). | |
| 135 | + pub fn is_clock_free(&self) -> bool { | |
| 136 | + self.0.is_clock_free() | |
| 137 | + } | |
| 124 | 138 | } |
| 125 | 139 | |
| 126 | 140 | /// A registry of host-provided ("ambient") functions, looked up by name when a |
| @@ -157,6 +171,12 @@ | ||
| 157 | 171 | pub now: jiff::Zoned, |
| 158 | 172 | /// Ambient host functions, consulted after context bindings and built-ins. |
| 159 | 173 | pub functions: &'a dyn FunctionRegistry, |
| 174 | + /// When set, constructing a temporal value with a *named* IANA zone (e.g. | |
| 175 | + /// `time("09:00:00@Europe/Berlin")`) yields an error-null instead; fixed | |
| 176 | + /// offsets (`+02:00`) and `Z` are unaffected. Hosts needing tzdb-independent, | |
| 177 | + /// IMMUTABLE-safe evaluation turn it on via [`EvalEnv::forbidding_named_zones`] | |
| 178 | + /// (feel-pg's `feel_eval_immutable`; RD-10). Default `false`. | |
| 179 | + pub reject_named_zones: bool, | |
| 160 | 180 | } |
| 161 | 181 | |
| 162 | 182 | impl<'a> EvalEnv<'a> { |
| @@ -167,6 +187,7 @@ | ||
| 167 | 187 | regex, |
| 168 | 188 | now, |
| 169 | 189 | functions: &EMPTY_REGISTRY, |
| 190 | + reject_named_zones: false, | |
| 170 | 191 | } |
| 171 | 192 | } |
| 172 | 193 | |
| @@ -180,6 +201,29 @@ | ||
| 180 | 201 | regex, |
| 181 | 202 | now, |
| 182 | 203 | functions, |
| 204 | + reject_named_zones: false, | |
| 205 | + } | |
| 206 | + } | |
| 207 | + | |
| 208 | + /// Return this env with the *named*-zone policy on: any temporal value carrying | |
| 209 | + /// a named IANA zone becomes an error-null during evaluation, while fixed | |
| 210 | + /// offsets and `Z` pass through. Used for IMMUTABLE evaluation, where a named | |
| 211 | + /// zone's offset would depend on the updatable IANA tzdb (RD-10). Pair it with | |
| 212 | + /// [`CompiledExpr::is_clock_free`] for a fully deterministic surface. | |
| 213 | + pub fn forbidding_named_zones(mut self) -> Self { | |
| 214 | + self.reject_named_zones = true; | |
| 215 | + self | |
| 216 | + } | |
| 217 | + | |
| 218 | + /// Apply the named-zone policy to a freshly-constructed temporal value: a | |
| 219 | + /// named-zone `time`/`date and time` becomes an error-null when the policy is | |
| 220 | + /// on; everything else passes through unchanged. A single tag test when the | |
| 221 | + /// policy is off, so the default path pays nothing. | |
| 222 | + pub(crate) fn vet_named_zone(&self, v: Value) -> Value { | |
| 223 | + if self.reject_named_zones && temporal::has_named_zone(&v) { | |
| 224 | + Value::Error | |
| 225 | + } else { | |
| 226 | + v | |
| 183 | 227 | } |
| 184 | 228 | } |
| 185 | 229 | } |
| @@ -265,6 +309,67 @@ | ||
| 265 | 309 | #[test] |
| 266 | 310 | fn compile_reports_parse_error() { |
| 267 | 311 | assert!(matches!(compile("1 +"), Err(FeelError::Parse(_)))); |
| 312 | + } | |
| 313 | + | |
| 314 | + #[test] | |
| 315 | + fn is_clock_free_finds_now_and_today_anywhere() { | |
| 316 | + // Pure expressions are clock-free, including temporal constructors and | |
| 317 | + // named-zone literals (the clock check is orthogonal to zones). | |
| 318 | + for src in [ | |
| 319 | + "1 + 1", | |
| 320 | + "date(2020, 1, 1)", | |
| 321 | + r#"time("09:00:00@Europe/Berlin")"#, | |
| 322 | + "for x in [1, 2, 3] return x + 1", | |
| 323 | + ] { | |
| 324 | + assert!( | |
| 325 | + compile(src).unwrap().is_clock_free(), | |
| 326 | + "expected clock-free: {src}" | |
| 327 | + ); | |
| 328 | + } | |
| 329 | + // `now()`/`today()` make it not clock-free, however deeply nested. | |
| 330 | + for src in [ | |
| 331 | + "now()", | |
| 332 | + "today()", | |
| 333 | + "1 + now()", | |
| 334 | + "if a then 1 else today()", | |
| 335 | + "[1, now(), 3]", | |
| 336 | + "{ a: now() }", | |
| 337 | + "some x in [1, 2] satisfies x > today()", | |
| 338 | + ] { | |
| 339 | + assert!( | |
| 340 | + !compile(src).unwrap().is_clock_free(), | |
| 341 | + "expected clock-reading: {src}" | |
| 342 | + ); | |
| 343 | + } | |
| 344 | + } | |
| 345 | + | |
| 346 | + #[test] | |
| 347 | + fn forbidding_named_zones_rejects_named_keeps_offsets() { | |
| 348 | + let forbid = | |
| 349 | + EvalEnv::new(&crate::regex_provider::DEFAULT, default_now()).forbidding_named_zones(); | |
| 350 | + let eval = | |
| 351 | + |src: &str, ctx: &Context| compile(src).unwrap().evaluate_with_env(ctx, &forbid).unwrap(); | |
| 352 | + let empty = Context::empty(); | |
| 353 | + | |
| 354 | + // Fixed offsets and `Z` are tzdb-independent -> always allowed. | |
| 355 | + assert!(matches!(eval(r#"time("09:00:00+02:00")"#, &empty), Value::Time(_))); | |
| 356 | + assert!(matches!(eval(r#"time("09:00:00Z")"#, &empty), Value::Time(_))); | |
| 357 | + | |
| 358 | + // A named zone is allowed by the default env (no policy) ... | |
| 359 | + assert!(matches!( | |
| 360 | + compile(r#"time("09:00:00@Europe/Berlin")"#) | |
| 361 | + .unwrap() | |
| 362 | + .evaluate(&empty) | |
| 363 | + .unwrap(), | |
| 364 | + Value::Time(_) | |
| 365 | + )); | |
| 366 | + // ... but becomes an error-null under the policy — whether the named zone | |
| 367 | + // comes from a constructor literal, an `@"…"` literal, or runtime data. | |
| 368 | + assert_eq!(eval(r#"time("09:00:00@Europe/Berlin")"#, &empty), Value::Error); | |
| 369 | + assert_eq!(eval(r#"@"2017-08-10T12:00:00@Europe/Berlin""#, &empty), Value::Error); | |
| 370 | + let mut ctx = Context::empty(); | |
| 371 | + ctx.insert("s", Value::String("2017-08-10T12:00:00@Europe/Berlin".into())); | |
| 372 | + assert_eq!(eval("date and time(s)", &ctx), Value::Error); | |
| 268 | 373 | } |
| 269 | 374 | |
| 270 | 375 | #[test] |
modified crates/feel-core/src/temporal.rs +18 −0
| @@ -46,6 +46,24 @@ | ||
| 46 | 46 | pub struct DateTime { |
| 47 | 47 | pub dt: civil::DateTime, |
| 48 | 48 | pub zone: Option<Zone>, |
| 49 | +} | |
| 50 | + | |
| 51 | +/// `true` if `v` is a `time` / `date and time` carrying a *named* IANA zone | |
| 52 | +/// (e.g. `@Europe/Berlin`) rather than a fixed offset or `Z`. A named zone's UTC | |
| 53 | +/// offset is resolved against the (updatable) IANA tzdb, so a deterministic / | |
| 54 | +/// IMMUTABLE evaluation surface bans it via [`crate::EvalEnv::vet_named_zone`] | |
| 55 | +/// (RD-10). Offsets and `Z` are tzdb-independent and always allowed. | |
| 56 | +pub(crate) fn has_named_zone(v: &Value) -> bool { | |
| 57 | + matches!( | |
| 58 | + v, | |
| 59 | + Value::Time(Time { | |
| 60 | + zone: Some(Zone::Named(_)), | |
| 61 | + .. | |
| 62 | + }) | Value::DateTime(DateTime { | |
| 63 | + zone: Some(Zone::Named(_)), | |
| 64 | + .. | |
| 65 | + }) | |
| 66 | + ) | |
| 49 | 67 | } |
| 50 | 68 | |
| 51 | 69 | // --------------------------------------------------------------------------- // |