Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsDocShell.h" |
michael@0 | 7 | #include "nsDSURIContentListener.h" |
michael@0 | 8 | #include "nsIChannel.h" |
michael@0 | 9 | #include "nsServiceManagerUtils.h" |
michael@0 | 10 | #include "nsDocShellCID.h" |
michael@0 | 11 | #include "nsIWebNavigationInfo.h" |
michael@0 | 12 | #include "nsIDocument.h" |
michael@0 | 13 | #include "nsIDOMWindow.h" |
michael@0 | 14 | #include "nsNetUtil.h" |
michael@0 | 15 | #include "nsAutoPtr.h" |
michael@0 | 16 | #include "nsIHttpChannel.h" |
michael@0 | 17 | #include "nsIScriptSecurityManager.h" |
michael@0 | 18 | #include "nsError.h" |
michael@0 | 19 | #include "nsCharSeparatedTokenizer.h" |
michael@0 | 20 | #include "nsIConsoleService.h" |
michael@0 | 21 | #include "nsIScriptError.h" |
michael@0 | 22 | #include "nsDocShellLoadTypes.h" |
michael@0 | 23 | |
michael@0 | 24 | using namespace mozilla; |
michael@0 | 25 | |
michael@0 | 26 | //***************************************************************************** |
michael@0 | 27 | //*** nsDSURIContentListener: Object Management |
michael@0 | 28 | //***************************************************************************** |
michael@0 | 29 | |
michael@0 | 30 | nsDSURIContentListener::nsDSURIContentListener(nsDocShell* aDocShell) |
michael@0 | 31 | : mDocShell(aDocShell), |
michael@0 | 32 | mParentContentListener(nullptr) |
michael@0 | 33 | { |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | nsDSURIContentListener::~nsDSURIContentListener() |
michael@0 | 37 | { |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | nsresult |
michael@0 | 41 | nsDSURIContentListener::Init() |
michael@0 | 42 | { |
michael@0 | 43 | nsresult rv; |
michael@0 | 44 | mNavInfo = do_GetService(NS_WEBNAVIGATION_INFO_CONTRACTID, &rv); |
michael@0 | 45 | NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to get webnav info"); |
michael@0 | 46 | return rv; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | |
michael@0 | 50 | //***************************************************************************** |
michael@0 | 51 | // nsDSURIContentListener::nsISupports |
michael@0 | 52 | //***************************************************************************** |
michael@0 | 53 | |
michael@0 | 54 | NS_IMPL_ADDREF(nsDSURIContentListener) |
michael@0 | 55 | NS_IMPL_RELEASE(nsDSURIContentListener) |
michael@0 | 56 | |
michael@0 | 57 | NS_INTERFACE_MAP_BEGIN(nsDSURIContentListener) |
michael@0 | 58 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIURIContentListener) |
michael@0 | 59 | NS_INTERFACE_MAP_ENTRY(nsIURIContentListener) |
michael@0 | 60 | NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) |
michael@0 | 61 | NS_INTERFACE_MAP_END |
michael@0 | 62 | |
michael@0 | 63 | //***************************************************************************** |
michael@0 | 64 | // nsDSURIContentListener::nsIURIContentListener |
michael@0 | 65 | //***************************************************************************** |
michael@0 | 66 | |
michael@0 | 67 | NS_IMETHODIMP |
michael@0 | 68 | nsDSURIContentListener::OnStartURIOpen(nsIURI* aURI, bool* aAbortOpen) |
michael@0 | 69 | { |
michael@0 | 70 | // If mDocShell is null here, that means someone's starting a load |
michael@0 | 71 | // in our docshell after it's already been destroyed. Don't let |
michael@0 | 72 | // that happen. |
michael@0 | 73 | if (!mDocShell) { |
michael@0 | 74 | *aAbortOpen = true; |
michael@0 | 75 | return NS_OK; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | nsCOMPtr<nsIURIContentListener> parentListener; |
michael@0 | 79 | GetParentContentListener(getter_AddRefs(parentListener)); |
michael@0 | 80 | if (parentListener) |
michael@0 | 81 | return parentListener->OnStartURIOpen(aURI, aAbortOpen); |
michael@0 | 82 | |
michael@0 | 83 | return NS_OK; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | NS_IMETHODIMP |
michael@0 | 87 | nsDSURIContentListener::DoContent(const char* aContentType, |
michael@0 | 88 | bool aIsContentPreferred, |
michael@0 | 89 | nsIRequest* request, |
michael@0 | 90 | nsIStreamListener** aContentHandler, |
michael@0 | 91 | bool* aAbortProcess) |
michael@0 | 92 | { |
michael@0 | 93 | nsresult rv; |
michael@0 | 94 | NS_ENSURE_ARG_POINTER(aContentHandler); |
michael@0 | 95 | NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE); |
michael@0 | 96 | |
michael@0 | 97 | // Check whether X-Frame-Options permits us to load this content in an |
michael@0 | 98 | // iframe and abort the load (unless we've disabled x-frame-options |
michael@0 | 99 | // checking). |
michael@0 | 100 | if (!CheckFrameOptions(request)) { |
michael@0 | 101 | *aAbortProcess = true; |
michael@0 | 102 | return NS_OK; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | *aAbortProcess = false; |
michael@0 | 106 | |
michael@0 | 107 | // determine if the channel has just been retargeted to us... |
michael@0 | 108 | nsLoadFlags loadFlags = 0; |
michael@0 | 109 | nsCOMPtr<nsIChannel> aOpenedChannel = do_QueryInterface(request); |
michael@0 | 110 | |
michael@0 | 111 | if (aOpenedChannel) |
michael@0 | 112 | aOpenedChannel->GetLoadFlags(&loadFlags); |
michael@0 | 113 | |
michael@0 | 114 | if(loadFlags & nsIChannel::LOAD_RETARGETED_DOCUMENT_URI) |
michael@0 | 115 | { |
michael@0 | 116 | // XXX: Why does this not stop the content too? |
michael@0 | 117 | mDocShell->Stop(nsIWebNavigation::STOP_NETWORK); |
michael@0 | 118 | |
michael@0 | 119 | mDocShell->SetLoadType(aIsContentPreferred ? LOAD_LINK : LOAD_NORMAL); |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | rv = mDocShell->CreateContentViewer(aContentType, request, aContentHandler); |
michael@0 | 123 | |
michael@0 | 124 | if (rv == NS_ERROR_REMOTE_XUL) { |
michael@0 | 125 | request->Cancel(rv); |
michael@0 | 126 | *aAbortProcess = true; |
michael@0 | 127 | return NS_OK; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | if (NS_FAILED(rv)) { |
michael@0 | 131 | // we don't know how to handle the content |
michael@0 | 132 | *aContentHandler = nullptr; |
michael@0 | 133 | return rv; |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | if (loadFlags & nsIChannel::LOAD_RETARGETED_DOCUMENT_URI) { |
michael@0 | 137 | nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(static_cast<nsIDocShell*>(mDocShell)); |
michael@0 | 138 | NS_ENSURE_TRUE(domWindow, NS_ERROR_FAILURE); |
michael@0 | 139 | domWindow->Focus(); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | return NS_OK; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | NS_IMETHODIMP |
michael@0 | 146 | nsDSURIContentListener::IsPreferred(const char* aContentType, |
michael@0 | 147 | char ** aDesiredContentType, |
michael@0 | 148 | bool* aCanHandle) |
michael@0 | 149 | { |
michael@0 | 150 | NS_ENSURE_ARG_POINTER(aCanHandle); |
michael@0 | 151 | NS_ENSURE_ARG_POINTER(aDesiredContentType); |
michael@0 | 152 | |
michael@0 | 153 | // the docshell has no idea if it is the preferred content provider or not. |
michael@0 | 154 | // It needs to ask its parent if it is the preferred content handler or not... |
michael@0 | 155 | |
michael@0 | 156 | nsCOMPtr<nsIURIContentListener> parentListener; |
michael@0 | 157 | GetParentContentListener(getter_AddRefs(parentListener)); |
michael@0 | 158 | if (parentListener) { |
michael@0 | 159 | return parentListener->IsPreferred(aContentType, |
michael@0 | 160 | aDesiredContentType, |
michael@0 | 161 | aCanHandle); |
michael@0 | 162 | } |
michael@0 | 163 | // we used to return false here if we didn't have a parent properly |
michael@0 | 164 | // registered at the top of the docshell hierarchy to dictate what |
michael@0 | 165 | // content types this docshell should be a preferred handler for. But |
michael@0 | 166 | // this really makes it hard for developers using iframe or browser tags |
michael@0 | 167 | // because then they need to make sure they implement |
michael@0 | 168 | // nsIURIContentListener otherwise all link clicks would get sent to |
michael@0 | 169 | // another window because we said we weren't the preferred handler type. |
michael@0 | 170 | // I'm going to change the default now...if we can handle the content, |
michael@0 | 171 | // and someone didn't EXPLICITLY set a nsIURIContentListener at the top |
michael@0 | 172 | // of our docshell chain, then we'll now always attempt to process the |
michael@0 | 173 | // content ourselves... |
michael@0 | 174 | return CanHandleContent(aContentType, |
michael@0 | 175 | true, |
michael@0 | 176 | aDesiredContentType, |
michael@0 | 177 | aCanHandle); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | NS_IMETHODIMP |
michael@0 | 181 | nsDSURIContentListener::CanHandleContent(const char* aContentType, |
michael@0 | 182 | bool aIsContentPreferred, |
michael@0 | 183 | char ** aDesiredContentType, |
michael@0 | 184 | bool* aCanHandleContent) |
michael@0 | 185 | { |
michael@0 | 186 | NS_PRECONDITION(aCanHandleContent, "Null out param?"); |
michael@0 | 187 | NS_ENSURE_ARG_POINTER(aDesiredContentType); |
michael@0 | 188 | |
michael@0 | 189 | *aCanHandleContent = false; |
michael@0 | 190 | *aDesiredContentType = nullptr; |
michael@0 | 191 | |
michael@0 | 192 | nsresult rv = NS_OK; |
michael@0 | 193 | if (aContentType) { |
michael@0 | 194 | uint32_t canHandle = nsIWebNavigationInfo::UNSUPPORTED; |
michael@0 | 195 | rv = mNavInfo->IsTypeSupported(nsDependentCString(aContentType), |
michael@0 | 196 | mDocShell, |
michael@0 | 197 | &canHandle); |
michael@0 | 198 | *aCanHandleContent = (canHandle != nsIWebNavigationInfo::UNSUPPORTED); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | return rv; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | NS_IMETHODIMP |
michael@0 | 205 | nsDSURIContentListener::GetLoadCookie(nsISupports ** aLoadCookie) |
michael@0 | 206 | { |
michael@0 | 207 | NS_IF_ADDREF(*aLoadCookie = nsDocShell::GetAsSupports(mDocShell)); |
michael@0 | 208 | return NS_OK; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | NS_IMETHODIMP |
michael@0 | 212 | nsDSURIContentListener::SetLoadCookie(nsISupports * aLoadCookie) |
michael@0 | 213 | { |
michael@0 | 214 | #ifdef DEBUG |
michael@0 | 215 | nsRefPtr<nsDocLoader> cookieAsDocLoader = |
michael@0 | 216 | nsDocLoader::GetAsDocLoader(aLoadCookie); |
michael@0 | 217 | NS_ASSERTION(cookieAsDocLoader && cookieAsDocLoader == mDocShell, |
michael@0 | 218 | "Invalid load cookie being set!"); |
michael@0 | 219 | #endif |
michael@0 | 220 | return NS_OK; |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | NS_IMETHODIMP |
michael@0 | 224 | nsDSURIContentListener::GetParentContentListener(nsIURIContentListener** |
michael@0 | 225 | aParentListener) |
michael@0 | 226 | { |
michael@0 | 227 | if (mWeakParentContentListener) |
michael@0 | 228 | { |
michael@0 | 229 | nsCOMPtr<nsIURIContentListener> tempListener = |
michael@0 | 230 | do_QueryReferent(mWeakParentContentListener); |
michael@0 | 231 | *aParentListener = tempListener; |
michael@0 | 232 | NS_IF_ADDREF(*aParentListener); |
michael@0 | 233 | } |
michael@0 | 234 | else { |
michael@0 | 235 | *aParentListener = mParentContentListener; |
michael@0 | 236 | NS_IF_ADDREF(*aParentListener); |
michael@0 | 237 | } |
michael@0 | 238 | return NS_OK; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | NS_IMETHODIMP |
michael@0 | 242 | nsDSURIContentListener::SetParentContentListener(nsIURIContentListener* |
michael@0 | 243 | aParentListener) |
michael@0 | 244 | { |
michael@0 | 245 | if (aParentListener) |
michael@0 | 246 | { |
michael@0 | 247 | // Store the parent listener as a weak ref. Parents not supporting |
michael@0 | 248 | // nsISupportsWeakReference assert but may still be used. |
michael@0 | 249 | mParentContentListener = nullptr; |
michael@0 | 250 | mWeakParentContentListener = do_GetWeakReference(aParentListener); |
michael@0 | 251 | if (!mWeakParentContentListener) |
michael@0 | 252 | { |
michael@0 | 253 | mParentContentListener = aParentListener; |
michael@0 | 254 | } |
michael@0 | 255 | } |
michael@0 | 256 | else |
michael@0 | 257 | { |
michael@0 | 258 | mWeakParentContentListener = nullptr; |
michael@0 | 259 | mParentContentListener = nullptr; |
michael@0 | 260 | } |
michael@0 | 261 | return NS_OK; |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | bool nsDSURIContentListener::CheckOneFrameOptionsPolicy(nsIHttpChannel *httpChannel, |
michael@0 | 265 | const nsAString& policy) { |
michael@0 | 266 | static const char allowFrom[] = "allow-from"; |
michael@0 | 267 | const uint32_t allowFromLen = ArrayLength(allowFrom) - 1; |
michael@0 | 268 | bool isAllowFrom = |
michael@0 | 269 | StringHead(policy, allowFromLen).LowerCaseEqualsLiteral(allowFrom); |
michael@0 | 270 | |
michael@0 | 271 | // return early if header does not have one of the values with meaning |
michael@0 | 272 | if (!policy.LowerCaseEqualsLiteral("deny") && |
michael@0 | 273 | !policy.LowerCaseEqualsLiteral("sameorigin") && |
michael@0 | 274 | !isAllowFrom) |
michael@0 | 275 | return true; |
michael@0 | 276 | |
michael@0 | 277 | nsCOMPtr<nsIURI> uri; |
michael@0 | 278 | httpChannel->GetURI(getter_AddRefs(uri)); |
michael@0 | 279 | |
michael@0 | 280 | // XXXkhuey when does this happen? Is returning true safe here? |
michael@0 | 281 | if (!mDocShell) { |
michael@0 | 282 | return true; |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | // We need to check the location of this window and the location of the top |
michael@0 | 286 | // window, if we're not the top. X-F-O: SAMEORIGIN requires that the |
michael@0 | 287 | // document must be same-origin with top window. X-F-O: DENY requires that |
michael@0 | 288 | // the document must never be framed. |
michael@0 | 289 | nsCOMPtr<nsIDOMWindow> thisWindow = do_GetInterface(static_cast<nsIDocShell*>(mDocShell)); |
michael@0 | 290 | // If we don't have DOMWindow there is no risk of clickjacking |
michael@0 | 291 | if (!thisWindow) |
michael@0 | 292 | return true; |
michael@0 | 293 | |
michael@0 | 294 | // GetScriptableTop, not GetTop, because we want this to respect |
michael@0 | 295 | // <iframe mozbrowser> boundaries. |
michael@0 | 296 | nsCOMPtr<nsIDOMWindow> topWindow; |
michael@0 | 297 | thisWindow->GetScriptableTop(getter_AddRefs(topWindow)); |
michael@0 | 298 | |
michael@0 | 299 | // if the document is in the top window, it's not in a frame. |
michael@0 | 300 | if (thisWindow == topWindow) |
michael@0 | 301 | return true; |
michael@0 | 302 | |
michael@0 | 303 | // Find the top docshell in our parent chain that doesn't have the system |
michael@0 | 304 | // principal and use it for the principal comparison. Finding the top |
michael@0 | 305 | // content-type docshell doesn't work because some chrome documents are |
michael@0 | 306 | // loaded in content docshells (see bug 593387). |
michael@0 | 307 | nsCOMPtr<nsIDocShellTreeItem> thisDocShellItem(do_QueryInterface( |
michael@0 | 308 | static_cast<nsIDocShell*> (mDocShell))); |
michael@0 | 309 | nsCOMPtr<nsIDocShellTreeItem> parentDocShellItem, |
michael@0 | 310 | curDocShellItem = thisDocShellItem; |
michael@0 | 311 | nsCOMPtr<nsIDocument> topDoc; |
michael@0 | 312 | nsresult rv; |
michael@0 | 313 | nsCOMPtr<nsIScriptSecurityManager> ssm = |
michael@0 | 314 | do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv); |
michael@0 | 315 | if (!ssm) { |
michael@0 | 316 | MOZ_CRASH(); |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | // Traverse up the parent chain and stop when we see a docshell whose |
michael@0 | 320 | // parent has a system principal, or a docshell corresponding to |
michael@0 | 321 | // <iframe mozbrowser>. |
michael@0 | 322 | while (NS_SUCCEEDED(curDocShellItem->GetParent(getter_AddRefs(parentDocShellItem))) && |
michael@0 | 323 | parentDocShellItem) { |
michael@0 | 324 | |
michael@0 | 325 | nsCOMPtr<nsIDocShell> curDocShell = do_QueryInterface(curDocShellItem); |
michael@0 | 326 | if (curDocShell && curDocShell->GetIsBrowserOrApp()) { |
michael@0 | 327 | break; |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | bool system = false; |
michael@0 | 331 | topDoc = do_GetInterface(parentDocShellItem); |
michael@0 | 332 | if (topDoc) { |
michael@0 | 333 | if (NS_SUCCEEDED(ssm->IsSystemPrincipal(topDoc->NodePrincipal(), |
michael@0 | 334 | &system)) && system) { |
michael@0 | 335 | // Found a system-principled doc: last docshell was top. |
michael@0 | 336 | break; |
michael@0 | 337 | } |
michael@0 | 338 | } |
michael@0 | 339 | else { |
michael@0 | 340 | return false; |
michael@0 | 341 | } |
michael@0 | 342 | curDocShellItem = parentDocShellItem; |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | // If this document has the top non-SystemPrincipal docshell it is not being |
michael@0 | 346 | // framed or it is being framed by a chrome document, which we allow. |
michael@0 | 347 | if (curDocShellItem == thisDocShellItem) |
michael@0 | 348 | return true; |
michael@0 | 349 | |
michael@0 | 350 | // If the value of the header is DENY, and the previous condition is |
michael@0 | 351 | // not met (current docshell is not the top docshell), prohibit the |
michael@0 | 352 | // load. |
michael@0 | 353 | if (policy.LowerCaseEqualsLiteral("deny")) { |
michael@0 | 354 | ReportXFOViolation(curDocShellItem, uri, eDENY); |
michael@0 | 355 | return false; |
michael@0 | 356 | } |
michael@0 | 357 | |
michael@0 | 358 | topDoc = do_GetInterface(curDocShellItem); |
michael@0 | 359 | nsCOMPtr<nsIURI> topUri; |
michael@0 | 360 | topDoc->NodePrincipal()->GetURI(getter_AddRefs(topUri)); |
michael@0 | 361 | |
michael@0 | 362 | // If the X-Frame-Options value is SAMEORIGIN, then the top frame in the |
michael@0 | 363 | // parent chain must be from the same origin as this document. |
michael@0 | 364 | if (policy.LowerCaseEqualsLiteral("sameorigin")) { |
michael@0 | 365 | rv = ssm->CheckSameOriginURI(uri, topUri, true); |
michael@0 | 366 | if (NS_FAILED(rv)) { |
michael@0 | 367 | ReportXFOViolation(curDocShellItem, uri, eSAMEORIGIN); |
michael@0 | 368 | return false; /* wasn't same-origin */ |
michael@0 | 369 | } |
michael@0 | 370 | } |
michael@0 | 371 | |
michael@0 | 372 | // If the X-Frame-Options value is "allow-from [uri]", then the top |
michael@0 | 373 | // frame in the parent chain must be from that origin |
michael@0 | 374 | if (isAllowFrom) { |
michael@0 | 375 | if (policy.Length() == allowFromLen || |
michael@0 | 376 | (policy[allowFromLen] != ' ' && |
michael@0 | 377 | policy[allowFromLen] != '\t')) { |
michael@0 | 378 | ReportXFOViolation(curDocShellItem, uri, eALLOWFROM); |
michael@0 | 379 | return false; |
michael@0 | 380 | } |
michael@0 | 381 | rv = NS_NewURI(getter_AddRefs(uri), |
michael@0 | 382 | Substring(policy, allowFromLen)); |
michael@0 | 383 | if (NS_FAILED(rv)) |
michael@0 | 384 | return false; |
michael@0 | 385 | |
michael@0 | 386 | rv = ssm->CheckSameOriginURI(uri, topUri, true); |
michael@0 | 387 | if (NS_FAILED(rv)) { |
michael@0 | 388 | ReportXFOViolation(curDocShellItem, uri, eALLOWFROM); |
michael@0 | 389 | return false; |
michael@0 | 390 | } |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | return true; |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | // Check if X-Frame-Options permits this document to be loaded as a subdocument. |
michael@0 | 397 | // This will iterate through and check any number of X-Frame-Options policies |
michael@0 | 398 | // in the request (comma-separated in a header, multiple headers, etc). |
michael@0 | 399 | bool nsDSURIContentListener::CheckFrameOptions(nsIRequest *request) |
michael@0 | 400 | { |
michael@0 | 401 | nsresult rv; |
michael@0 | 402 | nsCOMPtr<nsIChannel> chan = do_QueryInterface(request); |
michael@0 | 403 | if (!chan) { |
michael@0 | 404 | return true; |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(chan); |
michael@0 | 408 | if (!httpChannel) { |
michael@0 | 409 | // check if it is hiding in a multipart channel |
michael@0 | 410 | rv = mDocShell->GetHttpChannel(chan, getter_AddRefs(httpChannel)); |
michael@0 | 411 | if (NS_FAILED(rv)) |
michael@0 | 412 | return false; |
michael@0 | 413 | } |
michael@0 | 414 | |
michael@0 | 415 | if (!httpChannel) { |
michael@0 | 416 | return true; |
michael@0 | 417 | } |
michael@0 | 418 | |
michael@0 | 419 | nsAutoCString xfoHeaderCValue; |
michael@0 | 420 | httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("X-Frame-Options"), |
michael@0 | 421 | xfoHeaderCValue); |
michael@0 | 422 | NS_ConvertUTF8toUTF16 xfoHeaderValue(xfoHeaderCValue); |
michael@0 | 423 | |
michael@0 | 424 | // if no header value, there's nothing to do. |
michael@0 | 425 | if (xfoHeaderValue.IsEmpty()) |
michael@0 | 426 | return true; |
michael@0 | 427 | |
michael@0 | 428 | // iterate through all the header values (usually there's only one, but can |
michael@0 | 429 | // be many. If any want to deny the load, deny the load. |
michael@0 | 430 | nsCharSeparatedTokenizer tokenizer(xfoHeaderValue, ','); |
michael@0 | 431 | while (tokenizer.hasMoreTokens()) { |
michael@0 | 432 | const nsSubstring& tok = tokenizer.nextToken(); |
michael@0 | 433 | if (!CheckOneFrameOptionsPolicy(httpChannel, tok)) { |
michael@0 | 434 | // cancel the load and display about:blank |
michael@0 | 435 | httpChannel->Cancel(NS_BINDING_ABORTED); |
michael@0 | 436 | if (mDocShell) { |
michael@0 | 437 | nsCOMPtr<nsIWebNavigation> webNav(do_QueryObject(mDocShell)); |
michael@0 | 438 | if (webNav) { |
michael@0 | 439 | webNav->LoadURI(MOZ_UTF16("about:blank"), |
michael@0 | 440 | 0, nullptr, nullptr, nullptr); |
michael@0 | 441 | } |
michael@0 | 442 | } |
michael@0 | 443 | return false; |
michael@0 | 444 | } |
michael@0 | 445 | } |
michael@0 | 446 | |
michael@0 | 447 | return true; |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | void |
michael@0 | 451 | nsDSURIContentListener::ReportXFOViolation(nsIDocShellTreeItem* aTopDocShellItem, |
michael@0 | 452 | nsIURI* aThisURI, |
michael@0 | 453 | XFOHeader aHeader) |
michael@0 | 454 | { |
michael@0 | 455 | nsresult rv = NS_OK; |
michael@0 | 456 | |
michael@0 | 457 | nsCOMPtr<nsPIDOMWindow> topOuterWindow = do_GetInterface(aTopDocShellItem); |
michael@0 | 458 | if (!topOuterWindow) |
michael@0 | 459 | return; |
michael@0 | 460 | |
michael@0 | 461 | NS_ASSERTION(topOuterWindow->IsOuterWindow(), "Huh?"); |
michael@0 | 462 | nsPIDOMWindow* topInnerWindow = topOuterWindow->GetCurrentInnerWindow(); |
michael@0 | 463 | if (!topInnerWindow) |
michael@0 | 464 | return; |
michael@0 | 465 | |
michael@0 | 466 | nsCOMPtr<nsIURI> topURI; |
michael@0 | 467 | |
michael@0 | 468 | nsCOMPtr<nsIDocument> document; |
michael@0 | 469 | |
michael@0 | 470 | document = do_GetInterface(aTopDocShellItem); |
michael@0 | 471 | rv = document->NodePrincipal()->GetURI(getter_AddRefs(topURI)); |
michael@0 | 472 | if (NS_FAILED(rv)) |
michael@0 | 473 | return; |
michael@0 | 474 | |
michael@0 | 475 | if (!topURI) |
michael@0 | 476 | return; |
michael@0 | 477 | |
michael@0 | 478 | nsCString topURIString; |
michael@0 | 479 | nsCString thisURIString; |
michael@0 | 480 | |
michael@0 | 481 | rv = topURI->GetSpec(topURIString); |
michael@0 | 482 | if (NS_FAILED(rv)) |
michael@0 | 483 | return; |
michael@0 | 484 | |
michael@0 | 485 | rv = aThisURI->GetSpec(thisURIString); |
michael@0 | 486 | if (NS_FAILED(rv)) |
michael@0 | 487 | return; |
michael@0 | 488 | |
michael@0 | 489 | nsCOMPtr<nsIConsoleService> consoleService = |
michael@0 | 490 | do_GetService(NS_CONSOLESERVICE_CONTRACTID); |
michael@0 | 491 | nsCOMPtr<nsIScriptError> errorObject = |
michael@0 | 492 | do_CreateInstance(NS_SCRIPTERROR_CONTRACTID); |
michael@0 | 493 | |
michael@0 | 494 | if (!consoleService || !errorObject) |
michael@0 | 495 | return; |
michael@0 | 496 | |
michael@0 | 497 | nsString msg = NS_LITERAL_STRING("Load denied by X-Frame-Options: "); |
michael@0 | 498 | msg.Append(NS_ConvertUTF8toUTF16(thisURIString)); |
michael@0 | 499 | |
michael@0 | 500 | switch (aHeader) { |
michael@0 | 501 | case eDENY: |
michael@0 | 502 | msg.AppendLiteral(" does not permit framing."); |
michael@0 | 503 | break; |
michael@0 | 504 | case eSAMEORIGIN: |
michael@0 | 505 | msg.AppendLiteral(" does not permit cross-origin framing."); |
michael@0 | 506 | break; |
michael@0 | 507 | case eALLOWFROM: |
michael@0 | 508 | msg.AppendLiteral(" does not permit framing by "); |
michael@0 | 509 | msg.Append(NS_ConvertUTF8toUTF16(topURIString)); |
michael@0 | 510 | msg.AppendLiteral("."); |
michael@0 | 511 | break; |
michael@0 | 512 | } |
michael@0 | 513 | |
michael@0 | 514 | rv = errorObject->InitWithWindowID(msg, EmptyString(), EmptyString(), 0, 0, |
michael@0 | 515 | nsIScriptError::errorFlag, |
michael@0 | 516 | "X-Frame-Options", |
michael@0 | 517 | topInnerWindow->WindowID()); |
michael@0 | 518 | if (NS_FAILED(rv)) |
michael@0 | 519 | return; |
michael@0 | 520 | |
michael@0 | 521 | consoleService->LogMessage(errorObject); |
michael@0 | 522 | } |