Commit 9bdebe6c authored by Tim Huang's avatar Tim Huang Committed by tihuang@mozilla.com
Browse files

Bug 1966658 - Populate the partitionedPrincipal to the SessionHistoryEntry in...

Bug 1966658 - Populate the partitionedPrincipal to the SessionHistoryEntry in the parent process. r=smaug,sessionstore-reviewers,core-sessionstore-reviewers,farre

In this patch, we populate the partitionedPrincipal when we commit
SessionHistory to the parent process. In addition, we remove the
serialization and deserialization of partitionedPrincipalToInherit in
sessionHistory.

Differential Revision: https://phabricator.services.mozilla.com/D250263
parent 49f74917
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -289,6 +289,8 @@ run-if = ["fission"]

["browser_searchModeSwitcher_restore.js"]

["browser_sessionHistory_partitionedPrincipalToInherit.js"]

["browser_sessionHistory.js"]
https_first_disabled = true
support-files = ["file_sessionHistory_hashchange.html"]
+108 −0
Original line number Diff line number Diff line
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function test_partitioned_principal_to_inherit() {
  if (!Services.appinfo.sessionHistoryInParent) {
    ok(true, "sessionHistoryInParent is not enabled, skipping the test.");
    return;
  }

  // Create a new tab.
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "https://example.com"
  );
  let browser = tab.linkedBrowser;

  // Get the last entry in the session history.
  let sh = browser.browsingContext.sessionHistory;
  let entry = sh.getEntryAtIndex(sh.count - 1);
  let partitionedPrincipalToInherit = entry.partitionedPrincipalToInherit;

  // Check that the partitioned principal to inherit is properly set.
  ok(partitionedPrincipalToInherit, "partitionedPrincipalToInherit is set");
  is(
    partitionedPrincipalToInherit.originAttributes.partitionKey,
    "(https,example.com)",
    "correct partitionKey"
  );

  BrowserTestUtils.removeTab(tab);
});

