Title: [234211] trunk
Revision
234211
Author
[email protected]
Date
2018-07-25 13:33:17 -0700 (Wed, 25 Jul 2018)

Log Message

navigator.userAgent may return outdated value after webView.customUserAgent is set
https://bugs.webkit.org/show_bug.cgi?id=188009
<rdar://problem/42566456>

Reviewed by Alex Christensen.

Source/WebCore:

Invalidate the navigator.userAgent cache whenever the user agent gets changed to avoid exposing
outdated values to _javascript_.

* page/Navigator.cpp:
(WebCore::Navigator::userAgentChanged):
* page/Navigator.h:
* page/NavigatorID.idl:
* page/Page.cpp:
(WebCore::Page::userAgentChanged):
* page/Page.h:

Source/WebKit:

Let the page know when the user agent changes.

* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::setUserAgent):

Source/WebKitLegacy/mac:

Let the page know when the user agent changes.

* WebView/WebView.mm:
(-[WebView _preferencesChanged:]):
(-[WebView setApplicationNameForUserAgent:]):
(-[WebView _invalidateUserAgentCache]):
(-[WebView setCustomUserAgent:]):
* WebView/WebViewInternal.h:

Tools:

Add API test coverage.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKitCocoa/CustomUserAgent.mm: Added.
(TEST):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (234210 => 234211)


--- trunk/Source/WebCore/ChangeLog	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Source/WebCore/ChangeLog	2018-07-25 20:33:17 UTC (rev 234211)
@@ -1,3 +1,22 @@
+2018-07-25  Chris Dumez  <[email protected]>
+
+        navigator.userAgent may return outdated value after webView.customUserAgent is set
+        https://bugs.webkit.org/show_bug.cgi?id=188009
+        <rdar://problem/42566456>
+
+        Reviewed by Alex Christensen.
+
+        Invalidate the navigator.userAgent cache whenever the user agent gets changed to avoid exposing
+        outdated values to _javascript_.
+
+        * page/Navigator.cpp:
+        (WebCore::Navigator::userAgentChanged):
+        * page/Navigator.h:
+        * page/NavigatorID.idl:
+        * page/Page.cpp:
+        (WebCore::Page::userAgentChanged):
+        * page/Page.h:
+
 2018-07-25  David Fenton  <[email protected]>
 
         Unreviewed, rolling out r234187.

Modified: trunk/Source/WebCore/page/Navigator.cpp (234210 => 234211)


--- trunk/Source/WebCore/page/Navigator.cpp	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Source/WebCore/page/Navigator.cpp	2018-07-25 20:33:17 UTC (rev 234211)
@@ -84,6 +84,11 @@
     return m_userAgent;
 }
 
+void Navigator::userAgentChanged()
+{
+    m_userAgent = String();
+}
+
 bool Navigator::onLine() const
 {
     return platformStrategies()->loaderStrategy()->isOnLine();

Modified: trunk/Source/WebCore/page/Navigator.h (234210 => 234211)


--- trunk/Source/WebCore/page/Navigator.h	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Source/WebCore/page/Navigator.h	2018-07-25 20:33:17 UTC (rev 234211)
@@ -40,6 +40,7 @@
     bool cookieEnabled() const;
     bool javaEnabled() const;
     const String& userAgent() const final;
+    void userAgentChanged();
     bool onLine() const final;
     
 #if PLATFORM(IOS)

Modified: trunk/Source/WebCore/page/NavigatorID.idl (234210 => 234211)


--- trunk/Source/WebCore/page/NavigatorID.idl	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Source/WebCore/page/NavigatorID.idl	2018-07-25 20:33:17 UTC (rev 234211)
@@ -35,7 +35,7 @@
     readonly attribute DOMString platform;
     readonly attribute DOMString product;
     [Exposed=Window] readonly attribute DOMString productSub;
-    [CachedAttribute] readonly attribute DOMString userAgent;
+    readonly attribute DOMString userAgent;
     [Exposed=Window] readonly attribute DOMString vendor;
     [Exposed=Window] readonly attribute DOMString vendorSub;
 };

Modified: trunk/Source/WebCore/page/Page.cpp (234210 => 234211)


