Title: [224698] trunk/Source
Revision
224698
Author
bfulg...@apple.com
Date
2017-11-10 11:06:50 -0800 (Fri, 10 Nov 2017)

Log Message

Ignore HSTS for partitioned, cross-origin subresource requests
https://bugs.webkit.org/show_bug.cgi?id=178993
<rdar://problem/34962462>

Patch by John Wilander <wilan...@apple.com> on 2017-11-10
Reviewed by Brent Fulgham.

Source/WebCore:

No new tests. HSTS is not supported in layout tests.
Tested manually.

* platform/network/mac/WebCoreURLResponse.mm:
(WebCore::synthesizeRedirectResponseIfNecessary):

Source/WebCore/PAL:

* pal/spi/cf/CFNetworkSPI.h:
    Added
    - (BOOL)_schemeWasUpgradedDueToDynamicHSTS
    - (BOOL)_preventHSTSStorage
    - (BOOL)_ignoreHSTS
    - (void)_setPreventHSTSStorage:(BOOL)preventHSTSStorage
    - (void)_setIgnoreHSTS:(BOOL)ignoreHSTS

Source/WebKit:

* NetworkProcess/cocoa/NetworkSessionCocoa.mm:
(downgradeRequest):
    Convenience function to downgrade a request if
    CFNetwork as already upgraded it during
    canonicalization. This allows the rest of 
    WebKit's processing to function, such as UIR
    and mixed content blocking.
(updateIgnoreStrictTransportSecuritySettingIfNecessary):
    Adds and removed the ignore request accordingly.
(-[WKNetworkSessionDelegate URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:]):
    Now asks CFNetwork to ignore HSTS on resource loads we
    partition cookies for.
(-[WKNetworkSessionDelegate URLSession:task:_schemeUpgraded:completionHandler:]):
    Now asks CFNetwork to ignore HSTS on resource loads we
    partition cookies for.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (224697 => 224698)


--- trunk/Source/WebCore/ChangeLog	2017-11-10 19:01:39 UTC (rev 224697)
+++ trunk/Source/WebCore/ChangeLog	2017-11-10 19:06:50 UTC (rev 224698)
@@ -1,3 +1,17 @@
+2017-11-10  John Wilander  <wilan...@apple.com>
+
+        Ignore HSTS for partitioned, cross-origin subresource requests
+        https://bugs.webkit.org/show_bug.cgi?id=178993
+        <rdar://problem/34962462>
+
+        Reviewed by Brent Fulgham.
+
+        No new tests. HSTS is not supported in layout tests.
+        Tested manually.
+
+        * platform/network/mac/WebCoreURLResponse.mm:
+        (WebCore::synthesizeRedirectResponseIfNecessary):
+
 2017-11-10  Alex Christensen  <achristen...@webkit.org>
 
         REGRESSION(r224267): WebViews scheduled with custom run loop modes don't load

Modified: trunk/Source/WebCore/PAL/ChangeLog (224697 => 224698)


--- trunk/Source/WebCore/PAL/ChangeLog	2017-11-10 19:01:39 UTC (rev 224697)
+++ trunk/Source/WebCore/PAL/ChangeLog	2017-11-10 19:06:50 UTC (rev 224698)
@@ -1,3 +1,20 @@
+2017-11-10  John Wilander  <wilan...@apple.com>
+
+        Ignore HSTS for partitioned, cross-origin subresource requests
+        https://bugs.webkit.org/show_bug.cgi?id=178993
+        <rdar://problem/34962462>
+
+        Reviewed by Brent Fulgham.
+
+        * pal/spi/cf/CFNetworkSPI.h:
+            Added
+            - (BOOL)_schemeWasUpgradedDueToDynamicHSTS
+            - (BOOL)_preventHSTSStorage
+            - (BOOL)_ignoreHSTS
+            - (void)_setPreventHSTSStorage:(BOOL)preventHSTSStorage
+            - (void)_setIgnoreHSTS:(BOOL)ignoreHSTS
+
+
 2017-11-09  Dan Bernstein  <m...@apple.com>
 
         [Xcode] An extra copy of FileSizeFormatter.h is installed in /usr/local/include

Modified: trunk/Source/WebCore/PAL/pal/spi/cf/CFNetworkSPI.h (224697 => 224698)


