Skip to content

Commit

Permalink
Main frame PDFs served with CSP sandbox header do not load
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=284594
rdar://141166987

Reviewed by NOBODY (OOPS!).

The PDF plugin is an internal WebKit implementation detail, and thus
should not be subjected to the CSP sandbox. This patch makes sure we
bypass the sandbox in SubframeLoader::pluginIsLoadable(). Note that we
only do so for main frame PDFs. The embedded PDF case's behavior is
directed by whatwg/html#3958, and the WPT
`html/semantics/embedded-content/the-iframe-element/sandbox_004.htm`.

Also, add two new API tests that assert correct loading behavior. The
latter enables UnifiedPDFPlugin while the former tests legacy PDF
plugin.

- ContentSecurityPolicy.LoadPDFWithSandboxCSPDirective
- UnifiedPDF.LoadPDFWithSandboxCSPDirective

The rest of the change involves adding new test helpers to facilitate
the API tests, TestWKWebView interface to sample colors (which, by the
way, should be adopted by many tests), and the fallout unified source
build fixes required for a clean build.

* Source/WebCore/html/HTMLPlugInImageElement.h:
* Source/WebCore/loader/SubframeLoader.cpp:
(WebCore::FrameLoader::SubframeLoader::pluginIsLoadable const):
(WebCore::FrameLoader::SubframeLoader::requestPlugin):
(WebCore::FrameLoader::SubframeLoader::pluginIsLoadable): Deleted.
* Source/WebCore/loader/SubframeLoader.h:
* Tools/TestWebKitAPI/SourcesCocoa.txt:
* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* Tools/TestWebKitAPI/Tests/WebKit/WKThumbnailView.mm:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/ContentSecurityPolicy.mm:
(TEST(ContentSecurityPolicy, LoadPDFWithSandboxCSPDirective)):
* Tools/TestWebKitAPI/Tests/WebKitCocoa/ContentSecurityPolicyTestHelpers.h: Copied from Tools/TestWebKitAPI/cocoa/CGImagePixelReader.h.
* Tools/TestWebKitAPI/Tests/WebKitCocoa/ContentSecurityPolicyTestHelpers.mm: Copied from Tools/TestWebKitAPI/cocoa/CGImagePixelReader.h.
(TestWebKitAPI::runLoadPDFWithSandboxCSPDirectiveTest):
* Tools/TestWebKitAPI/Tests/WebKitCocoa/CookieAcceptPolicy.mm:

* Tools/TestWebKitAPI/Tests/WebKitCocoa/DocumentEditingContext.mm:
(applyStyle): Deleted.
(applyAhemStyle): Deleted.
* Tools/TestWebKitAPI/Tests/WebKitCocoa/EditorStateTests.mm:
(): Deleted.
(TestWebKitAPI::applyAhemStyle): Deleted.

Fix ODR violation by pushing applyAhemStyle into specific namespaces.

* Tools/TestWebKitAPI/Tests/WebKitCocoa/ElementTargetingTests.mm:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/NavigationAction.mm:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/SiteIsolation.mm:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/TextWidth.mm:
* Tools/TestWebKitAPI/Tests/WebKitCocoa/UnifiedPDFTests.mm:
(TestWebKitAPI::UNIFIED_PDF_TEST):
(TestWebKitAPI::sampleColorsInWebView): Deleted.
* Tools/TestWebKitAPI/Tests/ios/KeyboardInputTestsIOS.mm:
* Tools/TestWebKitAPI/Tests/mac/WKWebViewMacEditingTests.mm:
* Tools/TestWebKitAPI/cocoa/CGImagePixelReader.cpp:
* Tools/TestWebKitAPI/cocoa/CGImagePixelReader.h:
* Tools/TestWebKitAPI/cocoa/HTTPServer.h:
(TestWebKitAPI::HTTPResponse::HTTPResponse):
* Tools/TestWebKitAPI/cocoa/TestWKWebView.h:
* Tools/TestWebKitAPI/cocoa/TestWKWebView.mm:
(-[TestWKWebView sampleColors]):
(-[TestWKWebView sampleColorsWithInterval:]):
  • Loading branch information
