Skip to content

Sync ephemeral and first party storage cleanup #18331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions browser/ephemeral_storage/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,26 @@ source_set("browser_tests") {
]
}
}

source_set("unit_tests") {
testonly = true

sources = [ "ephemeral_storage_service_unittest.cc" ]

deps = [
"//base/test:test_support",
"//brave/components/ephemeral_storage",
"//chrome/browser",
"//chrome/test:test_support",
"//components/content_settings/core/browser",
"//components/keyed_service/core",
"//components/prefs",
"//components/user_prefs",
"//content/public/browser",
"//content/test:test_support",
"//net",
"//testing/gmock",
"//testing/gtest",
"//url",
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/browser/ephemeral_storage/brave_ephemeral_storage_service_delegate.h"

#include <utility>

#include "chrome/browser/browsing_data/chrome_browsing_data_remover_constants.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browsing_data_filter_builder.h"
#include "content/public/browser/browsing_data_remover.h"
#include "content/public/browser/dom_storage_context.h"
#include "net/base/features.h"

namespace ephemeral_storage {

BraveEphemeralStorageServiceDelegate::BraveEphemeralStorageServiceDelegate(
content::BrowserContext* context,
scoped_refptr<content_settings::CookieSettings> cookie_settings)
: context_(context), cookie_settings_(std::move(cookie_settings)) {
DCHECK(context_);
DCHECK(cookie_settings_);
}

BraveEphemeralStorageServiceDelegate::~BraveEphemeralStorageServiceDelegate() =
default;

void BraveEphemeralStorageServiceDelegate::CleanupTLDEphemeralArea(
const TLDEphemeralAreaKey& key) {
DVLOG(1) << __func__ << " " << key.first << " " << key.second;
auto* storage_partition = context_->GetStoragePartition(key.second);
if (!storage_partition) {
return;
}
auto filter = network::mojom::CookieDeletionFilter::New();
filter->ephemeral_storage_domain = key.first;
storage_partition->GetCookieManagerForBrowserProcess()->DeleteCookies(
std::move(filter), base::NullCallback());
for (const auto& opaque_origin :
cookie_settings_->TakeEphemeralStorageOpaqueOrigins(key.first)) {
storage_partition->GetDOMStorageContext()->DeleteLocalStorage(
blink::StorageKey::CreateFirstParty(opaque_origin), base::DoNothing());
}
}

void BraveEphemeralStorageServiceDelegate::CleanupFirstPartyStorageArea(
const std::string& registerable_domain) {
DVLOG(1) << __func__ << " " << registerable_domain;
DCHECK(base::FeatureList::IsEnabled(
net::features::kBraveForgetFirstPartyStorage));

content::BrowsingDataRemover::DataType data_to_remove =
content::BrowsingDataRemover::DATA_TYPE_COOKIES |
content::BrowsingDataRemover::DATA_TYPE_CACHE |
content::BrowsingDataRemover::DATA_TYPE_MEDIA_LICENSES |
content::BrowsingDataRemover::DATA_TYPE_DOM_STORAGE |
content::BrowsingDataRemover::DATA_TYPE_ATTRIBUTION_REPORTING |
content::BrowsingDataRemover::DATA_TYPE_PRIVACY_SANDBOX |
content::BrowsingDataRemover::DATA_TYPE_PRIVACY_SANDBOX_INTERNAL |
chrome_browsing_data_remover::DATA_TYPE_SITE_DATA;

content::BrowsingDataRemover::OriginType origin_type =
content::BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB |
content::BrowsingDataRemover::ORIGIN_TYPE_PROTECTED_WEB;

auto filter_builder = content::BrowsingDataFilterBuilder::Create(
content::BrowsingDataFilterBuilder::Mode::kDelete);
filter_builder->AddRegisterableDomain(registerable_domain);

content::BrowsingDataRemover* remover = context_->GetBrowsingDataRemover();
remover->RemoveWithFilter(base::Time(), base::Time::Max(), data_to_remove,
origin_type, std::move(filter_builder));
}

} // namespace ephemeral_storage
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_EPHEMERAL_STORAGE_BRAVE_EPHEMERAL_STORAGE_SERVICE_DELEGATE_H_
#define BRAVE_BROWSER_EPHEMERAL_STORAGE_BRAVE_EPHEMERAL_STORAGE_SERVICE_DELEGATE_H_

#include <string>

#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "brave/components/ephemeral_storage/ephemeral_storage_service_delegate.h"
#include "components/content_settings/core/browser/cookie_settings.h"

namespace content {
class BrowserContext;
}

namespace ephemeral_storage {

class BraveEphemeralStorageServiceDelegate
: public EphemeralStorageServiceDelegate {
public:
BraveEphemeralStorageServiceDelegate(
content::BrowserContext* context,
scoped_refptr<content_settings::CookieSettings> cookie_settings);
~BraveEphemeralStorageServiceDelegate() override;

void CleanupTLDEphemeralArea(const TLDEphemeralAreaKey& key) override;
void CleanupFirstPartyStorageArea(
const std::string& registerable_domain) override;

private:
raw_ptr<content::BrowserContext> context_ = nullptr;
scoped_refptr<content_settings::CookieSettings> cookie_settings_;
};

} // namespace ephemeral_storage

#endif // BRAVE_BROWSER_EPHEMERAL_STORAGE_BRAVE_EPHEMERAL_STORAGE_SERVICE_DELEGATE_H_
2 changes: 2 additions & 0 deletions browser/ephemeral_storage/ephemeral_storage_1p_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorage1pBrowserTest,
CloseWebContents(site_a_tab_network_cookies);
CloseWebContents(site_a_tab);
http_request_monitor_.Clear();
WaitForCleanupAfterKeepAlive();

// Load a.com tab again.
WebContents* site_a_tab2 = LoadURLInNewTab(a_site_ephemeral_storage_url_);
Expand Down Expand Up @@ -452,6 +453,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorage1pBrowserTest,
ExpectValuesFromFramesAreEmpty(FROM_HERE, GetValuesFromFrames(site_b_tab));

CloseWebContents(site_a_tab);
WaitForCleanupAfterKeepAlive();

// Load a.com tab again, expect non-ephemeral values are kept.
site_a_tab = LoadURLInNewTab(a_site_ephemeral_storage_url_);
Expand Down
71 changes: 59 additions & 12 deletions browser/ephemeral_storage/ephemeral_storage_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/time/time.h"
#include "brave/browser/ephemeral_storage/ephemeral_storage_service_factory.h"
#include "brave/browser/ephemeral_storage/ephemeral_storage_tab_helper.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
#include "brave/components/constants/brave_paths.h"
#include "brave/components/ephemeral_storage/ephemeral_storage_service.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
Expand Down Expand Up @@ -50,8 +52,6 @@ using net::test_server::HttpResponse;

namespace {

const int kKeepAliveInterval = 2;

const char* ToString(EphemeralStorageBrowserTest::StorageType storage_type) {
switch (storage_type) {
case EphemeralStorageBrowserTest::StorageType::Session:
Expand Down Expand Up @@ -166,9 +166,6 @@ void EphemeralStorageBrowserTest::SetUpOnMainThread() {
https_server_.GetURL("c.com", "/ephemeral_storage.html");
a_site_ephemeral_storage_with_network_cookies_url_ = https_server_.GetURL(
"a.com", "/ephemeral_storage_with_network_cookies.html");

ephemeral_storage::EphemeralStorageTabHelper::SetKeepAliveTimeDelayForTesting(
base::Seconds(kKeepAliveInterval));
}

void EphemeralStorageBrowserTest::SetUpHttpsServer() {
Expand Down Expand Up @@ -292,11 +289,10 @@ content::EvalJsResult EphemeralStorageBrowserTest::GetCookiesInFrame(
return content::EvalJs(host, "document.cookie");
}

void EphemeralStorageBrowserTest::WaitForCleanupAfterKeepAlive() {
base::RunLoop run_loop;
base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), base::Seconds(kKeepAliveInterval));
run_loop.Run();
size_t EphemeralStorageBrowserTest::WaitForCleanupAfterKeepAlive(Browser* b) {
return EphemeralStorageServiceFactory::GetInstance()
->GetForContext((b ? b : browser())->profile())
->FireCleanupTimersForTesting();
}

void EphemeralStorageBrowserTest::ExpectValuesFromFramesAreEmpty(
Expand Down Expand Up @@ -621,7 +617,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
// after keepalive values should be cleared
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), b_site_ephemeral_storage_url_));
WaitForCleanupAfterKeepAlive();
EXPECT_TRUE(WaitForCleanupAfterKeepAlive());
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), a_site_ephemeral_storage_url_));