--- trunk/Source/WebCore/PAL/pal/spi/cf/CFNetworkSPI.h	2017-11-10 19:01:39 UTC (rev 224697)
+++ trunk/Source/WebCore/PAL/pal/spi/cf/CFNetworkSPI.h	2017-11-10 19:06:50 UTC (rev 224698)
@@ -30,6 +30,18 @@
 #include <CFNetwork/CFNetwork.h>
 #include <pal/spi/cf/CFNetworkConnectionCacheSPI.h>
 
+#if ((PLATFORM(MAC) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 101302 && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300) || (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 110200) || (PLATFORM(WATCHOS) && __WATCH_OS_VERSION_MIN_REQUIRED >= 40200) || (PLATFORM(TVOS) && __TV_OS_VERSION_MIN_REQUIRED >= 110200))
+#define USE_CFNETWORK_IGNORE_HSTS 1
+
+/* FIXME: Remove after rdar 35390452 is fixed: */
+#if PLATFORM(MAC) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
+#undef USE_CFNETWORK_IGNORE_HSTS
+#define USE_CFNETWORK_IGNORE_HSTS 0
+#endif
+
+#endif
+
+
 #if PLATFORM(WIN) || USE(APPLE_INTERNAL_SDK)
 
 #include <CFNetwork/CFHTTPCookiesPriv.h>
@@ -97,11 +109,20 @@
 - (id)_initWithCFURLRequest:(CFURLRequestRef)request;
 - (id)_propertyForKey:(NSString *)key;
 - (void)_setProperty:(id)value forKey:(NSString *)key;
+#if USE(CFNETWORK_IGNORE_HSTS)
+- (BOOL)_schemeWasUpgradedDueToDynamicHSTS;
+- (BOOL)_preventHSTSStorage;
+- (BOOL)_ignoreHSTS;
+#endif
 @end
 
 @interface NSMutableURLRequest ()
 - (void)setContentDispositionEncodingFallbackArray:(NSArray *)theEncodingFallbackArray;
 - (void)setBoundInterfaceIdentifier:(NSString *)identifier;
+#if USE(CFNETWORK_IGNORE_HSTS)
+- (void)_setPreventHSTSStorage:(BOOL)preventHSTSStorage;
+- (void)_setIgnoreHSTS:(BOOL)ignoreHSTS;
+#endif
 @end
 
 @interface NSURLResponse ()
@@ -324,4 +345,24 @@
 - (void)_setMIMEType:(NSString *)type;
 @end
 
+static bool schemeWasUpgradedDueToDynamicHSTS(NSURLRequest *request)
+{
+    if ([request respondsToSelector:@selector(_schemeWasUpgradedDueToDynamicHSTS)])
+        return [request performSelector:@selector(_schemeWasUpgradedDueToDynamicHSTS)];
+    return false;
+}
+
+static void setIgnoreHSTS(NSMutableURLRequest *request, bool ignoreHSTS)
+{
+    if ([request respondsToSelector:@selector(_setIgnoreHSTS)])
+        [request performSelector:@selector(_setIgnoreHSTS) withObject:[NSNumber numberWithBool:ignoreHSTS]];
+}
+
+static bool ignoreHSTS(NSURLRequest *request)
+{
+    if ([request respondsToSelector:@selector(_ignoreHSTS)])
+        return [request performSelector:@selector(_ignoreHSTS)];
+    return false;
+}
+
 #endif // defined(__OBJC__)

Modified: trunk/Source/WebCore/platform/network/mac/WebCoreURLResponse.mm (224697 => 224698)


--- trunk/Source/WebCore/platform/network/mac/WebCoreURLResponse.mm	2017-11-10 19:01:39 UTC (rev 224697)
+++ trunk/Source/WebCore/platform/network/mac/WebCoreURLResponse.mm	2017-11-10 19:06:50 UTC (rev 224698)
@@ -31,6 +31,7 @@
 
 #import "MIMETypeRegistry.h"
 #import "UTIUtilities.h"
+#import <pal/spi/cf/CFNetworkSPI.h>
 #import <wtf/Assertions.h>
 #import <wtf/RetainPtr.h>
 
@@ -332,7 +333,7 @@
     if (redirectResponse)
         return redirectResponse;
 
