Truly independent web browser
LibWeb: Validate srcset descriptor numbers before using them
Andreas Kling80033e05dc6b
diff --git a/Libraries/LibWeb/HTML/SourceSet.cpp b/Libraries/LibWeb/HTML/SourceSet.cppindex 61719f30697..29c8a65f014 100644--- a/Libraries/LibWeb/HTML/SourceSet.cpp+++ b/Libraries/LibWeb/HTML/SourceSet.cpp@@ -4,11 +4,13 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/AllOf.h>#include <AK/Function.h>#include <AK/HashMap.h>#include <AK/QuickSort.h>#include <LibWeb/CSS/Parser/Parser.h>#include <LibWeb/DOM/Document.h>+#include <LibWeb/HTML/Numbers.h>#include <LibWeb/HTML/SourceSet.h>#include <LibWeb/Layout/Node.h>#include <LibWebCommon/Infra/CharacterTypes.h>@@ -273,8 +275,10 @@ descriptor_parser:auto last_character = descriptor_view.code_unit_at(descriptor_view.length_in_code_units() - 1);auto descriptor_without_last_character = descriptor_view.substring_view(0, descriptor_view.length_in_code_units() - 1);- auto as_int = descriptor_without_last_character.to_number<i32>();- auto as_float = descriptor_without_last_character.to_number<float>();+ auto is_valid_non_negative_integer = !descriptor_without_last_character.is_empty()+ && all_of(descriptor_without_last_character, [](auto code_point) { return is_ascii_digit(code_point); });+ auto as_int = is_valid_non_negative_integer ? descriptor_without_last_character.to_number<i32>(TrimWhitespace::No) : Optional<i32> {};+ auto as_float = is_valid_floating_point_number(descriptor_without_last_character) ? descriptor_without_last_character.to_number<float>(TrimWhitespace::No) : Optional<float> {};// - If the descriptor consists of a valid non-negative integer followed by a U+0077 LATIN SMALL LETTER W characterif (last_character == 'w' && as_int.has_value()) {@@ -286,9 +290,12 @@ descriptor_parser:error = true;}- // FIXME: 3. Apply the rules for parsing non-negative integers to the descriptor.- // If the result is zero, let error be yes. Otherwise, let width be the result.- width = as_int.value();+ // 3. Apply the rules for parsing non-negative integers to the descriptor. If the result is zero, let error be yes.+ // Otherwise, let width be the result.+ if (as_int.value() == 0)+ error = true;+ else+ width = as_int.value();}// - If the descriptor consists of a valid floating-point number followed by a U+0078 LATIN SMALL LETTER X character@@ -298,9 +305,12 @@ descriptor_parser:error = true;}- // FIXME: 2. Apply the rules for parsing floating-point number values to the descriptor.- // If the result is less than zero, let error be yes. Otherwise, let density be the result.- density = as_float.value();+ // 2. Apply the rules for parsing floating-point number values to the descriptor. If the result is less than zero,+ // let error be yes. Otherwise, let density be the result.+ if (as_float.value() < 0)+ error = true;+ else+ density = as_float.value();}// - If the descriptor consists of a valid non-negative integer followed by a U+0068 LATIN SMALL LETTER H characterelse if (last_character == 'h' && as_int.has_value()) {@@ -309,9 +319,12 @@ descriptor_parser:if (future_compat_h.has_value() || density.has_value()) {error = true;}- // FIXME: 2. Apply the rules for parsing non-negative integers to the descriptor.- // If the result is zero, let error be yes. Otherwise, let future-compat-h be the result.- future_compat_h = as_int.value();+ // 2. Apply the rules for parsing non-negative integers to the descriptor. If the result is zero, let error be yes.+ // Otherwise, let future-compat-h be the result.+ if (as_int.value() == 0)+ error = true;+ else+ future_compat_h = as_int.value();}// - Anything elseelse {diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/srcset/parse-a-srcset-attribute.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/srcset/parse-a-srcset-attribute.txtindex 6b6a860509a..9548f1d27ac 100644--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/srcset/parse-a-srcset-attribute.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/embedded-content/the-img-element/srcset/parse-a-srcset-attribute.txt@@ -2,8 +2,7 @@ Harness status: OKFound 236 tests-227 Pass-9 Fail+236 PassPass ""Pass ","Pass ",,,"@@ -65,8 +64,8 @@ Pass "data:,a 1w"Pass "data:,a 1h"Pass "data:,a 1h foo"Pass "data:,a foo 1h"-Fail "data:,a 0w"-Fail "data:,a -1w"+Pass "data:,a 0w"+Pass "data:,a -1w"Pass "data:,a 1w -1w"Pass "data:,a 1.0w"Pass "data:,a 1w 1.0w"@@ -74,7 +73,7 @@ Pass "data:,a 1e0w"Pass "data:,a 1w 1e0w"Pass "data:,a 1www"Pass "data:,a 1w 1www"-Fail "data:,a +1w"+Pass "data:,a +1w"Pass "data:,a 1w +1w"Pass "data:,a 1W"Pass "data:,a 1w 1W"@@ -127,7 +126,7 @@ Pass "data:,a 1w" (leading U+FEFF)Pass "data:,a 0x"Pass "data:,a -0x"Pass "data:,a 1x -0x"-Fail "data:,a -1x"+Pass "data:,a -1x"Pass "data:,a 1x -1x"Pass "data:,a 1e0x"Pass "data:,a 1E0x"@@ -136,14 +135,14 @@ Pass "data:,a 1.5e1x"Pass "data:,a -x"Pass "data:,a .x"Pass "data:,a -.x"-Fail "data:,a 1.x"+Pass "data:,a 1.x"Pass "data:,a .5x"Pass "data:,a .5e1x"Pass "data:,a 1x 1.5e1x"Pass "data:,a 1x 1e1.5x"Pass "data:,a 1.0x"Pass "data:,a 1x 1.0x"-Fail "data:,a +1x"+Pass "data:,a +1x"Pass "data:,a 1X"Pass "data:,a Infinityx"Pass "data:,a NaNx"@@ -189,12 +188,12 @@ Pass "data:,a 1x" (leading U+202F)Pass "data:,a 1x" (leading U+205F)Pass "data:,a 1x" (leading U+3000)Pass "data:,a 1x" (leading U+FEFF)-Fail "data:,a 1w 0h"-Fail "data:,a 1w -1h"+Pass "data:,a 1w 0h"+Pass "data:,a 1w -1h"Pass "data:,a 1w 1.0h"Pass "data:,a 1w 1e0h"Pass "data:,a 1w 1hhh"-Fail "data:,a 1w +1h"+Pass "data:,a 1w +1h"Pass "data:,a 1w 1H"Pass "data:,a 1w Infinityh"Pass "data:,a 1w NaNh"