From 4d46bda6dc173b6a2ce6b31553db555987fbdda5 Mon Sep 17 00:00:00 2001 From: "bzbarsky@mit.edu" Date: Mon, 9 Jul 2007 21:22:55 -0700 Subject: [PATCH] Eliminate more callers of CheckSameOriginPrincipal in favor of Equals/Subsumes. Bug 387212, r=mrbkap, sr=jst --- content/html/document/src/nsHTMLDocument.cpp | 21 ++++++------ docshell/base/nsDocShell.cpp | 19 +++++++---- dom/src/base/nsGlobalWindow.cpp | 34 +++++++++----------- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/content/html/document/src/nsHTMLDocument.cpp b/content/html/document/src/nsHTMLDocument.cpp index 7a482101fa3a..ab7ea266e4f5 100644 --- a/content/html/document/src/nsHTMLDocument.cpp +++ b/content/html/document/src/nsHTMLDocument.cpp @@ -628,14 +628,11 @@ CheckSameOrigin(nsINode* aNode1, nsINode* aNode2) NS_PRECONDITION(aNode1, "Null node?"); NS_PRECONDITION(aNode2, "Null node?"); - nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager(); - if (!secMan) { - return PR_FALSE; - } - + PRBool equal; return - NS_SUCCEEDED(secMan->CheckSameOriginPrincipal(aNode1->NodePrincipal(), - aNode2->NodePrincipal())); + NS_SUCCEEDED(aNode1->NodePrincipal()-> + Equals(aNode2->NodePrincipal(), &equal)) && + equal; } PRBool @@ -2146,9 +2143,10 @@ nsHTMLDocument::OpenCommon(const nsACString& aContentType, PRBool aReplace) // If callerPrincipal doesn't match our principal. make sure that // SetNewDocument gives us a new inner window and clears our scope. + PRBool samePrincipal; if (!callerPrincipal || - NS_FAILED(nsContentUtils::GetSecurityManager()-> - CheckSameOriginPrincipal(callerPrincipal, NodePrincipal()))) { + NS_FAILED(callerPrincipal->Equals(NodePrincipal(), &samePrincipal)) || + !samePrincipal) { SetIsInitialDocument(PR_FALSE); } @@ -4018,8 +4016,11 @@ nsHTMLDocument::SetDesignMode(const nsAString & aDesignMode) rv = secMan->GetSubjectPrincipal(getter_AddRefs(subject)); NS_ENSURE_SUCCESS(rv, rv); if (subject) { - rv = secMan->CheckSameOriginPrincipal(subject, NodePrincipal()); + PRBool subsumes; + rv = subject->Subsumes(NodePrincipal(), &subsumes); NS_ENSURE_SUCCESS(rv, rv); + + NS_ENSURE_TRUE(subsumes, NS_ERROR_DOM_PROP_ACCESS_DENIED); } } diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index aed29cf57457..caff39779cd6 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -1025,10 +1025,11 @@ nsDocShell::ValidateOrigin(nsIDocShellTreeItem* aOriginTreeItem, nsCOMPtr targetDocument(do_QueryInterface(targetDOMDocument)); NS_ENSURE_TRUE(targetDocument, PR_FALSE); + PRBool equal; return - NS_SUCCEEDED(securityManager-> - CheckSameOriginPrincipal(originDocument->NodePrincipal(), - targetDocument->NodePrincipal())); + NS_SUCCEEDED(originDocument->NodePrincipal()-> + Equals(targetDocument->NodePrincipal(), &equal)) && + equal; } NS_IMETHODIMP @@ -6209,12 +6210,16 @@ nsDocShell::CheckLoadingPermissions() } // Compare origins - sameOrigin = - securityManager->CheckSameOriginPrincipal(subjPrincipal, p); + PRBool equal; + sameOrigin = subjPrincipal->Equals(p, &equal); if (NS_SUCCEEDED(sameOrigin)) { - // Same origin, permit load + if (equal) { + // Same origin, permit load - return sameOrigin; + return sameOrigin; + } + + sameOrigin = NS_ERROR_DOM_PROP_ACCESS_DENIED; } nsCOMPtr tmp; diff --git a/dom/src/base/nsGlobalWindow.cpp b/dom/src/base/nsGlobalWindow.cpp index 5a2b11f412a9..c08a99a1e95b 100644 --- a/dom/src/base/nsGlobalWindow.cpp +++ b/dom/src/base/nsGlobalWindow.cpp @@ -992,10 +992,10 @@ nsGlobalWindow::WouldReuseInnerWindow(nsIDocument *aNewDocument) return PR_TRUE; } - if (nsContentUtils::GetSecurityManager() && - NS_SUCCEEDED(nsContentUtils::GetSecurityManager()-> - CheckSameOriginPrincipal(mDoc->NodePrincipal(), - aNewDocument->NodePrincipal()))) { + PRBool equal; + if (NS_SUCCEEDED(mDoc->NodePrincipal()->Equals(aNewDocument->NodePrincipal(), + &equal)) && + equal) { // The origin is the same. return PR_TRUE; } @@ -1329,10 +1329,10 @@ nsGlobalWindow::SetNewDocument(nsIDocument* aDocument, // the existing inner window or the new document is from the same // origin as the old document. if (!reUseInnerWindow && mNavigator && oldPrincipal) { - rv = nsContentUtils::GetSecurityManager()-> - CheckSameOriginPrincipal(oldPrincipal, aDocument->NodePrincipal()); + PRBool equal; + rv = oldPrincipal->Equals(aDocument->NodePrincipal(), &equal); - if (NS_FAILED(rv)) { + if (NS_FAILED(rv) || !equal) { // Different origins. Release the navigator object so it gets // recreated for the new document. The plugins or mime types // arrays may have changed. See bug 150087. @@ -6566,9 +6566,9 @@ nsGlobalWindow::SetTimeoutOrInterval(nsIScriptTimeoutHandler *aHandler, timeout->mScriptHandler = aHandler; // Get principal of currently executing code, save for execution of timeout. - // If either our principals subsume the subject principal, or we're from the - // same origin, then use the subject principal. Otherwise, use our principal - // to avoid running script in elevated principals. + // If our principals subsume the subject principal then use the subject + // principal. Otherwise, use our principal to avoid running script in + // elevated principals. nsCOMPtr subjectPrincipal; nsresult rv; @@ -6583,8 +6583,10 @@ nsGlobalWindow::SetTimeoutOrInterval(nsIScriptTimeoutHandler *aHandler, PRBool subsumes = PR_FALSE; nsCOMPtr ourPrincipal = GetPrincipal(); - // Note the direction of this test: We don't allow chrome setTimeouts on - // content windows, but we do allow content setTimeouts on chrome windows. + // Note the direction of this test: We don't allow setTimeouts running with + // chrome privileges on content windows, but we do allow setTimeouts running + // with content privileges on chrome windows (where they can't do very much, + // of course). rv = ourPrincipal->Subsumes(subjectPrincipal, &subsumes); if (NS_FAILED(rv)) { timeout->Release(); @@ -6595,13 +6597,7 @@ nsGlobalWindow::SetTimeoutOrInterval(nsIScriptTimeoutHandler *aHandler, if (subsumes) { timeout->mPrincipal = subjectPrincipal; } else { - // Subsumes does a very strict equality test. Allow sites of the same origin - // to set timeouts on each other. - - rv = nsContentUtils::GetSecurityManager()-> - CheckSameOriginPrincipal(subjectPrincipal, ourPrincipal); - timeout->mPrincipal = NS_SUCCEEDED(rv) ? subjectPrincipal : ourPrincipal; - rv = NS_OK; + timeout->mPrincipal = ourPrincipal; } PRTime delta = (PRTime)realInterval * PR_USEC_PER_MSEC;