add_task(async function test_partitioned_Principal_to_inherit_in_iframe() {
  if (!Services.appinfo.sessionHistoryInParent) {
    ok(true, "sessionHistoryInParent is not enabled, skipping the test.");
    return;
  }

  // Create a new tab.
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "https://example.com"
  );
  let browser = tab.linkedBrowser;

  // Load a same-origin iframe
  await SpecialPowers.spawn(browser, [], async _ => {
    let iframe = content.document.createElement("iframe");
    iframe.src = "https://example.com";

    await new content.Promise(resolve => {
      iframe.onload = resolve;
      content.document.body.appendChild(iframe);
    });
  });

  // Get the last child entry in the session history for the same-origin iframe.
  let sh = browser.browsingContext.sessionHistory;
  let entry = sh.getEntryAtIndex(sh.count - 1);
  let childEntry = entry.GetChildAt(entry.childCount - 1);
  let partitionedPrincipalToInherit = childEntry.partitionedPrincipalToInherit;

  // Check that the partitioned principal to inherit is properly set.
  ok(partitionedPrincipalToInherit, "partitionedPrincipalToInherit is set");
  is(
    partitionedPrincipalToInherit.originNoSuffix,
    "https://example.com",
    "correct originNoSuffix in the same-origin iframe"
  );
  is(
    partitionedPrincipalToInherit.originAttributes.partitionKey,
    "(https,example.com)",
    "correct partitionKey in the same-origin iframe"
  );

  // Load a cross-site iframe.
  await SpecialPowers.spawn(browser, [], async _ => {
    let iframe = content.document.createElement("iframe");
    iframe.src = "https://example.net";

    await new content.Promise(resolve => {
      iframe.onload = resolve;
      content.document.body.appendChild(iframe);
    });
  });

  // Get the last child entry in the session history for the cross-site iframe.
  entry = sh.getEntryAtIndex(sh.count - 1);
  childEntry = entry.GetChildAt(entry.childCount - 1);
  partitionedPrincipalToInherit = childEntry.partitionedPrincipalToInherit;

  // Check that the partitioned principal to inherit is properly set.
  ok(partitionedPrincipalToInherit, "partitionedPrincipalToInherit is set");
  is(
    partitionedPrincipalToInherit.originNoSuffix,
    "https://example.net",
    "correct originNoSuffix in the cross-site iframe"
  );
  is(
    partitionedPrincipalToInherit.originAttributes.partitionKey,
    "(https,example.com)",
    "correct partitionKey in the cross-site iframe"
  );

  BrowserTestUtils.removeTab(tab);
});
+5 −5
Original line number Diff line number Diff line
@@ -3791,7 +3791,7 @@ void BrowsingContext::SessionHistoryCommit(
    const LoadingSessionHistoryInfo& aInfo, uint32_t aLoadType,
    nsIURI* aPreviousURI, SessionHistoryInfo* aPreviousActiveEntry,
    bool aPersist, bool aCloneEntryChildren, bool aChannelExpired,
    uint32_t aCacheKey) {
    uint32_t aCacheKey, nsIPrincipal* aPartitionedPrincipal) {
  nsID changeID = {};
  if (XRE_IsContentProcess()) {
    RefPtr<ChildSHistory> rootSH = Top()->GetChildSessionHistory();
@@ -3830,11 +3830,11 @@ void BrowsingContext::SessionHistoryCommit(
    ContentChild* cc = ContentChild::GetSingleton();
    mozilla::Unused << cc->SendHistoryCommit(
        this, aInfo.mLoadId, changeID, aLoadType, aPersist, aCloneEntryChildren,
        aChannelExpired, aCacheKey);
        aChannelExpired, aCacheKey, aPartitionedPrincipal);
  } else {
    Canonical()->SessionHistoryCommit(aInfo.mLoadId, changeID, aLoadType,
                                      aPersist, aCloneEntryChildren,
                                      aChannelExpired, aCacheKey);
    Canonical()->SessionHistoryCommit(
        aInfo.mLoadId, changeID, aLoadType, aPersist, aCloneEntryChildren,
        aChannelExpired, aCacheKey, aPartitionedPrincipal);
  }
}

+2 −1
Original line number Diff line number Diff line
@@ -867,7 +867,8 @@ class BrowsingContext : public nsILoadContext, public nsWrapperCache {
                            uint32_t aLoadType, nsIURI* aCurrentURI,
                            SessionHistoryInfo* aPreviousActiveEntry,
                            bool aPersist, bool aCloneEntryChildren,
                            bool aChannelExpired, uint32_t aCacheKey);
                            bool aChannelExpired, uint32_t aCacheKey,
                            nsIPrincipal* aPartitionedPrincipal);

  // Set a new active entry on this browsing context. This is used for
  // implementing history.pushState/replaceState and same document navigations.
+3 −1
Original line number Diff line number Diff line
@@ -1013,7 +1013,8 @@ void CanonicalBrowsingContext::CallOnTopDescendants(

void CanonicalBrowsingContext::SessionHistoryCommit(
    uint64_t aLoadId, const nsID& aChangeID, uint32_t aLoadType, bool aPersist,
    bool aCloneEntryChildren, bool aChannelExpired, uint32_t aCacheKey) {
    bool aCloneEntryChildren, bool aChannelExpired, uint32_t aCacheKey,
    nsIPrincipal* aPartitionedPrincipal) {
  MOZ_LOG(gSHLog, LogLevel::Verbose,
          ("CanonicalBrowsingContext::SessionHistoryCommit %p %" PRIu64, this,
           aLoadId));
@@ -1037,6 +1038,7 @@ void CanonicalBrowsingContext::SessionHistoryCommit(
        newActiveEntry->SharedInfo()->mExpired = true;
      }

      newActiveEntry->SetPartitionedPrincipalToInherit(aPartitionedPrincipal);
      bool loadFromSessionHistory = !newActiveEntry->ForInitialLoad();
      newActiveEntry->SetForInitialLoad(false);
      SessionHistoryEntry::RemoveLoadId(aLoadId);
Loading