Skip to content

Truly independent web browser

LibWeb: Return the same array from IndexedDB keyPath getters

Andreas Kling91151f8f700c
diff --git a/Libraries/LibWeb/IndexedDB/IDBIndex.cpp b/Libraries/LibWeb/IndexedDB/IDBIndex.cpp
index c179e50d65e..48a29e33f28 100644
--- a/Libraries/LibWeb/IndexedDB/IDBIndex.cpp
+++ b/Libraries/LibWeb/IndexedDB/IDBIndex.cpp
@@ -36,6 +36,7 @@ void IDBIndex::visit_edges(GC::Cell::Visitor& visitor)
Base::visit_edges(visitor);
visitor.visit(m_index);
visitor.visit(m_object_store_handle);
+ visitor.visit(m_key_path_arrays);
}
JS::Object& IDBIndex::relevant_global_object() const
@@ -97,9 +98,21 @@ KeyPath const& IDBIndex::key_path() const
return m_index->key_path();
}
-JS::ThrowCompletionOr<JS::Value> IDBIndex::key_path_value(JS::Realm& realm) const
+// https://w3c.github.io/IndexedDB/#dom-idbindex-keypath
+JS::ThrowCompletionOr<JS::Value> IDBIndex::key_path_value(JS::Realm& realm)
{
- return convert_a_key_path_to_a_value(realm, key_path());
+ // The keyPath getter steps are to return this’s index’s key path. The key path is converted as a DOMString (if a
+ // string) or a sequence<DOMString> (if a list of strings), per [WEBIDL].
+ // NOTE: The returned value is not the same instance that was used when the index was created. However, if this
+ // attribute returns an object (specifically an Array), it returns the same object instance every time it is
+ // inspected. Changing the properties of the object has no effect on the index.
+ if (auto array = m_key_path_arrays.get(realm.global_object()); array.has_value())
+ return *array;
+
+ auto value = TRY(convert_a_key_path_to_a_value(realm, key_path()));
+ if (value.is_object())
+ m_key_path_arrays.set(realm.global_object(), value.as_object());
+ return value;
}
// https://w3c.github.io/IndexedDB/#dom-idbindex-opencursor
diff --git a/Libraries/LibWeb/IndexedDB/IDBIndex.h b/Libraries/LibWeb/IndexedDB/IDBIndex.h
index 50a2750327b..c5303a122e6 100644
--- a/Libraries/LibWeb/IndexedDB/IDBIndex.h
+++ b/Libraries/LibWeb/IndexedDB/IDBIndex.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/HashMap.h>
#include <LibGC/Heap.h>
#include <LibWeb/Bindings/IDBCursor.h>
#include <LibWeb/Bindings/Wrappable.h>
@@ -29,7 +30,7 @@ public:
Utf16String name() const { return m_name; }
GC::Ref<IDBObjectStore> object_store() { return m_object_store_handle; }
KeyPath const& key_path() const;
- JS::ThrowCompletionOr<JS::Value> key_path_value(JS::Realm&) const;
+ JS::ThrowCompletionOr<JS::Value> key_path_value(JS::Realm&);
bool multi_entry() const { return m_index->multi_entry(); }
bool unique() const { return m_index->unique(); }
@@ -60,6 +61,9 @@ private:
// An index handle has a name, which is initialized to the name of the associated index when the index handle is created.
Utf16String m_name;
+
+ // NB: The keyPath getter returns the same Array every time it is inspected in the same realm.
+ HashMap<GC::Ref<JS::Object>, GC::Ref<JS::Object>> m_key_path_arrays;
};
}
diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp
index 3ce7b23b2c2..e1bcc264eae 100644
--- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp
+++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.cpp
@@ -40,6 +40,7 @@ void IDBObjectStore::visit_edges(GC::Cell::Visitor& visitor)
visitor.visit(m_store);
visitor.visit(m_transaction);
visitor.visit(m_indexes);
+ visitor.visit(m_key_path_arrays);
}
JS::Object& IDBObjectStore::relevant_global_object() const
@@ -104,13 +105,25 @@ Optional<KeyPath> const& IDBObjectStore::key_path() const
return m_store->key_path();
}
-JS::ThrowCompletionOr<JS::Value> IDBObjectStore::key_path_value(JS::Realm& realm) const
+// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-keypath
+JS::ThrowCompletionOr<JS::Value> IDBObjectStore::key_path_value(JS::Realm& realm)
{
+ // The keyPath getter steps are to return this’s object store’s key path, or null if none. The key path is
+ // converted as a DOMString (if a string) or a sequence<DOMString> (if a list of strings), per [WEBIDL].
auto const& key_path = this->key_path();
if (!key_path.has_value())
return JS::js_null();
- return convert_a_key_path_to_a_value(realm, key_path.value());
+ // NOTE: The returned value is not the same instance that was used when the object store was created. However, if
+ // this attribute returns an object (specifically an Array), it returns the same object instance every time
+ // it is inspected. Changing the properties of the object has no effect on the object store.
+ if (auto array = m_key_path_arrays.get(realm.global_object()); array.has_value())
+ return *array;
+
+ auto value = TRY(convert_a_key_path_to_a_value(realm, key_path.value()));
+ if (value.is_object())
+ m_key_path_arrays.set(realm.global_object(), value.as_object());
+ return value;
}
// https://w3c.github.io/IndexedDB/#dom-idbobjectstore-indexnames
diff --git a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h
index 688d6218714..b0415838b81 100644
--- a/Libraries/LibWeb/IndexedDB/IDBObjectStore.h
+++ b/Libraries/LibWeb/IndexedDB/IDBObjectStore.h
@@ -33,7 +33,7 @@ public:
Utf16String name() const;
WebIDL::ExceptionOr<void> set_name(Utf16String const& value);
Optional<KeyPath> const& key_path() const;
- JS::ThrowCompletionOr<JS::Value> key_path_value(JS::Realm&) const;
+ JS::ThrowCompletionOr<JS::Value> key_path_value(JS::Realm&);
[[nodiscard]] GC::Ref<HTML::DOMStringList> index_names();
GC::Ref<IDBTransaction> transaction() const;
bool auto_increment() const;
@@ -78,6 +78,9 @@ private:
// An object store handle has an index set
AK::HashMap<Utf16String, GC::Ref<Index>> m_indexes;
+
+ // NB: The keyPath getter returns the same Array every time it is inspected in the same realm.
+ HashMap<GC::Ref<JS::Object>, GC::Ref<JS::Object>> m_key_path_arrays;
};
}
diff --git a/Tests/LibWeb/Text/expected/indexeddb-keypath-cross-realm-identity.txt b/Tests/LibWeb/Text/expected/indexeddb-keypath-cross-realm-identity.txt
new file mode 100644
index 00000000000..6f3206f657f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/indexeddb-keypath-cross-realm-identity.txt
@@ -0,0 +1,8 @@
+store: first array uses the current global: true
+store: other array uses the other global: true
+store: current array keeps its identity: true
+store: other array keeps its identity: true
+index: first array uses the current global: true
+index: other array uses the other global: true
+index: current array keeps its identity: true
+index: other array keeps its identity: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex_keyPath.any.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex_keyPath.any.txt
index 3b6f8d2ec4a..7fde0911c41 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex_keyPath.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbindex_keyPath.any.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 3 tests
-2 Pass
-1 Fail
-Fail IDBIndex's keyPath attribute returns the same object.
+3 Pass
+Pass IDBIndex's keyPath attribute returns the same object.
Pass IDBIndex's keyPath array with a single value
Pass IDBIndex's keyPath array with multiple values
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_keyPath.any.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_keyPath.any.txt
index bfca0907e54..925a7b0c0ec 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_keyPath.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_keyPath.any.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail IDBObjectStore's keyPath attribute returns the same object.
\ No newline at end of file
+1 Pass
+Pass IDBObjectStore's keyPath attribute returns the same object.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/indexeddb-keypath-cross-realm-identity.html b/Tests/LibWeb/Text/input/indexeddb-keypath-cross-realm-identity.html
new file mode 100644
index 00000000000..e4c36739671
--- /dev/null
+++ b/Tests/LibWeb/Text/input/indexeddb-keypath-cross-realm-identity.html
@@ -0,0 +1,53 @@
+<!doctype html>
+<script src="include.js"></script>
+<script>
+ asyncTest(async done => {
+ if (document.readyState !== "complete") {
+ await new Promise(resolve => window.addEventListener("load", resolve, { once: true }));
+ }
+ spoofCurrentURL("https://example.com/indexeddb-keypath-cross-realm-identity.html");
+ const frame = document.createElement("iframe");
+ const loaded = new Promise(resolve => frame.addEventListener("load", resolve, { once: true }));
+ document.body.appendChild(frame);
+ await loaded;
+ const other = frame.contentWindow;
+ const name = `keypath-cross-realm-${Date.now()}-${Math.random()}`;
+ const request = indexedDB.open(name, 1);
+ const check = (label, object, otherPrototype) => {
+ const first = object.keyPath;
+ const getter = other.Object.getOwnPropertyDescriptor(otherPrototype, "keyPath").get;
+ const otherFirst = getter.call(object);
+ println(
+ `${label}: first array uses the current global: ${Object.getPrototypeOf(first) === Array.prototype}`
+ );
+ println(
+ `${label}: other array uses the other global: ${Object.getPrototypeOf(otherFirst) === other.Array.prototype}`
+ );
+ println(`${label}: current array keeps its identity: ${first === object.keyPath}`);
+ println(`${label}: other array keeps its identity: ${otherFirst === getter.call(object)}`);
+ };
+ request.onupgradeneeded = () => {
+ const store = request.result.createObjectStore("store", { keyPath: ["key"] });
+ const index = store.createIndex("index", ["value"]);
+ check("store", store, other.IDBObjectStore.prototype);
+ check("index", index, other.IDBIndex.prototype);
+ };
+ const finish = () => {
+ frame.remove();
+ done();
+ };
+ request.onsuccess = () => {
+ request.result.close();
+ const deletion = indexedDB.deleteDatabase(name);
+ deletion.onsuccess = finish;
+ deletion.onerror = () => {
+ println(`delete error: ${deletion.error.name}`);
+ finish();
+ };
+ };
+ request.onerror = () => {
+ println(`open error: ${request.error.name}`);
+ finish();
+ };
+ });
+</script>