2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2011 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.
|
|
|
|
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/libcef/browser/origin_whitelist_impl.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
#include <string>
|
2012-11-20 21:08:36 +01:00
|
|
|
#include <vector>
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2023-01-30 18:43:54 +01:00
|
|
|
#include "base/functional/bind.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "base/lazy_instance.h"
|
2012-11-20 21:08:36 +01:00
|
|
|
#include "base/synchronization/lock.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/cef_origin_whitelist.h"
|
|
|
|
#include "cef/libcef/browser/browser_manager.h"
|
|
|
|
#include "cef/libcef/browser/context.h"
|
|
|
|
#include "cef/libcef/browser/thread_util.h"
|
2021-05-14 18:58:55 +02:00
|
|
|
#include "cef/libcef/common/mojom/cef.mojom.h"
|
2020-08-14 21:28:23 +02:00
|
|
|
#include "chrome/common/webui_url_constants.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "content/public/browser/render_process_host.h"
|
2020-08-14 21:28:23 +02:00
|
|
|
#include "content/public/common/url_constants.h"
|
|
|
|
#include "extensions/common/constants.h"
|
2013-07-24 22:15:18 +02:00
|
|
|
#include "url/gurl.h"
|
2020-08-14 21:28:23 +02:00
|
|
|
#include "url/origin.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Class that manages cross-origin whitelist registrations.
|
|
|
|
class CefOriginWhitelistManager {
|
|
|
|
public:
|
2021-12-06 21:40:25 +01:00
|
|
|
CefOriginWhitelistManager() = default;
|
|
|
|
|
|
|
|
CefOriginWhitelistManager(const CefOriginWhitelistManager&) = delete;
|
|
|
|
CefOriginWhitelistManager& operator=(const CefOriginWhitelistManager&) =
|
|
|
|
delete;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Retrieve the singleton instance.
|
|
|
|
static CefOriginWhitelistManager* GetInstance();
|
|
|
|
|
|
|
|
bool AddOriginEntry(const std::string& source_origin,
|
|
|
|
const std::string& target_protocol,
|
|
|
|
const std::string& target_domain,
|
|
|
|
bool allow_target_subdomains) {
|
2021-05-14 18:58:55 +02:00
|
|
|
auto info = cef::mojom::CrossOriginWhiteListEntry::New();
|
|
|
|
info->source_origin = source_origin;
|
|
|
|
info->target_protocol = target_protocol;
|
|
|
|
info->target_domain = target_domain;
|
|
|
|
info->allow_target_subdomains = allow_target_subdomains;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-11-20 21:08:36 +01:00
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-11-20 21:08:36 +01:00
|
|
|
// Verify that the origin entry doesn't already exist.
|
2021-05-14 18:58:55 +02:00
|
|
|
for (const auto& entry : origin_list_) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (entry == info) {
|
2012-11-20 21:08:36 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-11-20 21:08:36 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2021-05-14 18:58:55 +02:00
|
|
|
origin_list_.push_back(info->Clone());
|
2012-11-20 21:08:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SendModifyCrossOriginWhitelistEntry(true, info);
|
2012-04-03 03:34:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RemoveOriginEntry(const std::string& source_origin,
|
|
|
|
const std::string& target_protocol,
|
|
|
|
const std::string& target_domain,
|
|
|
|
bool allow_target_subdomains) {
|
2021-05-14 18:58:55 +02:00
|
|
|
auto info = cef::mojom::CrossOriginWhiteListEntry::New();
|
|
|
|
info->source_origin = source_origin;
|
|
|
|
info->target_protocol = target_protocol;
|
|
|
|
info->target_domain = target_domain;
|
|
|
|
info->allow_target_subdomains = allow_target_subdomains;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
bool found = false;
|
|
|
|
|
2012-11-20 21:08:36 +01:00
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
|
2021-05-14 18:58:55 +02:00
|
|
|
CrossOriginWhiteList::iterator it = origin_list_.begin();
|
2012-11-20 21:08:36 +01:00
|
|
|
for (; it != origin_list_.end(); ++it) {
|
2021-05-14 18:58:55 +02:00
|
|
|
if (*it == info) {
|
2012-11-20 21:08:36 +01:00
|
|
|
origin_list_.erase(it);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!found) {
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-11-20 21:08:36 +01:00
|
|
|
SendModifyCrossOriginWhitelistEntry(false, info);
|
2012-04-03 03:34:16 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearOrigins() {
|
2012-11-20 21:08:36 +01:00
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
origin_list_.clear();
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
SendClearCrossOriginWhitelist();
|
|
|
|
}
|
|
|
|
|
2012-11-20 21:08:36 +01:00
|
|
|
void GetCrossOriginWhitelistEntries(
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<CrossOriginWhiteList>* entries) const {
|
2012-11-20 21:08:36 +01:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2021-05-14 18:58:55 +02:00
|
|
|
if (!origin_list_.empty()) {
|
|
|
|
CrossOriginWhiteList vec;
|
|
|
|
for (const auto& entry : origin_list_) {
|
|
|
|
vec.push_back(entry->Clone());
|
|
|
|
}
|
|
|
|
*entries = std::move(vec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasCrossOriginWhitelistEntry(const url::Origin& source,
|
|
|
|
const url::Origin& target) const {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
|
|
|
|
if (!origin_list_.empty()) {
|
|
|
|
for (const auto& entry : origin_list_) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (IsMatch(source, target, entry)) {
|
2021-05-14 18:58:55 +02:00
|
|
|
return true;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-05-14 18:58:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Send the modify cross-origin whitelist entry message to all currently
|
|
|
|
// existing hosts.
|
2012-11-20 21:08:36 +01:00
|
|
|
static void SendModifyCrossOriginWhitelistEntry(
|
|
|
|
bool add,
|
2021-05-14 18:58:55 +02:00
|
|
|
const cef::mojom::CrossOriginWhiteListEntryPtr& info) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
content::RenderProcessHost::iterator i(
|
|
|
|
content::RenderProcessHost::AllHostsIterator());
|
|
|
|
for (; !i.IsAtEnd(); i.Advance()) {
|
2021-05-14 18:58:55 +02:00
|
|
|
auto render_manager =
|
|
|
|
CefBrowserManager::GetRenderManagerForProcess(i.GetCurrentValue());
|
|
|
|
render_manager->ModifyCrossOriginWhitelistEntry(add, info->Clone());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the clear cross-origin whitelists message to all currently existing
|
|
|
|
// hosts.
|
2012-11-20 21:08:36 +01:00
|
|
|
static void SendClearCrossOriginWhitelist() {
|
2012-04-03 03:34:16 +02:00
|
|
|
CEF_REQUIRE_UIT();
|
|
|
|
|
|
|
|
content::RenderProcessHost::iterator i(
|
|
|
|
content::RenderProcessHost::AllHostsIterator());
|
|
|
|
for (; !i.IsAtEnd(); i.Advance()) {
|
2021-05-14 18:58:55 +02:00
|
|
|
auto render_manager =
|
|
|
|
CefBrowserManager::GetRenderManagerForProcess(i.GetCurrentValue());
|
|
|
|
render_manager->ClearCrossOriginWhitelist();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:58:55 +02:00
|
|
|
static bool IsMatch(const url::Origin& source_origin,
|
|
|
|
const url::Origin& target_origin,
|
|
|
|
const cef::mojom::CrossOriginWhiteListEntryPtr& param) {
|
|
|
|
if (!source_origin.IsSameOriginWith(
|
|
|
|
url::Origin::Create(GURL(param->source_origin)))) {
|
|
|
|
// Source origin does not match.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target_origin.scheme() != param->target_protocol) {
|
|
|
|
// Target scheme does not match.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (param->allow_target_subdomains) {
|
|
|
|
if (param->target_domain.empty()) {
|
|
|
|
// Any domain will match.
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Match sub-domains.
|
|
|
|
return target_origin.DomainIs(param->target_domain.c_str());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Match full domain.
|
|
|
|
return (target_origin.host() == param->target_domain);
|
|
|
|
}
|
2012-11-20 21:08:36 +01:00
|
|
|
}
|
|
|
|
|
2021-05-14 18:58:55 +02:00
|
|
|
mutable base::Lock lock_;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-11-20 21:08:36 +01:00
|
|
|
// List of registered origins. Access must be protected by |lock_|.
|
2021-05-14 18:58:55 +02:00
|
|
|
CrossOriginWhiteList origin_list_;
|
2012-04-03 03:34:16 +02:00
|
|
|
};
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
base::LazyInstance<CefOriginWhitelistManager>::Leaky g_manager =
|
2012-04-03 03:34:16 +02:00
|
|
|
LAZY_INSTANCE_INITIALIZER;
|
|
|
|
|
|
|
|
CefOriginWhitelistManager* CefOriginWhitelistManager::GetInstance() {
|
|
|
|
return g_manager.Pointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
bool CefAddCrossOriginWhitelistEntry(const CefString& source_origin,
|
|
|
|
const CefString& target_protocol,
|
|
|
|
const CefString& target_domain,
|
|
|
|
bool allow_target_subdomains) {
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false);
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string source_url = source_origin;
|
|
|
|
GURL gurl = GURL(source_url);
|
|
|
|
if (gurl.is_empty() || !gurl.is_valid()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "Invalid source_origin URL: " << source_url;
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
return CefOriginWhitelistManager::GetInstance()->AddOriginEntry(
|
|
|
|
source_origin, target_protocol, target_domain, allow_target_subdomains);
|
|
|
|
} else {
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_UIT,
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(base::IgnoreResult(&CefAddCrossOriginWhitelistEntry),
|
|
|
|
source_origin, target_protocol, target_domain,
|
|
|
|
allow_target_subdomains));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefRemoveCrossOriginWhitelistEntry(const CefString& source_origin,
|
|
|
|
const CefString& target_protocol,
|
|
|
|
const CefString& target_domain,
|
|
|
|
bool allow_target_subdomains) {
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false);
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string source_url = source_origin;
|
|
|
|
GURL gurl = GURL(source_url);
|
|
|
|
if (gurl.is_empty() || !gurl.is_valid()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false) << "Invalid source_origin URL: " << source_url;
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
return CefOriginWhitelistManager::GetInstance()->RemoveOriginEntry(
|
|
|
|
source_origin, target_protocol, target_domain, allow_target_subdomains);
|
|
|
|
} else {
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(
|
|
|
|
CEF_UIT,
|
2021-06-04 03:34:56 +02:00
|
|
|
base::BindOnce(base::IgnoreResult(&CefRemoveCrossOriginWhitelistEntry),
|
|
|
|
source_origin, target_protocol, target_domain,
|
|
|
|
allow_target_subdomains));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefClearCrossOriginWhitelist() {
|
|
|
|
// Verify that the context is in a valid state.
|
|
|
|
if (!CONTEXT_STATE_VALID()) {
|
2023-05-08 17:07:57 +02:00
|
|
|
DCHECK(false);
|
2012-04-03 03:34:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CEF_CURRENTLY_ON_UIT()) {
|
|
|
|
CefOriginWhitelistManager::GetInstance()->ClearOrigins();
|
|
|
|
} else {
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_POST_TASK(
|
2021-06-04 03:34:56 +02:00
|
|
|
CEF_UIT,
|
|
|
|
base::BindOnce(base::IgnoreResult(&CefClearCrossOriginWhitelist)));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-20 21:08:36 +01:00
|
|
|
void GetCrossOriginWhitelistEntries(
|
2024-03-19 22:11:42 +01:00
|
|
|
std::optional<CrossOriginWhiteList>* entries) {
|
2012-11-20 21:08:36 +01:00
|
|
|
CefOriginWhitelistManager::GetInstance()->GetCrossOriginWhitelistEntries(
|
|
|
|
entries);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2013-04-18 19:58:23 +02:00
|
|
|
|
2020-08-14 21:28:23 +02:00
|
|
|
bool HasCrossOriginWhitelistEntry(const url::Origin& source,
|
|
|
|
const url::Origin& target) {
|
|
|
|
// Components of chrome that are implemented as extensions or platform apps
|
|
|
|
// are allowed to use chrome://resources/ and chrome://theme/ URLs.
|
|
|
|
// See also RegisterNonNetworkSubresourceURLLoaderFactories.
|
|
|
|
if (source.scheme() == extensions::kExtensionScheme &&
|
|
|
|
target.scheme() == content::kChromeUIScheme &&
|
|
|
|
(target.host() == chrome::kChromeUIThemeHost ||
|
|
|
|
target.host() == content::kChromeUIResourcesHost)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:58:55 +02:00
|
|
|
return CefOriginWhitelistManager::GetInstance()->HasCrossOriginWhitelistEntry(
|
|
|
|
source, target);
|
2013-04-18 19:58:23 +02:00
|
|
|
}
|