Skip to content

Commit

Permalink
Unify ephemeral storage trigger logic to use net::CookieOptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
goodov committed Jun 11, 2021
1 parent 079b442 commit 45ef6c8
Show file tree
Hide file tree
Showing 29 changed files with 714 additions and 123 deletions.
12 changes: 10 additions & 2 deletions browser/ephemeral_storage/ephemeral_storage_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
}

IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
DISABLED_NavigationCookiesArePartitioned) {
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 +510,16 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
b_cookie = content::GetCookies(browser()->profile(), GURL("https://b.com/"));
EXPECT_EQ("name=bcom", b_cookie);

// Navigating to a new TLD should clear all ephemeral cookies.
// Navigating to a new TLD should clear all ephemeral cookies after keep-alive
// timeout.
ui_test_utils::NavigateToURL(browser(), b_site_ephemeral_storage_url_);

base::RunLoop run_loop;
base::SequencedTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(),
base::TimeDelta::FromSeconds(kKeepAliveInterval));
run_loop.Run();

ui_test_utils::NavigateToURL(browser(), a_site_ephemeral_storage_url_);

ValuesFromFrames values_after = GetValuesFromFrames(web_contents);
Expand Down
5 changes: 5 additions & 0 deletions chromium_src/components/content_settings/browser/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
include_rules = [
"+../../../../../components/content_settings/browser",
"+components/content_settings/browser",
"+components/content_settings/common",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */

#include "components/content_settings/browser/content_settings_manager_impl.h"

#include "../../../../../components/content_settings/browser/content_settings_manager_impl.cc"

namespace content_settings {

void ContentSettingsManagerImpl::AllowEphemeralStorageAccess(
int32_t render_frame_id,
StorageType storage_type,
const url::Origin& origin,
const GURL& site_for_cookies,
const url::Origin& top_frame_origin,
base::OnceCallback<void(bool)> callback) {
std::move(callback).Run(cookie_settings_->ShouldUseEphemeralStorage(
origin.GetURL(), site_for_cookies, top_frame_origin));
}

} // namespace content_settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright 2021 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 http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_BROWSER_CONTENT_SETTINGS_MANAGER_IMPL_H_
#define BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_BROWSER_CONTENT_SETTINGS_MANAGER_IMPL_H_

#include "components/content_settings/common/content_settings_manager.mojom.h"

#define OnContentBlocked \
NotUsed() {} \
void AllowEphemeralStorageAccess( \
int32_t render_frame_id, StorageType storage_type, \
const url::Origin& origin, const GURL& site_for_cookies, \
const url::Origin& top_frame_origin, \
base::OnceCallback<void(bool)> callback) override; \
void OnContentBlocked

#include "../../../../../components/content_settings/browser/content_settings_manager_impl.h"

#undef OnContentBlocked

#endif // BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_BROWSER_CONTENT_SETTINGS_MANAGER_IMPL_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module content_settings.mojom;

import "url/mojom/origin.mojom";
import "url/mojom/url.mojom";

[BraveExtend]
interface ContentSettingsManager {
[Sync]
AllowEphemeralStorageAccess(
int32 render_frame_id,
StorageType storage_type,
url.mojom.Origin origin,
url.mojom.Url site_for_cookies,
url.mojom.Origin top_frame_origin) => (bool ephemeral_storage_allowed);
};
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ GURL GetFirstPartyURL(const GURL& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin) {
return top_frame_origin ? top_frame_origin->GetURL() : site_for_cookies;
}

bool IsFirstPartyAccessAllowed(
const GURL& first_party_url,
const CookieSettingsBase* const cookie_settings) {
Expand All @@ -88,6 +89,15 @@ bool IsFirstPartyAccessAllowed(

} // namespace

ScopedEphemeralStorageAwareness::ScopedEphemeralStorageAwareness(
bool* ephemeral_storage_aware)
: ephemeral_storage_aware_auto_reset_(ephemeral_storage_aware, true) {}
ScopedEphemeralStorageAwareness::~ScopedEphemeralStorageAwareness() = default;
ScopedEphemeralStorageAwareness::ScopedEphemeralStorageAwareness(
ScopedEphemeralStorageAwareness&& rhs) = default;
ScopedEphemeralStorageAwareness& ScopedEphemeralStorageAwareness::operator=(
ScopedEphemeralStorageAwareness&& rhs) = default;

bool CookieSettingsBase::ShouldUseEphemeralStorage(
const GURL& url,
const GURL& site_for_cookies,
Expand All @@ -107,13 +117,18 @@ bool CookieSettingsBase::ShouldUseEphemeralStorage(
return false;

bool allow_3p =
IsCookieAccessAllowed(url, site_for_cookies, top_frame_origin);
IsCookieAccessAllowedImpl(url, site_for_cookies, top_frame_origin);
bool allow_1p = IsFirstPartyAccessAllowed(first_party_url, this);

// only use ephemeral storage for block 3p
return allow_1p && !allow_3p;
}

ScopedEphemeralStorageAwareness
CookieSettingsBase::CreateScopedEphemeralStorageAwareness() const {
return ScopedEphemeralStorageAwareness(&ephemeral_storage_aware_);
}

bool CookieSettingsBase::IsEphemeralCookieAccessAllowed(
const GURL& url,
const GURL& first_party_url) const {
Expand All @@ -124,9 +139,8 @@ 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))
return true;

auto scoped_ephemeral_storage_awareness =
CreateScopedEphemeralStorageAwareness();
return IsCookieAccessAllowed(url, site_for_cookies, top_frame_origin);
}

Expand All @@ -140,6 +154,18 @@ bool CookieSettingsBase::IsCookieAccessAllowed(
const GURL& url,
const GURL& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin) const {
if (ephemeral_storage_aware_ &&
ShouldUseEphemeralStorage(url, site_for_cookies, top_frame_origin)) {
return true;
}

return IsCookieAccessAllowedImpl(url, site_for_cookies, top_frame_origin);
}

bool CookieSettingsBase::IsCookieAccessAllowedImpl(
const GURL& url,
const GURL& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin) const {
bool allow =
IsChromiumCookieAccessAllowed(url, site_for_cookies, top_frame_origin);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,30 @@
#ifndef BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_COOKIE_SETTINGS_BASE_H_
#define BRAVE_CHROMIUM_SRC_COMPONENTS_CONTENT_SETTINGS_CORE_COMMON_COOKIE_SETTINGS_BASE_H_

#include "base/auto_reset.h"

namespace content_settings {

// Helper to allow patchless ephemeral storage access in Chromium code.
class ScopedEphemeralStorageAwareness {
public:
explicit ScopedEphemeralStorageAwareness(bool* ephemeral_storage_aware);
ScopedEphemeralStorageAwareness(ScopedEphemeralStorageAwareness&&);
ScopedEphemeralStorageAwareness& operator=(ScopedEphemeralStorageAwareness&&);
~ScopedEphemeralStorageAwareness();

private:
base::AutoReset<bool> ephemeral_storage_aware_auto_reset_;
};

} // namespace content_settings

#define BRAVE_COOKIE_SETTINGS_BASE_H \
bool ShouldUseEphemeralStorage( \
const GURL& url, const GURL& site_for_cookies, \
const base::Optional<url::Origin>& top_frame_origin) const; \
ScopedEphemeralStorageAwareness CreateScopedEphemeralStorageAwareness() \
const; \
bool IsEphemeralCookieAccessAllowed(const GURL& url, \
const GURL& first_party_url) const; \
bool IsEphemeralCookieAccessAllowed( \
Expand All @@ -19,7 +39,14 @@
const GURL& first_party_url) const; \
bool IsChromiumCookieAccessAllowed( \
const GURL& url, const GURL& site_for_cookies, \
const base::Optional<url::Origin>& top_frame_origin) const;
const base::Optional<url::Origin>& top_frame_origin) const; \
\
private: \
bool IsCookieAccessAllowedImpl( \
const GURL& url, const GURL& site_for_cookies, \
const base::Optional<url::Origin>& top_frame_origin) const; \
\
mutable bool ephemeral_storage_aware_ = false;

#include "../../../../../../components/content_settings/core/common/cookie_settings_base.h"

Expand Down
26 changes: 26 additions & 0 deletions chromium_src/net/cookies/cookie_access_delegate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* Copyright (c) 2021 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 "net/cookies/cookie_access_delegate.h"

#include "base/notreached.h"

#include "../../../../net/cookies/cookie_access_delegate.cc"

namespace net {

bool CookieAccessDelegate::NotUsed() const {
return false;
}

bool CookieAccessDelegate::ShouldUseEphemeralStorage(
const GURL& url,
const net::SiteForCookies& site_for_cookies,
const base::Optional<url::Origin>& top_frame_origin) const {
NOTREACHED() << "Should be overridden";
return false;
}

} // namespace net
25 changes: 25 additions & 0 deletions chromium_src/net/cookies/cookie_access_delegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_CHROMIUM_SRC_NET_COOKIES_COOKIE_ACCESS_DELEGATE_H_
#define BRAVE_CHROMIUM_SRC_NET_COOKIES_COOKIE_ACCESS_DELEGATE_H_

#include "base/optional.h"
#include "net/cookies/site_for_cookies.h"
#include "url/gurl.h"
#include "url/origin.h"

#define ShouldTreatUrlAsTrustworthy \
NotUsed() const; \
virtual bool ShouldUseEphemeralStorage( \
const GURL& url, const net::SiteForCookies& site_for_cookies, \
const base::Optional<url::Origin>& top_frame_origin) const; \
virtual bool ShouldTreatUrlAsTrustworthy

#include "../../../../net/cookies/cookie_access_delegate.h"

#undef ShouldTreatUrlAsTrustworthy

#endif // BRAVE_CHROMIUM_SRC_NET_COOKIES_COOKIE_ACCESS_DELEGATE_H_
62 changes: 45 additions & 17 deletions chromium_src/net/cookies/cookie_monster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,55 @@ void CookieMonster::SetCookieableSchemes(
ChromiumCookieMonster::SetCookieableSchemes(schemes, std::move(callback));
}

void CookieMonster::GetEphemeralCookieListWithOptionsAsync(
const GURL& url,
const GURL& top_frame_url,
const CookieOptions& options,
GetCookieListCallback callback) {
ChromiumCookieMonster* ephemeral_monster =
GetOrCreateEphemeralCookieStoreForTopFrameURL(top_frame_url);
ephemeral_monster->GetCookieListWithOptionsAsync(url, options,
std::move(callback));
}

void CookieMonster::SetEphemeralCanonicalCookieAsync(
void CookieMonster::SetCanonicalCookieAsync(
std::unique_ptr<CanonicalCookie> cookie,
const GURL& source_url,
const GURL& top_frame_url,
const CookieOptions& options,
SetCookiesCallback callback) {
ChromiumCookieMonster* ephemeral_monster =
GetOrCreateEphemeralCookieStoreForTopFrameURL(top_frame_url);
ephemeral_monster->SetCanonicalCookieAsync(std::move(cookie), source_url,
options, std::move(callback));
if (options.should_use_ephemeral_storage()) {
if (!options.top_frame_origin()) {
// Shouldn't happen, but don't do anything in this case.
NOTREACHED();
MaybeRunCookieCallback(
std::move(callback),
CookieAccessResult(CookieInclusionStatus(
CookieInclusionStatus::EXCLUDE_UNKNOWN_ERROR)));
return;
}
ChromiumCookieMonster* ephemeral_monster =
GetOrCreateEphemeralCookieStoreForTopFrameURL(
options.top_frame_origin()->GetURL());
ephemeral_monster->SetCanonicalCookieAsync(std::move(cookie), source_url,
options, std::move(callback));
return;
}

ChromiumCookieMonster::SetCanonicalCookieAsync(std::move(cookie), source_url,
options, std::move(callback));
}

void CookieMonster::GetCookieListWithOptionsAsync(
const GURL& url,
const CookieOptions& options,
GetCookieListCallback callback) {
if (options.should_use_ephemeral_storage()) {
if (!options.top_frame_origin()) {
// Shouldn't happen, but don't do anything in this case.
NOTREACHED();
MaybeRunCookieCallback(std::move(callback), CookieAccessResultList(),
CookieAccessResultList());
return;
}
ChromiumCookieMonster* ephemeral_monster =
GetOrCreateEphemeralCookieStoreForTopFrameURL(
options.top_frame_origin()->GetURL());
ephemeral_monster->GetCookieListWithOptionsAsync(url, options,
std::move(callback));
return;
}

ChromiumCookieMonster::GetCookieListWithOptionsAsync(url, options,
std::move(callback));
}

} // namespace net
16 changes: 7 additions & 9 deletions chromium_src/net/cookies/cookie_monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ class NET_EXPORT CookieMonster : public ChromiumCookieMonster {
void SetCookieableSchemes(const std::vector<std::string>& schemes,
SetCookieableSchemesCallback callback) override;

void GetEphemeralCookieListWithOptionsAsync(const GURL& url,
const GURL& top_frame_url,
const CookieOptions& options,
GetCookieListCallback callback);
void SetEphemeralCanonicalCookieAsync(std::unique_ptr<CanonicalCookie> cookie,
const GURL& source_url,
const GURL& top_frame_url,
const CookieOptions& options,
SetCookiesCallback callback);
void SetCanonicalCookieAsync(std::unique_ptr<CanonicalCookie> cookie,
const GURL& source_url,
const CookieOptions& options,
SetCookiesCallback callback) override;
void GetCookieListWithOptionsAsync(const GURL& url,
const CookieOptions& options,
GetCookieListCallback callback) override;

private:
NetLogWithSource net_log_;
Expand Down
Loading

0 comments on commit 45ef6c8

Please sign in to comment.