Skip to content

Truly independent web browser

LibWeb: Reject IndexedDB key paths that are not identifiers

Andreas Kling506eaca8f411
diff --git a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp
index 89e470b7e28..cd54ac9f6c4 100644
--- a/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp
+++ b/Libraries/LibWeb/IndexedDB/Internal/Algorithms.cpp
@@ -20,6 +20,7 @@
#include <LibJS/Runtime/PrimitiveString.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibJS/Runtime/VM.h>
+#include <LibUnicode/CharacterTypes.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Bindings/WrapperWorld.h>
#include <LibWeb/DOM/EventDispatcher.h>
@@ -840,6 +841,36 @@ JS::Value convert_a_key_to_a_value(JS::Realm& realm, GC::Ref<Key> key)
VERIFY_NOT_REACHED();
}
+// https://tc39.es/ecma262/#prod-IdentifierName
+static bool is_identifier_name(Utf16View const& string)
+{
+ // FIXME: Accept the UnicodeEscapeSequences that IdentifierStart and IdentifierPart allow.
+ if (string.is_empty())
+ return false;
+
+ bool is_first_code_point = true;
+ for (auto code_point : string) {
+ // IdentifierStartChar ::
+ // UnicodeIDStart
+ // $
+ // _
+ // IdentifierPartChar ::
+ // UnicodeIDContinue
+ // $
+ // <ZWNJ>
+ // <ZWJ>
+ bool is_valid = code_point == '$' || code_point == '_';
+ if (is_first_code_point)
+ is_valid = is_valid || Unicode::code_point_has_identifier_start_property(code_point);
+ else
+ is_valid = is_valid || code_point == 0x200C || code_point == 0x200D || Unicode::code_point_has_identifier_continue_property(code_point);
+ if (!is_valid)
+ return false;
+ is_first_code_point = false;
+ }
+ return true;
+}
+
// https://w3c.github.io/IndexedDB/#valid-key-path
bool is_valid_key_path(KeyPath const& path)
{
@@ -850,13 +881,19 @@ bool is_valid_key_path(KeyPath const& path)
if (value.is_empty())
return true;
- // FIXME: * An identifier, which is a string matching the IdentifierName production from the ECMAScript Language Specification [ECMA-262].
- return true;
-
- // FIXME: * A string consisting of two or more identifiers separated by periods (U+002E FULL STOP).
+ // * An identifier, which is a string matching the IdentifierName production from the ECMAScript Language
+ // Specification [ECMA-262].
+ // * A string consisting of two or more identifiers separated by periods (U+002E FULL STOP).
+ auto view = value.utf16_view();
+ size_t identifier_start = 0;
+ for (size_t index = 0; index <= view.length_in_code_units(); ++index) {
+ if (index < view.length_in_code_units() && view.code_unit_at(index) != '.')
+ continue;
+ if (!is_identifier_name(view.substring_view(identifier_start, index - identifier_start)))
+ return false;
+ identifier_start = index + 1;
+ }
return true;
-
- return false;
},
[](Vector<Utf16String> const& values) -> bool {
// * A non-empty list containing only strings conforming to the above requirements.
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.txt
index 75092641ba2..dc5296f4263 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 21 tests
-15 Pass
-6 Fail
+17 Pass
+4 Fail
Pass Returns an IDBIndex and the properties are set correctly
Fail Attempt to create an index that requires unique values on an object store already contains duplicates
Pass The index is usable right after being made
@@ -14,14 +14,14 @@ Fail Event ordering for ConstraintError on request
Pass Index can be valid keys
Pass IDBObjectStore.createIndex() - empty name
Pass If an index with the name name already exists in this object store, the implementation must throw a DOMException of type ConstraintError
-Fail If keyPath is not a valid key path, the implementation must throw a DOMException of type SyntaxError
+Pass If keyPath is not a valid key path, the implementation must throw a DOMException of type SyntaxError
Pass If the object store has been deleted, the implementation must throw a DOMException of type InvalidStateError
Pass Operate out versionchange throw InvalidStateError
Pass InvalidStateError(Incorrect mode) vs. TransactionInactiveError. Mode check should precede state check of the transaction.
Pass InvalidStateError(Deleted ObjectStore) vs. TransactionInactiveError. Deletion check should precede transaction-state check.
Pass TransactionInactiveError vs. ConstraintError. Transaction-state check should precede index name check.
Pass ConstraintError vs. SyntaxError. Index name check should precede syntax check of the key path
-Fail SyntaxError vs. InvalidAccessError. Syntax check should precede multiEntry check of the key path.
+Pass SyntaxError vs. InvalidAccessError. Syntax check should precede multiEntry check of the key path.
Pass Explicit Primary Key
Pass Auto-Increment Primary Key
Pass Auto-Increment Primary Key - invalid key values elsewhere
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.worker.txt
index 119edee212f..4003f34f79b 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.worker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/idbobjectstore_createIndex.any.worker.txt
@@ -2,8 +2,8 @@ Harness status: Error
Found 21 tests
-15 Pass
-6 Fail
+17 Pass
+4 Fail
Pass Returns an IDBIndex and the properties are set correctly
Fail Attempt to create an index that requires unique values on an object store already contains duplicates
Pass The index is usable right after being made
@@ -14,14 +14,14 @@ Fail Event ordering for ConstraintError on request
Pass Index can be valid keys
Pass IDBObjectStore.createIndex() - empty name
Pass If an index with the name name already exists in this object store, the implementation must throw a DOMException of type ConstraintError
-Fail If keyPath is not a valid key path, the implementation must throw a DOMException of type SyntaxError
+Pass If keyPath is not a valid key path, the implementation must throw a DOMException of type SyntaxError
Pass If the object store has been deleted, the implementation must throw a DOMException of type InvalidStateError
Pass Operate out versionchange throw InvalidStateError
Pass InvalidStateError(Incorrect mode) vs. TransactionInactiveError. Mode check should precede state check of the transaction.
Pass InvalidStateError(Deleted ObjectStore) vs. TransactionInactiveError. Deletion check should precede transaction-state check.
Pass TransactionInactiveError vs. ConstraintError. Transaction-state check should precede index name check.
Pass ConstraintError vs. SyntaxError. Index name check should precede syntax check of the key path
-Fail SyntaxError vs. InvalidAccessError. Syntax check should precede multiEntry check of the key path.
+Pass SyntaxError vs. InvalidAccessError. Syntax check should precede multiEntry check of the key path.
Pass Explicit Primary Key
Pass Auto-Increment Primary Key
Pass Auto-Increment Primary Key - invalid key values elsewhere
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.sharedworker.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.sharedworker.txt
index 66390d14d4a..231149b2e0e 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.sharedworker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.sharedworker.txt
@@ -2,29 +2,28 @@ Harness status: OK
Found 24 tests
-1 Pass
-23 Fail
-Fail Invalid keyPath - "j a"
-Fail Invalid keyPath - ".yo"
-Fail Invalid keyPath - "yo,lo"
+24 Pass
+Pass Invalid keyPath - "j a"
+Pass Invalid keyPath - ".yo"
+Pass Invalid keyPath - "yo,lo"
Pass Invalid keyPath - []
-Fail Invalid keyPath - ["array with space"]
-Fail Invalid keyPath - multidimensional array (invalid toString)
-Fail Invalid keyPath - "3m"
-Fail Invalid keyPath - {toString->3m}
-Fail Invalid keyPath - "my.1337"
-Fail Invalid keyPath - "..yo"
-Fail Invalid keyPath - "y..o"
-Fail Invalid keyPath - "y.o."
-Fail Invalid keyPath - "y.o.."
-Fail Invalid keyPath - "m.*"
-Fail Invalid keyPath - "\"m\""
-Fail Invalid keyPath - "m%"
-Fail Invalid keyPath - "m/"
-Fail Invalid keyPath - "m/a"
-Fail Invalid keyPath - "m&"
-Fail Invalid keyPath - "m!"
-Fail Invalid keyPath - "*"
-Fail Invalid keyPath - "*.*"
-Fail Invalid keyPath - "^m"
-Fail Invalid keyPath - "/m/"
\ No newline at end of file
+Pass Invalid keyPath - ["array with space"]
+Pass Invalid keyPath - multidimensional array (invalid toString)
+Pass Invalid keyPath - "3m"
+Pass Invalid keyPath - {toString->3m}
+Pass Invalid keyPath - "my.1337"
+Pass Invalid keyPath - "..yo"
+Pass Invalid keyPath - "y..o"
+Pass Invalid keyPath - "y.o."
+Pass Invalid keyPath - "y.o.."
+Pass Invalid keyPath - "m.*"
+Pass Invalid keyPath - "\"m\""
+Pass Invalid keyPath - "m%"
+Pass Invalid keyPath - "m/"
+Pass Invalid keyPath - "m/a"
+Pass Invalid keyPath - "m&"
+Pass Invalid keyPath - "m!"
+Pass Invalid keyPath - "*"
+Pass Invalid keyPath - "*.*"
+Pass Invalid keyPath - "^m"
+Pass Invalid keyPath - "/m/"
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.txt
index 66390d14d4a..231149b2e0e 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.txt
@@ -2,29 +2,28 @@ Harness status: OK
Found 24 tests
-1 Pass
-23 Fail
-Fail Invalid keyPath - "j a"
-Fail Invalid keyPath - ".yo"
-Fail Invalid keyPath - "yo,lo"
+24 Pass
+Pass Invalid keyPath - "j a"
+Pass Invalid keyPath - ".yo"
+Pass Invalid keyPath - "yo,lo"
Pass Invalid keyPath - []
-Fail Invalid keyPath - ["array with space"]
-Fail Invalid keyPath - multidimensional array (invalid toString)
-Fail Invalid keyPath - "3m"
-Fail Invalid keyPath - {toString->3m}
-Fail Invalid keyPath - "my.1337"
-Fail Invalid keyPath - "..yo"
-Fail Invalid keyPath - "y..o"
-Fail Invalid keyPath - "y.o."
-Fail Invalid keyPath - "y.o.."
-Fail Invalid keyPath - "m.*"
-Fail Invalid keyPath - "\"m\""
-Fail Invalid keyPath - "m%"
-Fail Invalid keyPath - "m/"
-Fail Invalid keyPath - "m/a"
-Fail Invalid keyPath - "m&"
-Fail Invalid keyPath - "m!"
-Fail Invalid keyPath - "*"
-Fail Invalid keyPath - "*.*"
-Fail Invalid keyPath - "^m"
-Fail Invalid keyPath - "/m/"
\ No newline at end of file
+Pass Invalid keyPath - ["array with space"]
+Pass Invalid keyPath - multidimensional array (invalid toString)
+Pass Invalid keyPath - "3m"
+Pass Invalid keyPath - {toString->3m}
+Pass Invalid keyPath - "my.1337"
+Pass Invalid keyPath - "..yo"
+Pass Invalid keyPath - "y..o"
+Pass Invalid keyPath - "y.o."
+Pass Invalid keyPath - "y.o.."
+Pass Invalid keyPath - "m.*"
+Pass Invalid keyPath - "\"m\""
+Pass Invalid keyPath - "m%"
+Pass Invalid keyPath - "m/"
+Pass Invalid keyPath - "m/a"
+Pass Invalid keyPath - "m&"
+Pass Invalid keyPath - "m!"
+Pass Invalid keyPath - "*"
+Pass Invalid keyPath - "*.*"
+Pass Invalid keyPath - "^m"
+Pass Invalid keyPath - "/m/"
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.worker.txt
index 66390d14d4a..231149b2e0e 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.worker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/IndexedDB/keypath_invalid.any.worker.txt
@@ -2,29 +2,28 @@ Harness status: OK
Found 24 tests
-1 Pass
-23 Fail
-Fail Invalid keyPath - "j a"
-Fail Invalid keyPath - ".yo"
-Fail Invalid keyPath - "yo,lo"
+24 Pass
+Pass Invalid keyPath - "j a"
+Pass Invalid keyPath - ".yo"
+Pass Invalid keyPath - "yo,lo"
Pass Invalid keyPath - []
-Fail Invalid keyPath - ["array with space"]
-Fail Invalid keyPath - multidimensional array (invalid toString)
-Fail Invalid keyPath - "3m"
-Fail Invalid keyPath - {toString->3m}
-Fail Invalid keyPath - "my.1337"
-Fail Invalid keyPath - "..yo"
-Fail Invalid keyPath - "y..o"
-Fail Invalid keyPath - "y.o."
-Fail Invalid keyPath - "y.o.."
-Fail Invalid keyPath - "m.*"
-Fail Invalid keyPath - "\"m\""
-Fail Invalid keyPath - "m%"
-Fail Invalid keyPath - "m/"
-Fail Invalid keyPath - "m/a"
-Fail Invalid keyPath - "m&"
-Fail Invalid keyPath - "m!"
-Fail Invalid keyPath - "*"
-Fail Invalid keyPath - "*.*"
-Fail Invalid keyPath - "^m"
-Fail Invalid keyPath - "/m/"
\ No newline at end of file
+Pass Invalid keyPath - ["array with space"]
+Pass Invalid keyPath - multidimensional array (invalid toString)
+Pass Invalid keyPath - "3m"
+Pass Invalid keyPath - {toString->3m}
+Pass Invalid keyPath - "my.1337"
+Pass Invalid keyPath - "..yo"
+Pass Invalid keyPath - "y..o"
+Pass Invalid keyPath - "y.o."
+Pass Invalid keyPath - "y.o.."
+Pass Invalid keyPath - "m.*"
+Pass Invalid keyPath - "\"m\""
+Pass Invalid keyPath - "m%"
+Pass Invalid keyPath - "m/"
+Pass Invalid keyPath - "m/a"
+Pass Invalid keyPath - "m&"
+Pass Invalid keyPath - "m!"
+Pass Invalid keyPath - "*"
+Pass Invalid keyPath - "*.*"
+Pass Invalid keyPath - "^m"
+Pass Invalid keyPath - "/m/"
\ No newline at end of file