Files
palemoon27/dom/base/nsContentPolicy.cpp
T
roytam1 fdb63ff9b9 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1163201 - Part 1: Remove instances of #ifdef PR_LOGGING in dom/. r=froydnj (9979c0e74)
-  Bug 1183972 - No sync-dispatch of new GMPParent - r=cpearce (93339b530)
- Bug 1142935 - reset transports with NuwaAddConstructor(). r=tlee (277406812)
- Bug 1121676 - Use a lock to protect the list of top-level actors (r=bent) (3d0be2f87)
-  Bug 1121676 - Use static mutex to protect top-level protocols (r=bent) (4491dd318)
- Bug 1163201 - Part 2: Wrap expensive calls in PR_LOG_TEST. r=froydnj (7de4b9a97)
- Bug 1163201 - Part 3: Remove mSamples in |MediaEngineWebRTCAudioSource|. r=cpeterson (452442773)
- Bug 1163201 - Part 4: Fix b2g build. r=bustage (a824ea36d)
- Bug 1165518 - Part 1: Add Logging.h. r=froydnj (09d68aaa6)
- Bug 1162850 - Don't stop looking for style sheet load finishes after the FontFaceSet gets a DOMContentLoaded. r=jdaggett (c29fbffa0)
- Bug 1056479 p0 - rename ambiguous GetFontList method in Android fontlist. r=m_kato (76239d7a0)
- Bug 1056479 p1 - add language to FindFamily parameters. r=jfkthame (2271bd7d0)
- Bug 1056479 p1a - use lang as part of pref font fallback. r=karlt (5f5fd66c5)
- cleanup GetTableFromFontData() to match gecko code again (78076fc26)
- Bug 1056479 p2 - implement platform fontlist based on fontconfig. r=karlt (6a7631e44)
- Bug 1056479 p3 - fixup various reftests for Linux. r=jfkthame (b25360708)
- Bug 1056479 p4 - fix accessibility api for font-weight. r=jfkthame (efa8f5080)
- Bug 1056479 p5 - fixup printpreview test. r=jfkthame (3fe2ddc0b)
- Bug 1056479 p6 - handle font updates. r=jfkthame (eb78b2c54)
- Bug 1056479 p7 - fixup assertion for non-italic fallback. r=m_kato (f5e9f539e)
- Bug 1056479 p8 - switch gfxFontConfig to gfxFontconfig. r=karlt (4da146b50)
- Bug 1056479 p9 - fix build bustage. r=birtles (28f246c2b)
- Bug 1056479 p10 - activate bundled fonts. r=m_kato (d7627c3fa)
- Bug 1056479 p10 - activate bundled fonts. r=m_kato (251c02315)
- Bug 1056479 followup: Annotate gfxPlatformGtk::CreatePlatformFontList() as 'override'. rs=ehsan (993e65d6e)
2020-05-30 12:49:03 +08:00