Expand Down Expand Up @@ -668,6 +664,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
bool was_closed = browser()->tab_strip_model()->CloseWebContentsAt(
tab_index, TabCloseTypes::CLOSE_NONE);
EXPECT_TRUE(was_closed);
EXPECT_TRUE(WaitForCleanupAfterKeepAlive());

// Navigate the main tab to the same site.
ASSERT_TRUE(
Expand Down Expand Up @@ -842,7 +839,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
// timeout.
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), b_site_ephemeral_storage_url_));
WaitForCleanupAfterKeepAlive();
EXPECT_TRUE(WaitForCleanupAfterKeepAlive());
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), a_site_ephemeral_storage_url_));

Expand Down Expand Up @@ -1119,6 +1116,56 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageKeepAliveDisabledBrowserTest,
EXPECT_EQ("", values_after.iframe_2.cookies);
}

IN_PROC_BROWSER_TEST_F(EphemeralStorageKeepAliveDisabledBrowserTest,
ClosingTabClearsEphemeralStorage) {
WebContents* site_a_tab =
LoadURLInNewTab(a_site_ephemeral_storage_with_network_cookies_url_);
EXPECT_EQ(browser()->tab_strip_model()->count(), 2);

SetValuesInFrames(site_a_tab, "a.com value", "from=a.com");

ValuesFromFrames values_before = GetValuesFromFrames(site_a_tab);
EXPECT_EQ("a.com value", values_before.main_frame.local_storage);
EXPECT_EQ("a.com value", values_before.iframe_1.local_storage);
EXPECT_EQ("a.com value", values_before.iframe_2.local_storage);

EXPECT_EQ("a.com value", values_before.main_frame.session_storage);
EXPECT_EQ("a.com value", values_before.iframe_1.session_storage);
EXPECT_EQ("a.com value", values_before.iframe_2.session_storage);

EXPECT_EQ("name=acom_simple; from=a.com", values_before.main_frame.cookies);
EXPECT_EQ("name=bcom_simple; from=a.com", values_before.iframe_1.cookies);
EXPECT_EQ("name=bcom_simple; from=a.com", values_before.iframe_2.cookies);

// Close the new tab which we set ephemeral storage value in. This should
// clear the ephemeral storage since this is the last tab which has a.com as
// an eTLD.
int tab_index =
browser()->tab_strip_model()->GetIndexOfWebContents(site_a_tab);
bool was_closed = browser()->tab_strip_model()->CloseWebContentsAt(
tab_index, TabCloseTypes::CLOSE_NONE);
EXPECT_TRUE(was_closed);

// Navigate the main tab to the same site.
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), a_site_ephemeral_storage_url_));
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();

