2019-04-24 04:50:25 +02:00
|
|
|
// Copyright (c) 2019 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/net_service/cookie_helper.h"
|
|
|
|
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
#include "libcef/common/net_service/net_service_util.h"
|
|
|
|
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/functional/bind.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "content/public/browser/browser_context.h"
|
|
|
|
#include "content/public/browser/storage_partition.h"
|
2021-01-28 00:13:12 +01:00
|
|
|
#include "content/public/common/url_constants.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "net/base/load_flags.h"
|
|
|
|
#include "net/cookies/cookie_options.h"
|
|
|
|
#include "net/cookies/cookie_util.h"
|
|
|
|
#include "services/network/cookie_manager.h"
|
|
|
|
#include "services/network/public/cpp/resource_request.h"
|
|
|
|
|
|
|
|
namespace net_service {
|
2020-09-04 21:08:55 +02:00
|
|
|
namespace cookie_helper {
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-01-10 22:11:16 +01:00
|
|
|
// Do not keep a reference to the object returned by this method.
|
|
|
|
CefBrowserContext* GetBrowserContext(const CefBrowserContext::Getter& getter) {
|
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
DCHECK(!getter.is_null());
|
|
|
|
|
|
|
|
// Will return nullptr if the BrowserContext has been shut down.
|
|
|
|
return getter.Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not keep a reference to the object returned by this method.
|
2019-04-24 04:50:25 +02:00
|
|
|
network::mojom::CookieManager* GetCookieManager(
|
|
|
|
content::BrowserContext* browser_context) {
|
|
|
|
CEF_REQUIRE_UIT();
|
2021-06-04 03:34:56 +02:00
|
|
|
return browser_context->GetDefaultStoragePartition()
|
2019-04-24 04:50:25 +02:00
|
|
|
->GetCookieManagerForBrowserProcess();
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:28:21 +02:00
|
|
|
net::CookieOptions GetCookieOptions(const network::ResourceRequest& request,
|
|
|
|
bool for_loading_cookies) {
|
2021-01-28 00:13:12 +01:00
|
|
|
// Match the logic from InterceptionJob::FetchCookies and
|
|
|
|
// ChromeContentBrowserClient::ShouldIgnoreSameSiteCookieRestrictionsWhenTopLevel.
|
|
|
|
bool should_treat_as_first_party =
|
|
|
|
request.url.SchemeIsCryptographic() &&
|
|
|
|
request.site_for_cookies.scheme() == content::kChromeUIScheme;
|
2021-03-04 23:36:57 +01:00
|
|
|
bool is_main_frame_navigation =
|
|
|
|
request.trusted_params &&
|
|
|
|
request.trusted_params->isolation_info.request_type() ==
|
|
|
|
net::IsolationInfo::RequestType::kMainFrame;
|
2021-01-28 00:13:12 +01:00
|
|
|
|
2022-08-19 18:28:21 +02:00
|
|
|
// Match the logic from URLRequest::SetURLChain.
|
|
|
|
std::vector<GURL> url_chain{request.url};
|
|
|
|
if (request.navigation_redirect_chain.size() >= 2) {
|
|
|
|
// Keep |request.url| as the final entry in the chain.
|
|
|
|
url_chain.insert(url_chain.begin(),
|
|
|
|
request.navigation_redirect_chain.begin(),
|
|
|
|
request.navigation_redirect_chain.begin() +
|
|
|
|
request.navigation_redirect_chain.size() - 1);
|
|
|
|
}
|
|
|
|
|
2021-01-28 00:13:12 +01:00
|
|
|
net::CookieOptions options;
|
|
|
|
options.set_include_httponly();
|
2022-08-19 18:28:21 +02:00
|
|
|
if (for_loading_cookies) {
|
|
|
|
// Match the logic from URLRequestHttpJob::AddCookieHeaderAndStart.
|
|
|
|
options.set_same_site_cookie_context(
|
|
|
|
net::cookie_util::ComputeSameSiteContextForRequest(
|
|
|
|
request.method, url_chain, request.site_for_cookies,
|
|
|
|
request.request_initiator, is_main_frame_navigation,
|
|
|
|
should_treat_as_first_party));
|
|
|
|
} else {
|
|
|
|
// Match the logic from
|
|
|
|
// URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete.
|
|
|
|
options.set_same_site_cookie_context(
|
|
|
|
net::cookie_util::ComputeSameSiteContextForResponse(
|
|
|
|
url_chain, request.site_for_cookies, request.request_initiator,
|
|
|
|
is_main_frame_navigation, should_treat_as_first_party));
|
|
|
|
}
|
2021-01-28 00:13:12 +01:00
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
//
|
|
|
|
// LOADING COOKIES.
|
|
|
|
//
|
|
|
|
|
|
|
|
void ContinueWithLoadedCookies(const AllowCookieCallback& allow_cookie_callback,
|
|
|
|
DoneCookieCallback done_callback,
|
2020-07-08 19:23:29 +02:00
|
|
|
const net::CookieAccessResultList& cookies) {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
net::CookieList allowed_cookies;
|
2019-10-01 15:55:16 +02:00
|
|
|
for (const auto& status : cookies) {
|
2019-04-24 04:50:25 +02:00
|
|
|
bool allow = false;
|
2019-10-01 15:55:16 +02:00
|
|
|
allow_cookie_callback.Run(status.cookie, &allow);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (allow) {
|
2019-10-01 15:55:16 +02:00
|
|
|
allowed_cookies.push_back(status.cookie);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
std::move(done_callback).Run(cookies.size(), std::move(allowed_cookies));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetCookieListCallback(const AllowCookieCallback& allow_cookie_callback,
|
|
|
|
DoneCookieCallback done_callback,
|
2020-07-08 19:23:29 +02:00
|
|
|
const net::CookieAccessResultList& included_cookies,
|
|
|
|
const net::CookieAccessResultList&) {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
CEF_POST_TASK(CEF_IOT,
|
|
|
|
base::BindOnce(ContinueWithLoadedCookies, allow_cookie_callback,
|
2019-10-01 15:55:16 +02:00
|
|
|
std::move(done_callback), included_cookies));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2021-10-19 00:17:16 +02:00
|
|
|
void LoadCookiesOnUIThread(
|
2022-01-10 22:11:16 +01:00
|
|
|
const CefBrowserContext::Getter& browser_context_getter,
|
2021-10-19 00:17:16 +02:00
|
|
|
const GURL& url,
|
|
|
|
const net::CookieOptions& options,
|
2021-12-16 23:35:54 +01:00
|
|
|
net::CookiePartitionKeyCollection cookie_partition_key_collection,
|
2021-10-19 00:17:16 +02:00
|
|
|
const AllowCookieCallback& allow_cookie_callback,
|
|
|
|
DoneCookieCallback done_callback) {
|
2022-01-10 22:11:16 +01:00
|
|
|
auto cef_browser_context = GetBrowserContext(browser_context_getter);
|
|
|
|
auto browser_context =
|
|
|
|
cef_browser_context ? cef_browser_context->AsBrowserContext() : nullptr;
|
|
|
|
if (!browser_context) {
|
|
|
|
GetCookieListCallback(allow_cookie_callback, std::move(done_callback),
|
|
|
|
net::CookieAccessResultList(),
|
|
|
|
net::CookieAccessResultList());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
GetCookieManager(browser_context)
|
|
|
|
->GetCookieList(
|
2021-12-16 23:35:54 +01:00
|
|
|
url, options, cookie_partition_key_collection,
|
2019-04-24 04:50:25 +02:00
|
|
|
base::BindOnce(GetCookieListCallback, allow_cookie_callback,
|
|
|
|
std::move(done_callback)));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// SAVING COOKIES.
|
|
|
|
//
|
|
|
|
|
|
|
|
struct SaveCookiesProgress {
|
|
|
|
DoneCookieCallback done_callback_;
|
|
|
|
int total_count_;
|
|
|
|
net::CookieList allowed_cookies_;
|
|
|
|
int num_cookie_lines_left_;
|
|
|
|
};
|
|
|
|
|
2020-07-08 19:23:29 +02:00
|
|
|
void SetCanonicalCookieCallback(SaveCookiesProgress* progress,
|
|
|
|
const net::CanonicalCookie& cookie,
|
2020-08-29 00:39:23 +02:00
|
|
|
net::CookieAccessResult access_result) {
|
2019-04-24 04:50:25 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
progress->num_cookie_lines_left_--;
|
2020-08-29 00:39:23 +02:00
|
|
|
if (access_result.status.IsInclude()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
progress->allowed_cookies_.push_back(cookie);
|
2019-06-05 16:15:45 +02:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
// If all the cookie lines have been handled the request can be continued.
|
|
|
|
if (progress->num_cookie_lines_left_ == 0) {
|
|
|
|
CEF_POST_TASK(CEF_IOT,
|
|
|
|
base::BindOnce(std::move(progress->done_callback_),
|
|
|
|
progress->total_count_,
|
|
|
|
std::move(progress->allowed_cookies_)));
|
|
|
|
delete progress;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:11:16 +01:00
|
|
|
void SaveCookiesOnUIThread(
|
|
|
|
const CefBrowserContext::Getter& browser_context_getter,
|
|
|
|
const GURL& url,
|
|
|
|
const net::CookieOptions& options,
|
|
|
|
int total_count,
|
|
|
|
net::CookieList cookies,
|
|
|
|
DoneCookieCallback done_callback) {
|
2019-04-24 04:50:25 +02:00
|
|
|
DCHECK(!cookies.empty());
|
|
|
|
|
2022-01-10 22:11:16 +01:00
|
|
|
auto cef_browser_context = GetBrowserContext(browser_context_getter);
|
|
|
|
auto browser_context =
|
|
|
|
cef_browser_context ? cef_browser_context->AsBrowserContext() : nullptr;
|
|
|
|
if (!browser_context) {
|
|
|
|
std::move(done_callback).Run(0, net::CookieList());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
network::mojom::CookieManager* cookie_manager =
|
|
|
|
GetCookieManager(browser_context);
|
|
|
|
|
|
|
|
// |done_callback| needs to be executed once and only once after the list has
|
|
|
|
// been fully processed. |num_cookie_lines_left_| keeps track of how many
|
|
|
|
// async callbacks are currently pending.
|
|
|
|
auto progress = new SaveCookiesProgress;
|
|
|
|
progress->done_callback_ = std::move(done_callback);
|
|
|
|
progress->total_count_ = total_count;
|
|
|
|
|
|
|
|
// Make sure to wait for the loop to complete.
|
|
|
|
progress->num_cookie_lines_left_ = 1;
|
|
|
|
|
|
|
|
for (const auto& cookie : cookies) {
|
|
|
|
progress->num_cookie_lines_left_++;
|
|
|
|
cookie_manager->SetCanonicalCookie(
|
2020-06-09 19:48:00 +02:00
|
|
|
cookie, url, options,
|
2019-04-24 04:50:25 +02:00
|
|
|
base::BindOnce(&SetCanonicalCookieCallback, base::Unretained(progress),
|
|
|
|
cookie));
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:15:45 +02:00
|
|
|
SetCanonicalCookieCallback(
|
|
|
|
progress, net::CanonicalCookie(),
|
2020-08-29 00:39:23 +02:00
|
|
|
net::CookieAccessResult(net::CookieInclusionStatus(
|
|
|
|
net::CookieInclusionStatus::EXCLUDE_UNKNOWN_ERROR)));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-09-04 21:08:55 +02:00
|
|
|
bool IsCookieableScheme(
|
|
|
|
const GURL& url,
|
2021-06-04 03:34:56 +02:00
|
|
|
const absl::optional<std::vector<std::string>>& cookieable_schemes) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!url.has_scheme()) {
|
2020-09-04 21:08:55 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-04 21:08:55 +02:00
|
|
|
|
|
|
|
if (cookieable_schemes) {
|
|
|
|
// The client has explicitly registered the full set of schemes that should
|
|
|
|
// be supported.
|
|
|
|
const auto url_scheme = url.scheme_piece();
|
|
|
|
for (auto scheme : *cookieable_schemes) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (url_scheme == scheme) {
|
2020-09-04 21:08:55 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-04 21:08:55 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schemes that support cookies by default.
|
|
|
|
// This should match CookieMonster::kDefaultCookieableSchemes.
|
|
|
|
return url.SchemeIsHTTPOrHTTPS() || url.SchemeIsWSOrWSS();
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:11:16 +01:00
|
|
|
void LoadCookies(const CefBrowserContext::Getter& browser_context_getter,
|
2019-04-24 04:50:25 +02:00
|
|
|
const network::ResourceRequest& request,
|
|
|
|
const AllowCookieCallback& allow_cookie_callback,
|
|
|
|
DoneCookieCallback done_callback) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
|
|
|
if ((request.load_flags & net::LOAD_DO_NOT_SEND_COOKIES) ||
|
2020-02-10 18:10:17 +01:00
|
|
|
request.credentials_mode == network::mojom::CredentialsMode::kOmit ||
|
|
|
|
request.url.IsAboutBlank()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
// Continue immediately without loading cookies.
|
|
|
|
std::move(done_callback).Run(0, {});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_POST_TASK(
|
2022-08-19 18:28:21 +02:00
|
|
|
CEF_UIT,
|
|
|
|
base::BindOnce(LoadCookiesOnUIThread, browser_context_getter, request.url,
|
|
|
|
GetCookieOptions(request, /*for_loading_cookies=*/true),
|
|
|
|
net::CookiePartitionKeyCollection(), allow_cookie_callback,
|
|
|
|
std::move(done_callback)));
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:11:16 +01:00
|
|
|
void SaveCookies(const CefBrowserContext::Getter& browser_context_getter,
|
2019-04-24 04:50:25 +02:00
|
|
|
const network::ResourceRequest& request,
|
2020-03-04 01:29:39 +01:00
|
|
|
net::HttpResponseHeaders* headers,
|
2019-04-24 04:50:25 +02:00
|
|
|
const AllowCookieCallback& allow_cookie_callback,
|
|
|
|
DoneCookieCallback done_callback) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
|
2019-10-01 15:55:16 +02:00
|
|
|
if (request.credentials_mode == network::mojom::CredentialsMode::kOmit ||
|
2020-03-04 01:29:39 +01:00
|
|
|
request.url.IsAboutBlank() || !headers ||
|
|
|
|
!headers->HasHeader(net_service::kHTTPSetCookieHeaderName)) {
|
2019-04-24 04:50:25 +02:00
|
|
|
// Continue immediately without saving cookies.
|
|
|
|
std::move(done_callback).Run(0, {});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match the logic in
|
|
|
|
// URLRequestHttpJob::SaveCookiesAndNotifyHeadersComplete.
|
|
|
|
base::Time response_date;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!headers->GetDateValue(&response_date)) {
|
2019-04-24 04:50:25 +02:00
|
|
|
response_date = base::Time();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
const base::StringPiece name(net_service::kHTTPSetCookieHeaderName);
|
|
|
|
std::string cookie_string;
|
|
|
|
size_t iter = 0;
|
|
|
|
net::CookieList allowed_cookies;
|
|
|
|
int total_count = 0;
|
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
while (headers->EnumerateHeader(&iter, name, &cookie_string)) {
|
2019-04-24 04:50:25 +02:00
|
|
|
total_count++;
|
|
|
|
|
2020-07-08 19:23:29 +02:00
|
|
|
net::CookieInclusionStatus returned_status;
|
2019-04-24 04:50:25 +02:00
|
|
|
std::unique_ptr<net::CanonicalCookie> cookie = net::CanonicalCookie::Create(
|
2019-10-01 15:55:16 +02:00
|
|
|
request.url, cookie_string, base::Time::Now(),
|
2022-06-17 15:28:55 +02:00
|
|
|
absl::make_optional(response_date), /*partition_key=*/absl::nullopt,
|
2023-09-15 21:51:43 +02:00
|
|
|
/*block_truncated_cookies=*/true, &returned_status);
|
2019-10-01 15:55:16 +02:00
|
|
|
if (!returned_status.IsInclude()) {
|
2019-04-24 04:50:25 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool allow = false;
|
|
|
|
allow_cookie_callback.Run(*cookie, &allow);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (allow) {
|
2019-04-24 04:50:25 +02:00
|
|
|
allowed_cookies.push_back(*cookie);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-04-24 04:50:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!allowed_cookies.empty()) {
|
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_UIT,
|
2022-08-19 18:28:21 +02:00
|
|
|
base::BindOnce(
|
|
|
|
SaveCookiesOnUIThread, browser_context_getter, request.url,
|
|
|
|
GetCookieOptions(request, /*for_loading_cookies=*/false),
|
|
|
|
total_count, std::move(allowed_cookies), std::move(done_callback)));
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
std::move(done_callback).Run(total_count, std::move(allowed_cookies));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-04 21:08:55 +02:00
|
|
|
} // namespace cookie_helper
|
2023-09-15 21:51:43 +02:00
|
|
|
} // namespace net_service
|