Truly independent web browser
LibWeb: Parse substituted values from their components
Andreas Kling58bdecfc0028
diff --git a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rsindex f3458aef4fb..01f7a13f423 100644--- a/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs+++ b/Libraries/LibWeb/Rust/src/css/cascaded_properties.rs@@ -345,11 +345,14 @@ thread_local! {pub(crate) fn parse_substituted_value(base_context: &ParseContext,property_id: u16,- source: &[u16],values: &[ComponentValue],contains_attr_tainted_values: bool,) -> RetainedStyleValueData {crate::css::ffi_stats::bump(crate::css::ffi_stats::FfiOp::SubstitutionParse);+ // NB: Substitution can nest components deeper than text may, which no value holds.+ if crate::css::parser::component_value::exceeds_component_value_nesting_limit(values) {+ return RetainedStyleValueData::from_owned(StyleValueData::GuaranteedInvalid);+ }let random_function_index = crate::css::parser::value_parser::RandomFunctionIndex::default();let value_context = FfiValueParsingContext {kind: FfiValueParsingContextKind::Property,@@ -362,7 +365,7 @@ pub(crate) fn parse_substituted_value(context.contains_attr_tainted_values = contains_attr_tainted_values;context.value_contexts = ak::Span::new(std::slice::from_ref(&value_context));context.random_function_index = Some(&random_function_index);- match parse_css_value_with_source(&context, property_id, values, source) {+ match parse_css_value_with_source(&context, property_id, values, &[]) {ParseOutcome::Parsed(value) => RetainedStyleValueData::from_arc(value),ParseOutcome::Invalid | ParseOutcome::NotHandled => {RetainedStyleValueData::from_owned(StyleValueData::GuaranteedInvalid)diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rsindex c8ba427bdb7..d91e06359a9 100644--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs@@ -716,9 +716,6 @@ impl CustomPropertyRegistry {) => Some(parse_substituted_value(parse_context,property_id::CUSTOM,- // NB: The value keeps this text alone and tokenizes it where it is read, so it is the- // serialization that tokenizes back to the components.- &serialize_component_values_to_utf16(&components, ComponentSerializationMode::Retokenize),&components,contains_attr_tainted_values,)),@@ -1201,9 +1198,8 @@ const MAX_SUBSTITUTION_RECURSION_DEPTH: u32 = 64;pub(crate) enum NativeVarResolution {Resolved {- /// The substituted components, serialized to tokenize back to them where comments only keep- /// tokens apart.- source: Vec<u16>,+ /// The substituted components.+ components: ComponentValueList,contains_attr_tainted_values: bool,},Invalid,@@ -3818,8 +3814,8 @@ pub(crate) fn resolve_vars(match result {// A custom property carries its taint to the values that substitute it; another property is invalid where a// tainted value reaches a URL.- TokenResolution::Resolved(tokens) => NativeVarResolution::Resolved {- source: serialize_component_values_to_utf16(&tokens, ComponentSerializationMode::Retokenize),+ TokenResolution::Resolved(components) => NativeVarResolution::Resolved {+ components,contains_attr_tainted_values: if property_id == crate::css::property_metadata::property_id::CUSTOM {context.contains_attr_tainted_values} else {diff --git a/Libraries/LibWeb/Rust/src/css/parser/component_value.rs b/Libraries/LibWeb/Rust/src/css/parser/component_value.rsindex 7d764e07007..45485bd166e 100644--- a/Libraries/LibWeb/Rust/src/css/parser/component_value.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/component_value.rs@@ -667,6 +667,19 @@ pub(crate) fn consume_a_list_of_component_values(tokens: impl ComponentValueTokeconsume_a_counted_list_of_component_values(tokens).map(ComponentValueList::into_vec)}+/// Whether `values` nest deeper than a list of component values consumed from text may.+pub(crate) fn exceeds_component_value_nesting_limit(values: &[ComponentValue]) -> bool {+ fn exceeds(values: &[ComponentValue], depth: usize) -> bool {+ values.iter().any(|value| match &value.kind {+ ComponentKind::Token(_) => false,+ ComponentKind::Function { values, .. } | ComponentKind::SimpleBlock { values, .. } => {+ depth >= MAXIMUM_COMPONENT_VALUE_NESTING_DEPTH || exceeds(values, depth + 1)+ }+ })+ }+ exceeds(values, 0)+}+pub(crate) fn consume_a_counted_list_of_component_values(tokens: impl ComponentValueTokens,) -> Result<ComponentValueList, ()> {diff --git a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rsindex 2a76ddba275..4afe384cfbf 100644--- a/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs+++ b/Libraries/LibWeb/Rust/src/css/parser/value_parser.rs@@ -5993,32 +5993,28 @@ fn parse_css_value_after_substitution_scan(// text are written as that serialization.let serialization;let mut unresolved = if context.is_substituted_value || unresolved_source.is_empty() {- let text = if context.is_substituted_value {- unresolved_source- } else {- serialization = crate::css::serialize::serialize_component_values_to_utf16(- values,- crate::css::parser::component_value::ComponentSerializationMode::Retokenize,- );- &serialization- };+ serialization = crate::css::serialize::serialize_component_values_to_utf16(+ values,+ crate::css::parser::component_value::ComponentSerializationMode::Retokenize,+ );unresolved_value_with_components(- text,+ &serialization,UnresolvedValueComponents::OfSerialization(values),substitution_presence,)} else {+ serialization = Vec::new();unresolved_value(unresolved_source, values.to_vec(), substitution_presence)};if let StyleValueData::Unresolved(unresolved_payload) = &mut unresolved {unresolved_payload.payload_mut().contains_attr_tainted_values = context.contains_attr_tainted_values;if property_id == property_id::CUSTOM && context.is_substituted_value {- let first_non_whitespace = unresolved_source+ let first_non_whitespace = serialization.iter().position(|unit| !matches!(*unit, 0x09 | 0x0a | 0x0c | 0x0d | 0x20))- .unwrap_or(unresolved_source.len());- *unresolved_payload = unresolved_payload- .with_source_text(TokenizerInput::Utf16(&unresolved_source[first_non_whitespace..]));+ .unwrap_or(serialization.len());+ *unresolved_payload =+ unresolved_payload.with_source_text(TokenizerInput::Utf16(&serialization[first_non_whitespace..]));}}return ParseOutcome::Parsed(shared_style_value(unresolved));diff --git a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rsindex 85cc439a0f1..30b4d78616f 100644--- a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs+++ b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs@@ -1230,18 +1230,9 @@ pub(super) fn substitute_against_stores(let invalid = || RetainedStyleValueData::from_owned(StyleValueData::GuaranteedInvalid);match resolution {NativeVarResolution::Resolved {- source,+ components,contains_attr_tainted_values,- } => {- let Ok(values) = crate::css::style_value::components_of_text(- crate::css::css_tokenizer::TokenizerInput::Utf16(&source),- crate::css::css_tokenizer::Comments::Separating,- ) else {- crate::css::ffi_stats::bump(crate::css::ffi_stats::FfiOp::SubstitutionParse);- return invalid();- };- parse_substituted_value(&parse_context, property, &source, &values, contains_attr_tainted_values)- }+ } => parse_substituted_value(&parse_context, property, &components, contains_attr_tainted_values),NativeVarResolution::Invalid | NativeVarResolution::NotHandled => invalid(),}}diff --git a/Tests/LibWeb/Text/expected/css/style-engine/substitution-nesting-limit.txt b/Tests/LibWeb/Text/expected/css/style-engine/substitution-nesting-limit.txtnew file mode 100644index 00000000000..2bc68770941--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/style-engine/substitution-nesting-limit.txt@@ -0,0 +1,5 @@+shallow: 0.5+deep: 1+245 deep: true+275 deep: false+substituting that: falsediff --git a/Tests/LibWeb/Text/input/css/style-engine/substitution-nesting-limit.html b/Tests/LibWeb/Text/input/css/style-engine/substitution-nesting-limit.htmlnew file mode 100644index 00000000000..42472c60ce7--- /dev/null+++ b/Tests/LibWeb/Text/input/css/style-engine/substitution-nesting-limit.html@@ -0,0 +1,29 @@+<!doctype html>+<script src="../../include.js"></script>+<div id="target"></div>+<script>+ // Values nested deeper by substitution than text may be are invalid at computed-value time.+ test(() => {+ const nested = (depth, inner) => "(".repeat(depth) + inner + ")".repeat(depth);+ const target = document.getElementById("target");+ target.style.setProperty("--shallow", nested(10, "0.5"));+ target.style.setProperty("--deep", nested(200, "0.5"));+ const read = value => {+ target.style.setProperty("opacity", value);+ const opacity = getComputedStyle(target).opacity;+ target.style.removeProperty("opacity");+ return opacity;+ };+ println(`shallow: ${read(`calc(${nested(10, "var(--shallow)")})`)}`);+ println(`deep: ${read(`calc(${nested(200, "var(--deep)")})`)}`);++ // A custom property reaching that depth through others is invalid as well, as is one that substitutes it.+ target.style.setProperty("--chain-0", nested(5, "0.5"));+ const is_set = name => getComputedStyle(target).getPropertyValue(name) !== "";+ for (let link = 1; link <= 10; ++link)+ target.style.setProperty(`--chain-${link}`, nested(30, `var(--chain-${link - 1})`));+ println(`245 deep: ${is_set("--chain-8")}`);+ println(`275 deep: ${is_set("--chain-9")}`);+ println(`substituting that: ${is_set("--chain-10")}`);+ });+</script>