Skip to content

Truly independent web browser

LibWeb: Reject a var() with a malformed name at parse time

sideshowbarker9468e082612a
diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
index 3c2f509d1a3..7afc322d64d 100644
--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
@@ -4168,15 +4168,6 @@ mod tests {
panic!("expected environment fallback");
};
assert_eq!(serialize_tokens(&tokens), utf16(" 4px"));
-
- assert!(matches!(
- substitute_without_custom_properties("env(\"unknown-environment-variable\", 4px)"),
- TokenResolution::Invalid
- ));
- assert!(matches!(
- substitute_without_custom_properties("env(safe-area-inset-top 1.5, 4px)"),
- TokenResolution::Invalid
- ));
}
#[test]
diff --git a/Libraries/LibWeb/Rust/src/css/parser/arbitrary_substitution.rs b/Libraries/LibWeb/Rust/src/css/parser/arbitrary_substitution.rs
index e00104c182a..74695c4df9c 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/arbitrary_substitution.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/arbitrary_substitution.rs
@@ -192,6 +192,48 @@ fn parse_dashed_function_argument(values: &[ComponentValue], position: &mut usiz
// This mirrors parse_according_to_argument_grammar() in
// CSS/Parser/ArbitrarySubstitutionFunctions.cpp.
+/// The argument of a `var()` or `env()` before its first comma, without the whitespace around it.
+fn first_argument(values: &[ComponentValue]) -> &[ComponentValue] {
+ let comma = values.iter().position(ComponentValue::is_comma).unwrap_or(values.len());
+ crate::css::parser::component_value::trim_whitespace(&values[..comma])
+}
+
+// https://drafts.csswg.org/css-variables-1/#using-variables
+// var() = var( <custom-property-name> , <declaration-value>? )
+// AD-HOC: The argument grammar of var() takes any <declaration-value> as the name, which only the replacement algorithm
+// checks, at computed-value time. A name that isn't a <custom-property-name> makes the declaration invalid at
+// parse time instead, so that a var() with a typo in it doesn't override an earlier declaration.
+// FIXME: Remove this check, and the one for env() below, once checking the name only at substitution time, as the
+// argument grammar does, is web-compatible.
+fn names_a_custom_property(values: &[ComponentValue]) -> bool {
+ let name = first_argument(values);
+ name.len() == 1
+ && name[0]
+ .ident()
+ .is_some_and(|ident| ident.len() > 2 && ident[0] == b'-' as u16 && ident[1] == b'-' as u16)
+}
+
+// https://drafts.csswg.org/css-env-1/#env-function
+// env() = env( <custom-ident> <integer [0,inf]>*, <declaration-value>? )
+// AD-HOC: As for var(), a name that doesn't match this grammar makes the declaration invalid at parse time.
+fn names_an_environment_variable(values: &[ComponentValue]) -> bool {
+ let mut parts = first_argument(values).iter().filter(|value| !value.is_whitespace());
+ let Some(name) = parts.next().and_then(ComponentValue::ident) else {
+ return false;
+ };
+ crate::css::parser::value_parser::is_valid_custom_ident(name, &[])
+ && parts.all(|value| {
+ matches!(
+ value.kind,
+ ComponentKind::Token(ParserTokenKind::Number {
+ value,
+ number_type: crate::css::css_tokenizer::CssNumberType::Integer
+ | crate::css::css_tokenizer::CssNumberType::IntegerWithExplicitSign,
+ }) if value >= 0.0
+ )
+ })
+}
+
pub(crate) fn arguments_are_valid(function: ArbitrarySubstitutionFunction, values: &[ComponentValue]) -> bool {
let mut position = 0;
match function {
@@ -200,11 +242,19 @@ pub(crate) fn arguments_are_valid(function: ArbitrarySubstitutionFunction, value
&& position == values.len()
&& crate::css::custom_properties::literal_attr_arguments_are_valid(values)
}
- ArbitrarySubstitutionFunction::Env
- | ArbitrarySubstitutionFunction::Inherit
- | ArbitrarySubstitutionFunction::Var => {
+ ArbitrarySubstitutionFunction::Env => {
+ parse_declaration_value_then_optional_declaration_value(values, &mut position, EndToken::Comma)
+ && position == values.len()
+ && names_an_environment_variable(values)
+ }
+ ArbitrarySubstitutionFunction::Inherit => {
+ parse_declaration_value_then_optional_declaration_value(values, &mut position, EndToken::Comma)
+ && position == values.len()
+ }
+ ArbitrarySubstitutionFunction::Var => {
parse_declaration_value_then_optional_declaration_value(values, &mut position, EndToken::Comma)
&& position == values.len()
+ && names_a_custom_property(values)
}
ArbitrarySubstitutionFunction::DashedFunction => {
discard_whitespace(values, &mut position);
@@ -471,6 +521,8 @@ mod tests {
for source in [
"var(--name)",
"var(--name,)",
+ "var( --name ,fallback)",
+ "env(name 0 1, fallback)",
"attr(data-name type(<string>), fallback)",
"env(safe-area-inset-top, 0px)",
"inherit(color, red)",
@@ -483,6 +535,15 @@ mod tests {
for source in [
"var()",
"var(,--fallback)",
+ "var(--name 0px)",
+ "var(name)",
+ "var(--)",
+ "var(var(--name))",
+ "env(name 0px)",
+ "env(name -1)",
+ "env(name 1.5, fallback)",
+ "env(\"name\", fallback)",
+ "env(1px)",
"attr(data-name ! invalid)",
"env(name, fallback ! invalid)",
"inherit(value; fallback)",
diff --git a/Tests/LibWeb/Text/expected/css/var-with-a-malformed-name-is-invalid-at-parse-time.txt b/Tests/LibWeb/Text/expected/css/var-with-a-malformed-name-is-invalid-at-parse-time.txt
new file mode 100644
index 00000000000..355e6bf922d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/var-with-a-malformed-name-is-invalid-at-parse-time.txt
@@ -0,0 +1,19 @@
+#a: 200px
+#b: 200px
+#c: 100px
+var(--prop 20px) -> "200px"; supports false
+var(prop) -> "200px"; supports false
+var(1px) -> "200px"; supports false
+var(var(--n)) -> "200px"; supports false
+var(--a --b) -> "200px"; supports false
+var(-x) -> "200px"; supports false
+var(--) -> "200px"; supports false
+calc(var(--w) + var(--gap 0px)) -> "200px"; supports false
+var(--w, 0px) var(--x 1px) -> "200px"; supports false
+env(safe-area-inset-top 0px) -> "200px"; supports false
+env(1px) -> "200px"; supports false
+var(--w, 0px) -> "var(--w, 0px)"; supports true
+var(--w,) -> "var(--w,)"; supports true
+var( --w ) -> "var( --w )"; supports true
+var(--w ,0px) -> "var(--w ,0px)"; supports true
+env(safe-area-inset-top, 0px) -> "env(safe-area-inset-top, 0px)"; supports true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-env/env-parsing.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-env/env-parsing.txt
index 6c68490d83f..87d21e1826d 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-env/env-parsing.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-env/env-parsing.txt
@@ -2,13 +2,12 @@ Harness status: OK
Found 8 tests
-3 Pass
-5 Fail
+8 Pass
Pass e.style['width'] = "env(safe-area-inset-top)" should set the property value
Pass e.style['width'] = "env(safe-area-inset-top,)" should set the property value
Pass e.style['width'] = "env(safe-area-inset-top, )" should set the property value
-Fail e.style['width'] = "env(safe-area-inset-top ())" should not set the property value
-Fail e.style['width'] = "env(safe-area-inset-top () )" should not set the property value
-Fail e.style['width'] = "env(safe-area-inset-top() )" should not set the property value
-Fail e.style['width'] = "env(safe-area-inset-top (),)" should not set the property value
-Fail e.style['width'] = "env(safe-area-inset-top(),)" should not set the property value
\ No newline at end of file
+Pass e.style['width'] = "env(safe-area-inset-top ())" should not set the property value
+Pass e.style['width'] = "env(safe-area-inset-top () )" should not set the property value
+Pass e.style['width'] = "env(safe-area-inset-top() )" should not set the property value
+Pass e.style['width'] = "env(safe-area-inset-top (),)" should not set the property value
+Pass e.style['width'] = "env(safe-area-inset-top(),)" should not set the property value
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-env/indexed-env.tentative.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-env/indexed-env.tentative.txt
index 069fa2bfda6..4b3c2a518c2 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-env/indexed-env.tentative.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-env/indexed-env.tentative.txt
@@ -2,12 +2,11 @@ Harness status: OK
Found 8 tests
-4 Pass
-4 Fail
-Fail CSS Environment variable value "env(test1 test2, green)" must not successfully parse
-Fail CSS Environment variable value "env(test1 10 20 test2, green)" must not successfully parse
-Fail CSS Environment variable value "env(test 0.1, green)" must not successfully parse
-Fail CSS Environment variable value "env(test -1, green)" must not successfully parse
+8 Pass
+Pass CSS Environment variable value "env(test1 test2, green)" must not successfully parse
+Pass CSS Environment variable value "env(test1 10 20 test2, green)" must not successfully parse
+Pass CSS Environment variable value "env(test 0.1, green)" must not successfully parse
+Pass CSS Environment variable value "env(test -1, green)" must not successfully parse
Pass CSS Environment variable value "env(test 0, green)" must successfully parse and roundtrip
Pass CSS Environment variable value "env(test 0,)" must successfully parse and roundtrip
Pass CSS Environment variable value "env(test 0)" must successfully parse and roundtrip
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-env/syntax.tentative.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-env/syntax.tentative.txt
index 7d80ac15ed0..85e6f813297 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-env/syntax.tentative.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-env/syntax.tentative.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 18 tests
-16 Pass
-2 Fail
+18 Pass
Pass rgb(0, 128, 0)
Pass background-color: env(test) rgba(0, 0, 0, 0)
Pass background-color: ENV(test) rgba(0, 0, 0, 0)
@@ -14,8 +13,8 @@ Pass background-color: env(test, env(another)) rgba(0, 0, 0, 0)
Pass background-color: env(test, env(another, blue)) rgb(0, 0, 255)
Pass background-color: env(-test) rgba(0, 0, 0, 0)
Pass background-color: env(--test) rgba(0, 0, 0, 0)
-Fail background-color: env(10px) rgb(0, 128, 0)
-Fail background-color: env(env(test)) rgb(0, 128, 0)
+Pass background-color: env(10px) rgb(0, 128, 0)
+Pass background-color: env(env(test)) rgb(0, 128, 0)
Pass background-color: env( test) rgba(0, 0, 0, 0)
Pass background-color: env(test ) rgba(0, 0, 0, 0)
Pass background-color: env( test ) rgba(0, 0, 0, 0)
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-variables/var-parsing.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-variables/var-parsing.txt
index 6fd105204c9..0c5fc0d854b 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-variables/var-parsing.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-variables/var-parsing.txt
@@ -2,13 +2,12 @@ Harness status: OK
Found 8 tests
-3 Pass
-5 Fail
+8 Pass
Pass e.style['width'] = "var(--x)" should set the property value
Pass e.style['width'] = "var(--x,)" should set the property value
Pass e.style['width'] = "var(--x, )" should set the property value
-Fail e.style['width'] = "var(--x ())" should not set the property value
-Fail e.style['width'] = "var(--x () )" should not set the property value
-Fail e.style['width'] = "var(--x() )" should not set the property value
-Fail e.style['width'] = "var(--x (),)" should not set the property value
-Fail e.style['width'] = "var(--x(),)" should not set the property value
\ No newline at end of file
+Pass e.style['width'] = "var(--x ())" should not set the property value
+Pass e.style['width'] = "var(--x () )" should not set the property value
+Pass e.style['width'] = "var(--x() )" should not set the property value
+Pass e.style['width'] = "var(--x (),)" should not set the property value
+Pass e.style['width'] = "var(--x(),)" should not set the property value
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/css/var-with-a-malformed-name-is-invalid-at-parse-time.html b/Tests/LibWeb/Text/input/css/var-with-a-malformed-name-is-invalid-at-parse-time.html
new file mode 100644
index 00000000000..61c75ead344
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/var-with-a-malformed-name-is-invalid-at-parse-time.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body { --w: 100px; }
+ .t { max-width: 200px; }
+ #a { max-width: var(--w 0px); }
+ #b { max-width: calc(var(--w) + var(--gap 0px)); }
+ #c { max-width: var(--w, 0px); }
+</style>
+<div class="t" id="a"></div>
+<div class="t" id="b"></div>
+<div class="t" id="c"></div>
+<div id="target"></div>
+<script>
+ // A var() whose name isn't a custom property name, or an env() whose name isn't an identifier, makes the
+ // declaration invalid at parse time, so an earlier declaration of the property still applies.
+ test(() => {
+ for (const id of ["a", "b", "c"])
+ println(`#${id}: ${getComputedStyle(document.getElementById(id)).maxWidth}`);
+ const target = document.getElementById("target");
+ const values = [
+ "var(--prop 20px)", "var(prop)", "var(1px)", "var(var(--n))", "var(--a --b)", "var(-x)", "var(--)",
+ "calc(var(--w) + var(--gap 0px))", "var(--w, 0px) var(--x 1px)", "env(safe-area-inset-top 0px)", "env(1px)",
+ "var(--w, 0px)", "var(--w,)", "var( --w )", "var(--w ,0px)", "env(safe-area-inset-top, 0px)",
+ ];
+ for (const value of values) {
+ target.style.maxWidth = "200px";
+ target.style.maxWidth = value;
+ println(`${value} -> ${JSON.stringify(target.style.maxWidth)}; supports ${CSS.supports("max-width", value)}`);
+ }
+ });
+</script>