--- trunk/Source/WebCore/page/Page.cpp	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Source/WebCore/page/Page.cpp	2018-07-25 20:33:17 UTC (rev 234211)
@@ -1238,6 +1238,17 @@
     return m_userStyleSheet;
 }
 
+void Page::userAgentChanged()
+{
+    for (auto* frame = &m_mainFrame.get(); frame; frame = frame->tree().traverseNext()) {
+        auto* window = frame->window();
+        if (!window)
+            continue;
+        if (auto* navigator = window->optionalNavigator())
+            navigator->userAgentChanged();
+    }
+}
+
 void Page::invalidateStylesForAllLinks()
 {
     for (Frame* frame = &m_mainFrame.get(); frame; frame = frame->tree().traverseNext()) {

Modified: trunk/Source/WebCore/page/Page.h (234210 => 234211)


--- trunk/Source/WebCore/page/Page.h	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Source/WebCore/page/Page.h	2018-07-25 20:33:17 UTC (rev 234211)
@@ -439,6 +439,8 @@
     void userStyleSheetLocationChanged();
     const String& userStyleSheet() const;
 
+    WEBCORE_EXPORT void userAgentChanged();
+
     void dnsPrefetchingStateChanged();
     void storageBlockingStateChanged();
 

Modified: trunk/Source/WebKit/ChangeLog (234210 => 234211)


--- trunk/Source/WebKit/ChangeLog	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Source/WebKit/ChangeLog	2018-07-25 20:33:17 UTC (rev 234211)
@@ -1,3 +1,16 @@
+2018-07-25  Chris Dumez  <[email protected]>
+
+        navigator.userAgent may return outdated value after webView.customUserAgent is set
+        https://bugs.webkit.org/show_bug.cgi?id=188009
+        <rdar://problem/42566456>
+
+        Reviewed by Alex Christensen.
+
+        Let the page know when the user agent changes.
+
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::setUserAgent):
+
 2018-07-25  Alex Christensen  <[email protected]>
 
         Use CompletionHandler for policy decisions

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp (234210 => 234211)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2018-07-25 20:33:17 UTC (rev 234211)
@@ -2909,7 +2909,13 @@
     
 void WebPage::setUserAgent(const String& userAgent)
 {
+    if (m_userAgent == userAgent)
+        return;
+
     m_userAgent = userAgent;
+
+    if (m_page)
+        m_page->userAgentChanged();
 }
 
 void WebPage::suspendActiveDOMObjectsAndAnimations()

Modified: trunk/Source/WebKitLegacy/mac/ChangeLog (234210 => 234211)


--- trunk/Source/WebKitLegacy/mac/ChangeLog	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Source/WebKitLegacy/mac/ChangeLog	2018-07-25 20:33:17 UTC (rev 234211)
@@ -1,3 +1,20 @@
+2018-07-25  Chris Dumez  <[email protected]>
+
+        navigator.userAgent may return outdated value after webView.customUserAgent is set
+        https://bugs.webkit.org/show_bug.cgi?id=188009
+        <rdar://problem/42566456>
+
+        Reviewed by Alex Christensen.
+
+        Let the page know when the user agent changes.
+
+        * WebView/WebView.mm:
+        (-[WebView _preferencesChanged:]):
+        (-[WebView setApplicationNameForUserAgent:]):
+        (-[WebView _invalidateUserAgentCache]):
+        (-[WebView setCustomUserAgent:]):
+        * WebView/WebViewInternal.h:
+
 2018-07-24  Tim Horton  <[email protected]>
 
         Enable Web Content Filtering on watchOS

Modified: trunk/Source/WebKitLegacy/mac/WebView/WebView.mm (234210 => 234211)


--- trunk/Source/WebKitLegacy/mac/WebView/WebView.mm	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Source/WebKitLegacy/mac/WebView/WebView.mm	2018-07-25 20:33:17 UTC (rev 234211)
@@ -2795,7 +2795,7 @@
 {    
     ASSERT(preferences == [self preferences]);
     if (!_private->userAgentOverridden)
-        _private->userAgent = String();
+        [self _invalidateUserAgentCache];
 
     // Cache this value so we don't have to read NSUserDefaults on each page load
     _private->useSiteSpecificSpoofing = [preferences _useSiteSpecificSpoofing];
@@ -6580,9 +6580,19 @@
     [_private->applicationNameForUserAgent release];
     _private->applicationNameForUserAgent = name;
     if (!_private->userAgentOverridden)
-        _private->userAgent = String();
+        [self _invalidateUserAgentCache];
 }
 
+- (void)_invalidateUserAgentCache
+{
+    if (_private->userAgent.isNull())
+        return;
+
+    _private->userAgent = String();
+    if (_private->page)
+        _private->page->userAgentChanged();
+}
+
 - (NSString *)applicationNameForUserAgent
 {
     return [[_private->applicationNameForUserAgent retain] autorelease];
@@ -6590,6 +6600,7 @@
 
 - (void)setCustomUserAgent:(NSString *)userAgentString
 {
+    [self _invalidateUserAgentCache];
     _private->userAgent = userAgentString;
     _private->userAgentOverridden = userAgentString != nil;
 }

Modified: trunk/Source/WebKitLegacy/mac/WebView/WebViewInternal.h (234210 => 234211)


--- trunk/Source/WebKitLegacy/mac/WebView/WebViewInternal.h	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Source/WebKitLegacy/mac/WebView/WebViewInternal.h	2018-07-25 20:33:17 UTC (rev 234211)
@@ -267,6 +267,7 @@
 #endif
 
 - (void)_preferencesChanged:(WebPreferences *)preferences;
+- (void)_invalidateUserAgentCache;
 
 #if ENABLE(VIDEO) && defined(__cplusplus)
 - (void)_enterVideoFullscreenForVideoElement:(WebCore::HTMLVideoElement*)videoElement mode:(WebCore::HTMLMediaElementEnums::VideoFullscreenMode)mode;

Modified: trunk/Tools/ChangeLog (234210 => 234211)


--- trunk/Tools/ChangeLog	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Tools/ChangeLog	2018-07-25 20:33:17 UTC (rev 234211)
@@ -1,3 +1,17 @@
+2018-07-25  Chris Dumez  <[email protected]>
+
+        navigator.userAgent may return outdated value after webView.customUserAgent is set
+        https://bugs.webkit.org/show_bug.cgi?id=188009
+        <rdar://problem/42566456>
+
+        Reviewed by Alex Christensen.
+
+        Add API test coverage.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKitCocoa/CustomUserAgent.mm: Added.
+        (TEST):
+
 2018-07-25  Charlie Turner  <[email protected]>
 
         [Flatpak] Pass more environment variables to sandbox

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (234210 => 234211)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2018-07-25 18:32:00 UTC (rev 234210)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2018-07-25 20:33:17 UTC (rev 234211)
@@ -160,6 +160,7 @@
 		46397B951DC2C850009A78AE /* DOMNode.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46397B941DC2C850009A78AE /* DOMNode.mm */; };
 		4647B1261EBA3B850041D7EF /* ProcessDidTerminate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4647B1251EBA3B730041D7EF /* ProcessDidTerminate.cpp */; };
 		466C3843210637DE006A88DE /* notify-resourceLoadObserver.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 466C3842210637CE006A88DE /* notify-resourceLoadObserver.html */; };
