Skip to content

Commit

Permalink
Stop evaluating <script>s moved between Documents during fetching
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=202714
<rdar://problem/56208425>

Reviewed by Geoffrey Garen.

LayoutTests/imported/w3c:

Rebaseline WPT tests now that more checks are passing. Note that these checks were already passing in both Firefox and Chrome.

* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-fetch-error-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-fetch-error-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-parse-error-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-parse-error-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-parse-error-inline-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-success-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-success-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-success-inline-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-fetch-error-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-fetch-error-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-parse-error-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-parse-error-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-parse-error-inline-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-success-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-success-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-success-inline-classic-expected.txt:

Source/WebCore:

Stop evaluating <script>s moved between Documents during fetching:
- whatwg/html#2469
- whatwg/html#2673

Both Firefox and Chrome already behave this way.

No new tests, rebaselined existing tests.

* dom/ScriptElement.cpp:
(WebCore::ScriptElement::prepareScript):
Set the element's preparation-time document to its node document, as per:
- https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script (step 11)

(WebCore::ScriptElement::executePendingScript):
If scriptElement's preparation-time document is not equal to scriptElement's node document, then return, as per:
- https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-block (step 2)

* dom/ScriptElement.h:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@280924 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
cdumez@apple.com committed Aug 11, 2021
1 parent 8314787 commit 4008034
Show file tree
Hide file tree
Showing 20 changed files with 107 additions and 36 deletions.
27 changes: 27 additions & 0 deletions LayoutTests/imported/w3c/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
2021-08-11 Chris Dumez <cdumez@apple.com>

Stop evaluating <script>s moved between Documents during fetching
https://bugs.webkit.org/show_bug.cgi?id=202714
<rdar://problem/56208425>

Reviewed by Geoffrey Garen.

Rebaseline WPT tests now that more checks are passing. Note that these checks were already passing in both Firefox and Chrome.

* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-fetch-error-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-fetch-error-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-parse-error-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-parse-error-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-parse-error-inline-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-success-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-success-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/after-prepare-iframe-success-inline-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-fetch-error-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-fetch-error-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-parse-error-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-parse-error-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-parse-error-inline-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-success-external-classic-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-success-external-module-expected.txt:
* web-platform-tests/html/semantics/scripting-1/the-script-element/moving-between-documents/move-back-iframe-success-inline-classic-expected.txt:

2021-08-11 Chris Dumez <cdumez@apple.com>

