Skip to content

Commit

Permalink
Bug 1542309 - Set firstPartyDomain to public suffix if getBaseDomain …
Browse files Browse the repository at this point in the history
…fails. r=baku

Right now the firstPartyDomain is not set when host is in the public suffix list. The patch fixes it by setting firstPartyDomain to eTLD.getPublicSuffix in these cases.

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

--HG--
extra : moz-landing-system : lando
  • Loading branch information
acatarineu committed Apr 12, 2019
1 parent fba2d34 commit 6b2106e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions caps/OriginAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
return;
}

// Saving isInsufficientDomainLevels before rv is overwritten.
bool isInsufficientDomainLevels = (rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS);
nsAutoCString scheme;
rv = aURI->GetScheme(scheme);
NS_ENSURE_SUCCESS_VOID(rv);
Expand All @@ -97,6 +99,15 @@ void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
mFirstPartyDomain = blobPrincipal->OriginAttributesRef().mFirstPartyDomain;
return;
}

if (isInsufficientDomainLevels) {
nsAutoCString publicSuffix;
rv = tldService->GetPublicSuffix(aURI, publicSuffix);
if (NS_SUCCEEDED(rv)) {
mFirstPartyDomain = NS_ConvertUTF8toUTF16(publicSuffix);
}
return;
}
}

void OriginAttributes::SetFirstPartyDomain(const bool aIsTopLevelDocument,
Expand Down
29 changes: 29 additions & 0 deletions caps/tests/gtest/TestOriginAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/BasePrincipal.h"
#include "mozilla/Preferences.h"
#include "nsNetUtil.h"

using mozilla::OriginAttributes;
using mozilla::Preferences;

static void TestSuffix(const OriginAttributes& attrs) {
nsAutoCString suffix;
Expand All @@ -17,6 +20,14 @@ static void TestSuffix(const OriginAttributes& attrs) {
EXPECT_EQ(attrs, attrsFromSuffix);
}

static void TestFPD(const nsAString &spec, const nsAString &fpd) {
OriginAttributes attrs;
nsCOMPtr<nsIURI> url;
ASSERT_EQ(NS_NewURI(getter_AddRefs(url), spec), NS_OK);
attrs.SetFirstPartyDomain(true, url);
EXPECT_TRUE(attrs.mFirstPartyDomain.Equals(fpd));
}

TEST(OriginAttributes, Suffix_default)
{
OriginAttributes attrs;
Expand All @@ -34,3 +45,21 @@ TEST(OriginAttributes, Suffix_maxAppId_inIsolatedMozBrowser)
OriginAttributes attrs(4294967295, true);
TestSuffix(attrs);
}

TEST(OriginAttributes, FirstPartyDomain_default)
{
static const char prefKey[] = "privacy.firstparty.isolate";
bool oldPref = Preferences::GetBool(prefKey);
Preferences::SetBool(prefKey, true);
TestFPD(NS_LITERAL_STRING("http://www.example.com"),
NS_LITERAL_STRING("example.com"));
TestFPD(NS_LITERAL_STRING("http://s3.amazonaws.com"),
NS_LITERAL_STRING("s3.amazonaws.com"));
TestFPD(NS_LITERAL_STRING("http://com"), NS_LITERAL_STRING("com"));
TestFPD(NS_LITERAL_STRING("http://.com"), NS_LITERAL_STRING(""));
TestFPD(NS_LITERAL_STRING("http://..com"), NS_LITERAL_STRING(""));
TestFPD(NS_LITERAL_STRING("http://127.0.0.1"),
NS_LITERAL_STRING("127.0.0.1"));
TestFPD(NS_LITERAL_STRING("http://[::1]"), NS_LITERAL_STRING("[::1]"));
Preferences::SetBool(prefKey, oldPref);
}

0 comments on commit 6b2106e

Please sign in to comment.