Skip to content

Truly independent web browser

LibJS: Reject a nullish base before converting the key of a store

Shannon Booth74a0613d1d79
diff --git a/Libraries/LibJS/Rust/src/bytecode/property_access.rs b/Libraries/LibJS/Rust/src/bytecode/property_access.rs
index ea1dc1a4465..60ebba3d6a8 100644
--- a/Libraries/LibJS/Rust/src/bytecode/property_access.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/property_access.rs
@@ -450,7 +450,7 @@ fn throw_null_or_undefined_property_access<T>(
vm: &Vm,
base_value: Value,
base_identifier: Option<Utf16FlyString>,
- property_identifier: &PropertyKey,
+ property_identifier: &dyn Utf16Display,
) -> ThrowCompletionOr<T> {
assert!(base_value.is_nullish());
@@ -468,6 +468,30 @@ fn throw_null_or_undefined_property_access<T>(
)
}
+/// Throw the TypeError of a put to `base` if it is null or undefined. ToObject converts the base before the key is
+/// converted to a property key, so `property_name` is the property as the error names it: a key, or the value a key
+/// has yet to be computed from.
+pub fn throw_if_nullish_put_base(
+ vm: &Vm,
+ base: Value,
+ get_base_identifier: impl FnOnce() -> Option<Utf16FlyString>,
+ property_name: &dyn Utf16Display,
+ strict: Strict,
+) -> ThrowCompletionOr<()> {
+ if !base.is_nullish() {
+ return Ok(());
+ }
+ // Better error message than to_object would give
+ if strict == Strict::Yes {
+ return vm.throw_completion(
+ ErrorKind::TypeError,
+ ErrorType::ReferenceNullishSetProperty,
+ &[property_name, &base],
+ );
+ }
+ throw_null_or_undefined_property_access(vm, base, get_base_identifier(), property_name)
+}
+
#[allow(clippy::too_many_arguments)]
#[inline]
pub fn put_by_property_key(
@@ -482,19 +506,8 @@ pub fn put_by_property_key(
caches: Option<&PropertyLookupCache>,
cache_key: u64,
) -> ThrowCompletionOr<()> {
- // Better error message than to_object would give
- if strict == Strict::Yes && base.is_nullish() {
- return vm.throw_completion(
- ErrorKind::TypeError,
- ErrorType::ReferenceNullishSetProperty,
- &[name, &base],
- );
- }
-
// a. Let baseObj be ? ToObject(V.[[Base]]).
- if base.is_nullish() {
- return throw_null_or_undefined_property_access(vm, base, get_base_identifier(), name);
- }
+ throw_if_nullish_put_base(vm, base, get_base_identifier, name, strict)?;
let object = base.to_object(vm)?;
if kind == PutKind::Getter || kind == PutKind::Setter {
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs
index 5c54f4b22cc..56d9732c505 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/property_access.rs
@@ -22,7 +22,7 @@ use crate::bytecode::op;
use crate::bytecode::property_access::{
self, GetByIdMode, Strict, base_object_for_get, get_by_value_with_keyed_cache, get_cached_property_value,
get_with_property_lookup_cache, object_can_cache_property_additions, property_addition_is_cacheable,
- put_by_property_key,
+ put_by_property_key, throw_if_nullish_put_base,
};
use crate::gc::class::GcCell;
use crate::gc::class::class_of;
@@ -462,6 +462,18 @@ pub fn put_by_value(
let value = values.src;
let base = values.base;
let property = values.property;
+ // NB: PutValue converts the base to an object before it converts the key to a property key.
+ asm_try!(
+ vm,
+ pc,
+ throw_if_nullish_put_base(
+ vm,
+ base,
+ || optional_identifier(vm, &instruction.base_identifier),
+ &property,
+ strict_of(instruction.header.strict),
+ )
+ );
let property_key = asm_try!(vm, pc, property.to_property_key(vm));
let cache_key = match site_cache {
KeyedSiteCache::Use => keyed_property_lookup_cache_key(property, &property_key),
@@ -615,6 +627,18 @@ pub fn put_by_value_with_this(
let value = values.src;
let base = values.base;
let this_value = values.this_value;
+ // NB: PutValue converts the base to an object before it converts the key to a property key.
+ asm_try!(
+ vm,
+ pc,
+ throw_if_nullish_put_base(
+ vm,
+ base,
+ || None,
+ &values.property,
+ strict_of(instruction.header.strict)
+ )
+ );
let property_key = asm_try!(vm, pc, values.property.to_property_key(vm));
asm_try!(
vm,
@@ -672,6 +696,10 @@ pub fn delete_by_value(
instruction: &op::DeleteByValue,
values: &mut op::DeleteByValueValues,
) -> SlowPathControl {
+ // NB: delete converts the base to an object before it converts the key to a property key.
+ if values.base.is_nullish() {
+ asm_try!(vm, pc, values.base.to_object(vm));
+ }
let property_key = asm_try!(vm, pc, values.property.to_property_key(vm));
let result = asm_try!(
vm,
diff --git a/Tests/LibJS/Runtime/operators/assignment-member-references.js b/Tests/LibJS/Runtime/operators/assignment-member-references.js
index a87f3709610..29d14ebaa01 100644
--- a/Tests/LibJS/Runtime/operators/assignment-member-references.js
+++ b/Tests/LibJS/Runtime/operators/assignment-member-references.js
@@ -67,6 +67,41 @@ test("nullish bases are rejected before key conversion or the right-hand side",
}
});
+test("nullish bases are rejected before the key of a store or delete is converted", () => {
+ const expressions = [
+ "base[key] = 1",
+ "'use strict'; base[key] = 1",
+ "[base[key]] = [1]",
+ "({ a: base[key] } = { a: 1 })",
+ "for (base[key] of [1]);",
+ "delete base[key]",
+ ];
+ for (const expression of expressions) {
+ for (const base of [null, undefined]) {
+ const key = {
+ toString() {
+ throw new Error("The property key must not be converted");
+ },
+ };
+ const run = new Function("base", "key", expression);
+ expect(() => run(base, key)).toThrow(TypeError);
+ }
+ }
+
+ const key = {
+ toString() {
+ throw new Error("The property key must not be converted");
+ },
+ };
+ const home = {
+ method() {
+ super[key] = 1;
+ },
+ };
+ Object.setPrototypeOf(home, null);
+ expect(() => home.method()).toThrow(TypeError);
+});
+
test("member bases survive changes made while evaluating the property", () => {
for (const expression of ["base[(base = other, 'x')]++", "base[(base = other, 'x')] += 2"]) {
const object = { x: 1 };