Truly independent web browser
LibWeb: Fix two bugs in the parsing of window.open() features
Andreas Kling306eaf205f6f
diff --git a/Libraries/LibWeb/HTML/TokenizedFeatures.cpp b/Libraries/LibWeb/HTML/TokenizedFeatures.cppindex cdac039111a..c9a12f68adb 100644--- a/Libraries/LibWeb/HTML/TokenizedFeatures.cpp+++ b/Libraries/LibWeb/HTML/TokenizedFeatures.cpp@@ -5,6 +5,7 @@*/#include <AK/GenericLexer.h>+#include <LibWeb/HTML/Numbers.h>#include <LibWeb/HTML/TokenizedFeatures.h>#include <LibWebCommon/Infra/CharacterTypes.h>@@ -79,13 +80,15 @@ TokenizedFeature::Map tokenize_open_features(Utf16View features)lexer.ignore_while(Infra::is_ascii_whitespace);// 7. If the code point at position in features is a feature separator:- // 1. While position is not past the end of features and the code point at position in features is a feature separator:- // 1. If the code point at position in features is U+002C (,), then break.- // 2. Advance position by 1.- lexer.ignore_while([](auto character) { return Infra::is_ascii_whitespace(character) || character == '='; });-- // 2. Collect a sequence of code points that are not feature separators code points from features given position. Set value to the collected code points, converted to ASCII lowercase.- value = lexer.consume_until(is_feature_separator).to_ascii_lowercase();+ if (!lexer.is_eof() && is_feature_separator(lexer.peek())) {+ // 1. While position is not past the end of features and the code point at position in features is a feature separator:+ // 1. If the code point at position in features is U+002C (,), then break.+ // 2. Advance position by 1.+ lexer.ignore_while([](auto character) { return Infra::is_ascii_whitespace(character) || character == '='; });++ // 2. Collect a sequence of code points that are not feature separators code points from features given position. Set value to the collected code points, converted to ASCII lowercase.+ value = lexer.consume_until(is_feature_separator).to_ascii_lowercase();+ }// 8. If name is not the empty string, then set tokenizedFeatures[name] to value.if (!normalized_name.is_empty()) {@@ -117,14 +120,19 @@ T parse_boolean_feature(Utf16View value)return T::Yes;// 4. Let parsed be the result of parsing value as an integer.- auto parsed = value.to_number<i64>();+ // NOTE: The integer can be arbitrarily large, so we only look at its digits, which are nonzero if any of them is.+ auto parsed = parse_integer_digits(value);// 5. If parsed is an error, then set it to 0.- if (!parsed.has_value())- parsed = 0;-// 6. Return false if parsed is 0, and true otherwise.- return parsed == 0 ? T::No : T::Yes;+ if (!parsed.has_value())+ return T::No;+ for (size_t i = 0; i < parsed->length_in_code_units(); ++i) {+ auto code_unit = parsed->code_unit_at(i);+ if (code_unit != '0' && code_unit != '-' && code_unit != '+')+ return T::Yes;+ }+ return T::No;}template TokenizedFeature::Location parse_boolean_feature<TokenizedFeature::Location>(Utf16View value);diff --git a/Tests/LibWeb/Text/expected/HTML/window-open-boolean-feature-large-integers.txt b/Tests/LibWeb/Text/expected/HTML/window-open-boolean-feature-large-integers.txtnew file mode 100644index 00000000000..501f82d8284--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/window-open-boolean-feature-large-integers.txt@@ -0,0 +1,4 @@+noopener=2147483648: null+noopener=-99999999999999999999: null+noreferrer=2147483648: null+noopener=00000000000000000000: windowdiff --git a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/the-window-object/open-close/open-features-tokenization-noopener.txt b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/the-window-object/open-close/open-features-tokenization-noopener.txtindex 16c4e09f6b4..239fab83846 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/the-window-object/open-close/open-features-tokenization-noopener.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/the-window-object/open-close/open-features-tokenization-noopener.txt@@ -2,13 +2,12 @@ Harness status: OKFound 8 tests-6 Pass-2 Fail+8 PassPass Tokenization of "noopener" should skip window features separators before featurePass Feature "noopener" should be converted to ASCII lowercase-Fail After "noopener", tokenization should skip window features separators that are not "=" or ","+Pass After "noopener", tokenization should skip window features separators that are not "=" or ","Pass Tokenizing "noopener" should ignore window feature separators except "," after initial "=" and before valuePass Tokenizing "noopener" should read characters until first window feature separator as `value`-Fail Integer values other than 0 should activate the feature+Pass Integer values other than 0 should activate the featurePass Integer value of 0 should not activate "noopener"Pass Invalid feature names should not tokenize as "noopener"\ No newline at end of filediff --git a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/the-window-object/open-close/open-features-tokenization-noreferrer.txt b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/the-window-object/open-close/open-features-tokenization-noreferrer.txtindex da44a2c5bfc..d776fdd520b 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/browsers/the-window-object/open-close/open-features-tokenization-noreferrer.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/browsers/the-window-object/open-close/open-features-tokenization-noreferrer.txt@@ -2,13 +2,12 @@ Harness status: OKFound 8 tests-6 Pass-2 Fail+8 PassPass Tokenization of "noreferrer" should skip window features separators before featurePass Feature "noreferrer" should be converted to ASCII lowercase-Fail After "noreferrer", tokenization should skip window features separators that are not "=" or ","+Pass After "noreferrer", tokenization should skip window features separators that are not "=" or ","Pass Tokenizing "noreferrer" should ignore window feature separators except "," after initial "=" and before valuePass Tokenizing "noreferrer" should read characters until first window feature separator as `value`-Fail Integer values other than 0 should activate the feature+Pass Integer values other than 0 should activate the featurePass Integer value of 0 should not activate "noreferrer"Pass Invalid feature names should not tokenize as "noreferrer"\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/HTML/window-open-boolean-feature-large-integers.html b/Tests/LibWeb/Text/input/HTML/window-open-boolean-feature-large-integers.htmlnew file mode 100644index 00000000000..ad117ee315b--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/window-open-boolean-feature-large-integers.html@@ -0,0 +1,16 @@+<!doctype html>+<script src="../include.js"></script>+<script>+ test(() => {+ for (const features of [+ "noopener=2147483648",+ "noopener=-99999999999999999999",+ "noreferrer=2147483648",+ "noopener=00000000000000000000",+ ]) {+ const result = window.open("about:blank", "_blank", features);+ println(`${features}: ${result === null ? "null" : "window"}`);+ if (result) result.close();+ }+ });+</script>