Skip to content

Commit

Permalink
Cherry-pick 274963@main (3935730). https://bugs.webkit.org/show_bug.c…
Browse files Browse the repository at this point in the history
…gi?id=269643

    HTMLPreloadScanner should only use valid `base` urls

    https://bugs.webkit.org/show_bug.cgi?id=269643

    Reviewed by Ryosuke Niwa.

    Partial Merge: https://chromium.googlesource.com/chromium/blink/+/754b22f62f6fa5f0b938a90c0e92502eb7f5a7c3

    Before this patch, HTMLPreloadScanner accepted invalid `base` urls and
    used it to resolve urls encountered later in the scan.
    This patch ensures that only valid urls specified in `base href` are
    actually used as base urls.

    * Source/WebCore/html/parser/HTMLPreloadScanner.cpp:
    (TokenPreloadScanner::updatePredictedBaseURL):
    * LayoutTests/fast/parser/badurl-base-preloader-crash.html: Add Test Case
    * LayoutTests/fast/parser/badurl-base-preloader-crash-expected.txt: Add Test Case Expectation
    * LayoutTests/http/tests/loading/preload-ignore-invalid-base.html: Add Test Case
    * LayoutTests/http/tests/loading/resources/fail.js: Add Test Case Helper Script
    * LayoutTests/http/tests/loading/preload-ignore-invalid-base-expected.txt: Add Test Expectation

    Canonical link: https://commits.webkit.org/274963@main
  • Loading branch information
Ahmad-S792 authored and aperezdc committed Mar 14, 2024
1 parent cbc47a8 commit 873d58d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PASS: if scanning this document with preloader doesn't crash in debug builds
7 changes: 7 additions & 0 deletions LayoutTests/fast/parser/badurl-base-preloader-crash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<base href="gopher:��&#279%�:0"></base>
<script src=":"></script>
<script>
if (window.testRunner) testRunner.dumpAsText();
</script>
<p>PASS: if scanning this document with preloader doesn't crash in debug builds</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
main frame - didStartProvisionalLoadForFrame
main frame - didCommitLoadForFrame
main frame - didFinishDocumentLoadForFrame
main frame - didHandleOnloadEventsForFrame
main frame - didFinishLoadForFrame
PASS internals.isPreloaded("resources/fail.js") is false
PASS window.fail is false
PASS successfullyParsed is true

TEST COMPLETE

15 changes: 15 additions & 0 deletions LayoutTests/http/tests/loading/preload-ignore-invalid-base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<script src="/js-test-resources/js-test.js"></script>
<script>
window.fail = false;
</script>
<base href="gopher:???:"></base>
<script src="http://127.0.0.1:8000/resources/slow-script.pl?delay=100"></script>
<script>
shouldBeFalse('internals.isPreloaded("resources/fail.js")')
</script>
<!-- The resource should not be read, as the baseUrl is now set to empty from reading invalid url -->
<script src="resources/fail.js"></script>
<script>
shouldBeFalse('window.fail')
</script>
1 change: 1 addition & 0 deletions LayoutTests/http/tests/loading/resources/fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.fail=true;
2 changes: 1 addition & 1 deletion Source/WebCore/html/parser/HTMLPreloadScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ void TokenPreloadScanner::updatePredictedBaseURL(const HTMLToken& token, bool sh
return;
URL temp { m_documentURL, StringImpl::create8BitIfPossible(hrefAttribute->value) };
if (!shouldRestrictBaseURLSchemes || SecurityPolicy::isBaseURLSchemeAllowed(temp))
m_predictedBaseElementURL = WTFMove(temp).isolatedCopy();
m_predictedBaseElementURL = WTFMove(temp).isValid() ? WTFMove(temp).isolatedCopy() : URL();
}

HTMLPreloadScanner::HTMLPreloadScanner(const HTMLParserOptions& options, const URL& documentURL, float deviceScaleFactor)
Expand Down

0 comments on commit 873d58d

Please sign in to comment.