273 lines
11 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
// vim: ft=cpp tw=78 sw=4 et ts=8
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Implementation of the "@mozilla.org/layout/content-policy;1" contract.
*/
#include "prlog.h"
#include "nsISupports.h"
#include "nsXPCOM.h"
#include "nsContentPolicyUtils.h"
#include "mozilla/dom/nsCSPService.h"
#include "nsContentPolicy.h"
#include "nsIURI.h"
#include "nsIDOMNode.h"
#include "nsIDOMWindow.h"
#include "nsIContent.h"
#include "nsCOMArray.h"
#include "mozilla/dom/nsMixedContentBlocker.h"
NS_IMPL_ISUPPORTS(nsContentPolicy, nsIContentPolicy)
static PRLogModuleInfo* gConPolLog;
nsresult
NS_NewContentPolicy(nsIContentPolicy **aResult)
{
*aResult = new nsContentPolicy;
if (!*aResult)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(*aResult);
return NS_OK;
}
nsContentPolicy::nsContentPolicy()
: mPolicies(NS_CONTENTPOLICY_CATEGORY)
, mSimplePolicies(NS_SIMPLECONTENTPOLICY_CATEGORY)
{
if (! gConPolLog) {
gConPolLog = PR_NewLogModule("nsContentPolicy");
}
}
nsContentPolicy::~nsContentPolicy()
{
}
#ifdef DEBUG
#define WARN_IF_URI_UNINITIALIZED(uri,name) \
PR_BEGIN_MACRO \
if ((uri)) { \
nsAutoCString spec; \
(uri)->GetAsciiSpec(spec); \
if (spec.IsEmpty()) { \
NS_WARNING(name " is uninitialized, fix caller"); \
} \
} \
PR_END_MACRO
#else // ! defined(DEBUG)
#define WARN_IF_URI_UNINITIALIZED(uri,name)
#endif // defined(DEBUG)
inline nsresult
nsContentPolicy::CheckPolicy(CPMethod policyMethod,
SCPMethod simplePolicyMethod,
uint32_t contentType,
nsIURI *contentLocation,
nsIURI *requestingLocation,
nsISupports *requestingContext,
const nsACString &mimeType,
nsISupports *extra,
nsIPrincipal *requestPrincipal,
int16_t *decision)
{
//sanity-check passed-through parameters
NS_PRECONDITION(decision, "Null out pointer");
WARN_IF_URI_UNINITIALIZED(contentLocation, "Request URI");
WARN_IF_URI_UNINITIALIZED(requestingLocation, "Requesting URI");
#ifdef DEBUG
{
nsCOMPtr<nsIDOMNode> node(do_QueryInterface(requestingContext));
nsCOMPtr<nsIDOMWindow> window(do_QueryInterface(requestingContext));
NS_ASSERTION(!requestingContext || node || window,
"Context should be a DOM node or a DOM window!");
}
#endif
/*
* There might not be a requestinglocation. This can happen for
* iframes with an image as src. Get the uri from the dom node.
* See bug 254510
*/
if (!requestingLocation) {
nsCOMPtr<nsIDocument> doc;
nsCOMPtr<nsIContent> node = do_QueryInterface(requestingContext);
if (node) {
doc = node->OwnerDoc();
}
if (!doc) {
doc = do_QueryInterface(requestingContext);
}
if (doc) {
requestingLocation = doc->GetDocumentURI();
}
}
nsContentPolicyType externalType =
nsContentUtils::InternalContentPolicyTypeToExternal(contentType);
nsContentPolicyType externalTypeOrMCBInternal =
nsContentUtils::InternalContentPolicyTypeToExternalOrMCBInternal(contentType);
nsContentPolicyType externalTypeOrCSPInternal =
nsContentUtils::InternalContentPolicyTypeToExternalOrCSPInternal(contentType);
nsCOMPtr<nsIContentPolicy> mixedContentBlocker =
do_GetService(NS_MIXEDCONTENTBLOCKER_CONTRACTID);
nsCOMPtr<nsIContentPolicy> cspService =
do_GetService(CSPSERVICE_CONTRACTID);
/*
* Enumerate mPolicies and ask each of them, taking the logical AND of
* their permissions.
*/
nsresult rv;
nsCOMArray<nsIContentPolicy> entries;
mPolicies.GetEntries(entries);
int32_t count = entries.Count();
for (int32_t i = 0; i < count; i++) {
/* check the appropriate policy */
rv = (entries[i]->*policyMethod)(contentType, contentLocation,
requestingLocation, requestingContext,
mimeType, extra, requestPrincipal,
decision);
if (NS_SUCCEEDED(rv) && NS_CP_REJECTED(*decision)) {
/* policy says no, no point continuing to check */
return NS_OK;
}
}
nsCOMPtr<nsIDOMElement> topFrameElement;
bool isTopLevel = true;
nsCOMPtr<nsPIDOMWindow> window;
if (nsCOMPtr<nsINode> node = do_QueryInterface(requestingContext)) {
window = node->OwnerDoc()->GetWindow();
} else {
window = do_QueryInterface(requestingContext);
}
if (window) {
nsCOMPtr<nsIDocShell> docShell = window->GetDocShell();
nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(docShell);
loadContext->GetTopFrameElement(getter_AddRefs(topFrameElement));
MOZ_ASSERT(window->IsOuterWindow());
if (topFrameElement) {
nsCOMPtr<nsPIDOMWindow> topWindow = window->GetScriptableTop();
isTopLevel = topWindow == static_cast<nsIDOMWindow*>(window);
} else {
// If we don't have a top frame element, then requestingContext is
// part of the top-level XUL document. Presumably it's the <browser>
// element that content is being loaded into, so we call it the
// topFrameElement.
topFrameElement = do_QueryInterface(requestingContext);
isTopLevel = true;
}
}
nsCOMArray<nsISimpleContentPolicy> simpleEntries;
mSimplePolicies.GetEntries(simpleEntries);
count = simpleEntries.Count();
for (int32_t i = 0; i < count; i++) {
/* check the appropriate policy */
rv = (simpleEntries[i]->*simplePolicyMethod)(contentType, contentLocation,
requestingLocation,
topFrameElement, isTopLevel,
mimeType, extra, requestPrincipal,
decision);
if (NS_SUCCEEDED(rv) && NS_CP_REJECTED(*decision)) {
/* policy says no, no point continuing to check */
return NS_OK;
}
}
// everyone returned failure, or no policies: sanitize result
*decision = nsIContentPolicy::ACCEPT;
return NS_OK;
}
//uses the parameters from ShouldXYZ to produce and log a message
//logType must be a literal string constant
#define LOG_CHECK(logType) \
PR_BEGIN_MACRO \
/* skip all this nonsense if the call failed or logging is disabled */ \
if (NS_SUCCEEDED(rv) && PR_LOG_TEST(gConPolLog, PR_LOG_DEBUG)) { \
const char *resultName; \
if (decision) { \
resultName = NS_CP_ResponseName(*decision); \
} else { \
resultName = "(null ptr)"; \
} \
nsAutoCString spec("None"); \
if (contentLocation) { \
contentLocation->GetSpec(spec); \
} \
nsAutoCString refSpec("None"); \
if (requestingLocation) { \
requestingLocation->GetSpec(refSpec); \
} \
PR_LOG(gConPolLog, PR_LOG_DEBUG, \
("Content Policy: " logType ": <%s> <Ref:%s> result=%s", \
spec.get(), refSpec.get(), resultName) \
); \
} \
PR_END_MACRO
NS_IMETHODIMP
nsContentPolicy::ShouldLoad(uint32_t contentType,
nsIURI *contentLocation,
nsIURI *requestingLocation,
nsISupports *requestingContext,
const nsACString &mimeType,
nsISupports *extra,
nsIPrincipal *requestPrincipal,
int16_t *decision)
{
// ShouldProcess does not need a content location, but we do
NS_PRECONDITION(contentLocation, "Must provide request location");
nsresult rv = CheckPolicy(&nsIContentPolicy::ShouldLoad,
&nsISimpleContentPolicy::ShouldLoad,
contentType,
contentLocation, requestingLocation,
requestingContext, mimeType, extra,
requestPrincipal, decision);
LOG_CHECK("ShouldLoad");
return rv;
}
NS_IMETHODIMP
nsContentPolicy::ShouldProcess(uint32_t contentType,
nsIURI *contentLocation,
nsIURI *requestingLocation,
nsISupports *requestingContext,
const nsACString &mimeType,
nsISupports *extra,
nsIPrincipal *requestPrincipal,
int16_t *decision)
{
nsresult rv = CheckPolicy(&nsIContentPolicy::ShouldProcess,
&nsISimpleContentPolicy::ShouldProcess,
contentType,
contentLocation, requestingLocation,
requestingContext, mimeType, extra,
requestPrincipal, decision);
LOG_CHECK("ShouldProcess");
return rv;
}