// Closing the tab earlier should have cleared the ephemeral storage area.
ValuesFromFrames values_after = GetValuesFromFrames(web_contents);
EXPECT_EQ("a.com value", values_after.main_frame.local_storage);
EXPECT_EQ(nullptr, values_after.iframe_1.local_storage);
EXPECT_EQ(nullptr, values_after.iframe_2.local_storage);

EXPECT_EQ(nullptr, values_after.main_frame.session_storage);
EXPECT_EQ(nullptr, values_after.iframe_1.session_storage);
EXPECT_EQ(nullptr, values_after.iframe_2.session_storage);

EXPECT_EQ("name=acom_simple; from=a.com", values_after.main_frame.cookies);
EXPECT_EQ("", values_after.iframe_1.cookies);
EXPECT_EQ("", values_after.iframe_2.cookies);
}

class EphemeralStorageNoSiteIsolationAndKeepAliveDisabledBrowserTest
: public EphemeralStorageKeepAliveDisabledBrowserTest {
public:
Expand Down
2 changes: 1 addition & 1 deletion browser/ephemeral_storage/ephemeral_storage_browsertest.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class EphemeralStorageBrowserTest : public InProcessBrowserTest {
StorageType storage_type);
void SetCookieInFrame(content::RenderFrameHost* host, std::string cookie);
content::EvalJsResult GetCookiesInFrame(content::RenderFrameHost* host);
void WaitForCleanupAfterKeepAlive();
size_t WaitForCleanupAfterKeepAlive(Browser* browser = nullptr);
void ExpectValuesFromFramesAreEmpty(const base::Location& location,
const ValuesFromFrames& values);
void ExpectValuesFromFrameAreEmpty(const base::Location& location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

#include "brave/browser/ephemeral_storage/ephemeral_storage_browsertest.h"

#include "brave/browser/ephemeral_storage/ephemeral_storage_service_factory.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/ephemeral_storage/ephemeral_storage_service.h"
#include "chrome/browser/content_settings/cookie_settings_factory.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
Expand All @@ -27,23 +25,8 @@ class EphemeralStorageForgetByDefaultBrowserTest
: public EphemeralStorageBrowserTest {
public:
EphemeralStorageForgetByDefaultBrowserTest() {
const testing::TestInfo* info =
testing::UnitTest::GetInstance()->current_test_info();
constexpr base::StringPiece kDefaultKeepAliveTimeoutTests[] = {
"PRE_DontForgetFirstPartyIfSubDomainIsOpened",
"DontForgetFirstPartyIfSubDomainIsOpened",
"PRE_ForgetFirstPartyAfterRestart",
"ForgetFirstPartyAfterRestart",
};

base::FieldTrialParams feature_params;
if (!base::Contains(kDefaultKeepAliveTimeoutTests, info->name())) {
feature_params.emplace(
"BraveForgetFirstPartyStorageKeepAliveTimeInSeconds", "2");
}

scoped_feature_list_.InitAndEnableFeatureWithParameters(
net::features::kBraveForgetFirstPartyStorage, feature_params);
scoped_feature_list_.InitAndEnableFeature(
net::features::kBraveForgetFirstPartyStorage);
}

~EphemeralStorageForgetByDefaultBrowserTest() override = default;
Expand All @@ -67,12 +50,6 @@ class EphemeralStorageForgetByDefaultBrowserTest
EXPECT_EQ(cookie_value, first_party_values.iframe_2.cookies);
}

size_t FireCleanupTimersForTesting() {
return EphemeralStorageServiceFactory::GetInstance()
->GetForContext(browser()->profile())
->FireCleanupTimersForTesting();
}

protected:
base::test::ScopedFeatureList scoped_feature_list_;
};
Expand Down Expand Up @@ -229,7 +206,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultBrowserTest,
// After keepalive values should be cleared.
ASSERT_TRUE(ui_test_utils::NavigateToURL(incognito_browser,
b_site_ephemeral_storage_url_));
WaitForCleanupAfterKeepAlive();
WaitForCleanupAfterKeepAlive(incognito_browser);
ASSERT_TRUE(ui_test_utils::NavigateToURL(incognito_browser,
a_site_ephemeral_storage_url_));

Expand Down Expand Up @@ -315,7 +292,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultBrowserTest,

IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultBrowserTest,
ForgetFirstPartyAfterRestart) {
EXPECT_EQ(1u, FireCleanupTimersForTesting());
EXPECT_EQ(1u, WaitForCleanupAfterKeepAlive());
EXPECT_EQ(0u, GetAllCookies().size());
}

Expand Down Expand Up @@ -347,7 +324,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultBrowserTest,

IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultBrowserTest,
DontForgetFirstPartyIfSubDomainIsOpened) {
EXPECT_EQ(0u, FireCleanupTimersForTesting());
EXPECT_EQ(0u, WaitForCleanupAfterKeepAlive());
EXPECT_EQ(1u, GetAllCookies().size());
}

Expand Down Expand Up @@ -433,7 +410,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultIsDefaultBrowserTest,

IN_PROC_BROWSER_TEST_F(EphemeralStorageForgetByDefaultIsDefaultBrowserTest,
ForgetFirstPartyAfterRestart) {
EXPECT_EQ(1u, FireCleanupTimersForTesting());
EXPECT_EQ(1u, WaitForCleanupAfterKeepAlive());
EXPECT_EQ(0u, GetAllCookies().size());
}

Expand Down
Loading