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

only use ephemeral storage for block 3p setting #7647

Merged
merged 10 commits into from
Jan 25, 2021
23 changes: 1 addition & 22 deletions browser/ephemeral_storage/ephemeral_storage_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,6 @@ class EphemeralStorageBrowserTest : public InProcessBrowserTest {
command_line->AppendSwitch(switches::kIgnoreCertificateErrors);
}

void AllowAllCookies() {
auto* content_settings =
HostContentSettingsMapFactory::GetForProfile(browser()->profile());
brave_shields::SetCookieControlType(
content_settings, brave_shields::ControlType::ALLOW, GURL());
}

void SetValuesInFrame(RenderFrameHost* frame,
std::string storage_value,
std::string cookie_value) {
Expand Down Expand Up @@ -184,8 +177,6 @@ class EphemeralStorageBrowserTest : public InProcessBrowserTest {
};

IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest, StorageIsPartitioned) {
AllowAllCookies();

WebContents* first_party_tab = LoadURLInNewTab(b_site_ephemeral_storage_url_);
WebContents* site_a_tab1 = LoadURLInNewTab(a_site_ephemeral_storage_url_);
WebContents* site_a_tab2 = LoadURLInNewTab(a_site_ephemeral_storage_url_);
Expand Down Expand Up @@ -263,8 +254,6 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest, StorageIsPartitioned) {

IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
NavigatingClearsEphemeralStorage) {
AllowAllCookies();

ui_test_utils::NavigateToURL(browser(), a_site_ephemeral_storage_url_);
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();

Expand Down Expand Up @@ -303,8 +292,6 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,

IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
ClosingTabClearsEphemeralStorage) {
AllowAllCookies();

WebContents* site_a_tab = LoadURLInNewTab(a_site_ephemeral_storage_url_);
EXPECT_EQ(browser()->tab_strip_model()->count(), 2);

Expand Down Expand Up @@ -353,8 +340,6 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,

IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
ReloadDoesNotClearEphemeralStorage) {
AllowAllCookies();

ui_test_utils::NavigateToURL(browser(), a_site_ephemeral_storage_url_);
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();

Expand Down Expand Up @@ -392,8 +377,6 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,

IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
EphemeralStorageDoesNotLeakBetweenProfiles) {
AllowAllCookies();

ui_test_utils::NavigateToURL(browser(), a_site_ephemeral_storage_url_);
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();

Expand Down Expand Up @@ -456,9 +439,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
}

IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
NavigationCookiesArePartitioned) {
AllowAllCookies();

DISABLED_NavigationCookiesArePartitioned) {
GURL a_site_set_cookie_url = https_server_.GetURL(
"a.com", "/set-cookie?name=acom;path=/;SameSite=None;Secure");
GURL b_site_set_cookie_url = https_server_.GetURL(
Expand Down Expand Up @@ -510,8 +491,6 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,

IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
FirstPartyNestedInThirdParty) {
AllowAllCookies();

auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();

GURL a_site_set_cookie_url = https_server_.GetURL(
Expand Down
26 changes: 20 additions & 6 deletions browser/ephemeral_storage/ephemeral_storage_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "base/feature_list.h"
#include "base/hash/md5.h"
#include "base/no_destructor.h"
#include "base/optional.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/session_storage_namespace.h"
#include "content/public/browser/storage_partition.h"
Expand All @@ -27,6 +28,10 @@ namespace ephemeral_storage {

namespace {

// TODO(bridiver) - share these constants with DOMWindowStorage
constexpr char kSessionStorageSuffix[] = "/ephemeral-session-storage";
constexpr char kLocalStorageSuffix[] = "/ephemeral-local-storage";

// Session storage ids are expected to be 36 character long GUID strings. Since
// we are constructing our own ids, we convert our string into a 32 character
// hash and then use that make up our own GUID-like string. Because of the way
Expand Down Expand Up @@ -90,9 +95,9 @@ void EphemeralStorageTabHelper::CreateEphemeralStorageAreasForDomainAndURL(
// namespace, this will just give us a new reference. When the last tab helper
// drops the reference, the namespace should be deleted.
std::string local_partition_id =
StringToSessionStorageId(new_domain, "/ephemeral-local-storage");
local_storage_namespace_ =
content::CreateSessionStorageNamespace(partition, local_partition_id);
StringToSessionStorageId(new_domain, kLocalStorageSuffix);
local_storage_namespace_ = content::CreateSessionStorageNamespace(
partition, local_partition_id, base::nullopt);

// Session storage is always per-tab and never per-TLD, so we always delete
// and recreate the session storage when switching domains.
Expand All @@ -104,9 +109,18 @@ void EphemeralStorageTabHelper::CreateEphemeralStorageAreasForDomainAndURL(

std::string session_partition_id = StringToSessionStorageId(
content::GetSessionStorageNamespaceId(web_contents()),
"/ephemeral-session-storage");
session_storage_namespace_ =
content::CreateSessionStorageNamespace(partition, session_partition_id);
kSessionStorageSuffix);

auto* rfh = web_contents()->GetOpener();
session_storage_namespace_ = content::CreateSessionStorageNamespace(
partition, session_partition_id,
// clone the namespace if there is an opener
Copy link
Contributor

Choose a reason for hiding this comment

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

really a nit, but the style guide wants us to use proper punctuation, i.e. start sentenses with a capital and end with a dot.

https://google.github.io/styleguide/cppguide.html#Punctuation,_Spelling_and_Grammar

// https://html.spec.whatwg.org/multipage/browsers.html#copy-session-storage
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this clone copy non-ephemeral session storage data into the ephemeral session storage? I'm pretty sure that's why I didn't do this in the past.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it shouldn't be, I'm only passing the id of the parent session storage and it passes all the tests on https://dev-pages.brave.software/storage/ephemeral-storage.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

and since the opener will be suppressed if it's not same domain the owner window will always have ephemeral storage enabled, but it might be worth adding a check in case there's some edge case with subdomains

Copy link
Collaborator Author

@bridiver bridiver Jan 21, 2021

Choose a reason for hiding this comment

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

looks like it should be ok

// If the namespace doesn't exist or it's not populated yet, just create
      // an empty session storage by not marking it as pending a clone.
      if (clone_from_ns == namespaces_.end() ||
          !clone_from_ns->second->IsPopulated()) {
        break;
      }

Copy link
Contributor

Choose a reason for hiding this comment

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

would be nice to have a browser test

rfh ? base::make_optional<std::string>(StringToSessionStorageId(
content::GetSessionStorageNamespaceId(
WebContents::FromRenderFrameHost(rfh)),
kSessionStorageSuffix))
: base::nullopt);

tld_ephemeral_lifetime_ = content::TLDEphemeralLifetime::GetOrCreate(
browser_context, partition, new_domain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/content_settings/core/common/features.h"
#include "net/base/features.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "url/gurl.h"
#include "url/origin.h"
Expand Down Expand Up @@ -143,8 +144,46 @@ bool BraveIsAllowedThirdParty(
return false;
}

// TODO(bridiver) make this a common method that can be shared with
// BraveContentSettingsAgentImpl
bool ShouldUseEphemeralStorage(
const GURL& url,
const GURL& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin,
const CookieSettingsBase* const cookie_settings) {
if (!base::FeatureList::IsEnabled(net::features::kBraveEphemeralStorage))
return false;

if (!top_frame_origin || url::Origin::Create(url) == top_frame_origin)
return false;

bool block_3p = !cookie_settings->IsCookieAccessAllowed(url, site_for_cookies,
top_frame_origin);
bool block_1p = !cookie_settings->IsCookieAccessAllowed(
url, url, url::Origin::Create(url));
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a clever way of doing this. Nice!


// only use ephemeral storage for block 3p
return block_3p && !block_1p;
}

} // namespace

bool CookieSettingsBase::IsEphemeralCookieAccessAllowed(
const GURL& url,
const GURL& first_party_url) const {
return IsEphemeralCookieAccessAllowed(url, first_party_url, base::nullopt);
}

bool CookieSettingsBase::IsEphemeralCookieAccessAllowed(
const GURL& url,
const GURL& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin) const {
if (ShouldUseEphemeralStorage(url, site_for_cookies, top_frame_origin, this))
return true;

return IsCookieAccessAllowed(url, site_for_cookies, top_frame_origin);
}

bool CookieSettingsBase::IsCookieAccessAllowed(
const GURL& url, const GURL& first_party_url) const {
return IsCookieAccessAllowed(url, first_party_url, base::nullopt);
Expand All @@ -154,7 +193,6 @@ bool CookieSettingsBase::IsCookieAccessAllowed(
const GURL& url,
const GURL& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin) const {

// Get content settings only - do not consider default 3rd-party blocking.
ContentSetting setting;
GetCookieSettingInternal(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#define BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_COOKIE_SETTINGS_BASE_H_

#define BRAVE_COOKIE_SETTINGS_BASE_H \
private: \
bool IsEphemeralCookieAccessAllowed(const GURL& url, \
const GURL& first_party_url) const; \
Comment on lines +10 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you could eliminate this method by giving the three-argument version a default argument:

  bool IsEphemeralCookieAccessAllowed(                                    \
      const GURL& url, const GURL& site_for_cookies,                      \
      const base::Optional<url::Origin>& top_frame_origin = base::nullopt) const;   

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wanted to stay consistent with the original methods for clarity

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

and also when the two argument version is removed that might cause us to miss something

bool IsEphemeralCookieAccessAllowed( \
const GURL& url, const GURL& site_for_cookies, \
const base::Optional<url::Origin>& top_frame_origin) const; \
bool IsChromiumCookieAccessAllowed(const GURL& url, \
const GURL& first_party_url) const; \
bool IsChromiumCookieAccessAllowed( \
const GURL& url, \
const GURL& site_for_cookies, \
const GURL& url, const GURL& site_for_cookies, \
const base::Optional<url::Origin>& top_frame_origin) const;

#include "../../../../../../components/content_settings/core/common/cookie_settings_base.h"
Expand Down
14 changes: 10 additions & 4 deletions chromium_src/content/browser/browser_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>

#include "base/memory/ref_counted.h"
#include "base/optional.h"
#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
#include "content/browser/dom_storage/session_storage_namespace_impl.h"
#include "content/browser/renderer_host/render_view_host_delegate.h"
Expand All @@ -20,13 +21,19 @@ namespace content {

scoped_refptr<content::SessionStorageNamespace> CreateSessionStorageNamespace(
content::StoragePartition* partition,
const std::string& namespace_id) {
const std::string& namespace_id,
base::Optional<std::string> clone_from_namespace_id) {
content::DOMStorageContextWrapper* context_wrapper =
static_cast<content::DOMStorageContextWrapper*>(
partition->GetDOMStorageContext());

return content::SessionStorageNamespaceImpl::Create(context_wrapper,
namespace_id);
if (clone_from_namespace_id) {
return content::SessionStorageNamespaceImpl::CloneFrom(
context_wrapper, namespace_id, clone_from_namespace_id.value(), true);
} else {
return content::SessionStorageNamespaceImpl::Create(context_wrapper,
namespace_id);
}
}

std::string GetSessionStorageNamespaceId(WebContents* web_contents) {
Expand All @@ -38,7 +45,6 @@ std::string GetSessionStorageNamespaceId(WebContents* web_contents) {

} // namespace content


namespace content {
bool BrowserContext::IsTor() const {
return false;
Expand Down
12 changes: 8 additions & 4 deletions chromium_src/content/public/browser/browser_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
#ifndef BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_
#define BRAVE_CHROMIUM_SRC_CONTENT_PUBLIC_BROWSER_BROWSER_CONTEXT_H_

#define IsOffTheRecord IsTor() const; \
virtual bool IsOffTheRecord
#define IsOffTheRecord \
IsTor() const; \
virtual bool IsOffTheRecord
#include "../../../../../content/public/browser/browser_context.h"
#undef IsOffTheRecord

#include <string>

#include "base/memory/ref_counted.h"
#include "base/optional.h"
#include "content/common/content_export.h"
#include "content/public/browser/tld_ephemeral_lifetime.h"

Expand All @@ -24,8 +26,10 @@ class SessionStorageNamespace;
class StoragePartition;

CONTENT_EXPORT scoped_refptr<content::SessionStorageNamespace>
CreateSessionStorageNamespace(content::StoragePartition* partition,
const std::string& namespace_id);
CreateSessionStorageNamespace(
content::StoragePartition* partition,
const std::string& namespace_id,
base::Optional<std::string> clone_from_namespace_id);

CONTENT_EXPORT std::string GetSessionStorageNamespaceId(WebContents*);

Expand Down
54 changes: 0 additions & 54 deletions chromium_src/net/url_request/url_request_http_job.cc

This file was deleted.

Loading