HTMLMetaElement http-equiv should not be processed in shadow trees
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move fetch-error external classic script to iframe after-prepare
PASS <script> load: Move fetch-error external classic script to iframe after-prepare
FAIL <script> error: Move fetch-error external classic script to iframe after-prepare assert_unreached: Script error evennt fired unexpectedly Reached unreachable code
PASS <script> error: Move fetch-error external classic script to iframe after-prepare
PASS window error: Move fetch-error external classic script to iframe after-prepare

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move fetch-error external module script to iframe after-prepare
PASS <script> load: Move fetch-error external module script to iframe after-prepare
FAIL <script> error: Move fetch-error external module script to iframe after-prepare assert_unreached: Script error evennt fired unexpectedly Reached unreachable code
PASS <script> error: Move fetch-error external module script to iframe after-prepare
PASS window error: Move fetch-error external module script to iframe after-prepare

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CONSOLE MESSAGE: SyntaxError: Left side of assignment is not a reference.
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move parse-error external classic script to iframe after-prepare
FAIL <script> load: Move parse-error external classic script to iframe after-prepare assert_unreached: Script load event fired unexpectedly Reached unreachable code
PASS <script> load: Move parse-error external classic script to iframe after-prepare
PASS <script> error: Move parse-error external classic script to iframe after-prepare
FAIL window error: Move parse-error external classic script to iframe after-prepare assert_unreached: Window error event shouldn't fired on destination window Reached unreachable code
PASS window error: Move parse-error external classic script to iframe after-prepare

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CONSOLE MESSAGE: SyntaxError: Left side of assignment is not a reference.
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move parse-error external module script to iframe after-prepare
PASS <script> load: Move parse-error external module script to iframe after-prepare
FAIL <script> error: Move parse-error external module script to iframe after-prepare assert_unreached: Script error evennt fired unexpectedly Reached unreachable code
PASS <script> error: Move parse-error external module script to iframe after-prepare
PASS window error: Move parse-error external module script to iframe after-prepare

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CONSOLE MESSAGE: SyntaxError: Left side of assignment is not a reference.
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move parse-error inline classic script to iframe after-prepare
FAIL <script> load: Move parse-error inline classic script to iframe after-prepare assert_unreached: Script load event fired unexpectedly Reached unreachable code
PASS <script> load: Move parse-error inline classic script to iframe after-prepare
PASS <script> error: Move parse-error inline classic script to iframe after-prepare
FAIL window error: Move parse-error inline classic script to iframe after-prepare assert_unreached: Window error event shouldn't fired on destination window Reached unreachable code
PASS window error: Move parse-error inline classic script to iframe after-prepare

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CONSOLE MESSAGE: exec
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
FAIL Eval: Move success external classic script to iframe after-prepare assert_false: The script must not have executed in destination window expected false got "executed"
FAIL <script> load: Move success external classic script to iframe after-prepare assert_unreached: Script load event fired unexpectedly Reached unreachable code
PASS Eval: Move success external classic script to iframe after-prepare
PASS <script> load: Move success external classic script to iframe after-prepare
PASS <script> error: Move success external classic script to iframe after-prepare
PASS window error: Move success external classic script to iframe after-prepare

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CONSOLE MESSAGE: TypeError: Requested module is not instantiated yet.
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move success external module script to iframe after-prepare
FAIL <script> load: Move success external module script to iframe after-prepare assert_unreached: Script load event fired unexpectedly Reached unreachable code
PASS <script> load: Move success external module script to iframe after-prepare
PASS <script> error: Move success external module script to iframe after-prepare
FAIL window error: Move success external module script to iframe after-prepare assert_unreached: Window error event shouldn't fired on destination window Reached unreachable code
PASS window error: Move success external module script to iframe after-prepare

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
FAIL Eval: Move success inline classic script to iframe after-prepare assert_false: The script must not have executed in destination window expected false got "executed"
FAIL <script> load: Move success inline classic script to iframe after-prepare assert_unreached: Script load event fired unexpectedly Reached unreachable code
PASS Eval: Move success inline classic script to iframe after-prepare
PASS <script> load: Move success inline classic script to iframe after-prepare
PASS <script> error: Move success inline classic script to iframe after-prepare
PASS window error: Move success inline classic script to iframe after-prepare

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move fetch-error external classic script to iframe move-back
PASS <script> load: Move fetch-error external classic script to iframe move-back
FAIL <script> error: Move fetch-error external classic script to iframe move-back assert_unreached: Script error evennt fired unexpectedly Reached unreachable code
PASS <script> error: Move fetch-error external classic script to iframe move-back
PASS window error: Move fetch-error external classic script to iframe move-back

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move fetch-error external module script to iframe move-back
PASS <script> load: Move fetch-error external module script to iframe move-back
FAIL <script> error: Move fetch-error external module script to iframe move-back assert_unreached: Script error evennt fired unexpectedly Reached unreachable code
PASS <script> error: Move fetch-error external module script to iframe move-back
PASS window error: Move fetch-error external module script to iframe move-back

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CONSOLE MESSAGE: SyntaxError: Left side of assignment is not a reference.
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move parse-error external classic script to iframe move-back
FAIL <script> load: Move parse-error external classic script to iframe move-back assert_unreached: Script load event fired unexpectedly Reached unreachable code
PASS <script> load: Move parse-error external classic script to iframe move-back
PASS <script> error: Move parse-error external classic script to iframe move-back
FAIL window error: Move parse-error external classic script to iframe move-back assert_unreached: Window error event shouldn't fired on source window Reached unreachable code
PASS window error: Move parse-error external classic script to iframe move-back

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CONSOLE MESSAGE: SyntaxError: Left side of assignment is not a reference.
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move parse-error external module script to iframe move-back
PASS <script> load: Move parse-error external module script to iframe move-back
FAIL <script> error: Move parse-error external module script to iframe move-back assert_unreached: Script error evennt fired unexpectedly Reached unreachable code
PASS <script> error: Move parse-error external module script to iframe move-back
PASS window error: Move parse-error external module script to iframe move-back

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CONSOLE MESSAGE: SyntaxError: Left side of assignment is not a reference.
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move parse-error inline classic script to iframe move-back
FAIL <script> load: Move parse-error inline classic script to iframe move-back assert_unreached: Script load event fired unexpectedly Reached unreachable code
PASS <script> load: Move parse-error inline classic script to iframe move-back
PASS <script> error: Move parse-error inline classic script to iframe move-back
FAIL window error: Move parse-error inline classic script to iframe move-back assert_unreached: Window error event shouldn't fired on source window Reached unreachable code
PASS window error: Move parse-error inline classic script to iframe move-back

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CONSOLE MESSAGE: exec
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
FAIL Eval: Move success external classic script to iframe move-back assert_false: The script must not have executed in source window expected false got "executed"
FAIL <script> load: Move success external classic script to iframe move-back assert_unreached: Script load event fired unexpectedly Reached unreachable code
PASS Eval: Move success external classic script to iframe move-back
PASS <script> load: Move success external classic script to iframe move-back
PASS <script> error: Move success external classic script to iframe move-back
PASS window error: Move success external classic script to iframe move-back

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CONSOLE MESSAGE: TypeError: Requested module is not instantiated yet.
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
PASS Eval: Move success external module script to iframe move-back
FAIL <script> load: Move success external module script to iframe move-back assert_unreached: Script load event fired unexpectedly Reached unreachable code
PASS <script> load: Move success external module script to iframe move-back
PASS <script> error: Move success external module script to iframe move-back
FAIL window error: Move success external module script to iframe move-back assert_unreached: Window error event shouldn't fired on source window Reached unreachable code
PASS window error: Move success external module script to iframe move-back

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
CONSOLE MESSAGE: Not executing script because it moved between documents during fetching