-    if ([[[newRequest URL] scheme] isEqualToString:[[currentRequest URL] scheme]])
+    if ([[[newRequest URL] scheme] isEqualToString:[[currentRequest URL] scheme]] && !schemeWasUpgradedDueToDynamicHSTS(newRequest))
         return nil;
 
     // If the new request is a different protocol than the current request, synthesize a redirect response.

Modified: trunk/Source/WebKit/ChangeLog (224697 => 224698)


--- trunk/Source/WebKit/ChangeLog	2017-11-10 19:01:39 UTC (rev 224697)
+++ trunk/Source/WebKit/ChangeLog	2017-11-10 19:06:50 UTC (rev 224698)
@@ -1,3 +1,27 @@
+2017-11-10  John Wilander  <wilan...@apple.com>
+
+        Ignore HSTS for partitioned, cross-origin subresource requests
+        https://bugs.webkit.org/show_bug.cgi?id=178993
+        <rdar://problem/34962462>
+
+        Reviewed by Brent Fulgham.
+
+        * NetworkProcess/cocoa/NetworkSessionCocoa.mm:
+        (downgradeRequest):
+            Convenience function to downgrade a request if
+            CFNetwork as already upgraded it during
+            canonicalization. This allows the rest of 
+            WebKit's processing to function, such as UIR
+            and mixed content blocking.
+        (updateIgnoreStrictTransportSecuritySettingIfNecessary):
+            Adds and removed the ignore request accordingly.
+        (-[WKNetworkSessionDelegate URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:]):
+            Now asks CFNetwork to ignore HSTS on resource loads we
+            partition cookies for.
+        (-[WKNetworkSessionDelegate URLSession:task:_schemeUpgraded:completionHandler:]):
+            Now asks CFNetwork to ignore HSTS on resource loads we
+            partition cookies for.
+
 2017-11-10  Alex Christensen  <achristen...@webkit.org>
 
         _WKThumbnailView initialization with WKWebView needs to call correct initWithFrame

Modified: trunk/Source/WebKit/NetworkProcess/cocoa/NetworkSessionCocoa.mm (224697 => 224698)


--- trunk/Source/WebKit/NetworkProcess/cocoa/NetworkSessionCocoa.mm	2017-11-10 19:01:39 UTC (rev 224697)
+++ trunk/Source/WebKit/NetworkProcess/cocoa/NetworkSessionCocoa.mm	2017-11-10 19:06:50 UTC (rev 224698)
@@ -165,6 +165,46 @@
     completionHandler(WebCore::createHTTPBodyNSInputStream(*body).get());
 }
 
