Bug 1678310: Expose siteOriginNoSuffix on nsIPrincipal r=baku,nika

Differential Revision: https://phabricator.services.mozilla.com/D97605
This commit is contained in:
Christoph Kerschbaumer 2020-11-28 07:21:32 +00:00
Родитель 88f679f680
Коммит 0f05ea9b88
7 изменённых файлов: 140 добавлений и 16 удалений

Просмотреть файл

@ -106,7 +106,21 @@ BasePrincipal::GetOriginNoSuffix(nsACString& aOrigin) {
NS_IMETHODIMP
BasePrincipal::GetSiteOrigin(nsACString& aSiteOrigin) {
MOZ_ASSERT(mInitialized);
return GetOrigin(aSiteOrigin);
nsresult rv = GetSiteOriginNoSuffix(aSiteOrigin);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString suffix;
rv = GetOriginSuffix(suffix);
NS_ENSURE_SUCCESS(rv, rv);
aSiteOrigin.Append(suffix);
return NS_OK;
}
NS_IMETHODIMP
BasePrincipal::GetSiteOriginNoSuffix(nsACString& aSiteOrigin) {
MOZ_ASSERT(mInitialized);
return GetOriginNoSuffix(aSiteOrigin);
}
// Returns the inner Json::value of the serialized principal

Просмотреть файл

@ -156,7 +156,8 @@ class BasePrincipal : public nsJSPrincipals {
bool* aIsInIsolatedMozBrowserElement) final;
NS_IMETHOD GetUserContextId(uint32_t* aUserContextId) final;
NS_IMETHOD GetPrivateBrowsingId(uint32_t* aPrivateBrowsingId) final;
NS_IMETHOD GetSiteOrigin(nsACString& aOrigin) override;
NS_IMETHOD GetSiteOrigin(nsACString& aSiteOrigin) final;
NS_IMETHOD GetSiteOriginNoSuffix(nsACString& aSiteOrigin) override;
NS_IMETHOD IsThirdPartyURI(nsIURI* uri, bool* aRes) override;
NS_IMETHOD IsThirdPartyPrincipal(nsIPrincipal* uri, bool* aRes) override;
NS_IMETHOD IsThirdPartyChannel(nsIChannel* aChannel, bool* aRes) override;

Просмотреть файл

@ -440,7 +440,7 @@ ContentPrincipal::GetBaseDomain(nsACString& aBaseDomain) {
}
NS_IMETHODIMP
ContentPrincipal::GetSiteOrigin(nsACString& aSiteOrigin) {
ContentPrincipal::GetSiteOriginNoSuffix(nsACString& aSiteOrigin) {
// Handle some special URIs first.
nsAutoCString baseDomain;
bool handled;
@ -450,7 +450,7 @@ ContentPrincipal::GetSiteOrigin(nsACString& aSiteOrigin) {
if (handled) {
// This is a special URI ("file:", "about:", "view-source:", etc). Just
// return the origin.
return GetOrigin(aSiteOrigin);
return GetOriginNoSuffix(aSiteOrigin);
}
// For everything else, we ask the TLD service. Note that, unlike in
@ -491,15 +491,7 @@ ContentPrincipal::GetSiteOrigin(nsACString& aSiteOrigin) {
rv = GenerateOriginNoSuffixFromURI(siteUri, aSiteOrigin);
MOZ_ASSERT(NS_SUCCEEDED(rv), "failed to create siteOriginNoSuffix");
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString suffix;
rv = GetOriginSuffix(suffix);
MOZ_ASSERT(NS_SUCCEEDED(rv), "failed to create suffix");
NS_ENSURE_SUCCESS(rv, rv);
aSiteOrigin.Append(suffix);
return NS_OK;
return rv;
}
nsresult ContentPrincipal::GetSiteIdentifier(SiteIdentifier& aSite) {

Просмотреть файл

@ -30,7 +30,7 @@ class ContentPrincipal final : public BasePrincipal {
NS_IMETHOD SetDomain(nsIURI* aDomain) override;
NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
NS_IMETHOD GetAddonId(nsAString& aAddonId) override;
NS_IMETHOD GetSiteOrigin(nsACString& aSiteOrigin) override;
NS_IMETHOD GetSiteOriginNoSuffix(nsACString& aSiteOrigin) override;
bool IsContentPrincipal() const override { return true; }
ContentPrincipal();

Просмотреть файл

@ -418,11 +418,15 @@ interface nsIPrincipal : nsISerializable
* For most principals, |siteOrigin| matches |origin| precisely. Only
* principals which allow mutating |domain|, such as ContentPrincipal,
* override the default implementation in BasePrincipal.
*
* TODO(nika): Use this in DocGroup.
*/
readonly attribute ACString siteOrigin;
/**
* The base part of |siteOrigin| without the concatenation with
* |originSuffix|.
*/
readonly attribute ACString siteOriginNoSuffix;
/**
* The base domain of the principal URI to which this principal pertains
* (generally the document URI), handling null principals and

Просмотреть файл

@ -0,0 +1,112 @@
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const scriptSecMan = Services.scriptSecurityManager;
function makeURI(uri) {
return Services.io.newURI(uri);
}
// SystemPrincipal checks
let systemPrincipal = scriptSecMan.getSystemPrincipal();
Assert.ok(systemPrincipal.isSystemPrincipal);
Assert.equal(systemPrincipal.origin, "[System Principal]");
Assert.equal(systemPrincipal.originNoSuffix, "[System Principal]");
Assert.equal(systemPrincipal.siteOrigin, "[System Principal]");
Assert.equal(systemPrincipal.siteOriginNoSuffix, "[System Principal]");
// ContentPrincipal checks
let uri1 = Services.io.newURI("http://example.com");
let prinicpal1 = scriptSecMan.createContentPrincipal(uri1, {
userContextId: 11,
});
Assert.ok(prinicpal1.isContentPrincipal);
Assert.equal(prinicpal1.origin, "http://example.com^userContextId=11");
Assert.equal(prinicpal1.originNoSuffix, "http://example.com");
Assert.equal(prinicpal1.siteOrigin, "http://example.com^userContextId=11");
Assert.equal(prinicpal1.siteOriginNoSuffix, "http://example.com");
let uri2 = Services.io.newURI("http://test1.example.com");
let prinicpal2 = scriptSecMan.createContentPrincipal(uri2, {
userContextId: 22,
});
Assert.ok(prinicpal2.isContentPrincipal);
Assert.equal(prinicpal2.origin, "http://test1.example.com^userContextId=22");
Assert.equal(prinicpal2.originNoSuffix, "http://test1.example.com");
Assert.equal(prinicpal2.siteOrigin, "http://example.com^userContextId=22");
Assert.equal(prinicpal2.siteOriginNoSuffix, "http://example.com");
let uri3 = Services.io.newURI("https://test1.test2.example.com:5555");
let prinicpal3 = scriptSecMan.createContentPrincipal(uri3, {
userContextId: 5555,
});
Assert.ok(prinicpal3.isContentPrincipal);
Assert.equal(
prinicpal3.origin,
"https://test1.test2.example.com:5555^userContextId=5555"
);
Assert.equal(prinicpal3.originNoSuffix, "https://test1.test2.example.com:5555");
Assert.equal(prinicpal3.siteOrigin, "https://example.com^userContextId=5555");
Assert.equal(prinicpal3.siteOriginNoSuffix, "https://example.com");
let aboutURI = Services.io.newURI("about:preferences");
let aboutPrincipal = scriptSecMan.createContentPrincipal(aboutURI, {
userContextId: 66,
});
Assert.ok(aboutPrincipal.isContentPrincipal);
Assert.equal(aboutPrincipal.origin, "about:preferences^userContextId=66");
Assert.equal(aboutPrincipal.originNoSuffix, "about:preferences");
Assert.equal(aboutPrincipal.siteOrigin, "about:preferences^userContextId=66");
Assert.equal(aboutPrincipal.siteOriginNoSuffix, "about:preferences");
// NullPrincipal checks
let nullPrincipal = scriptSecMan.createNullPrincipal({ userContextId: 33 });
Assert.ok(nullPrincipal.isNullPrincipal);
Assert.ok(nullPrincipal.origin.includes("moz-nullprincipal"));
Assert.ok(nullPrincipal.origin.includes("^userContextId=33"));
Assert.ok(nullPrincipal.originNoSuffix.includes("moz-nullprincipal"));
Assert.ok(!nullPrincipal.originNoSuffix.includes("^userContextId=33"));
Assert.ok(nullPrincipal.siteOrigin.includes("moz-nullprincipal"));
Assert.ok(nullPrincipal.siteOrigin.includes("^userContextId=33"));
Assert.ok(nullPrincipal.siteOriginNoSuffix.includes("moz-nullprincipal"));
Assert.ok(!nullPrincipal.siteOriginNoSuffix.includes("^userContextId=33"));
// ExpandedPrincipal checks
let expandedPrincipal = Cu.getObjectPrincipal(Cu.Sandbox([prinicpal2]));
Assert.ok(expandedPrincipal.isExpandedPrincipal);
Assert.equal(
expandedPrincipal.origin,
"[Expanded Principal [http://test1.example.com^userContextId=22]]^userContextId=22"
);
Assert.equal(
expandedPrincipal.originNoSuffix,
"[Expanded Principal [http://test1.example.com^userContextId=22]]"
);
Assert.equal(
expandedPrincipal.siteOrigin,
"[Expanded Principal [http://test1.example.com^userContextId=22]]^userContextId=22"
);
Assert.equal(
expandedPrincipal.siteOriginNoSuffix,
"[Expanded Principal [http://test1.example.com^userContextId=22]]"
);
let ipv6URI = Services.io.newURI("https://[2001:db8::ff00:42:8329]:123");
let ipv6Principal = scriptSecMan.createContentPrincipal(ipv6URI, {
userContextId: 6,
});
Assert.ok(ipv6Principal.isContentPrincipal);
Assert.equal(
ipv6Principal.origin,
"https://[2001:db8::ff00:42:8329]:123^userContextId=6"
);
Assert.equal(
ipv6Principal.originNoSuffix,
"https://[2001:db8::ff00:42:8329]:123"
);
Assert.equal(
ipv6Principal.siteOrigin,
"https://[2001:db8::ff00:42:8329]^userContextId=6"
);
Assert.equal(
ipv6Principal.siteOriginNoSuffix,
"https://[2001:db8::ff00:42:8329]"
);

Просмотреть файл

@ -4,3 +4,4 @@ head =
[test_origin.js]
[test_uri_escaping.js]
[test_ipv6_host_literal.js]
[test_site_origin.js]