1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
* Copyright (C) 2013-2025 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.
* 3. Neither the name of Apple Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE 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 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"
#include "MixedContentChecker.h"
#include "Document.h"
#include "FrameLoader.h"
#include "LegacySchemeRegistry.h"
#include "LocalFrameInlines.h"
#include "LocalFrameLoaderClient.h"
#include "SecurityOrigin.h"
namespace WebCore {
static bool isDocumentSecure(const Frame& frame)
{
// FIXME: Use document.isDocumentSecure(), instead of comparing against "https" scheme, when all ports stop using loopback in LayoutTests
// sandboxed iframes have an opaque origin so we should perform the mixed content check considering the origin
// the iframe would have had if it were not sandboxed.
if (RefPtr origin = frame.frameDocumentSecurityOrigin())
return origin->protocol() == "https"_s || (origin->isOpaque() && frame.frameURLProtocol() == "https"_s);
return false;
}
static bool isDataContextSecure(const Frame& frame)
{
RefPtr currentFrame = frame;
while (currentFrame) {
RefPtr localFrame = dynamicDowncast<const LocalFrame>(currentFrame);
RefPtr<Document> document;
if (localFrame)
document = localFrame->document();
if (isDocumentSecure(*currentFrame))
return true;
RefPtr parentFrame = currentFrame->tree().parent();
if (!parentFrame && localFrame)
parentFrame = localFrame->loader().client().provisionalParentFrame();
currentFrame = parentFrame;
}
return false;
}
static bool isMixedContent(const Frame& frame, const URL& url)
{
if (isDocumentSecure(frame) || (frame.frameURLProtocol() == "data"_s && isDataContextSecure(frame)))
return !SecurityOrigin::isSecure(url);
return false;
}
static bool destinationIsImageAudioOrVideo(FetchOptions::Destination destination)
{
return destination == FetchOptions::Destination::Audio || destination == FetchOptions::Destination::Image || destination == FetchOptions::Destination::Video;
}
static bool destinationIsImageAndInitiatorIsImageset(FetchOptions::Destination destination, Initiator initiator)
{
return destination == FetchOptions::Destination::Image && initiator == Initiator::Imageset;
}
bool MixedContentChecker::shouldUpgradeInsecureContent(LocalFrame& frame, IsUpgradable isUpgradable, const URL& url, FetchOptions::Destination destination, Initiator initiator)
{
RefPtr document = frame.document();
if (!document || isUpgradable != IsUpgradable::Yes)
return false;
// https://www.w3.org/TR/mixed-content/#upgrade-algorithm
// Editor’s Draft, 23 February 2023
// 4.1. Upgrade a mixed content request to a potentially trustworthy URL, if appropriate
if (!isMixedContent(frame, url))
return false;
// 4.1 The request's URL is not upgraded in the following cases.
if (!canModifyRequest(url, destination, initiator))
return false;
frame.reportMixedContentViolation(false, url);
return true;
}
bool MixedContentChecker::canModifyRequest(const URL& url, FetchOptions::Destination destination, Initiator initiator)
{
// 4.1.1 request’s URL is a potentially trustworthy URL.
if (url.protocolIs("https"_s))
return false;
// 4.1.2 request’s URL’s host is an IP address.
if (URL::hostIsIPAddress(url.host()) && !shouldTreatAsPotentiallyTrustworthy(url))
return false;
// 4.1.4 request’s destination is not "image", "audio", or "video".
if (!destinationIsImageAudioOrVideo(destination))
return false;
// 4.1.5 request’s destination is "image" and request’s initiator is "imageset".
auto schemeIsHandledBySchemeHandler = LegacySchemeRegistry::schemeIsHandledBySchemeHandler(url.protocol());
if (!schemeIsHandledBySchemeHandler && destinationIsImageAndInitiatorIsImageset(destination, initiator))
return false;
return true;
}
bool MixedContentChecker::shouldBlockRequest(Frame& frame, const URL& url, IsUpgradable isUpgradable)
{
RefPtr<Document> document;
if (RefPtr localFrame = dynamicDowncast<LocalFrame>(frame))
document = localFrame->document();
if (!isMixedContent(frame, url))
return false;
if ((LegacySchemeRegistry::schemeIsHandledBySchemeHandler(url.protocol()) || shouldTreatAsPotentiallyTrustworthy(url)) && isUpgradable == IsUpgradable::Yes)
return false;
frame.reportMixedContentViolation(true, url);
return true;
}
} // namespace WebCore
|