Skip to content

Commit 2695cfa

Browse files
committed
Add ephemeral storage support for network cookies
1 parent fe5a8ec commit 2695cfa

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

browser/ephemeral_storage/ephemeral_storage_browsertest.cc

+54
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "components/prefs/pref_service.h"
1919
#include "content/public/browser/notification_types.h"
2020
#include "content/public/browser/render_frame_host.h"
21+
#include "content/public/browser/storage_partition.h"
2122
#include "content/public/browser/web_contents.h"
2223
#include "content/public/test/browser_test.h"
2324
#include "content/public/test/test_navigation_observer.h"
@@ -449,3 +450,56 @@ IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
449450
EXPECT_EQ("", private_values.iframe_1.cookies);
450451
EXPECT_EQ("", private_values.iframe_2.cookies);
451452
}
453+
454+
IN_PROC_BROWSER_TEST_F(EphemeralStorageBrowserTest,
455+
NavigationCookiesArePartitioned) {
456+
AllowAllCookies();
457+
458+
GURL a_site_set_cookie_url = https_server_.GetURL(
459+
"a.com", "/set-cookie?name=acom;path=/;SameSite=None;Secure");
460+
GURL b_site_set_cookie_url = https_server_.GetURL(
461+
"b.com", "/set-cookie?name=bcom;path=/;SameSite=None;Secure");
462+
463+
ui_test_utils::NavigateToURL(browser(), a_site_set_cookie_url);
464+
ui_test_utils::NavigateToURL(browser(), b_site_set_cookie_url);
465+
ui_test_utils::NavigateToURL(browser(), a_site_ephemeral_storage_url_);
466+
467+
std::string a_cookie =
468+
content::GetCookies(browser()->profile(), GURL("https://a.com/"));
469+
std::string b_cookie =
470+
content::GetCookies(browser()->profile(), GURL("https://b.com/"));
471+
EXPECT_EQ("name=acom", a_cookie);
472+
EXPECT_EQ("name=bcom", b_cookie);
473+
474+
// The third-party iframe should not have the b.com cookie that was set on the
475+
// main frame.
476+
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
477+
RenderFrameHost* main_frame = web_contents->GetMainFrame();
478+
RenderFrameHost* iframe_a = content::ChildFrameAt(main_frame, 0);
479+
RenderFrameHost* iframe_b = content::ChildFrameAt(main_frame, 0);
480+
ASSERT_EQ("", GetCookiesInFrame(iframe_a));
481+
ASSERT_EQ("", GetCookiesInFrame(iframe_b));
482+
483+
// Setting the cookie directly on the third-party iframe should only set the
484+
// cookie in the ephemeral storage area for that frame.
485+
GURL b_site_set_ephemeral_cookie_url = https_server_.GetURL(
486+
"b.com", "/set-cookie?name=bcom_ephemeral;path=/;SameSite=None;Secure");
487+
NavigateIframeToURL(web_contents, "third_party_iframe_a",
488+
b_site_set_ephemeral_cookie_url);
489+
ASSERT_EQ("name=bcom_ephemeral", GetCookiesInFrame(iframe_a));
490+
ASSERT_EQ("name=bcom_ephemeral", GetCookiesInFrame(iframe_b));
491+
492+
// The cookie set in the ephemeral area should not visible in the main
493+
// cookie storage.
494+
b_cookie = content::GetCookies(browser()->profile(), GURL("https://b.com/"));
495+
EXPECT_EQ("name=bcom", b_cookie);
496+
497+
// Navigating to a new TLD should clear all ephemeral cookies.
498+
ui_test_utils::NavigateToURL(browser(), b_site_ephemeral_storage_url_);
499+
ui_test_utils::NavigateToURL(browser(), a_site_ephemeral_storage_url_);
500+
501+
ValuesFromFrames values_after = GetValuesFromFrames(web_contents);
502+
EXPECT_EQ("name=acom", values_after.main_frame.cookies);
503+
EXPECT_EQ("", values_after.iframe_1.cookies);
504+
EXPECT_EQ("", values_after.iframe_2.cookies);
505+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
4+
* You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#include "net/url_request/url_request_http_job.h"
7+
8+
#include "net/base/features.h"
9+
#include "net/cookies/cookie_monster.h"
10+
11+
namespace {
12+
13+
bool ShouldUseEphemeralStorage(net::URLRequestHttpJob* http_job) {
14+
if (!base::FeatureList::IsEnabled(net::features::kBraveEphemeralStorage))
15+
return false;
16+
17+
const net::IsolationInfo& isolation_info =
18+
http_job->request()->isolation_info();
19+
if (!isolation_info.top_frame_origin().has_value() ||
20+
!isolation_info.frame_origin().has_value())
21+
return false;
22+
if (*isolation_info.top_frame_origin() == *isolation_info.frame_origin())
23+
return false;
24+
25+
return true;
26+
}
27+
28+
} // namespace
29+
30+
#define BRAVE_ADDCOOKIEHEADERANDSTART \
31+
if (ShouldUseEphemeralStorage(this)) { \
32+
DCHECK(request()->isolation_info().top_frame_origin().has_value()); \
33+
static_cast<CookieMonster*>(cookie_store) \
34+
->GetEphemeralCookieListWithOptionsAsync( \
35+
request_->url(), \
36+
request()->isolation_info().top_frame_origin()->GetURL(), options, \
37+
base::BindOnce(&URLRequestHttpJob::SetCookieHeaderAndStart, \
38+
weak_factory_.GetWeakPtr(), options)); \
39+
} else // NOLINT
40+
41+
#define BRAVE_SAVECOOKIESANDNOTIFYHEADERSCOMPLETE \
42+
if (ShouldUseEphemeralStorage(this)) { \
43+
DCHECK(request()->isolation_info().top_frame_origin().has_value()); \
44+
static_cast<CookieMonster*>(request_->context()->cookie_store()) \
45+
->SetEphemeralCanonicalCookieAsync( \
46+
std::move(cookie), request_->url(), \
47+
request()->isolation_info().top_frame_origin()->GetURL(), options, \
48+
base::BindOnce(&URLRequestHttpJob::OnSetCookieResult, \
49+
weak_factory_.GetWeakPtr(), options, \
50+
cookie_to_return, cookie_string)); \
51+
} else // NOLINT
52+
53+
#include "../../../../../net/url_request/url_request_http_job.cc"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
diff --git a/net/url_request/url_request_http_job.cc b/net/url_request/url_request_http_job.cc
2+
index f5e754f4ea0288a685a6932b00f692e3c6638621..d7da91c3607d205fd19cae3be369d4e58989f8bb 100644
3+
--- a/net/url_request/url_request_http_job.cc
4+
+++ b/net/url_request/url_request_http_job.cc
5+
@@ -583,6 +583,7 @@ void URLRequestHttpJob::AddCookieHeaderAndStart() {
6+
net::cookie_util::ComputeSameSiteContextForRequest(
7+
request_->method(), request_->url(), request_->site_for_cookies(),
8+
request_->initiator(), force_ignore_site_for_cookies));
9+
+ BRAVE_ADDCOOKIEHEADERANDSTART
10+
cookie_store->GetCookieListWithOptionsAsync(
11+
request_->url(), options,
12+
base::BindOnce(&URLRequestHttpJob::SetCookieHeaderAndStart,
13+
@@ -770,6 +771,7 @@ void URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete(int result) {
14+
continue;
15+
}
16+
17+
+ BRAVE_SAVECOOKIESANDNOTIFYHEADERSCOMPLETE
18+
request_->context()->cookie_store()->SetCanonicalCookieAsync(
19+
std::move(cookie), request_->url(), options,
20+
base::BindOnce(&URLRequestHttpJob::OnSetCookieResult,

0 commit comments

Comments
 (0)