void ImportLoader::Open() { AutoError ae(this, false); nsCOMPtr<nsILoadGroup> loadGroup = mImportParent->MasterDocument()->GetDocumentLoadGroup(); nsCOMPtr<nsIChannel> channel; nsresult rv = NS_NewChannel(getter_AddRefs(channel), mURI, mImportParent, nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS, nsIContentPolicy::TYPE_SUBDOCUMENT, loadGroup, nullptr, // aCallbacks nsIRequest::LOAD_BACKGROUND); NS_ENSURE_SUCCESS_VOID(rv); rv = channel->AsyncOpen2(this); NS_ENSURE_SUCCESS_VOID(rv); BlockScripts(); ae.Pass(); }
nsIPrincipal* ImportLoader::Principal() { MOZ_ASSERT(mImportParent); nsCOMPtr<nsIDocument> master = mImportParent->MasterDocument(); nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(master); MOZ_ASSERT(sop); return sop->GetPrincipal(); }
void ImportLoader::Open() { AutoError ae(this, false); // Imports should obey to the master documents CSP. nsCOMPtr<nsIDocument> master = mImportParent->MasterDocument(); nsIPrincipal* principal = Principal(); int16_t shouldLoad = nsIContentPolicy::ACCEPT; nsresult rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_SUBDOCUMENT, mURI, principal, mImportParent, NS_LITERAL_CSTRING("text/html"), /* extra = */ nullptr, &shouldLoad, nsContentUtils::GetContentPolicy(), nsContentUtils::GetSecurityManager()); if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) { NS_WARN_IF_FALSE(NS_CP_ACCEPTED(shouldLoad), "ImportLoader rejected by CSP"); return; } nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager(); rv = secMan->CheckLoadURIWithPrincipal(principal, mURI, nsIScriptSecurityManager::STANDARD); NS_ENSURE_SUCCESS_VOID(rv); nsCOMPtr<nsILoadGroup> loadGroup = master->GetDocumentLoadGroup(); nsCOMPtr<nsIChannel> channel; rv = NS_NewChannel(getter_AddRefs(channel), mURI, mImportParent, nsILoadInfo::SEC_NORMAL, nsIContentPolicy::TYPE_SUBDOCUMENT, loadGroup, nullptr, // aCallbacks nsIRequest::LOAD_BACKGROUND); NS_ENSURE_SUCCESS_VOID(rv); // Init CORSListenerProxy and omit credentials. nsRefPtr<nsCORSListenerProxy> corsListener = new nsCORSListenerProxy(this, principal, /* aWithCredentials */ false); rv = corsListener->Init(channel, true); NS_ENSURE_SUCCESS_VOID(rv); rv = channel->AsyncOpen(corsListener, nullptr); NS_ENSURE_SUCCESS_VOID(rv); BlockScripts(); ae.Pass(); }
NS_IMETHODIMP ImportLoader::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext) { AutoError ae(this); nsIPrincipal* principal = Principal(); nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest); if (!channel) { return NS_ERROR_DOM_ABORT_ERR; } if (nsContentUtils::IsSystemPrincipal(principal)) { // We should never import non-system documents and run their scripts with system principal! nsCOMPtr<nsIPrincipal> channelPrincipal; nsContentUtils::GetSecurityManager()->GetChannelResultPrincipal(channel, getter_AddRefs(channelPrincipal)); if (!nsContentUtils::IsSystemPrincipal(channelPrincipal)) { return NS_ERROR_FAILURE; } } channel->SetOwner(principal); nsAutoCString type; channel->GetContentType(type); if (!type.EqualsLiteral("text/html")) { NS_WARNING("ImportLoader wrong content type"); return NS_ERROR_DOM_ABORT_ERR; } // The scope object is same for all the imports in an import tree, // let's get it form the import parent. nsCOMPtr<nsIGlobalObject> global = mImportParent->GetScopeObject(); nsCOMPtr<nsIDOMDocument> importDoc; nsCOMPtr<nsIURI> baseURI = mImportParent->GetBaseURI(); const nsAString& emptyStr = EmptyString(); nsresult rv = NS_NewDOMDocument(getter_AddRefs(importDoc), emptyStr, emptyStr, nullptr, mURI, baseURI, principal, false, global, DocumentFlavorHTML); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); // The imported document must know which master document it belongs to. mDocument = do_QueryInterface(importDoc); nsCOMPtr<nsIDocument> master = mImportParent->MasterDocument(); mDocument->SetMasterDocument(master); // We want to inherit the sandbox flags and fullscreen enabled flag // from the master document. mDocument->SetSandboxFlags(master->GetSandboxFlags()); // We have to connect the blank document we created with the channel we opened, // and create its own LoadGroup for it. nsCOMPtr<nsIStreamListener> listener; nsCOMPtr<nsILoadGroup> loadGroup; channel->GetLoadGroup(getter_AddRefs(loadGroup)); nsCOMPtr<nsILoadGroup> newLoadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID); NS_ENSURE_TRUE(newLoadGroup, NS_ERROR_OUT_OF_MEMORY); newLoadGroup->SetLoadGroup(loadGroup); rv = mDocument->StartDocumentLoad("import", channel, newLoadGroup, nullptr, getter_AddRefs(listener), true); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); nsCOMPtr<nsIURI> originalURI; rv = channel->GetOriginalURI(getter_AddRefs(originalURI)); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); nsCOMPtr<nsIURI> URI; rv = channel->GetURI(getter_AddRefs(URI)); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); MOZ_ASSERT(URI, "URI of a channel should never be null"); bool equals; rv = URI->Equals(originalURI, &equals); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); if (!equals) { // In case of a redirection we must add the new URI to the import map. Manager()->AddLoaderWithNewURI(this, URI); } // Let's start the parser. mParserStreamListener = listener; rv = listener->OnStartRequest(aRequest, aContext); NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR); ae.Pass(); return NS_OK; }