Truly independent web browser
LibWeb: Reject percentages outside their calculation context
Andreas Klingfc3ad96dbd75
diff --git a/Libraries/LibWeb/Rust/src/css/calc.rs b/Libraries/LibWeb/Rust/src/css/calc.rsindex f0eea86cc51..5f440b1fbe8 100644--- a/Libraries/LibWeb/Rust/src/css/calc.rs+++ b/Libraries/LibWeb/Rust/src/css/calc.rs@@ -354,6 +354,7 @@ pub(crate) fn serialize_context_free_calculation(values,crate::css::parser::calc_parser::CalcParserContext {percentages_resolve_as: None,+ allow_percentages: false,property: 0,random_function_index: None,allowed_color_channels: 0,diff --git a/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rsindex 7dd7262aed5..70b6eb22656 100644--- a/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/calc_parser.rs@@ -41,6 +41,7 @@ type Result<T> = std::result::Result<T, CalcParseError>;#[derive(Clone, Copy)]pub(crate) struct CalcParserContext<'a> {pub percentages_resolve_as: Option<u8>,+ pub allow_percentages: bool,pub property: u16,pub random_function_index: Option<&'a RandomFunctionIndex>,pub allowed_color_channels: u64,@@ -55,6 +56,7 @@ impl CalcParserContext<'_> {fn for_test(percentages_resolve_as: Option<u8>) -> Self {Self {percentages_resolve_as,+ allow_percentages: true,property: 0,random_function_index: None,allowed_color_channels: 0,@@ -180,6 +182,11 @@ impl<'a> CalculationParser<'a> {})))}ComponentKind::Token(ParserTokenKind::Percentage { value, .. }) => {+ // https://drafts.csswg.org/css-values-4/#calc-context+ // Math functions always inherit the calculation context from wherever they’re used.+ if !self.context.allow_percentages {+ return Err(CalcParseError::Invalid);+ }Ok(Arc::new(CalcNode::Numeric(CalcNumericValue::Percentage(*value))))}ComponentKind::Token(ParserTokenKind::Dimension { value, unit, .. }) => {@@ -812,6 +819,7 @@ mod tests {let random_function_index = RandomFunctionIndex::new(4);let context = CalcParserContext {percentages_resolve_as: None,+ allow_percentages: true,property: 1,random_function_index: Some(&random_function_index),allowed_color_channels: 0,diff --git a/Libraries/LibWeb/Rust/src/css/parser/color_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/color_parser.rsindex 16b2b5a5c98..742bb7f2c9f 100644--- a/Libraries/LibWeb/Rust/src/css/parser/color_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/color_parser.rs@@ -373,6 +373,7 @@ fn parse_calculated_numeric(values,CalcParserContext {percentages_resolve_as: None,+ allow_percentages: accepted.contains(&NumericKind::Percentage),property,random_function_index: context.random_function_index,allowed_color_channels,diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rsindex 8eef36cf07d..c629992a345 100644--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs@@ -1255,6 +1255,17 @@ pub(crate) fn parse_calculated_numeric_value_allowing_size_keyword(values,crate::css::parser::calc_parser::CalcParserContext {percentages_resolve_as,+ // AD-HOC: Number and length contexts keep accepting percentages, which a later type check can still+ // resolve or reject (for example, a ratio of two percentages is a number).+ allow_percentages: percentages_resolve_as.is_some()+ || matches!(+ value_type,+ VALUE_TYPE_NUMBER+ | VALUE_TYPE_INTEGER+ | VALUE_TYPE_LENGTH+ | VALUE_TYPE_PERCENTAGE+ | VALUE_TYPE_OPACITY_VALUE+ ),property,random_function_index: context.random_function_index,allowed_color_channels: 0,diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/acos-asin-atan-atan2-invalid.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/acos-asin-atan-atan2-invalid.txtindex 70aea76c10c..5f1157bd068 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/acos-asin-atan-atan2-invalid.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/acos-asin-atan-atan2-invalid.txt@@ -2,8 +2,7 @@ Harness status: OKFound 63 tests-62 Pass-1 Fail+63 PassPass e.style['transform'] = "rotate(asin())" should not set the property valuePass e.style['transform'] = "rotate(asin( ))" should not set the property valuePass e.style['transform'] = "rotate(asin(,))" should not set the property value@@ -59,7 +58,7 @@ Pass e.style['transform'] = "rotate(atan2(1deg / ))" should not set the propertyPass e.style['transform'] = "rotate(atan2(1deg 2deg))" should not set the property valuePass e.style['transform'] = "rotate(atan2(1deg, , 2deg))" should not set the property valuePass e.style['transform'] = "rotate(atan2(90px))" should not set the property value-Fail e.style['transform'] = "rotate(atan2(90px, 100%))" should not set the property value+Pass e.style['transform'] = "rotate(atan2(90px, 100%))" should not set the property valuePass e.style['transform'] = "rotate(atan2(30deg + 1.0471967rad, 0))" should not set the property valuePass e.style['transform'] = "rotate(atan2( 0 ,))" should not set the property valuePass e.style['transform'] = "rotate(atan2( () 30deg - 0.523599rad ))" should not set the property valuediff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/percentage-without-context.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/percentage-without-context.txtindex ef52045860a..4857c71fe1c 100644--- a/Tests/LibWeb/Text/expected/wpt-import/css/css-values/percentage-without-context.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-values/percentage-without-context.txt@@ -2,16 +2,16 @@ Harness status: OKFound 12 tests-12 Fail-Fail e.style['font-style'] = "oblique calc(sign(50%) * 1deg)" should not set the property value-Fail e.style['background'] = "linear-gradient(calc(sign(50%) * 1turn), #3f87a6, #ebf8e1, #f69d3c)" should not set the property value-Fail e.style['background'] = "conic-gradient(from calc(sign(50%) * 1rad) at 10% 50%, #e66465, #9198e5)" should not set the property value-Fail e.style['filter'] = "hue-rotate(calc(sign(50%) * 1deg))" should not set the property value-Fail e.style['offset-rotate'] = "calc(sign(50%) * 1deg)" should not set the property value-Fail e.style['offset-path'] = "ray(calc(sign(50%) * 1deg))" should not set the property value-Fail e.style['color'] = "hsl(calc(sign(50%) * 1deg) 82% 43%)" should not set the property value-Fail e.style['transform'] = "rotate(calc(sign(50%) * 1deg))" should not set the property value-Fail e.style['animation-duration'] = "calc(sign(50%) * 1s)" should not set the property value-Fail e.style['animation-delay'] = "calc(sign(50%) * 1s)" should not set the property value-Fail e.style['transition-duration'] = "calc(sign(50%) * 1s)" should not set the property value-Fail e.style['transition-delay'] = "calc(sign(50%) * 1s)" should not set the property value\ No newline at end of file+12 Pass+Pass e.style['font-style'] = "oblique calc(sign(50%) * 1deg)" should not set the property value+Pass e.style['background'] = "linear-gradient(calc(sign(50%) * 1turn), #3f87a6, #ebf8e1, #f69d3c)" should not set the property value+Pass e.style['background'] = "conic-gradient(from calc(sign(50%) * 1rad) at 10% 50%, #e66465, #9198e5)" should not set the property value+Pass e.style['filter'] = "hue-rotate(calc(sign(50%) * 1deg))" should not set the property value+Pass e.style['offset-rotate'] = "calc(sign(50%) * 1deg)" should not set the property value+Pass e.style['offset-path'] = "ray(calc(sign(50%) * 1deg))" should not set the property value+Pass e.style['color'] = "hsl(calc(sign(50%) * 1deg) 82% 43%)" should not set the property value+Pass e.style['transform'] = "rotate(calc(sign(50%) * 1deg))" should not set the property value+Pass e.style['animation-duration'] = "calc(sign(50%) * 1s)" should not set the property value+Pass e.style['animation-delay'] = "calc(sign(50%) * 1s)" should not set the property value+Pass e.style['transition-duration'] = "calc(sign(50%) * 1s)" should not set the property value+Pass e.style['transition-delay'] = "calc(sign(50%) * 1s)" should not set the property value\ No newline at end of file