aprotyas committed Dec 18, 2024
1 parent cb10203 commit 2177892
Show file tree
Hide file tree
Showing 28 changed files with 455 additions and 295 deletions.
3 changes: 2 additions & 1 deletion Source/WebCore/html/HTMLPlugInImageElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class HTMLPlugInImageElement : public HTMLPlugInElement {

bool needsWidgetUpdate() const { return m_needsWidgetUpdate; }
void setNeedsWidgetUpdate(bool needsWidgetUpdate) { m_needsWidgetUpdate = needsWidgetUpdate; }

bool shouldBypassCSPForPDFPlugin(const String& contentType) const;

protected:
HTMLPlugInImageElement(const QualifiedName& tagName, Document&);
Expand All @@ -68,7 +70,6 @@ class HTMLPlugInImageElement : public HTMLPlugInElement {
private:
bool isPlugInImageElement() const final { return true; }

bool shouldBypassCSPForPDFPlugin(const String&) const;
bool canLoadPlugInContent(const String& relativeURL, const String& mimeType) const;
bool canLoadURL(const URL&) const;

Expand Down
7 changes: 4 additions & 3 deletions Source/WebCore/loader/SubframeLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ bool FrameLoader::SubframeLoader::resourceWillUsePlugin(const String& url, const
return shouldUsePlugin(completedURL, mimeType, false, useFallback);
}

bool FrameLoader::SubframeLoader::pluginIsLoadable(const URL& url)
bool FrameLoader::SubframeLoader::pluginIsLoadable(const URL& url, const HTMLPlugInImageElement& ownerElement, const String& mimeType) const
{
if (RefPtr document = m_frame->document()) {
if (document->isSandboxed(SandboxFlag::Plugins))
bool isFullMainFramePlugin = m_frame->isMainFrame() && dynamicDowncast<PluginDocument>(m_frame->document());
if (document->isSandboxed(SandboxFlag::Plugins) && !(isFullMainFramePlugin && ownerElement.shouldBypassCSPForPDFPlugin(mimeType)))
return false;

Ref securityOrigin = document->securityOrigin();
Expand Down Expand Up @@ -185,7 +186,7 @@ bool FrameLoader::SubframeLoader::requestPlugin(HTMLPlugInImageElement& ownerEle
if (!(m_frame->settings().legacyPluginQuirkForMailSignaturesEnabled() || MIMETypeRegistry::isApplicationPluginMIMEType(mimeType)))
return false;

if (!pluginIsLoadable(url))
if (!pluginIsLoadable(url, ownerElement, mimeType))
return false;

ASSERT(ownerElement.hasTagName(objectTag) || ownerElement.hasTagName(embedTag));
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/loader/SubframeLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class FrameLoader::SubframeLoader {
bool loadPlugin(HTMLPlugInImageElement&, const URL&, const String& mimeType, const Vector<AtomString>& paramNames, const Vector<AtomString>& paramValues, bool useFallback);

bool shouldUsePlugin(const URL&, const String& mimeType, bool hasFallback, bool& useFallback);
bool pluginIsLoadable(const URL&);
bool pluginIsLoadable(const URL&, const HTMLPlugInImageElement&, const String& mimeType) const;

URL completeURL(const String&) const;

Expand Down
1 change: 1 addition & 0 deletions Tools/TestWebKitAPI/SourcesCocoa.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Tests/WebKitCocoa/ConsoleMessageWithDetails.mm
Tests/WebKitCocoa/ContentFiltering.mm
Tests/WebKitCocoa/ContentRuleListNotification.mm
Tests/WebKitCocoa/ContentSecurityPolicy.mm
Tests/WebKitCocoa/ContentSecurityPolicyTestHelpers.mm
Tests/WebKitCocoa/ContextMenus.mm
Tests/WebKitCocoa/CookieAcceptPolicy.mm
Tests/WebKitCocoa/CookiePrivateBrowsing.mm
Expand Down
4 changes: 4 additions & 0 deletions Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -2436,6 +2436,8 @@
33BE5AF8137B5AAE00705813 /* MouseMoveAfterCrash_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MouseMoveAfterCrash_Bundle.cpp; sourceTree = "<group>"; };
33C2C9C02651F5B900E407F6 /* SmallSet.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = SmallSet.cpp; sourceTree = "<group>"; };
33C39CFF2B07D4B9008FA14B /* WKWebExtensionAPIDeclarativeNetRequest.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WKWebExtensionAPIDeclarativeNetRequest.mm; sourceTree = "<group>"; };
33D799652D0BBC3500B63ED5 /* ContentSecurityPolicyTestHelpers.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = ContentSecurityPolicyTestHelpers.mm; sourceTree = "<group>"; };
33D799662D0BBC3500B63ED5 /* ContentSecurityPolicyTestHelpers.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ContentSecurityPolicyTestHelpers.h; sourceTree = "<group>"; };
33DB57422CFD46700045C37C /* metalSpecTOC.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = metalSpecTOC.pdf; sourceTree = "<group>"; };
33DB57442CFD9D100045C37C /* WKWebViewForTestingImmediateActions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WKWebViewForTestingImmediateActions.h; sourceTree = "<group>"; };
33DB57452CFD9D100045C37C /* WKWebViewForTestingImmediateActions.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WKWebViewForTestingImmediateActions.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4285,6 +4287,8 @@
A14FC5891B89927100D107EB /* ContentFilteringPlugIn.mm */,
5CA1DED81F74A87100E71BD3 /* ContentRuleListNotification.mm */,
CEBCA12E1E3A660100C73293 /* ContentSecurityPolicy.mm */,
33D799662D0BBC3500B63ED5 /* ContentSecurityPolicyTestHelpers.h */,
33D799652D0BBC3500B63ED5 /* ContentSecurityPolicyTestHelpers.mm */,
5C121E8C2410703200486F9B /* ContentWorldPlugIn.mm */,
5C3B1D2522A74EA400BCF4D0 /* ContextMenus.mm */,
5C2936911D5BF63E00DEAB1E /* CookieAcceptPolicy.mm */,
Expand Down
1 change: 1 addition & 0 deletions Tools/TestWebKitAPI/Tests/WebKit/WKThumbnailView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#import "TestWKWebView.h"
#import "UISideCompositingScope.h"
#import "WKWebViewConfigurationExtras.h"
#import <WebCore/Color.h>
#import <WebKit/WKWebViewPrivate.h>
#import <WebKit/_WKThumbnailView.h>
#import <wtf/RetainPtr.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#import "config.h"

#import "ContentSecurityPolicyTestHelpers.h"
#import "HTTPServer.h"
#import "TestNavigationDelegate.h"
#import "TestWKWebView.h"
Expand Down Expand Up @@ -126,3 +127,8 @@
[webView loadRequest:server.request()];
[webView _test_waitForDidFinishNavigation];
}

TEST(ContentSecurityPolicy, LoadPDFWithSandboxCSPDirective)
{
TestWebKitAPI::runLoadPDFWithSandboxCSPDirectiveTest([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2024 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

OBJC_CLASS TestWKWebView;

namespace TestWebKitAPI {

void runLoadPDFWithSandboxCSPDirectiveTest(TestWKWebView *);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2024 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 "ContentSecurityPolicyTestHelpers.h"

#import "HTTPServer.h"
#import "PlatformUtilities.h"
#import "TestWKWebView.h"
#import <WebCore/Color.h>
#import <wtf/RetainPtr.h>
#import <wtf/Vector.h>

namespace TestWebKitAPI {

static HTTPServer pdfServerWithSandboxCSPDirective()
{
RetainPtr pdfURL = [NSBundle.test_resourcesBundle URLForResource:@"test" withExtension:@"pdf"];
HTTPResponse response { [NSData dataWithContentsOfURL:pdfURL.get()] };
response.headerFields.set("Content-Security-Policy"_s, "sandbox allow-scripts;"_s);
return { { { "/"_s, response } } };
}

void runLoadPDFWithSandboxCSPDirectiveTest(TestWKWebView *webView)
{
HTTPServer server { pdfServerWithSandboxCSPDirective() };

[webView synchronouslyLoadRequest:[NSURLRequest requestWithURL:[NSBundle.test_resourcesBundle URLForResource:@"test" withExtension:@"pdf"]]];
auto colorsWithPlainResponse = [webView sampleColors];

[webView synchronouslyLoadRequest:server.request()];
auto colorsWithCSPResponse = [webView sampleColors];

EXPECT_EQ(colorsWithPlainResponse, colorsWithCSPResponse);
}

}
7 changes: 7 additions & 0 deletions Tools/TestWebKitAPI/Tests/WebKitCocoa/CookieAcceptPolicy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@
#import "config.h"

#import "DeprecatedGlobalValues.h"
#import "HTTPServer.h"
#import "PlatformUtilities.h"
#import "TestNavigationDelegate.h"
#import "TestUIDelegate.h"
#import "TestWKWebView.h"
#import <WebKit/WKHTTPCookieStorePrivate.h>
#import <WebKit/WKProcessPoolPrivate.h>
#import <WebKit/WKWebView.h>
#import <WebKit/WKWebViewConfiguration.h>
#import <WebKit/WKWebViewConfigurationPrivate.h>
#import <WebKit/WKWebsiteDataStorePrivate.h>
#import <WebKit/_WKWebsiteDataStoreConfiguration.h>
#import <pal/spi/cf/CFNetworkSPI.h>
#import <wtf/RetainPtr.h>
#import <wtf/text/MakeString.h>
Expand Down
Loading

0 comments on commit 2177892

Please sign in to comment.