+		46A911592108E6780078D40D /* CustomUserAgent.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46A911582108E66B0078D40D /* CustomUserAgent.mm */; };
 		46AE5A3720F9066D00E0873E /* SimpleServiceWorkerRegistrations-3.sqlite3 in Copy Resources */ = {isa = PBXBuildFile; fileRef = 4656A75720F9054F0002E21F /* SimpleServiceWorkerRegistrations-3.sqlite3 */; };
 		46C519DA1D355AB200DAA51A /* LocalStorageNullEntries.mm in Sources */ = {isa = PBXBuildFile; fileRef = 46C519D81D355A7300DAA51A /* LocalStorageNullEntries.mm */; };
 		46C519E61D3563FD00DAA51A /* LocalStorageNullEntries.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 46C519E21D35629600DAA51A /* LocalStorageNullEntries.html */; };
@@ -1401,6 +1402,7 @@
 		4647B1251EBA3B730041D7EF /* ProcessDidTerminate.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ProcessDidTerminate.cpp; sourceTree = "<group>"; };
 		4656A75720F9054F0002E21F /* SimpleServiceWorkerRegistrations-3.sqlite3 */ = {isa = PBXFileReference; lastKnownFileType = file; path = "SimpleServiceWorkerRegistrations-3.sqlite3"; sourceTree = "<group>"; };
 		466C3842210637CE006A88DE /* notify-resourceLoadObserver.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "notify-resourceLoadObserver.html"; sourceTree = "<group>"; };
+		46A911582108E66B0078D40D /* CustomUserAgent.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = CustomUserAgent.mm; sourceTree = "<group>"; };
 		46C519D81D355A7300DAA51A /* LocalStorageNullEntries.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = LocalStorageNullEntries.mm; sourceTree = "<group>"; };
 		46C519E21D35629600DAA51A /* LocalStorageNullEntries.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = LocalStorageNullEntries.html; sourceTree = "<group>"; };
 		46C519E31D35629600DAA51A /* LocalStorageNullEntries.localstorage */ = {isa = PBXFileReference; lastKnownFileType = file; path = LocalStorageNullEntries.localstorage; sourceTree = "<group>"; };