PASS Sanity check around top-level Window
FAIL Eval: Move success inline classic script to iframe move-back assert_false: The script must not have executed in source window expected false got "executed"
FAIL <script> load: Move success inline classic script to iframe move-back assert_unreached: Script load event fired unexpectedly Reached unreachable code
PASS Eval: Move success inline classic script to iframe move-back
PASS <script> load: Move success inline classic script to iframe move-back
PASS <script> error: Move success inline classic script to iframe move-back
PASS window error: Move success inline classic script to iframe move-back

27 changes: 27 additions & 0 deletions Source/WebCore/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
2021-08-11 Chris Dumez <cdumez@apple.com>

Stop evaluating <script>s moved between Documents during fetching
https://bugs.webkit.org/show_bug.cgi?id=202714
<rdar://problem/56208425>

Reviewed by Geoffrey Garen.

Stop evaluating <script>s moved between Documents during fetching:
- https://github.com/whatwg/html/issues/2469
- https://github.com/whatwg/html/pull/2673

Both Firefox and Chrome already behave this way.

No new tests, rebaselined existing tests.

* dom/ScriptElement.cpp:
(WebCore::ScriptElement::prepareScript):
Set the element's preparation-time document to its node document, as per:
- https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script (step 11)

(WebCore::ScriptElement::executePendingScript):
If scriptElement's preparation-time document is not equal to scriptElement's node document, then return, as per:
- https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-block (step 2)

* dom/ScriptElement.h:

2021-08-11 Youenn Fablet <youenn@apple.com>

Media element is not always autoplaying when going from background to foreground if it is initially not in viewport
Expand Down
7 changes: 7 additions & 0 deletions Source/WebCore/dom/ScriptElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ bool ScriptElement::prepareScript(const TextPosition& scriptStartPosition, Legac
if (scriptType == ScriptType::Classic && hasNoModuleAttribute())
return false;

m_preparationTimeDocumentIdentifier = document.identifier();

if (!document.frame()->script().canExecuteScripts(AboutToExecuteScript))
return false;

Expand Down Expand Up @@ -451,6 +453,11 @@ void ScriptElement::executeScriptAndDispatchEvent(LoadableScript& loadableScript

void ScriptElement::executePendingScript(PendingScript& pendingScript)
{
if (m_element.document().identifier() != m_preparationTimeDocumentIdentifier) {
m_element.document().addConsoleMessage(MessageSource::Security, MessageLevel::Error, "Not executing script because it moved between documents during fetching"_s);
return;
}

if (auto* loadableScript = pendingScript.loadableScript())
executeScriptAndDispatchEvent(*loadableScript);
else {
Expand Down
4 changes: 4 additions & 0 deletions Source/WebCore/dom/ScriptElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#pragma once

#include "ContainerNode.h"
#include "DocumentIdentifier.h"
#include "LoadableScript.h"
#include "ReferrerPolicy.h"
#include "UserGestureIndicator.h"
Expand Down Expand Up @@ -136,6 +137,9 @@ class ScriptElement {
String m_fallbackCharacterEncoding;
RefPtr<LoadableScript> m_loadableScript;

// https://html.spec.whatwg.org/multipage/scripting.html#preparation-time-document
DocumentIdentifier m_preparationTimeDocumentIdentifier;

MonotonicTime m_creationTime;
RefPtr<UserGestureToken> m_userGestureToken;
};
Expand Down

0 comments on commit 4008034

Please sign in to comment.