Bug 220658 - A DOMWindow should not be reused if its JSDOMWindow has been created
Summary: A DOMWindow should not be reused if its JSDOMWindow has been created
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Page Loading (show other bugs)
Version: WebKit Local Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: youenn fablet
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-01-15 08:32 PST by youenn fablet
Modified: 2021-01-20 03:32 PST (History)
9 users (show)

See Also:


Attachments
WIP (2.36 KB, patch)
2021-01-15 10:46 PST, youenn fablet
no flags Details | Formatted Diff | Diff
Patch (8.46 KB, patch)
2021-01-18 00:42 PST, youenn fablet
no flags Details | Formatted Diff | Diff
Patch for landing (8.46 KB, patch)
2021-01-20 01:06 PST, youenn fablet
ews-feeder: commit-queue-
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description youenn fablet 2021-01-15 08:32:26 PST
WindowProxy should be updated when the DOMWindow is transitioned from one document to another
Comment 1 youenn fablet 2021-01-15 08:32:48 PST
<rdar://problem/70335075>
Comment 2 youenn fablet 2021-01-15 10:46:31 PST
Created attachment 417715 [details]
WIP
Comment 3 youenn fablet 2021-01-15 10:50:25 PST
One repro step for the problem:
- Update MobileMiniBrowser to be limitsNavigationsToAppBoundDomains and add webkit.org  to the allowed list
- Add a navigation delegate to block about:blank and instead make it load another secure domain than webkit.org.
- Load webkit.org and attach an inspector to it
- Through inspector, trigger loading to about:blank
- This should go to the other domain page.

At that point, the JSDOMWindow will be created before having the document with the secure context. Window properties that are secure will not show up.

I am not sure how to further reduce the test case.
It seems this is triggered by limitsNavigationsToAppBoundDomains so I am unsure whether this is the exact right fix.
Comment 4 Geoffrey Garen 2021-01-15 14:24:21 PST
I'm not sure why limitsNavigationsToAppBoundDomains specifically triggers this; it seems like any app or extension use of JavaScript in the page could potentially trigger this.

Code change seems OK to me; but EWS is angry.
Comment 5 youenn fablet 2021-01-18 00:28:47 PST
(In reply to Geoffrey Garen from comment #4)
> I'm not sure why limitsNavigationsToAppBoundDomains specifically triggers
> this; it seems like any app or extension use of JavaScript in the page could
> potentially trigger this.

Right, my testing was wrong without limitsNavigationsToAppBoundDomains.
The bug can be done with early JS injection, prior to the document having its security origin and URL set. Web Inspector also triggers this.
Comment 6 youenn fablet 2021-01-18 00:30:00 PST
> Code change seems OK to me; but EWS is angry.

We need to handle the window.open in same process case where the window document secure context is set by its opener, hence the wrapping is done correctly.
Comment 7 youenn fablet 2021-01-18 00:42:57 PST
Created attachment 417810 [details]
Patch
Comment 8 youenn fablet 2021-01-18 00:47:24 PST
(In reply to youenn fablet from comment #7)
> Created attachment 417810 [details]
> Patch

Patch covers the case of isSecureContext.
Patch also covers most of the Quirked properties as well, but it is difficult to ensure this in the future as this might depend on how quirks will implement their checks.
Comment 9 youenn fablet 2021-01-18 02:12:08 PST
Comment on attachment 417810 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=417810&action=review

> Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewEvaluateJavaScript.mm:822
> +    [webView stringByEvaluatingJavaScript:@""];

Maybe there is a known pattern, but it might be difficult for developers to know when they can evaluate JS in a page being loaded, before page JS starts executing but after the DOMWindow has its final document.
Maybe we should consider delaying evaluation of this JS a bit so that the DOMWindow would have a correct SecurityOrigin.
Also, the transition of the window to a new document is clearing location or navigator, which might have subtle and potentially racy effects.
Comment 10 Geoffrey Garen 2021-01-19 10:39:56 PST
Comment on attachment 417810 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=417810&action=review

r=me

> Source/WebCore/page/DOMWindow.h:487
> +    bool m_wasWrappedWithoutInitializedDocument { false };

I would call this wasWrappedWithoutInitializedSecurityOrigin, since it's the security origin that was not initialized.
Comment 11 youenn fablet 2021-01-20 01:06:24 PST
Created attachment 417950 [details]
Patch for landing
Comment 12 EWS 2021-01-20 02:41:35 PST
Committed r271642: <https://trac.webkit.org/changeset/271642>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 417950 [details].