@@ -2255,6 +2257,7 @@
 				9B1056411F9045C700D5583F /* CopyHTML.mm */,
 				9999108A1F393C8B008AD455 /* Copying.mm */,
 				9B7A37C21F8AEBA5004AA228 /* CopyURL.mm */,
+				46A911582108E66B0078D40D /* CustomUserAgent.mm */,
 				2DC4CF761D2D9DD800ECCC94 /* DataDetection.mm */,
 				518EE51C20A78D3300E024F3 /* DecidePolicyForNavigationAction.mm */,
 				CEA7F57B20895F5B0078EF6E /* DidResignInputElementStrongPasswordAppearance.mm */,
@@ -3639,6 +3642,7 @@
 				7CCE7F291A411B1000447C4C /* CustomProtocolsInvalidScheme.mm in Sources */,
 				7CCE7F2A1A411B1000447C4C /* CustomProtocolsSyncXHRTest.mm in Sources */,
 				7CCE7F2B1A411B1000447C4C /* CustomProtocolsTest.mm in Sources */,
+				46A911592108E6780078D40D /* CustomUserAgent.mm in Sources */,
 				751B05D61F8EAC410028A09E /* DatabaseTrackerTest.mm in Sources */,
 				2DC4CF771D2D9DD800ECCC94 /* DataDetection.mm in Sources */,
 				F4D4F3B61E4E2BCB00BB2767 /* DataInteractionSimulator.mm in Sources */,

Added: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/CustomUserAgent.mm (0 => 234211)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/CustomUserAgent.mm	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/CustomUserAgent.mm	2018-07-25 20:33:17 UTC (rev 234211)
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2018 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#if WK_API_ENABLED
+
+#import "PlatformUtilities.h"
+#import "TestNavigationDelegate.h"
+#import <WebKit/WebKit.h>
+#import <wtf/RetainPtr.h>
+
+static bool done = false;
+
+TEST(CustomUserAgent, UpdateCachedNavigatorUserAgent)
+{
+    auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
+    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
+
+    [webView _test_waitForDidFinishNavigation];
+
+    // Query navigator.userAgent once so it gets cached.
+    [webView evaluateJavaScript:@"navigator.userAgent;" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
+        ASSERT_TRUE(!error);
+        NSString *userAgent = (NSString *)response;
+        ASSERT_TRUE(!!userAgent);
+        // Override user agent with a custom one.
+        webView.get().customUserAgent = @"Custom UserAgent";
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+
+    // Query navigator.userAgent again to make sure its cache was invalidated and it returns the updated value.
+    [webView evaluateJavaScript:@"navigator.userAgent;" completionHandler:^(id _Nullable response, NSError * _Nullable error) {
+        ASSERT_TRUE(!error);
+        NSString *userAgent = (NSString *)response;
+        ASSERT_TRUE(!!userAgent);
+        EXPECT_WK_STREQ(@"Custom UserAgent", userAgent);
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+}
+
+#endif // WK_API_ENABLED
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to