Skip to content

Truly independent web browser

LibWeb: Don't fetch resources from temporary fragment parsing documents

Andreas Kling8c11d8939885
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
index c1612ff5e56..0c0fa32dae5 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
@@ -151,6 +151,12 @@ void HTMLInputElement::adopted_from(DOM::Document& old_document)
if (m_load_event_delayer.has_value())
m_load_event_delayer.emplace(document());
+
+ // NB: See handle_src_attribute().
+ if (old_document.is_temporary_document_for_fragment_parsing()) {
+ if (auto src = attribute(AttributeNames::src); src.has_value())
+ handle_src_attribute(*src).release_value_but_fixme_should_propagate_errors();
+ }
}
void HTMLInputElement::set_being_activated(bool activated)
@@ -1835,6 +1841,12 @@ WebIDL::ExceptionOr<void> HTMLInputElement::handle_src_attribute(Utf16View value
if (type_state() != TypeAttributeState::ImageButton)
return {};
+ // AD-HOC: An element in a temporary document for fragment parsing does not fetch anything, so that markup parsed
+ // into such a document, for example by the Sanitizer API, causes no network requests for elements that
+ // are never inserted anywhere. The image is fetched once the element is adopted into another document.
+ if (document().is_temporary_document_for_fragment_parsing())
+ return {};
+
m_last_src_value = Utf16String::from_utf16(value);
// 1. Let url be the result of encoding-parsing a URL given the src attribute's value, relative to the element's
diff --git a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp b/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
index 46320fca2c7..9e011ab892e 100644
--- a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
@@ -130,6 +130,10 @@ void HTMLVideoElement::adopted_from(DOM::Document& old_document)
m_load_event_delayer.emplace(document());
release_presented_video_frame_held_for_callbacks();
+
+ // NB: See determine_element_poster_frame().
+ if (old_document.is_temporary_document_for_fragment_parsing())
+ determine_element_poster_frame(attribute(HTML::AttributeNames::poster)).release_value_but_fixme_should_propagate_errors();
}
void HTMLVideoElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_)
@@ -253,6 +257,13 @@ Optional<CSSPixelSize> HTMLVideoElement::natural_element_size() const
// https://html.spec.whatwg.org/multipage/media.html#attr-video-poster
WebIDL::ExceptionOr<void> HTMLVideoElement::determine_element_poster_frame(Optional<Utf16String> const& poster)
{
+ // AD-HOC: An element in a temporary document for fragment parsing does not fetch anything, so that markup parsed
+ // into such a document, for example by the Sanitizer API, causes no network requests for elements that
+ // are never inserted anywhere. The poster frame is determined once the element is adopted into another
+ // document.
+ if (document().is_temporary_document_for_fragment_parsing())
+ return {};
+
auto& realm = HTML::relevant_realm(*this);
// 1. If there is an existing instance of this algorithm running for this video element, abort that instance of
// this algorithm without changing the poster frame.
diff --git a/Libraries/LibWeb/SVG/SVGFEImageElement.cpp b/Libraries/LibWeb/SVG/SVGFEImageElement.cpp
index dfa8c08b411..50fc6ffb850 100644
--- a/Libraries/LibWeb/SVG/SVGFEImageElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGFEImageElement.cpp
@@ -54,8 +54,27 @@ void SVGFEImageElement::attribute_changed(Utf16FlyString const& name, Optional<U
}
}
+void SVGFEImageElement::adopted_from(DOM::Document& old_document)
+{
+ Base::adopted_from(old_document);
+
+ // NB: See process_href().
+ if (old_document.is_temporary_document_for_fragment_parsing()) {
+ auto href = get_attribute_ns(Optional<Utf16FlyString> {}, SVG::AttributeNames::href);
+ if (!href.has_value())
+ href = get_attribute_ns(Namespace::XLink, SVG::AttributeNames::href);
+ process_href(href);
+ }
+}
+
void SVGFEImageElement::process_href(Optional<Utf16String> const& href)
{
+ // AD-HOC: An element in a temporary document for fragment parsing does not fetch anything, so that markup parsed
+ // into such a document, for example by the Sanitizer API, causes no network requests for elements that
+ // are never inserted anywhere. The URL is processed once the element is adopted into another document.
+ if (document().is_temporary_document_for_fragment_parsing())
+ return;
+
if (!href.has_value()) {
m_href = {};
return;
diff --git a/Libraries/LibWeb/SVG/SVGFEImageElement.h b/Libraries/LibWeb/SVG/SVGFEImageElement.h
index f9131807b17..865c39d02d2 100644
--- a/Libraries/LibWeb/SVG/SVGFEImageElement.h
+++ b/Libraries/LibWeb/SVG/SVGFEImageElement.h
@@ -34,6 +34,7 @@ private:
virtual void visit_edges(Cell::Visitor&) override;
virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;
+ virtual void adopted_from(DOM::Document&) override;
void process_href(Optional<Utf16String> const& href);
diff --git a/Libraries/LibWeb/SVG/SVGImageElement.cpp b/Libraries/LibWeb/SVG/SVGImageElement.cpp
index 226c26f0a86..26980179be6 100644
--- a/Libraries/LibWeb/SVG/SVGImageElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGImageElement.cpp
@@ -62,6 +62,14 @@ void SVGImageElement::adopted_from(DOM::Document& old_document)
if (m_load_event_delayer.has_value())
m_load_event_delayer.emplace(document());
+
+ // NB: See process_the_url().
+ if (old_document.is_temporary_document_for_fragment_parsing()) {
+ auto href = get_attribute_ns(Optional<Utf16FlyString> {}, SVG::AttributeNames::href);
+ if (!href.has_value())
+ href = get_attribute_ns(Namespace::XLink, SVG::AttributeNames::href);
+ process_the_url(href);
+ }
}
void SVGImageElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_)
@@ -88,6 +96,12 @@ void SVGImageElement::attribute_changed(Utf16FlyString const& name, Optional<Utf
// https://www.w3.org/TR/SVG2/linking.html#processingURL
void SVGImageElement::process_the_url(Optional<Utf16String> const& href)
{
+ // AD-HOC: An element in a temporary document for fragment parsing does not fetch anything, so that markup parsed
+ // into such a document, for example by the Sanitizer API, causes no network requests for elements that
+ // are never inserted anywhere. The URL is processed once the element is adopted into another document.
+ if (document().is_temporary_document_for_fragment_parsing())
+ return;
+
if (!href.has_value()) {
m_href = {};
abandon_the_current_fetch();
diff --git a/Libraries/LibWeb/SVG/SVGUseElement.cpp b/Libraries/LibWeb/SVG/SVGUseElement.cpp
index 3d89b03ceb9..532c45afae7 100644
--- a/Libraries/LibWeb/SVG/SVGUseElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGUseElement.cpp
@@ -271,6 +271,12 @@ GC::Ptr<DOM::Element> SVGUseElement::referenced_element() const
// https://svgwg.org/svg2-draft/linking.html#processingURL-fetch
void SVGUseElement::fetch_the_document(URL::URL const& url)
{
+ // AD-HOC: An element in a temporary document for fragment parsing does not fetch anything, so that markup parsed
+ // into such a document, for example by the Sanitizer API, causes no network requests for elements that
+ // are never inserted anywhere. The document is fetched once the element is adopted into another document.
+ if (document().is_temporary_document_for_fragment_parsing())
+ return;
+
m_load_event_delayer.emplace(document());
m_resource_request = HTML::SharedResourceRequest::get_or_create(document(), url, HTML::CORSSettingAttribute::NoCORS);
m_resource_request->add_callbacks(
diff --git a/Tests/LibWeb/Text/expected/HTML/Element-setHTMLUnsafe-removed-elements-do-not-fetch.txt b/Tests/LibWeb/Text/expected/HTML/Element-setHTMLUnsafe-removed-elements-do-not-fetch.txt
index 94e3a639e90..f94e4e21326 100644
--- a/Tests/LibWeb/Text/expected/HTML/Element-setHTMLUnsafe-removed-elements-do-not-fetch.txt
+++ b/Tests/LibWeb/Text/expected/HTML/Element-setHTMLUnsafe-removed-elements-do-not-fetch.txt
@@ -1,10 +1,10 @@
video-poster: kept element fetched: true
-video-poster: removed element fetched: true
+video-poster: removed element fetched: false
input-image: kept element fetched: true
-input-image: removed element fetched: true
+input-image: removed element fetched: false
svg-image: kept element fetched: true
-svg-image: removed element fetched: true
+svg-image: removed element fetched: false
svg-feimage: kept element fetched: true
-svg-feimage: removed element fetched: true
+svg-feimage: removed element fetched: false
svg-use: kept element fetched: true
-svg-use: removed element fetched: true
+svg-use: removed element fetched: false