From ab175b2de3b0dd62b3894e10cbbe436d465ad43f Mon Sep 17 00:00:00 2001 From: "mstoltz%netscape.com" Date: Tue, 13 Jun 2000 23:56:30 +0000 Subject: [PATCH] Fix for bug 31818. Dogfood. r=brendan git-svn-id: svn://10.0.0.236/trunk@72152 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/docshell/base/nsDocShell.cpp | 67 +++++++++++++------ mozilla/docshell/base/nsDocShell.h | 9 +-- mozilla/docshell/base/nsDocShellLoadInfo.cpp | 15 +++++ mozilla/docshell/base/nsDocShellLoadInfo.h | 1 + mozilla/docshell/base/nsIDocShellLoadInfo.idl | 8 ++- mozilla/docshell/base/nsWebShell.cpp | 5 +- mozilla/dom/src/jsurl/nsJSProtocolHandler.cpp | 43 +++++------- mozilla/webshell/src/nsWebShell.cpp | 5 +- 8 files changed, 102 insertions(+), 51 deletions(-) diff --git a/mozilla/docshell/base/nsDocShell.cpp b/mozilla/docshell/base/nsDocShell.cpp index 19d20a29e7d..7c33ad4a175 100644 --- a/mozilla/docshell/base/nsDocShell.cpp +++ b/mozilla/docshell/base/nsDocShell.cpp @@ -55,6 +55,7 @@ // Interfaces Needed #include "nsICharsetConverterManager.h" #include "nsIHTTPChannel.h" +#include "nsIDataChannel.h" #include "nsIProgressEventSink.h" #include "nsILayoutHistoryState.h" #include "nsILocaleService.h" @@ -62,6 +63,8 @@ #include "nsITimer.h" #include "nsIFileStream.h" +#include "nsIPrincipal.h" + // For reporting errors with the console service. // These can go away if error reporting is propagated up past nsDocShell. #include "nsIConsoleService.h" @@ -204,6 +207,7 @@ NS_IMETHODIMP nsDocShell::LoadURI(nsIURI* aURI, nsIDocShellLoadInfo* aLoadInfo) NS_ENSURE_ARG(aURI); nsCOMPtr referrer; + nsCOMPtr owner; PRBool replace = PR_FALSE; PRBool refresh = PR_FALSE; if(aLoadInfo) @@ -211,10 +215,10 @@ NS_IMETHODIMP nsDocShell::LoadURI(nsIURI* aURI, nsIDocShellLoadInfo* aLoadInfo) aLoadInfo->GetReferrer(getter_AddRefs(referrer)); aLoadInfo->GetReplaceSessionHistorySlot(&replace); aLoadInfo->GetRefresh(&refresh); + aLoadInfo->GetOwner(getter_AddRefs(owner)); } - - NS_ENSURE_SUCCESS(InternalLoad(aURI, referrer, nsnull, nsnull, + NS_ENSURE_SUCCESS(InternalLoad(aURI, referrer, owner, nsnull, nsnull, replace ? loadNormalReplace : (refresh ? loadRefresh : loadNormal)), NS_ERROR_FAILURE); return NS_OK; @@ -1090,7 +1094,7 @@ NS_IMETHODIMP nsDocShell::Reload(PRInt32 aReloadType) type = loadReloadBypassProxyAndCache; NS_ENSURE_SUCCESS(InternalLoad(mCurrentURI, mReferrerURI, nsnull, nsnull, - type), NS_ERROR_FAILURE); + nsnull, type), NS_ERROR_FAILURE); return NS_OK; } @@ -2358,7 +2362,8 @@ NS_IMETHODIMP nsDocShell::SetupNewViewer(nsIContentViewer* aNewViewer) //***************************************************************************** NS_IMETHODIMP nsDocShell::InternalLoad(nsIURI* aURI, nsIURI* aReferrer, - const char* aWindowTarget, nsIInputStream* aPostData, loadType aLoadType) + nsISupports* aOwner, const char* aWindowTarget, nsIInputStream* aPostData, + loadType aLoadType) { // Check to see if the new URI is an anchor in the existing document. if (aLoadType == loadNormal || @@ -2385,7 +2390,7 @@ NS_IMETHODIMP nsDocShell::InternalLoad(nsIURI* aURI, nsIURI* aReferrer, nsURILoadCommand loadCmd = nsIURILoader::viewNormal; if(loadLink == aLoadType) loadCmd = nsIURILoader::viewUserClick; - NS_ENSURE_SUCCESS(DoURILoad(aURI, aReferrer, loadCmd, aWindowTarget, + NS_ENSURE_SUCCESS(DoURILoad(aURI, aReferrer, aOwner, loadCmd, aWindowTarget, aPostData), NS_ERROR_FAILURE); return NS_OK; @@ -2597,8 +2602,24 @@ NS_IMETHODIMP nsDocShell::KeywordURIFixup(const PRUnichar* aStringURI, return NS_ERROR_FAILURE; } -NS_IMETHODIMP nsDocShell::DoURILoad(nsIURI* aURI, nsIURI* aReferrerURI, - nsURILoadCommand aLoadCmd, const char* aWindowTarget, +NS_IMETHODIMP nsDocShell::GetCurrentDocumentOwner(nsISupports** aOwner) +{ + nsresult rv; + *aOwner = nsnull; + nsCOMPtr docv(do_QueryInterface(mContentViewer)); + if (!docv) return NS_ERROR_FAILURE; + nsCOMPtr doc; + rv = docv->GetDocument(*getter_AddRefs(doc)); + if (NS_FAILED(rv) || !doc) return NS_ERROR_FAILURE; + nsCOMPtr principal; + rv = doc->GetPrincipal(getter_AddRefs(principal)); + if (NS_FAILED(rv) || !principal) return NS_ERROR_FAILURE; + rv = principal->QueryInterface(NS_GET_IID(nsISupports),(void**)aOwner); + return rv; +} + +NS_IMETHODIMP nsDocShell::DoURILoad(nsIURI* aURI, nsIURI* aReferrerURI, + nsISupports* aOwner, nsURILoadCommand aLoadCmd, const char* aWindowTarget, nsIInputStream* aPostData) { nsCOMPtr uriLoader(do_GetService(NS_URI_LOADER_PROGID)); @@ -2622,17 +2643,7 @@ NS_IMETHODIMP nsDocShell::DoURILoad(nsIURI* aURI, nsIURI* aReferrerURI, else return NS_ERROR_FAILURE; } - - //XXX Wrong, but needed for now. See bug 31818. - static const char kJavaScriptScheme[] = "javascript"; - nsXPIDLCString scheme; - aURI->GetScheme(getter_Copies(scheme)); - if (0 == PL_strncasecmp(NS_STATIC_CAST(const char*, scheme), kJavaScriptScheme, sizeof(kJavaScriptScheme) - 1)) { - channel->SetOriginalURI(aReferrerURI ? aReferrerURI : aURI); - } - else { - channel->SetOriginalURI(aURI); - } + channel->SetOriginalURI(aURI); // Mark the channel as being a document URI... nsLoadFlags loadAttribs = 0; @@ -2706,6 +2717,24 @@ NS_IMETHODIMP nsDocShell::DoURILoad(nsIURI* aURI, nsIURI* aReferrerURI, httpChannel->SetReferrer(aReferrerURI, nsIHTTPChannel::REFERRER_LINK_CLICK); } + else + { + nsCOMPtr ioChannel(do_QueryInterface(channel)); + if(ioChannel) // Might be a javascript: URL load, need to set owner + { + static const char jsSchemeName[] = "javascript"; + char* scheme; + aURI->GetScheme(&scheme); + if (PL_strcasecmp(scheme, jsSchemeName) == 0) + channel->SetOwner(aOwner); + } + else + { // Also set owner for data: URLs + nsCOMPtr dataChannel(do_QueryInterface(channel)); + if (dataChannel) + channel->SetOwner(aOwner); + } + } NS_ENSURE_SUCCESS(uriLoader->OpenURI(channel, aLoadCmd, aWindowTarget, NS_STATIC_CAST(nsIDocShell*, this)), NS_ERROR_FAILURE); @@ -3143,7 +3172,7 @@ NS_IMETHODIMP nsDocShell::LoadHistoryEntry(nsISHEntry* aEntry) } - NS_ENSURE_SUCCESS(InternalLoad(uri, nsnull, nsnull, postData, loadHistory), + NS_ENSURE_SUCCESS(InternalLoad(uri, nsnull, nsnull, nsnull, postData, loadHistory), NS_ERROR_FAILURE); return NS_OK; diff --git a/mozilla/docshell/base/nsDocShell.h b/mozilla/docshell/base/nsDocShell.h index 481eeb94c6f..7f477da44bc 100644 --- a/mozilla/docshell/base/nsDocShell.h +++ b/mozilla/docshell/base/nsDocShell.h @@ -184,15 +184,16 @@ protected: loadRefresh } loadType; - NS_IMETHOD InternalLoad(nsIURI* aURI, nsIURI* aReferrerURI, - const char* aWindowTarget=nsnull, nsIInputStream* aPostData=nsnull, - loadType aLoadType=loadNormal); + NS_IMETHOD InternalLoad(nsIURI* aURI, nsIURI* aReferrerURI, + nsISupports* owner, const char* aWindowTarget=nsnull, + nsIInputStream* aPostData=nsnull, loadType aLoadType=loadNormal); NS_IMETHOD CreateFixupURI(const PRUnichar* aStringURI, nsIURI** aURI); NS_IMETHOD FileURIFixup(const PRUnichar* aStringURI, nsIURI** aURI); NS_IMETHOD ConvertFileToStringURI(nsString& aIn, nsString& aOut); NS_IMETHOD ConvertStringURIToFileCharset(nsString& aIn, nsCString& aOut); NS_IMETHOD KeywordURIFixup(const PRUnichar* aStringURI, nsIURI** aURI); - NS_IMETHOD DoURILoad(nsIURI* aURI, nsIURI* aReferrer, + NS_IMETHOD GetCurrentDocumentOwner(nsISupports** aOwner); + NS_IMETHOD DoURILoad(nsIURI* aURI, nsIURI* aReferrer, nsISupports *aOwner, nsURILoadCommand aLoadCmd, const char* aWindowTarget, nsIInputStream* aPostData); NS_IMETHOD StopCurrentLoads(); diff --git a/mozilla/docshell/base/nsDocShellLoadInfo.cpp b/mozilla/docshell/base/nsDocShellLoadInfo.cpp index 898ec72fa31..fd4fa1e9da2 100644 --- a/mozilla/docshell/base/nsDocShellLoadInfo.cpp +++ b/mozilla/docshell/base/nsDocShellLoadInfo.cpp @@ -96,6 +96,21 @@ NS_IMETHODIMP nsDocShellLoadInfo::SetRefresh(PRBool aRefresh) return NS_OK; } +NS_IMETHODIMP nsDocShellLoadInfo::GetOwner(nsISupports** aOwner) +{ + NS_ENSURE_ARG_POINTER(aOwner); + + *aOwner = mOwner; + NS_IF_ADDREF(*aOwner); + return NS_OK; +} + +NS_IMETHODIMP nsDocShellLoadInfo::SetOwner(nsISupports* aOwner) +{ + mOwner = aOwner; + return NS_OK; +} + //***************************************************************************** // nsDocShellLoadInfo: Helpers //***************************************************************************** diff --git a/mozilla/docshell/base/nsDocShellLoadInfo.h b/mozilla/docshell/base/nsDocShellLoadInfo.h index bea0bbded22..3fc5c8be4bf 100644 --- a/mozilla/docshell/base/nsDocShellLoadInfo.h +++ b/mozilla/docshell/base/nsDocShellLoadInfo.h @@ -47,6 +47,7 @@ protected: nsCOMPtr mReferrer; PRBool mReplaceSessionHistorySlot; PRBool mRefresh; + nsCOMPtr mOwner; }; #endif /* nsDocShellLoadInfo_h__ */ diff --git a/mozilla/docshell/base/nsIDocShellLoadInfo.idl b/mozilla/docshell/base/nsIDocShellLoadInfo.idl index 1048ff64183..faf1e9e24db 100644 --- a/mozilla/docshell/base/nsIDocShellLoadInfo.idl +++ b/mozilla/docshell/base/nsIDocShellLoadInfo.idl @@ -49,4 +49,10 @@ interface nsIDocShellLoadInfo : nsISupports directive */ attribute boolean refresh; -}; \ No newline at end of file + + /* + The owner of the load, that is, the entity responsible for + causing the load to occur. This should be a nsIPrincipal typically. + */ + attribute nsISupports owner; +}; diff --git a/mozilla/docshell/base/nsWebShell.cpp b/mozilla/docshell/base/nsWebShell.cpp index 9b298516948..e9580795bff 100644 --- a/mozilla/docshell/base/nsWebShell.cpp +++ b/mozilla/docshell/base/nsWebShell.cpp @@ -1019,7 +1019,10 @@ nsWebShell::HandleLinkClickEvent(nsIContent *aContent, nsCOMPtr uri; NS_NewURI(getter_AddRefs(uri), aURLSpec, nsnull); - InternalLoad(uri, mCurrentURI, target, aPostDataStream, loadLink); + nsCOMPtr owner; + GetCurrentDocumentOwner(getter_AddRefs(owner)); + + InternalLoad(uri, mCurrentURI, owner, target, aPostDataStream, loadLink); } break; case eLinkVerb_Embed: diff --git a/mozilla/dom/src/jsurl/nsJSProtocolHandler.cpp b/mozilla/dom/src/jsurl/nsJSProtocolHandler.cpp index fb3f43bade7..7479f8bb0c1 100644 --- a/mozilla/dom/src/jsurl/nsJSProtocolHandler.cpp +++ b/mozilla/dom/src/jsurl/nsJSProtocolHandler.cpp @@ -115,33 +115,26 @@ nsEvaluateStringProxy::EvaluateString(char **aRetValue, PRBool *aIsUndefined) if (NS_FAILED(rv)) return rv; // Get principal of code for execution - NS_WITH_SERVICE(nsIScriptSecurityManager, securityManager, - NS_SCRIPTSECURITYMANAGER_PROGID, &rv); - if (NS_FAILED(rv)) return rv; - - // Get principal + nsCOMPtr owner; + rv = mChannel->GetOwner(getter_AddRefs(owner)); nsCOMPtr principal; - nsCOMPtr referringUri; - // XXX this is wrong: see bugs 31818 and 29831. Norris is looking at it. - rv = mChannel->GetOriginalURI(getter_AddRefs(referringUri)); - if (NS_FAILED(rv)) { - // No referrer available. Use the current javascript: URI, which will mean - // that this script will be in another trust domain than any other script - // since SameOrigin should be false for anything other than the same - // javascript: URI. -#if 0 - nsCOMPtr docShell; - docShell = do_QueryInterface(globalOwner, &rv); - if (NS_FAILED(rv)) return rv; - rv = docShell->GetCurrentURI(getter_AddRefs(referringUri)); -#else - rv = mChannel->GetURI(getter_AddRefs(referringUri)); -#endif - if (NS_FAILED(rv)) return rv; + if (owner) + { + principal = do_QueryInterface(owner, &rv); + NS_ASSERTION(principal, "Channel's owner is not a principal"); + if (!principal) return NS_ERROR_FAILURE; + } + else // No owner from channel, use the current URI to generate a principal + { + nsCOMPtr uri; + rv = mChannel->GetURI(getter_AddRefs(uri)); + if (NS_FAILED(rv) || !uri) return NS_ERROR_FAILURE; + NS_WITH_SERVICE(nsIScriptSecurityManager, securityManager, + NS_SCRIPTSECURITYMANAGER_PROGID, &rv); + if (NS_FAILED(rv)) return rv; + rv =securityManager->GetCodebasePrincipal(uri, getter_AddRefs(principal)); + if (NS_FAILED(rv) || !principal) return NS_ERROR_FAILURE; } - rv = securityManager->GetCodebasePrincipal(referringUri, - getter_AddRefs(principal)); - if (NS_FAILED(rv)) return rv; nsCOMPtr jsURI; rv = mChannel->GetURI(getter_AddRefs(jsURI)); diff --git a/mozilla/webshell/src/nsWebShell.cpp b/mozilla/webshell/src/nsWebShell.cpp index 9b298516948..e9580795bff 100644 --- a/mozilla/webshell/src/nsWebShell.cpp +++ b/mozilla/webshell/src/nsWebShell.cpp @@ -1019,7 +1019,10 @@ nsWebShell::HandleLinkClickEvent(nsIContent *aContent, nsCOMPtr uri; NS_NewURI(getter_AddRefs(uri), aURLSpec, nsnull); - InternalLoad(uri, mCurrentURI, target, aPostDataStream, loadLink); + nsCOMPtr owner; + GetCurrentDocumentOwner(getter_AddRefs(owner)); + + InternalLoad(uri, mCurrentURI, owner, target, aPostDataStream, loadLink); } break; case eLinkVerb_Embed: