Skip to content

Commit ac40d58

Browse files
committed
Prevents extensions from accessing protected resources.
Fixes brave/brave-browser#42998
1 parent f3ef3d8 commit ac40d58

File tree

6 files changed

+125
-9
lines changed

6 files changed

+125
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include_rules = [
2+
"+brave/components/skus/renderer",
3+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Copyright (c) 2025 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 https://mozilla.org/MPL/2.0/. */
5+
6+
#include "extensions/common/permissions/permissions_data.h"
7+
8+
#include "brave/components/skus/renderer/skus_utils.h"
9+
#include "url/origin.h"
10+
11+
namespace extensions {
12+
13+
bool IsBraveProtectedUrl(const GURL& url) {
14+
if (skus::IsSafeOrigin(url)) {
15+
return true;
16+
}
17+
return false;
18+
}
19+
20+
} // namespace extensions
21+
22+
namespace {
23+
24+
bool IsBraveRestrictedUrl(const GURL& document_url,
25+
const extensions::ExtensionId& extension_id,
26+
extensions::mojom::ManifestLocation location,
27+
std::string* error) {
28+
if (extensions::PermissionsData::CanExecuteScriptEverywhere(extension_id,
29+
location)) {
30+
return false;
31+
}
32+
33+
if (extensions::IsBraveProtectedUrl(document_url)) {
34+
return true;
35+
}
36+
37+
return false;
38+
}
39+
40+
} // namespace
41+
42+
// Disable some content scripts until users click on the extension icon
43+
#define BRAVE_CAN_RUN_ON_PAGE \
44+
if (IsBraveRestrictedUrl(document_url, extension_id_, location_, error)) { \
45+
return PageAccess::kWithheld; \
46+
}
47+
48+
#include "src/extensions/common/permissions/permissions_data.cc"
49+
50+
#undef BRAVE_CAN_RUN_ON_PAGE

components/skus/renderer/skus_utils.cc

+32-9
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,37 @@
55

66
#include "brave/components/skus/renderer/skus_utils.h"
77

8+
#include <algorithm>
9+
#include <string>
810
#include <vector>
911

1012
#include "base/no_destructor.h"
1113
#include "third_party/blink/public/platform/web_security_origin.h"
1214
#include "third_party/blink/public/platform/web_url.h"
1315
#include "url/gurl.h"
1416

15-
namespace skus {
17+
namespace {
18+
// NOTE: please open a security review when appending to this list.
19+
std::vector<std::string> safe_origins_string{
20+
"https://account.brave.com", "https://account.bravesoftware.com",
21+
"https://account.brave.software"};
1622

17-
bool IsSafeOrigin(const blink::WebSecurityOrigin& origin) {
18-
// NOTE: please open a security review when appending to this list.
19-
static base::NoDestructor<std::vector<blink::WebSecurityOrigin>> safe_origins{
20-
{{blink::WebSecurityOrigin::Create(GURL("https://account.brave.com"))},
21-
{blink::WebSecurityOrigin::Create(
22-
GURL("https://account.bravesoftware.com"))},
23-
{blink::WebSecurityOrigin::Create(
24-
GURL("https://account.brave.software"))}}};
23+
base::NoDestructor<std::vector<blink::WebSecurityOrigin>>
24+
WebSecurityOriginList() {
25+
std::vector<blink::WebSecurityOrigin> list(safe_origins_string.size());
26+
std::transform(safe_origins_string.begin(), safe_origins_string.end(),
27+
list.begin(), [](auto& origin_string) {
28+
return blink::WebSecurityOrigin::Create(GURL(origin_string));
29+
});
30+
return base::NoDestructor(list);
31+
}
32+
33+
} // namespace
2534

35+
namespace skus {
36+
bool IsSafeOrigin(const blink::WebSecurityOrigin& origin) {
37+
static base::NoDestructor<std::vector<blink::WebSecurityOrigin>>
38+
safe_origins = WebSecurityOriginList();
2639
for (const blink::WebSecurityOrigin& safe_origin : *safe_origins) {
2740
if (safe_origin.IsSameOriginWith(origin)) {
2841
return true;
@@ -31,4 +44,14 @@ bool IsSafeOrigin(const blink::WebSecurityOrigin& origin) {
3144
return false;
3245
}
3346

47+
bool IsSafeOrigin(const GURL& origin) {
48+
for (const std::string& safe_origin_string : safe_origins_string) {
49+
auto safe_origin = url::Origin::Create(GURL(safe_origin_string));
50+
if (safe_origin.IsSameOriginWith(origin)) {
51+
return true;
52+
}
53+
}
54+
return false;
55+
}
56+
3457
} // namespace skus

components/skus/renderer/skus_utils.h

+16
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,28 @@
66
#ifndef BRAVE_COMPONENTS_SKUS_RENDERER_SKUS_UTILS_H_
77
#define BRAVE_COMPONENTS_SKUS_RENDERER_SKUS_UTILS_H_
88

9+
class GURL;
10+
911
namespace blink {
1012
class WebSecurityOrigin;
1113
} // namespace blink
1214

1315
namespace skus {
16+
// This version is used in a renderer process where blink is initialized.
17+
// For example, if you are in a render frame observer where you get the origin
18+
// via `render_frame()->GetWebFrame()->GetSecurityOrigin()`.
19+
//
20+
// NOTE: You'll get DCHECK/CHECK errors for trying to create a
21+
// `blink::WebString` if you're not in a blink context (tests are fine).
22+
//
23+
// See //third_party/blink/renderer/platform/weborigin/security_origin.cc
1424
bool IsSafeOrigin(const blink::WebSecurityOrigin& origin);
25+
26+
// This version is safe for use elsewhere. The internal `IsSameOriginWith`
27+
// check is different than the version above.
28+
//
29+
// See //url/origin.cc
30+
bool IsSafeOrigin(const GURL& origin);
1531
} // namespace skus
1632

1733
#endif // BRAVE_COMPONENTS_SKUS_RENDERER_SKUS_UTILS_H_
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/extensions/common/BUILD.gn b/extensions/common/BUILD.gn
2+
index 118c7255c77e74bd8c5cdb011c7f9bc96288de40..51d7fc9375d5f4a9629b1d8842b50e1ab3e8e675 100644
3+
--- a/extensions/common/BUILD.gn
4+
+++ b/extensions/common/BUILD.gn
5+
@@ -551,6 +551,7 @@ static_library("common") {
6+
"//url",
7+
]
8+
9+
+ deps += ["//brave/components/skus/renderer"]
10+
if (enable_extensions) {
11+
deps += [ "//extensions:extensions_resources" ]
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/extensions/common/permissions/permissions_data.cc b/extensions/common/permissions/permissions_data.cc
2+
index ffdce9beaa7c207df5ab304dbcf4d97c631139ca..94131fdbd37a094b1376442fe44b5c2befde30f6 100644
3+
--- a/extensions/common/permissions/permissions_data.cc
4+
+++ b/extensions/common/permissions/permissions_data.cc
5+
@@ -649,6 +649,7 @@ PermissionsData::PageAccess PermissionsData::CanRunOnPage(
6+
return PageAccess::kDenied;
7+
}
8+
}
9+
+ BRAVE_CAN_RUN_ON_PAGE
10+
11+
if (tab_url_patterns && tab_url_patterns->MatchesURL(document_url))
12+
return PageAccess::kAllowed;

0 commit comments

Comments
 (0)