+#if USE(CFNETWORK_IGNORE_HSTS)
+static NSURLRequest* downgradeRequest(NSURLRequest *request)
+{
+    NSMutableURLRequest *nsMutableRequest = [[request mutableCopy] autorelease];
+    if ([nsMutableRequest.URL.scheme isEqualToString:@"https"]) {
+        NSURLComponents *components = [[NSURLComponents componentsWithURL:nsMutableRequest.URL resolvingAgainstBaseURL:NO] autorelease];
+        components.scheme = @"http";
+        [nsMutableRequest setURL:components.URL];
+        ASSERT([nsMutableRequest.URL.scheme isEqualToString:@"http"]);
+        return nsMutableRequest;
+    }
+
+    ASSERT_NOT_REACHED();
+    return request;
+}
+#endif
+
+static NSURLRequest* updateIgnoreStrictTransportSecuritySettingIfNecessary(NSURLRequest *request, bool shouldIgnoreHSTS)
+{
+#if USE(CFNETWORK_IGNORE_HSTS)
+    if ([request.URL.scheme isEqualToString:@"https"] && shouldIgnoreHSTS && ignoreHSTS(request)) {
+        // The request was upgraded for some other reason than HSTS.
+        // Don't ignore HSTS to avoid the risk of another downgrade.
+        NSMutableURLRequest *nsMutableRequest = [[request mutableCopy] autorelease];
+        setIgnoreHSTS(nsMutableRequest, false);
+        return nsMutableRequest;
+    }
+    
+    if ([request.URL.scheme isEqualToString:@"http"] && ignoreHSTS(request) != shouldIgnoreHSTS) {
+        NSMutableURLRequest *nsMutableRequest = [[request mutableCopy] autorelease];
+        setIgnoreHSTS(nsMutableRequest, shouldIgnoreHSTS);
+        return nsMutableRequest;
+    }
+#else
+    UNUSED_PARAM(shouldIgnoreHSTS);
+#endif
+
+    return request;
+}
+
 - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest *))completionHandler
 {
     auto taskIdentifier = task.taskIdentifier;
@@ -172,13 +212,26 @@
 
     if (auto* networkDataTask = [self existingTask:task]) {
         auto completionHandlerCopy = Block_copy(completionHandler);
-        networkDataTask->willPerformHTTPRedirection(response, request, [completionHandlerCopy, taskIdentifier](auto&& request) {
+
+        bool shouldIgnoreHSTS = false;
+#if USE(CFNETWORK_IGNORE_HSTS)
+        shouldIgnoreHSTS = schemeWasUpgradedDueToDynamicHSTS(request) && !(WebCore::NetworkStorageSession::storageSession(_session->sessionID())->cookieStoragePartition(request)).isEmpty();
+        if (shouldIgnoreHSTS) {
+            request = downgradeRequest(request);
+            ASSERT([request.URL.scheme isEqualToString:@"http"]);
+            LOG(NetworkSession, "%llu Downgraded %s from https to http", taskIdentifier, request.URL.absoluteString.UTF8String);
+        }
+#endif
+
+        networkDataTask->willPerformHTTPRedirection(response, request, [completionHandlerCopy, taskIdentifier, shouldIgnoreHSTS](auto&& request) {
 #if !LOG_DISABLED
             LOG(NetworkSession, "%llu willPerformHTTPRedirection completionHandler (%s)", taskIdentifier, request.url().string().utf8().data());
 #else
             UNUSED_PARAM(taskIdentifier);
 #endif
-            completionHandlerCopy(request.nsURLRequest(WebCore::UpdateHTTPBody));
+            auto nsRequest = request.nsURLRequest(WebCore::UpdateHTTPBody);
+            nsRequest = updateIgnoreStrictTransportSecuritySettingIfNecessary(nsRequest, shouldIgnoreHSTS);
+            completionHandlerCopy(nsRequest);
             Block_release(completionHandlerCopy);
         });
     } else {
@@ -191,16 +244,28 @@
 {
     auto taskIdentifier = task.taskIdentifier;
     LOG(NetworkSession, "%llu _schemeUpgraded %s", taskIdentifier, request.URL.absoluteString.UTF8String);
-    
+
+    bool shouldIgnoreHSTS = false;
+#if USE(CFNETWORK_IGNORE_HSTS)
+    shouldIgnoreHSTS = schemeWasUpgradedDueToDynamicHSTS(request) && !(WebCore::NetworkStorageSession::storageSession(_session->sessionID())->cookieStoragePartition(request)).isEmpty();
+    if (shouldIgnoreHSTS) {
+        request = downgradeRequest(request);
+        ASSERT([request.URL.scheme isEqualToString:@"http"]);
+        LOG(NetworkSession, "%llu Downgraded %s from https to http", taskIdentifier, request.URL.absoluteString.UTF8String);
+    }
+#endif
+
     if (auto* networkDataTask = [self existingTask:task]) {
         auto completionHandlerCopy = Block_copy(completionHandler);
-        networkDataTask->willPerformHTTPRedirection(WebCore::synthesizeRedirectResponseIfNecessary([task currentRequest], request, nil), request, [completionHandlerCopy, taskIdentifier](auto&& request) {
+        networkDataTask->willPerformHTTPRedirection(WebCore::synthesizeRedirectResponseIfNecessary([task currentRequest], request, nil), request, [completionHandlerCopy, taskIdentifier, shouldIgnoreHSTS](auto&& request) {
 #if !LOG_DISABLED
             LOG(NetworkSession, "%llu _schemeUpgraded completionHandler (%s)", taskIdentifier, request.url().string().utf8().data());
 #else
             UNUSED_PARAM(taskIdentifier);
 #endif
-            completionHandlerCopy(request.nsURLRequest(WebCore::UpdateHTTPBody));
+            auto nsRequest = request.nsURLRequest(WebCore::UpdateHTTPBody);
+            nsRequest = updateIgnoreStrictTransportSecuritySettingIfNecessary(nsRequest, shouldIgnoreHSTS);
+            completionHandlerCopy(nsRequest);
             Block_release(completionHandlerCopy);
         });
     } else {
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to