Skip to content

Truly independent web browser

LibWeb: Validate literal attr() arguments while parsing

Andreas Kling05b2ac914aaf
diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
index e06be814286..4b2ca4004a4 100644
--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
@@ -3003,9 +3003,12 @@ fn parse_attr_syntax(tokens: &[ComponentValue]) -> Result<(Vec<u16>, AttrSyntax)
let Some(name) = tokens.first().and_then(ComponentValue::ident) else {
return Err(AttrSyntaxParseFailure::SyntaxOmitted);
};
- let rest = trim_whitespace(&tokens[1..]);
+ Ok((name.to_vec(), parse_attr_type(trim_whitespace(&tokens[1..]))?))
+}
+
+fn parse_attr_type(rest: &[ComponentValue]) -> Result<AttrSyntax, AttrSyntaxParseFailure> {
if rest.is_empty() {
- return Ok((name.to_vec(), AttrSyntax::Omitted));
+ return Ok(AttrSyntax::Omitted);
}
if let Some(syntax) = rest[0].ident() {
let syntax = if syntax.eq_ignore_ascii_case("raw-string") {
@@ -3020,13 +3023,13 @@ fn parse_attr_syntax(tokens: &[ComponentValue]) -> Result<(Vec<u16>, AttrSyntax)
if rest.len() != 1 {
return Err(AttrSyntaxParseFailure::SyntaxSpecified);
}
- return Ok((name.to_vec(), syntax));
+ return Ok(syntax);
}
if rest[0].is_delim(b'%') {
if rest.len() != 1 {
return Err(AttrSyntaxParseFailure::SyntaxSpecified);
}
- return Ok((name.to_vec(), AttrSyntax::Unit(vec![u16::from(b'%')])));
+ return Ok(AttrSyntax::Unit(vec![u16::from(b'%')]));
}
if let Some((function, values)) = rest[0].function()
&& function.eq_ignore_ascii_case("type")
@@ -3037,11 +3040,44 @@ fn parse_attr_syntax(tokens: &[ComponentValue]) -> Result<(Vec<u16>, AttrSyntax)
if rest.len() != 1 {
return Err(AttrSyntaxParseFailure::SyntaxSpecified);
}
- return Ok((name.to_vec(), AttrSyntax::Syntax(syntax)));
+ return Ok(AttrSyntax::Syntax(syntax));
}
Err(AttrSyntaxParseFailure::SyntaxOmitted)
}
+pub(crate) fn literal_attr_arguments_are_valid(values: &[ComponentValue]) -> bool {
+ let comma = values.iter().position(ComponentValue::is_comma).unwrap_or(values.len());
+ let first_argument = trim_whitespace(&values[..comma]);
+ if contains_arbitrary_substitution_function(first_argument) {
+ return true;
+ }
+ // https://drafts.csswg.org/css-values-5/#attr-notation
+ // INTEROP: Chrome and Firefox reject malformed literal names and types at parse time,
+ // rather than using the replacement algorithm's fallback. Defer substituted arguments.
+ // NB: Namespace components cannot contain intervening whitespace.
+ let name_end = if first_argument.first().is_some_and(|value| value.is_delim(b'|')) {
+ 2
+ } else if first_argument.first().and_then(ComponentValue::ident).is_some() {
+ if first_argument.get(1).is_some_and(|value| value.is_delim(b'|')) {
+ 3
+ } else {
+ 1
+ }
+ } else {
+ return false;
+ };
+ if first_argument
+ .get(name_end - 1)
+ .and_then(ComponentValue::ident)
+ .is_none()
+ {
+ return false;
+ }
+ let attr_type = trim_whitespace(&first_argument[name_end..]);
+ // NB: An unknown unit identifier is grammatically valid and uses the computed-value fallback.
+ (attr_type.len() == 1 && attr_type[0].ident().is_some()) || parse_attr_type(attr_type).is_ok()
+}
+
fn parse_attr_value_with_syntax(
registry: Option<&CustomPropertyRegistry>,
values: &[ComponentValue],
diff --git a/Libraries/LibWeb/Rust/src/css/parser/arbitrary_substitution.rs b/Libraries/LibWeb/Rust/src/css/parser/arbitrary_substitution.rs
index a465858bc41..fbfe9aa1d96 100644
--- a/Libraries/LibWeb/Rust/src/css/parser/arbitrary_substitution.rs
+++ b/Libraries/LibWeb/Rust/src/css/parser/arbitrary_substitution.rs
@@ -197,8 +197,12 @@ fn parse_dashed_function_argument(values: &[ComponentValue], position: &mut usiz
pub(crate) fn arguments_are_valid(function: ArbitrarySubstitutionFunction, values: &[ComponentValue]) -> bool {
let mut position = 0;
match function {
- ArbitrarySubstitutionFunction::Attr
- | ArbitrarySubstitutionFunction::Env
+ ArbitrarySubstitutionFunction::Attr => {
+ parse_declaration_value_then_optional_declaration_value(values, &mut position, EndToken::Comma)
+ && position == values.len()
+ && crate::css::custom_properties::literal_attr_arguments_are_valid(values)
+ }
+ ArbitrarySubstitutionFunction::Env
| ArbitrarySubstitutionFunction::Inherit
| ArbitrarySubstitutionFunction::Var => {
parse_declaration_value_then_optional_declaration_value(values, &mut position, EndToken::Comma)
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/attr-all-types.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/attr-all-types.txt
index 82d53ac6326..eecda58146f 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/attr-all-types.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/attr-all-types.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 172 tests
-165 Pass
-7 Fail
+169 Pass
+3 Fail
Pass Invariant behaviour for var
Pass CSS Values and Units Test: attr
Pass CSS Values and Units Test: attr 1
@@ -64,7 +64,7 @@ Pass CSS Values and Units Test: attr 55
Pass CSS Values and Units Test: attr 56
Pass CSS Values and Units Test: attr 57
Pass CSS Values and Units Test: attr 58
-Fail CSS Values and Units Test: attr 59
+Pass CSS Values and Units Test: attr 59
Pass CSS Values and Units Test: attr 60
Pass CSS Values and Units Test: attr 61
Pass CSS Values and Units Test: attr 62
@@ -133,8 +133,8 @@ Pass CSS Values and Units Test: attr 124
Pass CSS Values and Units Test: attr 125
Fail CSS Values and Units Test: attr 126
Pass CSS Values and Units Test: attr 127
-Fail CSS Values and Units Test: attr 128
-Fail CSS Values and Units Test: attr 129
+Pass CSS Values and Units Test: attr 128
+Pass CSS Values and Units Test: attr 129
Pass CSS Values and Units Test: attr 130
Pass CSS Values and Units Test: attr 131
Pass CSS Values and Units Test: attr 132
@@ -152,7 +152,7 @@ Pass CSS Values and Units Test: attr 143
Pass CSS Values and Units Test: attr 144
Pass CSS Values and Units Test: attr 145
Pass CSS Values and Units Test: attr 146
-Fail CSS Values and Units Test: attr 147
+Pass CSS Values and Units Test: attr 147
Pass CSS Values and Units Test: attr 148
Pass CSS Values and Units Test: attr 149
Pass CSS Values and Units Test: attr 150
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/attr-cycle.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/attr-cycle.txt
index 621c242b08a..457f28f6ded 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/attr-cycle.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/attr-cycle.txt
@@ -2,9 +2,9 @@ Harness status: OK
Found 28 tests
-25 Pass
-3 Fail
-Fail CSS Values and Units Test: attr
+26 Pass
+2 Fail
+Pass CSS Values and Units Test: attr
Pass CSS Values and Units Test: attr 1
Pass CSS Values and Units Test: attr 2
Fail CSS Values and Units Test: attr 3