Skip to content
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

Invalidate Ephemeral Storage origin key on page reload. #20202

Merged
merged 1 commit into from
Sep 25, 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
82 changes: 69 additions & 13 deletions browser/ephemeral_storage/ephemeral_storage_1p_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@ class EphemeralStorage1pBrowserTest : public EphemeralStorageBrowserTest {
base::test::ScopedFeatureList scoped_feature_list_;
};

class EphemeralStorage1pDisabledBrowserTest
: public EphemeralStorageBrowserTest {
public:
EphemeralStorage1pDisabledBrowserTest() {
scoped_feature_list_.InitAndDisableFeature(
net::features::kBraveFirstPartyEphemeralStorage);
}
~EphemeralStorage1pDisabledBrowserTest() override = default;

private:
base::test::ScopedFeatureList scoped_feature_list_;
};

IN_PROC_BROWSER_TEST_F(EphemeralStorage1pBrowserTest, FirstPartyIsEphemeral) {
SetCookieSetting(a_site_ephemeral_storage_url_, CONTENT_SETTING_SESSION_ONLY);

Expand Down Expand Up @@ -540,6 +527,75 @@ IN_PROC_BROWSER_TEST_F(
EXPECT_EQ("name=bcom_simple", site_a_tab_values.iframe_2.cookies);
}

IN_PROC_BROWSER_TEST_F(EphemeralStorage1pBrowserTest,
UseEphemeralStorageAfterReload) {
WebContents* first_party_tab = LoadURLInNewTab(a_site_ephemeral_storage_url_);

// Set values in the main frame.
SetValuesInFrame(first_party_tab->GetPrimaryMainFrame(), "a.com",
"from=a.com");

{
ValuesFromFrame first_party_values =
GetValuesFromFrame(first_party_tab->GetPrimaryMainFrame());
EXPECT_EQ("a.com", first_party_values.local_storage);
EXPECT_EQ("a.com", first_party_values.session_storage);
EXPECT_EQ("from=a.com", first_party_values.cookies);
}

// Enable 1p Ephemeral Storage mode.
SetCookieSetting(a_site_ephemeral_storage_url_, CONTENT_SETTING_SESSION_ONLY);

{
ValuesFromFrame first_party_values =
GetValuesFromFrame(first_party_tab->GetPrimaryMainFrame());
// Local/Session storage should still access the non-Ephemeral backend.
EXPECT_EQ("a.com", first_party_values.local_storage);
EXPECT_EQ("a.com", first_party_values.session_storage);
// Cookies storage always uses sync calls, which means it will access
// Ephemeral Storage immediately after Content Settings change.
EXPECT_EQ("", first_party_values.cookies);
}

// Reload the page.
first_party_tab->GetController().Reload(content::ReloadType::NORMAL, true);
WaitForLoadStop(first_party_tab);

// After reload all values should be read from the Ephemeral Storage and be
// empty.
ExpectValuesFromFramesAreEmpty(FROM_HERE,
GetValuesFromFrames(first_party_tab));

// Disable 1p Ephemeral Storage mode.
SetCookieSetting(a_site_ephemeral_storage_url_, CONTENT_SETTING_DEFAULT);

// Reload the page.
first_party_tab->GetController().Reload(content::ReloadType::NORMAL, true);
WaitForLoadStop(first_party_tab);

// Data should be read from non-Ephemeral Storage.
{
ValuesFromFrame first_party_values =
GetValuesFromFrame(first_party_tab->GetPrimaryMainFrame());
EXPECT_EQ("a.com", first_party_values.local_storage);
EXPECT_EQ("a.com", first_party_values.session_storage);
EXPECT_EQ("from=a.com", first_party_values.cookies);
}
}

class EphemeralStorage1pDisabledBrowserTest
: public EphemeralStorageBrowserTest {
public:
EphemeralStorage1pDisabledBrowserTest() {
scoped_feature_list_.InitAndDisableFeature(
net::features::kBraveFirstPartyEphemeralStorage);
}
~EphemeralStorage1pDisabledBrowserTest() override = default;

private:
base::test::ScopedFeatureList scoped_feature_list_;
};

// By default SESSION_ONLY setting means that data for a website should be
// deleted after a restart, but this also implicitly changes how a website
// behaves in 3p context: when the setting is explicit, Chromium removes 3p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ bool BraveContentSettingsAgentImpl::IsFirstPartyCosmeticFilteringEnabled(
return setting == CONTENT_SETTING_BLOCK;
}

void BraveContentSettingsAgentImpl::DidCommitProvisionalLoad(
ui::PageTransition transition) {
ContentSettingsAgentImpl::DidCommitProvisionalLoad(transition);
// Invalidate Ephemeral Storage opaque origins. Page reload might change the
// Ephemeral Storage mode, in this case we should re-request it.
cached_ephemeral_storage_origins_.clear();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

BraveFarblingLevel BraveContentSettingsAgentImpl::GetBraveFarblingLevel() {
blink::WebLocalFrame* frame = render_frame()->GetWebFrame();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class BraveContentSettingsAgentImpl

bool IsFirstPartyCosmeticFilteringEnabled(const GURL& url) override;

// RenderFrameObserver:
void DidCommitProvisionalLoad(ui::PageTransition transition) override;

protected:
bool AllowScript(bool enabled_per_settings) override;
bool AllowScriptFromSource(bool enabled_per_settings,
Expand Down Expand Up @@ -98,7 +101,7 @@ class BraveContentSettingsAgentImpl
GURL blocked_script_url_;

// Status of "reduce language identifiability" feature.
bool reduce_language_enabled_;
bool reduce_language_enabled_ = false;

base::flat_map<url::Origin, blink::WebSecurityOrigin>
cached_ephemeral_storage_origins_;
Expand Down