From 2a476af2b8c87692ef576bc661ee7408b4ab5b56 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 27 Nov 2026 16:47:00 -0800 Subject: [PATCH] Scripts: charge an indexer's object and key to the run's data limits Reading `object[k]` on an object of a host class with an indexer copied the object's class name and handle, and a name key, to the indexer without charging them to the data limits. A member call on the same object was charged and could be refused. The indexer now reads the object and a name key the same way, so `object[k]` throws a RangeError when the handle or the name exceeds the limits. --- crates/js/src/host.rs | 17 +++++++++++++++++ crates/js/src/host_classes.rs | 8 ++++++-- crates/js/src/host_tests.rs | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/crates/js/src/host.rs b/crates/js/src/host.rs index d9da47979b..58e5f5ef19 100644 --- a/crates/js/src/host.rs +++ b/crates/js/src/host.rs @@ -307,6 +307,23 @@ impl Interp { self.host_ref_of(id)?.ok_or_else(|| Abrupt::Internal("host object without a class".into())) } + /// The arguments an indexer named `name` gets for `object[k]`: the object of a host class `id` + /// and the key `k`, charged to the data limits as the arguments of a member call are. `None` + /// when `id` is not an object of a host class. + pub(crate) fn read_indexer_args(&mut self, name: &Rc, id: ObjId, k: Data) -> JsResult>> { + if self.host_ref_size(id)?.is_none() { + return Ok(None); + } + let mut reading = + Reading { limits: self.data_limits, bytes: 0, entries: 0, ancestors: Vec::new(), path: Vec::new(), function: name.clone(), hosted: true }; + let this = self.read_host_ref(id, &mut reading)?; + if let Data::String(s) = &k { + reading.path.push(Step::Index(0)); + self.charge_reading(&mut reading, s.encode_utf16().count().saturating_mul(3).saturating_add(2))?; + } + Ok(Some(vec![this, k])) + } + fn read_array(&mut self, id: ObjId, length: u32, r: &mut Reading) -> JsResult { self.charge_entries(r, length as usize)?; let mut items = Vec::with_capacity(length as usize); diff --git a/crates/js/src/host_classes.rs b/crates/js/src/host_classes.rs index 00ee38ce4f..295fd07053 100644 --- a/crates/js/src/host_classes.rs +++ b/crates/js/src/host_classes.rs @@ -239,8 +239,12 @@ impl Interp { /// gets `true` as a third argument. pub(crate) fn host_index(&mut self, f: &HostFunction, id: ObjId, k: Data, probe: bool) -> JsResult { self.use_fuel()?; - let Some(this) = self.host_ref_of(id)? else { return Ok(Value::Undefined) }; - let mut args = vec![this, k]; + let Class::Host(h) = &self.object(id)?.class else { return Ok(Value::Undefined) }; + let name: Rc = match self.host_classes.get(h.class as usize) { + Some(e) => format!("{}[]", e.name).into(), + None => return Err(Abrupt::Internal("host class out of range".into())), + }; + let Some(mut args) = self.read_indexer_args(&name, id, k)? else { return Ok(Value::Undefined) }; if probe { args.push(Data::Bool(true)); } diff --git a/crates/js/src/host_tests.rs b/crates/js/src/host_tests.rs index abd80993ff..b16880f8c7 100644 --- a/crates/js/src/host_tests.rs +++ b/crates/js/src/host_tests.rs @@ -840,6 +840,24 @@ fn the_handles_of_host_objects_given_to_the_host_count_toward_the_data_limits() assert_eq!(r.run("thing.take([thing, thing]) === undefined").unwrap(), JsValue::Bool(true)); } +#[test] +fn the_object_an_indexer_reads_counts_toward_the_data_limits() { + let mut r = realm(100_000_000); + let index: HostFunction = Rc::new(|_| Ok(Data::Number(1.0))); + let class = HostClass { name: "List".into(), parent: None, members: Vec::new(), index: Some(index), construct: None }; + r.define_host_classes(vec![class], vec![("long".into(), host_ref("List", &[0.0; 1024])), ("short".into(), host_ref("List", &[0.0]))]).unwrap(); + r.set_data_limits(DataLimits { depth: 64, bytes: 16 << 20, entries: 1_000 }); + let probe = "var m = 'none'; try { long[0]; } catch (e) { m = e.name + '|' + e.message; } m"; + let JsValue::String(out) = r.run(probe).unwrap() else { panic!("not a string") }; + assert!(out.starts_with("RangeError|") && out.contains("too many entries"), "{out}"); + // A long name given to the indexer counts too. + r.set_data_limits(DataLimits { depth: 64, bytes: 1_000, entries: 1_000 }); + let probe = "var m = 'none'; try { short[new Array(600).join('x')]; } catch (e) { m = e.name + '|' + e.message; } m"; + let JsValue::String(out) = r.run(probe).unwrap() else { panic!("not a string") }; + assert!(out.starts_with("RangeError|") && out.contains("too large"), "{out}"); + assert_eq!(r.run("short[0] + short.name").unwrap(), JsValue::Number(2.0)); +} + #[test] fn a_host_class_constructor_called_with_new_cannot_wait() { let mut r = realm(1_000_000);