Skip to content

Commit

Permalink
Bug 1601941 - Refactor GetURI usage in nsScriptSecurityManager.cpp r=…
Browse files Browse the repository at this point in the history
…ckerschb

Differential Revision: https://phabricator.services.mozilla.com/D56185

--HG--
extra : moz-landing-system : lando
  • Loading branch information
strseb committed Dec 11, 2019
1 parent 08274e0 commit 8b390d5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
7 changes: 6 additions & 1 deletion caps/ContentPrincipal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,12 @@ bool ContentPrincipal::MayLoadInternal(nsIURI* aURI) {
uint32_t ContentPrincipal::GetHashValue() {
MOZ_ASSERT(mURI, "Need a principal URI");

return nsScriptSecurityManager::HashPrincipalByOrigin(this);
nsCOMPtr<nsIURI> uri;
GetDomain(getter_AddRefs(uri));
if (!uri) {
GetURI(getter_AddRefs(uri));
};
return NS_SecurityHashURI(uri);
}

NS_IMETHODIMP
Expand Down
23 changes: 9 additions & 14 deletions caps/nsScriptSecurityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ class nsAutoInPrincipalDomainOriginSetter {
uint32_t nsAutoInPrincipalDomainOriginSetter::sInPrincipalDomainOrigin;

static nsresult GetOriginFromURI(nsIURI* aURI, nsACString& aOrigin) {
if (!aURI) {
return NS_ERROR_NULL_POINTER;
}
if (nsAutoInPrincipalDomainOriginSetter::sInPrincipalDomainOrigin > 1) {
// Allow a single recursive call to GetPrincipalDomainOrigin, since that
// might be happening on a different principal from the first call. But
Expand Down Expand Up @@ -180,14 +183,15 @@ static nsresult GetOriginFromURI(nsIURI* aURI, nsACString& aOrigin) {

static nsresult GetPrincipalDomainOrigin(nsIPrincipal* aPrincipal,
nsACString& aOrigin) {
aOrigin.Truncate();
nsCOMPtr<nsIURI> uri;
aPrincipal->GetDomain(getter_AddRefs(uri));
if (!uri) {
aPrincipal->GetURI(getter_AddRefs(uri));
nsresult rv = GetOriginFromURI(uri, aOrigin);
if (NS_SUCCEEDED(rv)) {
return rv;
}
NS_ENSURE_TRUE(uri, NS_ERROR_UNEXPECTED);

return GetOriginFromURI(uri, aOrigin);
// If there is no Domain fallback to the Principals Origin
return aPrincipal->GetOriginNoSuffix(aOrigin);
}

inline void SetPendingExceptionASCII(JSContext* cx, const char* aMsg) {
Expand Down Expand Up @@ -509,15 +513,6 @@ nsScriptSecurityManager::CheckSameOriginURI(nsIURI* aSourceURI,
return NS_OK;
}

/*static*/
uint32_t nsScriptSecurityManager::HashPrincipalByOrigin(
nsIPrincipal* aPrincipal) {
nsCOMPtr<nsIURI> uri;
aPrincipal->GetDomain(getter_AddRefs(uri));
if (!uri) aPrincipal->GetURI(getter_AddRefs(uri));
return SecurityHashURI(uri);
}

NS_IMETHODIMP
nsScriptSecurityManager::CheckLoadURIFromScript(JSContext* cx, nsIURI* aURI) {
// Get principal of currently executing script.
Expand Down
2 changes: 0 additions & 2 deletions caps/nsScriptSecurityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ class nsScriptSecurityManager final : public nsIScriptSecurityManager {
static nsresult ReportError(const char* aMessageTag, nsIURI* aSource,
nsIURI* aTarget, bool aFromPrivateWindow);

static uint32_t HashPrincipalByOrigin(nsIPrincipal* aPrincipal);

static bool GetStrictFileOriginPolicy() { return sStrictFileOriginPolicy; }

void DeactivateDomainPolicy();
Expand Down

0 comments on commit 8b390d5

Please sign in to comment.