2011-07-15 14:31:34 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
1999-11-06 07:51:05 +03:00
|
|
|
|
1999-11-12 10:28:25 +03:00
|
|
|
#include "nsDocShell.h"
|
1999-11-06 07:51:05 +03:00
|
|
|
#include "nsDSURIContentListener.h"
|
2000-01-29 09:10:54 +03:00
|
|
|
#include "nsIChannel.h"
|
2005-02-27 21:33:27 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2001-04-17 03:05:08 +04:00
|
|
|
#include "nsXPIDLString.h"
|
2005-02-27 21:33:27 +03:00
|
|
|
#include "nsDocShellCID.h"
|
|
|
|
#include "nsIWebNavigationInfo.h"
|
2011-07-15 14:31:34 +04:00
|
|
|
#include "nsIDOMWindow.h"
|
2012-08-27 23:46:24 +04:00
|
|
|
#include "nsNetUtil.h"
|
2004-12-08 20:32:06 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2010-10-15 02:26:59 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2012-06-11 20:17:35 +04:00
|
|
|
#include "nsCharSeparatedTokenizer.h"
|
2011-12-22 20:51:00 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-09-21 06:47:18 +04:00
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "nsIScriptError.h"
|
2011-12-22 20:51:00 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
1999-11-06 07:51:05 +03:00
|
|
|
//*****************************************************************************
|
|
|
|
//*** nsDSURIContentListener: Object Management
|
|
|
|
//*****************************************************************************
|
|
|
|
|
2005-02-18 21:20:42 +03:00
|
|
|
nsDSURIContentListener::nsDSURIContentListener(nsDocShell* aDocShell)
|
|
|
|
: mDocShell(aDocShell),
|
2012-07-30 18:20:58 +04:00
|
|
|
mParentContentListener(nullptr)
|
1999-11-06 07:51:05 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDSURIContentListener::~nsDSURIContentListener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2001-04-17 03:05:08 +04:00
|
|
|
nsresult
|
|
|
|
nsDSURIContentListener::Init()
|
|
|
|
{
|
2005-02-27 21:33:27 +03:00
|
|
|
nsresult rv;
|
|
|
|
mNavInfo = do_GetService(NS_WEBNAVIGATION_INFO_CONTRACTID, &rv);
|
2006-03-30 22:40:56 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to get webnav info");
|
2001-04-17 03:05:08 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-06 07:51:05 +03:00
|
|
|
//*****************************************************************************
|
|
|
|
// nsDSURIContentListener::nsISupports
|
|
|
|
//*****************************************************************************
|
|
|
|
|
2000-04-16 17:00:52 +04:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsDSURIContentListener)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsDSURIContentListener)
|
2000-01-14 11:16:50 +03:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDSURIContentListener)
|
2001-04-13 17:05:16 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIURIContentListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIURIContentListener)
|
2002-07-10 01:25:12 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2000-01-14 11:16:50 +03:00
|
|
|
NS_INTERFACE_MAP_END
|
1999-11-06 07:51:05 +03:00
|
|
|
|
|
|
|
//*****************************************************************************
|
|
|
|
// nsDSURIContentListener::nsIURIContentListener
|
|
|
|
//*****************************************************************************
|
|
|
|
|
2001-05-14 06:16:27 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsDSURIContentListener::OnStartURIOpen(nsIURI* aURI, bool* aAbortOpen)
|
2000-03-24 03:26:51 +03:00
|
|
|
{
|
2006-04-12 02:37:59 +04:00
|
|
|
// If mDocShell is null here, that means someone's starting a load
|
|
|
|
// in our docshell after it's already been destroyed. Don't let
|
|
|
|
// that happen.
|
|
|
|
if (!mDocShell) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aAbortOpen = true;
|
2006-04-12 02:37:59 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-07-10 01:25:12 +04:00
|
|
|
nsCOMPtr<nsIURIContentListener> parentListener;
|
|
|
|
GetParentContentListener(getter_AddRefs(parentListener));
|
|
|
|
if (parentListener)
|
|
|
|
return parentListener->OnStartURIOpen(aURI, aAbortOpen);
|
2000-03-24 03:26:51 +03:00
|
|
|
|
2001-04-13 17:05:16 +04:00
|
|
|
return NS_OK;
|
2000-03-24 03:26:51 +03:00
|
|
|
}
|
|
|
|
|
2001-05-14 06:16:27 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDSURIContentListener::DoContent(const char* aContentType,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aIsContentPreferred,
|
2001-05-14 06:16:27 +04:00
|
|
|
nsIRequest* request,
|
|
|
|
nsIStreamListener** aContentHandler,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* aAbortProcess)
|
1999-11-06 07:51:05 +03:00
|
|
|
{
|
2001-05-14 06:16:27 +04:00
|
|
|
nsresult rv;
|
2001-04-13 17:05:16 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aContentHandler);
|
2001-04-14 02:26:28 +04:00
|
|
|
NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE);
|
2010-10-15 02:26:59 +04:00
|
|
|
|
|
|
|
// Check whether X-Frame-Options permits us to load this content in an
|
2011-12-22 20:51:00 +04:00
|
|
|
// iframe and abort the load (unless we've disabled x-frame-options
|
|
|
|
// checking).
|
2010-10-15 02:26:59 +04:00
|
|
|
if (!CheckFrameOptions(request)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aAbortProcess = true;
|
2010-10-15 02:26:59 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*aAbortProcess = false;
|
2000-02-17 04:58:34 +03:00
|
|
|
|
2001-04-13 17:05:16 +04:00
|
|
|
// determine if the channel has just been retargeted to us...
|
|
|
|
nsLoadFlags loadFlags = 0;
|
|
|
|
nsCOMPtr<nsIChannel> aOpenedChannel = do_QueryInterface(request);
|
2000-02-17 04:58:34 +03:00
|
|
|
|
2001-04-14 02:26:28 +04:00
|
|
|
if (aOpenedChannel)
|
|
|
|
aOpenedChannel->GetLoadFlags(&loadFlags);
|
2000-10-12 03:17:02 +04:00
|
|
|
|
2001-04-14 06:02:19 +04:00
|
|
|
if(loadFlags & nsIChannel::LOAD_RETARGETED_DOCUMENT_URI)
|
2001-04-13 17:05:16 +04:00
|
|
|
{
|
2001-08-14 08:46:20 +04:00
|
|
|
// XXX: Why does this not stop the content too?
|
|
|
|
mDocShell->Stop(nsIWebNavigation::STOP_NETWORK);
|
2001-10-29 09:03:27 +03:00
|
|
|
|
|
|
|
mDocShell->SetLoadType(aIsContentPreferred ? LOAD_LINK : LOAD_NORMAL);
|
2001-04-13 17:05:16 +04:00
|
|
|
}
|
2000-02-17 04:58:34 +03:00
|
|
|
|
2001-05-14 06:16:27 +04:00
|
|
|
rv = mDocShell->CreateContentViewer(aContentType, request, aContentHandler);
|
2011-01-14 18:33:57 +03:00
|
|
|
|
|
|
|
if (rv == NS_ERROR_REMOTE_XUL) {
|
|
|
|
request->Cancel(rv);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-08-24 22:45:11 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// it's okay if we don't know how to handle the content
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-11-06 07:51:05 +03:00
|
|
|
|
2001-11-27 04:22:34 +03:00
|
|
|
if (loadFlags & nsIChannel::LOAD_RETARGETED_DOCUMENT_URI) {
|
2011-07-15 14:31:34 +04:00
|
|
|
nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(static_cast<nsIDocShell*>(mDocShell));
|
2001-12-03 15:42:49 +03:00
|
|
|
NS_ENSURE_TRUE(domWindow, NS_ERROR_FAILURE);
|
2001-11-27 04:22:34 +03:00
|
|
|
domWindow->Focus();
|
|
|
|
}
|
2001-04-13 17:05:16 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-11-06 07:51:05 +03:00
|
|
|
}
|
|
|
|
|
2001-05-14 06:16:27 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDSURIContentListener::IsPreferred(const char* aContentType,
|
|
|
|
char ** aDesiredContentType,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* aCanHandle)
|
1999-11-06 07:51:05 +03:00
|
|
|
{
|
2001-04-13 17:05:16 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aCanHandle);
|
|
|
|
NS_ENSURE_ARG_POINTER(aDesiredContentType);
|
|
|
|
|
|
|
|
// the docshell has no idea if it is the preferred content provider or not.
|
2009-02-06 05:05:03 +03:00
|
|
|
// It needs to ask its parent if it is the preferred content handler or not...
|
2001-04-13 17:05:16 +04:00
|
|
|
|
2002-07-10 01:25:12 +04:00
|
|
|
nsCOMPtr<nsIURIContentListener> parentListener;
|
|
|
|
GetParentContentListener(getter_AddRefs(parentListener));
|
|
|
|
if (parentListener) {
|
|
|
|
return parentListener->IsPreferred(aContentType,
|
2001-05-14 06:16:27 +04:00
|
|
|
aDesiredContentType,
|
|
|
|
aCanHandle);
|
2001-04-13 17:05:16 +04:00
|
|
|
}
|
2001-10-27 06:52:39 +04:00
|
|
|
// we used to return false here if we didn't have a parent properly
|
|
|
|
// registered at the top of the docshell hierarchy to dictate what
|
|
|
|
// content types this docshell should be a preferred handler for. But
|
|
|
|
// this really makes it hard for developers using iframe or browser tags
|
|
|
|
// because then they need to make sure they implement
|
|
|
|
// nsIURIContentListener otherwise all link clicks would get sent to
|
|
|
|
// another window because we said we weren't the preferred handler type.
|
|
|
|
// I'm going to change the default now...if we can handle the content,
|
|
|
|
// and someone didn't EXPLICITLY set a nsIURIContentListener at the top
|
|
|
|
// of our docshell chain, then we'll now always attempt to process the
|
|
|
|
// content ourselves...
|
|
|
|
return CanHandleContent(aContentType,
|
2011-10-17 18:59:28 +04:00
|
|
|
true,
|
2001-10-27 06:52:39 +04:00
|
|
|
aDesiredContentType,
|
|
|
|
aCanHandle);
|
1999-11-06 07:51:05 +03:00
|
|
|
}
|
|
|
|
|
2001-05-14 06:16:27 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDSURIContentListener::CanHandleContent(const char* aContentType,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aIsContentPreferred,
|
2001-05-14 06:16:27 +04:00
|
|
|
char ** aDesiredContentType,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool* aCanHandleContent)
|
2000-02-04 11:45:34 +03:00
|
|
|
{
|
2005-02-27 21:33:27 +03:00
|
|
|
NS_PRECONDITION(aCanHandleContent, "Null out param?");
|
2001-04-13 17:05:16 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aDesiredContentType);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*aCanHandleContent = false;
|
2012-07-30 18:20:58 +04:00
|
|
|
*aDesiredContentType = nullptr;
|
2001-05-14 06:16:27 +04:00
|
|
|
|
2005-02-27 21:33:27 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (aContentType) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t canHandle = nsIWebNavigationInfo::UNSUPPORTED;
|
2005-02-27 21:33:27 +03:00
|
|
|
rv = mNavInfo->IsTypeSupported(nsDependentCString(aContentType),
|
|
|
|
mDocShell,
|
|
|
|
&canHandle);
|
|
|
|
*aCanHandleContent = (canHandle != nsIWebNavigationInfo::UNSUPPORTED);
|
2001-04-13 17:05:16 +04:00
|
|
|
}
|
|
|
|
|
2005-02-27 21:33:27 +03:00
|
|
|
return rv;
|
2000-02-04 11:45:34 +03:00
|
|
|
}
|
|
|
|
|
2000-05-06 10:05:03 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDSURIContentListener::GetLoadCookie(nsISupports ** aLoadCookie)
|
2000-03-11 03:49:47 +03:00
|
|
|
{
|
2005-02-20 20:18:22 +03:00
|
|
|
NS_IF_ADDREF(*aLoadCookie = nsDocShell::GetAsSupports(mDocShell));
|
2004-12-08 20:32:06 +03:00
|
|
|
return NS_OK;
|
2000-03-11 03:49:47 +03:00
|
|
|
}
|
|
|
|
|
2000-05-06 10:05:03 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDSURIContentListener::SetLoadCookie(nsISupports * aLoadCookie)
|
2000-03-11 03:49:47 +03:00
|
|
|
{
|
2004-12-08 20:32:06 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsRefPtr<nsDocLoader> cookieAsDocLoader =
|
|
|
|
nsDocLoader::GetAsDocLoader(aLoadCookie);
|
|
|
|
NS_ASSERTION(cookieAsDocLoader && cookieAsDocLoader == mDocShell,
|
|
|
|
"Invalid load cookie being set!");
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
2000-03-11 03:49:47 +03:00
|
|
|
}
|
|
|
|
|
2000-05-06 10:05:03 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDSURIContentListener::GetParentContentListener(nsIURIContentListener**
|
|
|
|
aParentListener)
|
2000-01-29 09:10:54 +03:00
|
|
|
{
|
2002-07-10 01:25:12 +04:00
|
|
|
if (mWeakParentContentListener)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURIContentListener> tempListener =
|
|
|
|
do_QueryReferent(mWeakParentContentListener);
|
|
|
|
*aParentListener = tempListener;
|
|
|
|
NS_IF_ADDREF(*aParentListener);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aParentListener = mParentContentListener;
|
|
|
|
NS_IF_ADDREF(*aParentListener);
|
|
|
|
}
|
2001-04-13 17:05:16 +04:00
|
|
|
return NS_OK;
|
2000-01-29 09:10:54 +03:00
|
|
|
}
|
|
|
|
|
2000-05-06 10:05:03 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDSURIContentListener::SetParentContentListener(nsIURIContentListener*
|
|
|
|
aParentListener)
|
2000-01-29 09:10:54 +03:00
|
|
|
{
|
2002-07-10 01:25:12 +04:00
|
|
|
if (aParentListener)
|
|
|
|
{
|
|
|
|
// Store the parent listener as a weak ref. Parents not supporting
|
|
|
|
// nsISupportsWeakReference assert but may still be used.
|
2012-07-30 18:20:58 +04:00
|
|
|
mParentContentListener = nullptr;
|
2003-07-25 23:06:59 +04:00
|
|
|
mWeakParentContentListener = do_GetWeakReference(aParentListener);
|
2002-07-10 01:25:12 +04:00
|
|
|
if (!mWeakParentContentListener)
|
|
|
|
{
|
|
|
|
mParentContentListener = aParentListener;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mWeakParentContentListener = nullptr;
|
|
|
|
mParentContentListener = nullptr;
|
2002-07-10 01:25:12 +04:00
|
|
|
}
|
2001-04-13 17:05:16 +04:00
|
|
|
return NS_OK;
|
2000-01-29 09:10:54 +03:00
|
|
|
}
|
2010-10-15 02:26:59 +04:00
|
|
|
|
2012-06-11 20:17:35 +04:00
|
|
|
bool nsDSURIContentListener::CheckOneFrameOptionsPolicy(nsIRequest *request,
|
|
|
|
const nsAString& policy) {
|
2012-08-27 23:46:24 +04:00
|
|
|
static const char allowFrom[] = "allow-from ";
|
2012-08-29 18:56:51 +04:00
|
|
|
const uint32_t allowFromLen = ArrayLength(allowFrom) - 1;
|
2012-08-27 23:46:24 +04:00
|
|
|
bool isAllowFrom =
|
|
|
|
StringHead(policy, allowFromLen).LowerCaseEqualsLiteral(allowFrom);
|
|
|
|
|
|
|
|
// return early if header does not have one of the values with meaning
|
2012-06-11 20:17:35 +04:00
|
|
|
if (!policy.LowerCaseEqualsLiteral("deny") &&
|
2012-08-27 23:46:24 +04:00
|
|
|
!policy.LowerCaseEqualsLiteral("sameorigin") &&
|
|
|
|
!isAllowFrom)
|
2011-12-22 20:51:00 +04:00
|
|
|
return true;
|
2010-10-15 02:26:59 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
|
|
|
|
if (!httpChannel) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-21 06:47:18 +04:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
httpChannel->GetURI(getter_AddRefs(uri));
|
2010-10-15 02:26:59 +04:00
|
|
|
|
2012-09-21 06:47:18 +04:00
|
|
|
// XXXkhuey when does this happen? Is returning true safe here?
|
|
|
|
if (!mDocShell) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-10 00:42:17 +04:00
|
|
|
|
2012-09-21 06:47:18 +04:00
|
|
|
// We need to check the location of this window and the location of the top
|
|
|
|
// window, if we're not the top. X-F-O: SAMEORIGIN requires that the
|
|
|
|
// document must be same-origin with top window. X-F-O: DENY requires that
|
|
|
|
// the document must never be framed.
|
|
|
|
nsCOMPtr<nsIDOMWindow> thisWindow = do_GetInterface(static_cast<nsIDocShell*>(mDocShell));
|
|
|
|
// If we don't have DOMWindow there is no risk of clickjacking
|
|
|
|
if (!thisWindow)
|
|
|
|
return true;
|
2012-07-10 00:42:17 +04:00
|
|
|
|
2012-09-21 06:47:18 +04:00
|
|
|
// GetScriptableTop, not GetTop, because we want this to respect
|
|
|
|
// <iframe mozbrowser> boundaries.
|
|
|
|
nsCOMPtr<nsIDOMWindow> topWindow;
|
|
|
|
thisWindow->GetScriptableTop(getter_AddRefs(topWindow));
|
2010-10-15 02:26:59 +04:00
|
|
|
|
2012-09-21 06:47:18 +04:00
|
|
|
// if the document is in the top window, it's not in a frame.
|
|
|
|
if (thisWindow == topWindow)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Find the top docshell in our parent chain that doesn't have the system
|
|
|
|
// principal and use it for the principal comparison. Finding the top
|
|
|
|
// content-type docshell doesn't work because some chrome documents are
|
|
|
|
// loaded in content docshells (see bug 593387).
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> thisDocShellItem(do_QueryInterface(
|
|
|
|
static_cast<nsIDocShell*> (mDocShell)));
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> parentDocShellItem,
|
|
|
|
curDocShellItem = thisDocShellItem;
|
|
|
|
nsCOMPtr<nsIDocument> topDoc;
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> ssm =
|
|
|
|
do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
|
|
|
|
if (!ssm) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse up the parent chain and stop when we see a docshell whose
|
|
|
|
// parent has a system principal, or a docshell corresponding to
|
|
|
|
// <iframe mozbrowser>.
|
|
|
|
while (NS_SUCCEEDED(curDocShellItem->GetParent(getter_AddRefs(parentDocShellItem))) &&
|
|
|
|
parentDocShellItem) {
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocShell> curDocShell = do_QueryInterface(curDocShellItem);
|
Bug 802366 - The main event: Let a browser process inherit its app's id. r=bz,cjones
The main bug fixed here is that in half of our interfaces, we use "is browser frame/element" to mean "browser or app", and in the other half, we use it to mean "is browser not app".
There's a related, functional bug also fixed here, which is that a browser process doesn't inherit its parent's app-id. This causes problems e.g. for IndexedDB: If a browser inside an app uses IndexedDB, the DB should have the app's app-id.
I also modified Tab{Parent,Child} and nsFrameLoader to call "app" "ownOrContainingApp", to emphasize that we might have inherited the app from a parent process. I left nsIDocShell::appId alone, because changing that would have necessitated changing nsILoadGroup and therefore a /lot/ of users in Necko; it's also not clear it would have clarified anything in those cases.
2012-11-10 22:32:37 +04:00
|
|
|
if (curDocShell && curDocShell->GetIsBrowserOrApp()) {
|
2012-09-21 06:47:18 +04:00
|
|
|
break;
|
|
|
|
}
|
2010-10-15 02:26:59 +04:00
|
|
|
|
2012-09-21 06:47:18 +04:00
|
|
|
bool system = false;
|
|
|
|
topDoc = do_GetInterface(parentDocShellItem);
|
|
|
|
if (topDoc) {
|
|
|
|
if (NS_SUCCEEDED(ssm->IsSystemPrincipal(topDoc->NodePrincipal(),
|
|
|
|
&system)) && system) {
|
|
|
|
// Found a system-principled doc: last docshell was top.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-06-11 20:17:35 +04:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-21 06:47:18 +04:00
|
|
|
curDocShellItem = parentDocShellItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this document has the top non-SystemPrincipal docshell it is not being
|
|
|
|
// framed or it is being framed by a chrome document, which we allow.
|
|
|
|
if (curDocShellItem == thisDocShellItem)
|
|
|
|
return true;
|
2012-06-11 20:17:35 +04:00
|
|
|
|
2012-09-21 06:47:18 +04:00
|
|
|
// If the value of the header is DENY, and the previous condition is
|
|
|
|
// not met (current docshell is not the top docshell), prohibit the
|
|
|
|
// load.
|
|
|
|
if (policy.LowerCaseEqualsLiteral("deny")) {
|
|
|
|
ReportXFOViolation(curDocShellItem, uri, eDENY);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
topDoc = do_GetInterface(curDocShellItem);
|
|
|
|
nsCOMPtr<nsIURI> topUri;
|
|
|
|
topDoc->NodePrincipal()->GetURI(getter_AddRefs(topUri));
|
|
|
|
|
|
|
|
// If the X-Frame-Options value is SAMEORIGIN, then the top frame in the
|
|
|
|
// parent chain must be from the same origin as this document.
|
|
|
|
if (policy.LowerCaseEqualsLiteral("sameorigin")) {
|
|
|
|
rv = ssm->CheckSameOriginURI(uri, topUri, true);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ReportXFOViolation(curDocShellItem, uri, eSAMEORIGIN);
|
|
|
|
return false; /* wasn't same-origin */
|
2010-10-15 02:26:59 +04:00
|
|
|
}
|
2012-09-21 06:47:18 +04:00
|
|
|
}
|
2012-08-27 23:46:24 +04:00
|
|
|
|
2012-09-21 06:47:18 +04:00
|
|
|
// If the X-Frame-Options value is "allow-from [uri]", then the top
|
|
|
|
// frame in the parent chain must be from that origin
|
|
|
|
if (isAllowFrom) {
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri),
|
|
|
|
Substring(policy, allowFromLen));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
rv = ssm->CheckSameOriginURI(uri, topUri, true);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
ReportXFOViolation(curDocShellItem, uri, eALLOWFROM);
|
|
|
|
return false;
|
2012-08-27 23:46:24 +04:00
|
|
|
}
|
2012-06-11 20:17:35 +04:00
|
|
|
}
|
2010-10-15 02:26:59 +04:00
|
|
|
|
2012-06-11 20:17:35 +04:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-15 02:26:59 +04:00
|
|
|
|
2012-06-11 20:17:35 +04:00
|
|
|
// Check if X-Frame-Options permits this document to be loaded as a subdocument.
|
|
|
|
// This will iterate through and check any number of X-Frame-Options policies
|
|
|
|
// in the request (comma-separated in a header, multiple headers, etc).
|
|
|
|
bool nsDSURIContentListener::CheckFrameOptions(nsIRequest *request)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
|
|
|
|
if (!httpChannel) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString xfoHeaderCValue;
|
2012-06-11 20:17:35 +04:00
|
|
|
httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("X-Frame-Options"),
|
|
|
|
xfoHeaderCValue);
|
|
|
|
NS_ConvertUTF8toUTF16 xfoHeaderValue(xfoHeaderCValue);
|
|
|
|
|
|
|
|
// if no header value, there's nothing to do.
|
|
|
|
if (xfoHeaderValue.IsEmpty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// iterate through all the header values (usually there's only one, but can
|
|
|
|
// be many. If any want to deny the load, deny the load.
|
|
|
|
nsCharSeparatedTokenizer tokenizer(xfoHeaderValue, ',');
|
|
|
|
while (tokenizer.hasMoreTokens()) {
|
|
|
|
const nsSubstring& tok = tokenizer.nextToken();
|
|
|
|
if (!CheckOneFrameOptionsPolicy(request, tok)) {
|
|
|
|
// cancel the load and display about:blank
|
|
|
|
httpChannel->Cancel(NS_BINDING_ABORTED);
|
|
|
|
if (mDocShell) {
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav(do_QueryObject(mDocShell));
|
|
|
|
if (webNav) {
|
|
|
|
webNav->LoadURI(NS_LITERAL_STRING("about:blank").get(),
|
2012-07-30 18:20:58 +04:00
|
|
|
0, nullptr, nullptr, nullptr);
|
2012-06-11 20:17:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2010-10-15 02:26:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2012-09-21 06:47:18 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
nsDSURIContentListener::ReportXFOViolation(nsIDocShellTreeItem* aTopDocShellItem,
|
|
|
|
nsIURI* aThisURI,
|
|
|
|
XFOHeader aHeader)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> topOuterWindow = do_GetInterface(aTopDocShellItem);
|
|
|
|
if (!topOuterWindow)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NS_ASSERTION(topOuterWindow->IsOuterWindow(), "Huh?");
|
|
|
|
nsPIDOMWindow* topInnerWindow = topOuterWindow->GetCurrentInnerWindow();
|
|
|
|
if (!topInnerWindow)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> topURI;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
|
|
|
|
document = do_GetInterface(aTopDocShellItem);
|
|
|
|
rv = document->NodePrincipal()->GetURI(getter_AddRefs(topURI));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!topURI)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsCString topURIString;
|
|
|
|
nsCString thisURIString;
|
|
|
|
|
|
|
|
rv = topURI->GetSpec(topURIString);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
|
|
|
|
rv = aThisURI->GetSpec(thisURIString);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIConsoleService> consoleService =
|
|
|
|
do_GetService(NS_CONSOLESERVICE_CONTRACTID);
|
|
|
|
nsCOMPtr<nsIScriptError> errorObject =
|
|
|
|
do_CreateInstance(NS_SCRIPTERROR_CONTRACTID);
|
|
|
|
|
|
|
|
if (!consoleService || !errorObject)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsString msg = NS_LITERAL_STRING("Load denied by X-Frame-Options: ");
|
|
|
|
msg.Append(NS_ConvertUTF8toUTF16(thisURIString));
|
|
|
|
|
|
|
|
switch (aHeader) {
|
|
|
|
case eDENY:
|
|
|
|
msg.AppendLiteral(" does not permit framing.");
|
|
|
|
break;
|
|
|
|
case eSAMEORIGIN:
|
|
|
|
msg.AppendLiteral(" does not permit cross-origin framing.");
|
|
|
|
break;
|
|
|
|
case eALLOWFROM:
|
|
|
|
msg.AppendLiteral(" does not permit framing by ");
|
|
|
|
msg.Append(NS_ConvertUTF8toUTF16(topURIString));
|
|
|
|
msg.AppendLiteral(".");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = errorObject->InitWithWindowID(msg, EmptyString(), EmptyString(), 0, 0,
|
|
|
|
nsIScriptError::errorFlag,
|
|
|
|
"X-Frame-Options",
|
|
|
|
topInnerWindow->WindowID());
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
|
|
|
|
|
|
|
consoleService->LogMessage(errorObject);
|
|
|
|
}
|