Truly independent web browser
LibWeb: Refuse an IDB key path at which a typed array cannot take a key
Shannon Booth615322647db6
diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cppindex 3b2ee0583af..a5a05e40ff2 100644--- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp+++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp@@ -1204,6 +1204,19 @@ WebIDL::ExceptionOr<ErrorOr<GC::Ref<Key>>> extract_a_key_from_a_value_using_a_kereturn multi_entry ? TRY(convert_a_value_to_a_multi_entry_key(realm, r)) : TRY(convert_a_value_to_a_key(realm, r));}+// AD-HOC: Whether CreateDataProperty(value, identifier, key) would succeed, which inject a key into a value using a+// key path asserts: a TypedArray refuses a canonical numeric string, such as "Infinity", that is not an+// index of it.+static bool can_create_data_property(JS::VM& vm, JS::Object const& value, Utf16String const& identifier)+{+ if (!is<JS::TypedArrayBase>(value))+ return true;+ if (identifier == u"-0"sv)+ return false;+ auto number = MUST(JS::Value { JS::PrimitiveString::create(vm, identifier) }.to_double(vm));+ return JS::number_to_utf16_string(number) != identifier;+}+// https://w3c.github.io/IndexedDB/#check-that-a-key-could-be-injected-into-a-valuebool check_that_a_key_could_be_injected_into_a_value(JS::Realm& realm, JS::Value value, KeyPath const& key_path){@@ -1216,7 +1229,7 @@ bool check_that_a_key_could_be_injected_into_a_value(JS::Realm& realm, JS::ValueVERIFY(!identifiers.is_empty());// 3. Remove the last item of identifiers.- (void)identifiers.take_last();+ auto last = identifiers.take_last();// 4. For each remaining identifier of identifiers, if any:for (auto const& identifier : identifiers) {@@ -1231,14 +1244,14 @@ bool check_that_a_key_could_be_injected_into_a_value(JS::Realm& realm, JS::Value// 3. If hop is false, return true.if (!hop)- return true;+ return can_create_data_property(realm.vm(), value.as_object(), identifier);// 4. Let value be ! Get(value, identifier).value = MUST(value.as_object().get(identifier_property));}// 5. Return true if value is an Object or an Array, or false otherwise.- return value.is_object() || MUST(value.is_array(realm.vm()));+ return (value.is_object() || MUST(value.is_array(realm.vm()))) && can_create_data_property(realm.vm(), value.as_object(), last);}// https://w3c.github.io/IndexedDB/#fire-an-error-eventdiff --git a/Tests/LibWeb/Text/expected/indexeddb-inject-key-into-typed-array.txt b/Tests/LibWeb/Text/expected/indexeddb-inject-key-into-typed-array.txtnew file mode 100644index 00000000000..dce3322fefb--- /dev/null+++ b/Tests/LibWeb/Text/expected/indexeddb-inject-key-into-typed-array.txt@@ -0,0 +1,5 @@+typed array, key path Infinity: put threw DataError+nested typed array, key path a.NaN: put threw DataError+nested typed array, key path a.NaN.x: put threw DataError+typed array, key path x: key 1+plain object, key path Infinity: key 1diff --git a/Tests/LibWeb/Text/input/indexeddb-inject-key-into-typed-array.html b/Tests/LibWeb/Text/input/indexeddb-inject-key-into-typed-array.htmlnew file mode 100644index 00000000000..9a359f7597e--- /dev/null+++ b/Tests/LibWeb/Text/input/indexeddb-inject-key-into-typed-array.html@@ -0,0 +1,43 @@+<!DOCTYPE html>+<script src="include.js"></script>+<script>+asyncTest(async done => {+ spoofCurrentURL("https://example.com/indexeddb-inject-key-into-typed-array.html");++ const cases = [+ ["typed array, key path Infinity", "Infinity", () => new Uint8Array(4)],+ ["nested typed array, key path a.NaN", "a.NaN", () => ({ a: new Uint8Array(4) })],+ ["nested typed array, key path a.NaN.x", "a.NaN.x", () => ({ a: new Uint8Array(4) })],+ ["typed array, key path x", "x", () => new Uint8Array(4)],+ ["plain object, key path Infinity", "Infinity", () => ({})],+ ];+ for (const [name, keyPath, value] of cases) {+ const dbName = "test-inject-key-into-typed-array-" + Date.now() + Math.random();+ await new Promise(resolve => {+ const request = indexedDB.open(dbName, 1);+ request.onupgradeneeded = event => {+ const store = event.target.result.createObjectStore("store", { keyPath, autoIncrement: true });+ let put;+ try {+ put = store.put(value());+ } catch (error) {+ println(`${name}: put threw ${error.name}`);+ return;+ }+ put.onsuccess = () => println(`${name}: key ${put.result}`);+ put.onerror = () => println(`${name}: put failed with ${put.error.name}`);+ };+ request.onsuccess = () => {+ request.result.close();+ indexedDB.deleteDatabase(dbName);+ resolve();+ };+ request.onerror = () => {+ println(`${name}: open failed with ${request.error.name}`);+ resolve();+ };+ });+ }+ done();+});+</script>