Skip to content

Truly independent web browser

LibWeb: Reject variable references in property initial values

Andreas Klinge888bc8b83d9
diff --git a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
index 2093a2fdd3f..e03e5abb3f3 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/syntax_parser.rs
@@ -24,6 +24,9 @@ use crate::css::keyframes::{FfiKeyframe, KeyframeData};
use crate::css::layer_names::LayerNames;
use crate::css::media_list::{MediaList, MediaListData};
use crate::css::namespace_rule::NamespaceRuleData;
+use crate::css::parser::arbitrary_substitution::{
+ ArbitrarySubstitutionFunction, arbitrary_substitution_function, arguments_are_valid, declaration_value_is_valid,
+};
use crate::css::parser::component_value::{
ComponentKind, ComponentSerializationMode, ComponentValue, ParserTokens, consume_a_component_value,
consume_a_list_of_component_values, trim_whitespace,
@@ -922,6 +925,33 @@ fn at_rule_declarations(rule: &AtRule) -> impl Iterator<Item = &Declaration> {
.flatten()
}
+fn initial_value_contains_variable_reference(values: &[ComponentValue]) -> Option<bool> {
+ let mut contains_reference = false;
+ for value in values {
+ let nested_values = match &value.kind {
+ ComponentKind::Function { name, values } => {
+ match arbitrary_substitution_function(name.as_ref()) {
+ Some(ArbitrarySubstitutionFunction::Var) => {
+ let (name, _) = crate::css::style_value::split_var_arguments(values)?;
+ if name.len() < 3 {
+ return None;
+ }
+ contains_reference = true;
+ }
+ // NB: Other substitutions retain their descriptor handling, but cannot hide a var() reference.
+ Some(function) if !arguments_are_valid(function, values) => return None,
+ Some(_) | None => {}
+ }
+ values.as_ref()
+ }
+ ComponentKind::SimpleBlock { values, .. } => values.as_ref(),
+ ComponentKind::Token(_) => continue,
+ };
+ contains_reference |= initial_value_contains_variable_reference(nested_values)?;
+ }
+ Some(contains_reference)
+}
+
fn parse_property_prelude(rule: &AtRule, context: &ParseContext) -> ParsedRulePrelude {
if !rule.has_block {
return ParsedRulePrelude::Invalid;
@@ -935,6 +965,7 @@ fn parse_property_prelude(rule: &AtRule, context: &ParseContext) -> ParsedRulePr
let mut syntax_source: Option<ParserString> = None;
let mut inherits = None;
let mut initial_value_source = None;
+ let mut initial_value_is_invalid = false;
for declaration in at_rule_declarations(rule) {
let source = component_list_source(&declaration.value);
if parse_descriptor(
@@ -946,6 +977,15 @@ fn parse_property_prelude(rule: &AtRule, context: &ParseContext) -> ParsedRulePr
)
.is_none()
{
+ // NB: A var() reference matches the descriptor's <declaration-value>? grammar, so it replaces an earlier
+ // initial value. It is not computationally independent, so it makes the rule invalid.
+ if equals_ascii_case_insensitive(declaration.name.as_ref(), b"initial-value")
+ && !declaration.important
+ && declaration_value_is_valid(&declaration.value)
+ && initial_value_contains_variable_reference(&declaration.value) == Some(true)
+ {
+ initial_value_is_invalid = true;
+ }
continue;
}
if equals_ascii_case_insensitive(declaration.name.as_ref(), b"syntax") {
@@ -973,6 +1013,7 @@ fn parse_property_prelude(rule: &AtRule, context: &ParseContext) -> ParsedRulePr
}
});
} else if equals_ascii_case_insensitive(declaration.name.as_ref(), b"initial-value") {
+ initial_value_is_invalid = false;
initial_value_source = Some(serialize_component_values_to_utf16(
&declaration.value,
ComponentSerializationMode::Retokenize,
@@ -980,6 +1021,9 @@ fn parse_property_prelude(rule: &AtRule, context: &ParseContext) -> ParsedRulePr
}
}
+ if initial_value_is_invalid {
+ return ParsedRulePrelude::Invalid;
+ }
let (Some(syntax_source), Some(inherits)) = (syntax_source, inherits) else {
return ParsedRulePrelude::Invalid;
};
diff --git a/Libraries/LibWeb/Rust/src/css/style_value.rs b/Libraries/LibWeb/Rust/src/css/style_value.rs
index fd5f6bfd38c..81a8582c6c4 100644
--- a/Libraries/LibWeb/Rust/src/css/style_value.rs
+++ b/Libraries/LibWeb/Rust/src/css/style_value.rs
@@ -117,7 +117,7 @@ pub(crate) fn utf16_equals_ascii_case_insensitive(value: &[u16], expected: &[u8]
.all(|(&left, &right)| u8::try_from(left).is_ok_and(|left| left.eq_ignore_ascii_case(&right)))
}
-fn split_var_arguments(values: &[ComponentValue]) -> Option<(&[u16], Option<&[ComponentValue]>)> {
+pub(crate) fn split_var_arguments(values: &[ComponentValue]) -> Option<(&[u16], Option<&[ComponentValue]>)> {
if !crate::css::parser::arbitrary_substitution::arguments_are_valid_for_ffi(5, values) {
return None;
}
diff --git a/Tests/LibWeb/Text/expected/css/property-initial-substitution-descriptors.txt b/Tests/LibWeb/Text/expected/css/property-initial-substitution-descriptors.txt
index 3737f7b1c97..9cf9318fad0 100644
--- a/Tests/LibWeb/Text/expected/css/property-initial-substitution-descriptors.txt
+++ b/Tests/LibWeb/Text/expected/css/property-initial-substitution-descriptors.txt
@@ -1,5 +1,28 @@
-substitution: 1
+substitution: 0
later valid: 1
-later substitution: 1
+later substitution: 0
important substitution: 1
later important: 1
+var fallback env: 0
+env fallback var: 0
+var then env: 0
+env then var: 0
+later mixed: 0
+mixed then valid: 1
+mixed then valid initial value: value
+malformed var: 1
+malformed var initial value: value
+malformed env: 1
+malformed env initial value: value
+env only: 1
+env only initial value: value
+nested malformed var: 1
+nested malformed var initial value: value
+mixed important: 1
+mixed important initial value: value
+typed later mixed: 0
+typed later nested: 0
+typed malformed env: 1
+typed malformed env initial value: 1px
+typed later valid: 1
+typed later valid initial value: 1px
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/at-property.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/at-property.txt
index ba4b0890201..b60c3c5cd39 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/at-property.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-properties-values-api/at-property.txt
@@ -2,8 +2,7 @@ Harness status: OK
Found 106 tests
-105 Pass
-1 Fail
+106 Pass
Pass Attribute 'syntax' returns expected value for ["<color>"]
Pass Attribute 'syntax' returns expected value for ["<color> | none"]
Pass Attribute 'syntax' returns expected value for ["<color># | <image> | none"]
@@ -41,7 +40,7 @@ Pass Attribute 'initial-value' returns expected value for [red]
Pass Attribute 'initial-value' returns expected value for [foo]
Pass Attribute 'initial-value' returns expected value for [foo(){}]
Pass Attribute 'initial-value' makes the @property rule invalid for [3em]
-Fail Attribute 'initial-value' makes the @property rule invalid for [var(--x)]
+Pass Attribute 'initial-value' makes the @property rule invalid for [var(--x)]
Pass Attribute 'inherits' returns expected value for [true]
Pass Attribute 'inherits' returns expected value for [false]
Pass Attribute 'inherits' makes the @property rule invalid for [none]
diff --git a/Tests/LibWeb/Text/input/css/property-initial-substitution-descriptors.html b/Tests/LibWeb/Text/input/css/property-initial-substitution-descriptors.html
index bcc5b59735a..cdd3e73eea1 100644
--- a/Tests/LibWeb/Text/input/css/property-initial-substitution-descriptors.html
+++ b/Tests/LibWeb/Text/input/css/property-initial-substitution-descriptors.html
@@ -8,12 +8,44 @@
["later substitution", "initial-value: value; initial-value: var(--missing)"],
["important substitution", "initial-value: var(--missing) !important"],
["later important", "initial-value: var(--missing); initial-value: value !important"],
+ ["var fallback env", "initial-value: var(--missing, env(safe-area-inset-top))"],
+ ["env fallback var", "initial-value: env(safe-area-inset-top, var(--missing))"],
+ ["var then env", "initial-value: var(--missing) env(safe-area-inset-top)"],
+ ["env then var", "initial-value: env(safe-area-inset-top) var(--missing)"],
+ ["later mixed", "initial-value: value; initial-value: var(--missing, env(safe-area-inset-top))"],
+ ["mixed then valid", "initial-value: var(--missing, env(safe-area-inset-top)); initial-value: value"],
+ ["malformed var", "initial-value: value; initial-value: var(missing)"],
+ ["malformed env", "initial-value: value; initial-value: var(--missing, env())"],
+ ["env only", "initial-value: value; initial-value: env(safe-area-inset-top)"],
+ ["nested malformed var", "initial-value: value; initial-value: env(safe-area-inset-top, var(missing))"],
+ [
+ "mixed important",
+ "initial-value: value; initial-value: var(--missing, env(safe-area-inset-top)) !important",
+ ],
+ [
+ "typed later mixed",
+ "initial-value: 1px; initial-value: var(--missing) env(safe-area-inset-top)",
+ "<length>",
+ ],
+ [
+ "typed later nested",
+ "initial-value: 1px; initial-value: env(safe-area-inset-top, var(--missing))",
+ "<length>",
+ ],
+ ["typed malformed env", "initial-value: 1px; initial-value: var(--missing) env()", "<length>"],
+ [
+ "typed later valid",
+ "initial-value: var(--missing, env(safe-area-inset-top)); initial-value: 1px",
+ "<length>",
+ ],
];
- for (const [index, [label, descriptors]] of cases.entries()) {
+ for (const [index, [label, descriptors, syntax = "*"]] of cases.entries()) {
const style = document.createElement("style");
- style.textContent = `@property --initial-${index} { syntax: "*"; inherits: false; ${descriptors}; }`;
+ style.textContent = `@property --initial-${index} { syntax: ${JSON.stringify(syntax)}; inherits: false; ${descriptors}; }`;
document.head.appendChild(style);
println(`${label}: ${style.sheet.cssRules.length}`);
+ if (index >= 5 && style.sheet.cssRules.length)
+ println(`${label} initial value: ${style.sheet.cssRules[0].initialValue}`);
style.remove();
}
});