From b9da74d16ebc2c16aa1ec46997331a023a02a72b Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Mon, 22 Jun 2026 21:57:48 +0200 Subject: [PATCH] LibWeb: Use fatal decoding when loading XML documents Decode XML document bytes with TextCodec::ErrorMode::Fatal and treat decode failure as an XML loading error. This removes the separate decoder validation pass and lets the decode operation itself enforce XML's requirement that documents contain only properly encoded characters. --- Libraries/LibWeb/DOM/DocumentLoading.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Libraries/LibWeb/DOM/DocumentLoading.cpp b/Libraries/LibWeb/DOM/DocumentLoading.cpp index 0845c455ee..356484d128 100644 --- a/Libraries/LibWeb/DOM/DocumentLoading.cpp +++ b/Libraries/LibWeb/DOM/DocumentLoading.cpp @@ -73,11 +73,12 @@ bool build_xml_document(DOM::Document& document, ByteBuffer const& data, Optiona } VERIFY(decoder.has_value()); // Well-formed XML documents contain only properly encoded characters - if (!decoder->validate(data)) { + auto source_or_error = decoder->to_utf8(data, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal); + if (source_or_error.is_error()) { convert_to_xml_error_document(document, "XML Document contains improperly-encoded characters"_utf16); return false; } - auto source = decoder->to_utf8(data, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal).release_value_but_fixme_should_propagate_errors(); + auto source = source_or_error.release_value(); XML::Parser parser(source, { .resolve_named_html_entity = resolve_named_html_entity }); XMLDocumentBuilder builder { document }; auto result = parser.parse_with_listener(builder); @@ -198,12 +199,6 @@ static WebIDL::ExceptionOr> load_xml_document(HTML::Navig } VERIFY(decoder.has_value()); // Well-formed XML documents contain only properly encoded characters - if (!decoder->validate(data)) { - // FIXME: Insert error message into the document. - dbgln("XML Document contains improperly-encoded characters"); - convert_to_xml_error_document(document, "XML Document contains improperly-encoded characters"_utf16); - return; - } auto source = decoder->to_utf8(data, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal); if (source.is_error()) { // FIXME: Insert error message into the document.