-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WebKit] Add an option key to prevent network loads when serializing …
…attributed strings https://bugs.webkit.org/show_bug.cgi?id=264745 rdar://118309399 Reviewed by Tim Horton. Add support for a `NSAttributedStringDocumentReadingOptionKey` which allows clients to serialize attributed strings from web content, without loading any requests over the network. This is particularly ideal for clients such as UIFoundation, when serializing HTML markup and/or web archive data, which otherwise has the potential to trigger arbitrary resource loads upon conversion. Note that we add an opt-in flag for network loads, because we want to eventually default to *not* loading content from over the network when serializing attributed strings. Given this eventual plan, it makes more sense for an app to need to explicitly opt _in_ to network loads, rather than opt out. Test: WKWebView.DoNotAllowNetworkLoads * Source/WebKit/UIProcess/API/Cocoa/NSAttributedString.mm: (+[_WKAttributedStringWebViewCache configuration]): Respect the new `_WKAllowNetworkLoadsOption` option by setting `_allowedNetworkHosts` to an empty set on the configuration, if the value is false-y. (+[_WKAttributedStringWebViewCache maybeUpdateShouldAllowNetworkLoads:]): (+[_WKAttributedStringWebViewCache maybeConsumeBundlePaths:]): (+[_WKAttributedStringWebViewCache invalidateGlobalConfigurationIfNeeded:]): Refactor this logic to update the global configuration (and clear any cached web views if needed), if either network load disablement or the allowed file URL list changes. (+[NSAttributedString _loadFromHTMLWithOptions:contentLoader:completionHandler:]): * Source/WebKit/UIProcess/API/Cocoa/NSAttributedStringPrivate.h: * Tools/TestWebKitAPI/SourcesCocoa.txt: * Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj: * Tools/TestWebKitAPI/Tests/WebKitCocoa/ResourceLoadDelegate.mm: Lift the `TestResourceLoadDelegate` class out of this API test and into a separate helper file, so that we can use it in a different API test suite (see below). (-[TestResourceLoadDelegate webView:resourceLoad:didSendRequest:]): Deleted. (-[TestResourceLoadDelegate webView:resourceLoad:didPerformHTTPRedirection:newRequest:]): Deleted. (-[TestResourceLoadDelegate webView:resourceLoad:didReceiveChallenge:]): Deleted. (-[TestResourceLoadDelegate webView:resourceLoad:didReceiveResponse:]): Deleted. (-[TestResourceLoadDelegate webView:resourceLoad:didCompleteWithError:response:]): Deleted. * Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebViewGetContents.mm: Add a new API test to exercise the case where `_WKAllowNetworkLoadsOption` is set to `@NO`, by adding a resource load delegate to the web view to listen for any attempted network access. When this new option is set, we should never get a call to the `didSendRequest:` delegate method. * Tools/TestWebKitAPI/cocoa/TestResourceLoadDelegate.h: Added. * Tools/TestWebKitAPI/cocoa/TestResourceLoadDelegate.mm: Added. (-[TestResourceLoadDelegate webView:resourceLoad:didSendRequest:]): (-[TestResourceLoadDelegate webView:resourceLoad:didPerformHTTPRedirection:newRequest:]): (-[TestResourceLoadDelegate webView:resourceLoad:didReceiveChallenge:]): (-[TestResourceLoadDelegate webView:resourceLoad:didReceiveResponse:]): (-[TestResourceLoadDelegate webView:resourceLoad:didCompleteWithError:response:]): Canonical link: https://commits.webkit.org/270753@main
- Loading branch information
Showing
8 changed files
with
191 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (C) 2023 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#import <WebKit/WebKit.h> | ||
#import <WebKit/_WKResourceLoadDelegate.h> | ||
|
||
@class _WKResourceLoadInfo; | ||
|
||
@interface TestResourceLoadDelegate : NSObject <_WKResourceLoadDelegate> | ||
|
||
@property (nonatomic, copy) void (^didSendRequest)(WKWebView *, _WKResourceLoadInfo *, NSURLRequest *); | ||
@property (nonatomic, copy) void (^didPerformHTTPRedirection)(WKWebView *, _WKResourceLoadInfo *, NSURLResponse *, NSURLRequest *); | ||
@property (nonatomic, copy) void (^didReceiveChallenge)(WKWebView *, _WKResourceLoadInfo *, NSURLAuthenticationChallenge *); | ||
@property (nonatomic, copy) void (^didReceiveResponse)(WKWebView *, _WKResourceLoadInfo *, NSURLResponse *); | ||
@property (nonatomic, copy) void (^didCompleteWithError)(WKWebView *, _WKResourceLoadInfo *, NSError *, NSURLResponse *); | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (C) 2023 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. | ||
*/ | ||
|
||
#import "config.h" | ||
#import "TestResourceLoadDelegate.h" | ||
|
||
#import <WebKit/_WKResourceLoadInfo.h> | ||
|
||
@implementation TestResourceLoadDelegate | ||
|
||
- (void)webView:(WKWebView *)webView resourceLoad:(_WKResourceLoadInfo *)resourceLoad didSendRequest:(NSURLRequest *)request | ||
{ | ||
if (_didSendRequest) | ||
_didSendRequest(webView, resourceLoad, request); | ||
} | ||
|
||
- (void)webView:(WKWebView *)webView resourceLoad:(_WKResourceLoadInfo *)resourceLoad didPerformHTTPRedirection:(NSURLResponse *)response newRequest:(NSURLRequest *)request | ||
{ | ||
if (_didPerformHTTPRedirection) | ||
_didPerformHTTPRedirection(webView, resourceLoad, response, request); | ||
} | ||
|
||
- (void)webView:(WKWebView *)webView resourceLoad:(_WKResourceLoadInfo *)resourceLoad didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge | ||
{ | ||
if (_didReceiveChallenge) | ||
_didReceiveChallenge(webView, resourceLoad, challenge); | ||
} | ||
|
||
- (void)webView:(WKWebView *)webView resourceLoad:(_WKResourceLoadInfo *)resourceLoad didReceiveResponse:(NSURLResponse *)response | ||
{ | ||
if (_didReceiveResponse) | ||
_didReceiveResponse(webView, resourceLoad, response); | ||
} | ||
|
||
- (void)webView:(WKWebView *)webView resourceLoad:(_WKResourceLoadInfo *)resourceLoad didCompleteWithError:(NSError *)error response:(NSURLResponse *)response | ||
{ | ||
if (_didCompleteWithError) | ||
_didCompleteWithError(webView, resourceLoad, error, response); | ||
} | ||
|
||
@end |