Skip to content

Fix Allow Scripts Once logic for embedded scripts and iframes. #12023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ IN_PROC_BROWSER_TEST_F(BraveShieldsWebContentsObserverBrowserTest,
brave_shields_web_contents_observer()->Reset();
GetWebContents()->GetController().Reload(content::ReloadType::NORMAL, true);
EXPECT_TRUE(WaitForLoadStop(GetWebContents()));
EXPECT_EQ(brave_shields_web_contents_observer()->block_javascript_count(), 1);
EXPECT_GT(brave_shields_web_contents_observer()->block_javascript_count(), 0);

// Disable JavaScript blocking again now.
content_settings()->SetContentSettingCustomScope(
Expand All @@ -138,10 +138,30 @@ IN_PROC_BROWSER_TEST_F(BraveShieldsWebContentsObserverBrowserTest,
EXPECT_EQ(CONTENT_SETTING_ALLOW, block_javascript_setting);

// Reload the test page now that JavaScript has been allowed again.
// Do it twice, because first reload will still trigger blocked events as
// renderer caches AllowScript results in
// ContentSettingsAgentImpl::cached_script_permissions_.
GetWebContents()->GetController().Reload(content::ReloadType::NORMAL, true);
EXPECT_TRUE(WaitForLoadStop(GetWebContents()));

brave_shields_web_contents_observer()->Reset();
GetWebContents()->GetController().Reload(content::ReloadType::NORMAL, true);
EXPECT_TRUE(WaitForLoadStop(GetWebContents()));
EXPECT_EQ(brave_shields_web_contents_observer()->block_javascript_count(), 0);
}

IN_PROC_BROWSER_TEST_F(BraveShieldsWebContentsObserverBrowserTest,
EmbeddedJavaScriptTriggersBlockedEvent) {
// Enable JavaScript blocking globally.
content_settings()->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(),
ContentSettingsType::JAVASCRIPT, CONTENT_SETTING_BLOCK);

// Load a simple HTML that attempts to run some JavaScript.
EXPECT_TRUE(ui_test_utils::NavigateToURL(
browser(), embedded_test_server()->GetURL("a.com", "/embedded_js.html")));
EXPECT_TRUE(WaitForLoadStop(GetWebContents()));
EXPECT_GT(brave_shields_web_contents_observer()->block_javascript_count(), 0);
}

} // namespace brave_shields
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,25 @@ BraveContentSettingsAgentImpl::BraveContentSettingsAgentImpl(

BraveContentSettingsAgentImpl::~BraveContentSettingsAgentImpl() {}

void BraveContentSettingsAgentImpl::DidCommitProvisionalLoad(
ui::PageTransition transition) {
temporarily_allowed_scripts_ =
std::move(preloaded_temporarily_allowed_scripts_);
ContentSettingsAgentImpl::DidCommitProvisionalLoad(transition);
}

bool BraveContentSettingsAgentImpl::IsScriptTemporilyAllowed(
const GURL& script_url) {
// Check if scripts from this origin are temporily allowed or not.
// Also matches the full script URL to support data URL cases which we use
// the full URL to allow it.
return base::Contains(temporarily_allowed_scripts_,
url::Origin::Create(script_url).Serialize()) ||
base::Contains(temporarily_allowed_scripts_, script_url.spec());
bool allow = base::Contains(temporarily_allowed_scripts_,
url::Origin::Create(script_url).Serialize()) ||
base::Contains(temporarily_allowed_scripts_, script_url.spec());
if (!allow) {
// Also check rules in the main frame, because this frame rules may be out
// of sync.
content::RenderFrame* main_frame = render_frame()->GetMainRenderFrame();
if (main_frame && main_frame != render_frame()) {
allow = static_cast<BraveContentSettingsAgentImpl*>(
ContentSettingsAgentImpl::Get(main_frame))
->IsScriptTemporilyAllowed(script_url);
}
}
return allow;
}

void BraveContentSettingsAgentImpl::BraveSpecificDidBlockJavaScript(
Expand All @@ -122,6 +126,10 @@ bool BraveContentSettingsAgentImpl::AllowScript(bool enabled_per_settings) {
allow = allow || IsBraveShieldsDown(frame, secondary_url) ||
IsScriptTemporilyAllowed(secondary_url);

if (!allow) {
blocked_script_url_ = secondary_url;
}

return allow;
}

Expand Down Expand Up @@ -342,7 +350,7 @@ bool BraveContentSettingsAgentImpl::AllowAutoplay(bool play_requested) {

void BraveContentSettingsAgentImpl::SetAllowScriptsFromOriginsOnce(
const std::vector<std::string>& origins) {
preloaded_temporarily_allowed_scripts_ = std::move(origins);
temporarily_allowed_scripts_ = origins;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

origins is not movable (it's a mojo-generated method).

}

void BraveContentSettingsAgentImpl::BindBraveShieldsReceiver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@ class BraveContentSettingsAgentImpl
FRIEND_TEST_ALL_PREFIXES(BraveContentSettingsAgentImplAutoplayBrowserTest,
AutoplayAllowedByDefault);

bool IsBraveShieldsDown(
const blink::WebFrame* frame,
const GURL& secondary_url);

// RenderFrameObserver
void DidCommitProvisionalLoad(ui::PageTransition transition) override;
bool IsBraveShieldsDown(const blink::WebFrame* frame,
const GURL& secondary_url);

bool IsScriptTemporilyAllowed(const GURL& script_url);

Expand All @@ -99,9 +95,6 @@ class BraveContentSettingsAgentImpl
// cache blocked script url which will later be used in `DidNotAllowScript()`
GURL blocked_script_url_;

// temporary allowed script origins we preloaded for the next load
base::flat_set<std::string> preloaded_temporarily_allowed_scripts_;
Copy link
Member Author

@goodov goodov Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't work on reloads, an embedded script is executed before this is applied.


base::flat_map<url::Origin, blink::WebSecurityOrigin>
cached_ephemeral_storage_origins_;

Expand Down
4 changes: 4 additions & 0 deletions test/data/embedded_js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><head><title>embedded js</title></head>
<body>
<script src="data:application/javascript;base64,dmFyIGZyYW1lID0gZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgnaWZyYW1lJyk7CmRvY3VtZW50LmJvZHkuYXBwZW5kQ2hpbGQoZnJhbWUpOw=="></script>
</body></html>