fix(wasm): guard js_to_value against cyclic objects
public
fix(wasm): guard js_to_value against cyclic objects js_to_value recursed without bound, so a cyclic JS context object (a.b = a is legal in JS) could overflow the stack. Thread the same id()/depth guard feel-py uses: a back-edge (an object identical to an ancestor) or an over-deep node maps to null, giving DAG semantics. No other behaviour change — own-enumerable getters still fire as before. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
| Changeset | ab470441bdeb4110e1789f48a09ccea5f7cbfc64 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | dfeef6a90898 |
| Children | |
| Pushlog | by timo via ssh |
modified crates/feel-wasm/src/lib.rs +31 −3
| @@ -203,10 +203,22 @@ | ||
| 203 | 203 | } |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | +/// Maximum nesting depth converted from the JS context — a back-stop alongside | |
| 207 | +/// the identity-based cycle guard below (deeper than this maps to `null`). | |
| 208 | +const MAX_DEPTH: usize = 256; | |
| 209 | + | |
| 206 | 210 | /// Map a native `JsValue` to a `feel-core` [`Value`], accepting a JS `Date` |
| 207 | 211 | /// anywhere as a FEEL date and time (ADR-18). Numbers, booleans, strings, arrays |
| 208 | 212 | /// (-> list), plain objects (-> context) and null/undefined are supported. |
| 209 | 213 | fn js_to_value(value: &JsValue) -> Value { |
| 214 | + js_to_value_guarded(value, &mut Vec::new(), 0) | |
| 215 | +} | |
| 216 | + | |
| 217 | +/// As [`js_to_value`], threading a cycle/depth guard: a JS object graph can be | |
| 218 | +/// cyclic (`a.b = a` is legal), which would recurse forever. `seen` holds the | |
| 219 | +/// objects on the current path (a back-edge — an object identical to an ancestor — | |
| 220 | +/// becomes `null`, giving DAG semantics); `depth` is the back-stop. | |
| 221 | +fn js_to_value_guarded(value: &JsValue, seen: &mut Vec<JsValue>, depth: usize) -> Value { | |
| 210 | 222 | if value.is_null() || value.is_undefined() { |
| 211 | 223 | return Value::Null; |
| 212 | 224 | } |
| @@ -224,17 +236,33 @@ | ||
| 224 | 236 | let iso = date.to_iso_string().as_string().unwrap_or_default(); |
| 225 | 237 | return feel_core::temporal_from_iso("date and time", &iso).unwrap_or(Value::Null); |
| 226 | 238 | } |
| 239 | + // Only arrays/objects recurse, so the guard sits here: a cycle back-edge or an | |
| 240 | + // over-deep node collapses to `null` instead of overflowing the stack. | |
| 241 | + if depth > MAX_DEPTH || seen.iter().any(|s| js_sys::Object::is(s, value)) { | |
| 242 | + return Value::Null; | |
| 243 | + } | |
| 227 | 244 | if js_sys::Array::is_array(value) { |
| 228 | 245 | let arr: &js_sys::Array = value.unchecked_ref(); |
| 229 | - return Value::List(arr.iter().map(|x| js_to_value(&x)).collect()); | |
| 246 | + seen.push(value.clone()); | |
| 247 | + let items = arr | |
| 248 | + .iter() | |
| 249 | + .map(|x| js_to_value_guarded(&x, seen, depth + 1)) | |
| 250 | + .collect(); | |
| 251 | + seen.pop(); | |
| 252 | + return Value::List(items); | |
| 230 | 253 | } |
| 231 | 254 | if value.is_object() { |
| 232 | 255 | let obj: &js_sys::Object = value.unchecked_ref(); |
| 256 | + seen.push(value.clone()); | |
| 233 | 257 | let mut map = BTreeMap::new(); |
| 234 | 258 | for key in js_sys::Object::keys(obj).iter() { |
| 235 | 259 | let val = js_sys::Reflect::get(value, &key).unwrap_or(JsValue::NULL); |
| 236 | - map.insert(key.as_string().unwrap_or_default(), js_to_value(&val)); | |
| 237 | - } | |
| 260 | + map.insert( | |
| 261 | + key.as_string().unwrap_or_default(), | |
| 262 | + js_to_value_guarded(&val, seen, depth + 1), | |
| 263 | + ); | |
| 264 | + } | |
| 265 | + seen.pop(); | |
| 238 | 266 | return Value::Context(map); |
| 239 | 267 | } |
| 240 | 268 | Value::Null |