Skip to content

Commit

Permalink
Sorted parameters and fixed block specific scripts by full urls
Browse files Browse the repository at this point in the history
  • Loading branch information
spylogsster committed Mar 9, 2023
1 parent 8e0f12f commit cbb16e6
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 35 deletions.
25 changes: 16 additions & 9 deletions browser/brave_shields/brave_shields_web_contents_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ BraveShieldsWebContentsObserver::BraveShieldsWebContentsObserver(
receivers_(web_contents, this) {}

void BraveShieldsWebContentsObserver::RenderFrameCreated(RenderFrameHost* rfh) {
if (rfh && allowed_script_origins_.size()) {
if (rfh && allowed_scripts_.size()) {
GetBraveShieldsRemote(rfh)->SetAllowScriptsFromOriginsOnce(
allowed_script_origins_);
allowed_scripts_);
}
if (rfh) {
if (content::BrowserContext* context = rfh->GetBrowserContext()) {
Expand Down Expand Up @@ -233,7 +233,7 @@ void BraveShieldsWebContentsObserver::ReadyToCommitNavigation(
!navigation_handle->IsSameDocument()) {
if (reload_type == content::ReloadType::NONE) {
// For new loads, we reset the counters for both blocked scripts and URLs.
allowed_script_origins_.clear();
allowed_scripts_.clear();
blocked_url_paths_.clear();
} else if (reload_type == content::ReloadType::NORMAL) {
// For normal reloads (or loads to the current URL, internally converted
Expand All @@ -246,7 +246,7 @@ void BraveShieldsWebContentsObserver::ReadyToCommitNavigation(
navigation_handle->GetWebContents()->ForEachRenderFrameHost(
[this](content::RenderFrameHost* rfh) {
GetBraveShieldsRemote(rfh)->SetAllowScriptsFromOriginsOnce(
allowed_script_origins_);
allowed_scripts_);
if (content::BrowserContext* context = rfh->GetBrowserContext()) {
if (PrefService* pref_service = user_prefs::UserPrefs::Get(context)) {
GetBraveShieldsRemote(rfh)->SetReduceLanguageEnabled(
Expand All @@ -257,16 +257,23 @@ void BraveShieldsWebContentsObserver::ReadyToCommitNavigation(
}

void BraveShieldsWebContentsObserver::BlockAllowedScripts(
const std::vector<std::string>& origins) {
for (const auto& origin : origins) {
base::Erase(allowed_script_origins_, origin);
const std::vector<std::string>& scripts) {
for (const auto& script : scripts) {
auto origin = url::Origin::Create(GURL(script));
bool is_origin = origin.Serialize() == script;
base::EraseIf(allowed_scripts_, [is_origin, script,
origin](const std::string& value) {
// scripts array may have both origins or full scripts paths.
return is_origin ? url::Origin::Create(GURL(value)) == origin
: value == script;
});
}
}

void BraveShieldsWebContentsObserver::AllowScriptsOnce(
const std::vector<std::string>& origins) {
allowed_script_origins_.insert(std::end(allowed_script_origins_),
std::begin(origins), std::end(origins));
allowed_scripts_.insert(std::end(allowed_scripts_), std::begin(origins),
std::end(origins));
}

// static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class BraveShieldsWebContentsObserver
mojo::AssociatedRemote<brave_shields::mojom::BraveShields>&
GetBraveShieldsRemote(content::RenderFrameHost* rfh);

std::vector<std::string> allowed_script_origins_;
std::vector<std::string> allowed_scripts_;
// We keep a set of the current page's blocked URLs in case the page
// continually tries to load the same blocked URLs.
std::set<std::string> blocked_url_paths_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ IN_PROC_BROWSER_TEST_F(BraveShieldsWebContentsObserverBrowserTest,
EXPECT_EQ(GetBlockedJsList().size(), 2u);
EXPECT_EQ(GetAllowedJsList().size(), 1u);

brave_shields_web_contents_observer()->BlockAllowedScripts(
{url::Origin::Create(blocked_list.back()).Serialize()});
ClearAllResourcesList();
GetWebContents()->GetController().Reload(content::ReloadType::NORMAL, true);
EXPECT_TRUE(WaitForLoadStop(GetWebContents()));
EXPECT_EQ(GetBlockedJsList().size(), 3u);
EXPECT_EQ(GetAllowedJsList().size(), 0u);

// Disable JavaScript blocking again now.
content_settings()->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ function TreeList (props: Props) {
[props.blockedList])

const handleAllowAllScripts = () => {
const origins = props.blockedList.map(entry => new URL(entry.url).origin)
getPanelBrowserAPI().dataHandler.allowScriptsOnce(origins)
const scripts = props.blockedList.map(entry => entry.url)
getPanelBrowserAPI().dataHandler.allowScriptsOnce(scripts)
}

const handleBlockAllScripts = () => {
const origins = allowedList.map(entry => new URL(entry.url).origin)
getPanelBrowserAPI().dataHandler.blockAllowedScripts(origins)
const scripts = allowedList.map(entry => entry.url)
getPanelBrowserAPI().dataHandler.blockAllowedScripts(scripts)
}

const handleBlockScript = (name: string) => {
getPanelBrowserAPI().dataHandler.blockAllowedScripts([new URL(name).origin])
const handleBlockScript = (url: string) => {
getPanelBrowserAPI().dataHandler.blockAllowedScripts([url])
}

const handleAllowScript = allowedList.length === 0 ? undefined
: (name: string) => {
getPanelBrowserAPI().dataHandler.allowScriptsOnce([new URL(name).origin])
const handleAllowScript = !props.allowedList ? undefined
: (url: string) => {
getPanelBrowserAPI().dataHandler.allowScriptsOnce([url])
}

return (
Expand Down Expand Up @@ -105,9 +105,9 @@ function TreeList (props: Props) {
return (<TreeNode
key={origin}
host={origin}
resourceList={allowedScriptsByOrigin.get(origin) ?? []}
onPermissionButtonClick={handleBlockScript}
permissionButtonTitle={getLocale('braveShieldsBlockScript')}
resourceList={allowedScriptsByOrigin.get(origin) ?? []}
/>)
})}
</ScriptsList>
Expand All @@ -116,7 +116,7 @@ function TreeList (props: Props) {
<ScriptsInfo>
<span>{props.blockedList.length}</span>
<span>{props.totalBlockedTitle}</span>
{allowedList.length > 0 && (<span>
{props.allowedList && (<span>
{<a href="#" onClick={handleAllowAllScripts}>
{getLocale('braveShieldsAllowScriptsAll')}
</a>
Expand All @@ -126,11 +126,11 @@ function TreeList (props: Props) {
<ScriptsList>
{[...blockedScriptsByOrigin.keys()].map((origin, idx) => {
return (<TreeNode
key={idx}
key={origin}
host={origin}
permissionButtonTitle={getLocale('braveShieldsAllowScriptOnce')}
onPermissionButtonClick={handleAllowScript}
resourceList={blockedScriptsByOrigin.get(origin) ?? []}
onPermissionButtonClick={handleAllowScript}
permissionButtonTitle={getLocale('braveShieldsAllowScriptOnce')}
/>)
})}
</ScriptsList>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import {
} from '../../state/component_types'

interface UrlElementProps {
name: string
path?: string
host: string
permissionButtonTitle: string
onTextExpand?: () => void
onPermissionButtonClick: PermissionButtonHandler
isHost: boolean
}

function ResourceElement (props: UrlElementProps) {
Expand All @@ -28,15 +28,16 @@ function ResourceElement (props: UrlElementProps) {
setExpanded(true)
props.onTextExpand?.()
}

const url = props.path ? props.host + props.path : props.host
const handlePermissionButtonClick = () => {
props.onPermissionButtonClick?.(props.name)
props.onPermissionButtonClick?.(url)

}

const urlTextClass = classnames({
[style.textUrl]: true,
[style.textMultiline]: isExpanded,
[style.textHost]: props.isHost
[style.textHost]: props.path === undefined
})

const containerClass = classnames({
Expand All @@ -47,7 +48,7 @@ function ResourceElement (props: UrlElementProps) {
return (
<div className={containerClass}>
<div className={urlTextClass} onClick={handleTextClick}>
{props.name}
{props.path ? props.path : props.host}
</div>
{props.onPermissionButtonClick && (
<button onClick={handlePermissionButtonClick}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ function TreeNode (props: TreeNodeProps) {
resourcesListElement = (
<div ref={treeChildrenBoxRef}>
{
props.resourceList.map((resource: string, idx) => {
props.resourceList.map((path: string, idx) => {
return (<ResourceElement
key={idx}
isHost={false}
permissionButtonTitle={props.permissionButtonTitle}
name={resource}
path={path}
host={props.host}
onTextExpand={measure}
onPermissionButtonClick={props.onPermissionButtonClick}
permissionButtonTitle={props.permissionButtonTitle}
/>)
})
}
Expand Down Expand Up @@ -152,7 +152,8 @@ function TreeNode (props: TreeNodeProps) {
{verticalAxisSVGElement}
</S.TreeControlBox>
<S.TreeContents>
<ResourceElement name={props.host} isHost={true}
<ResourceElement
host={props.host}
onPermissionButtonClick={props.onPermissionButtonClick}
permissionButtonTitle={props.permissionButtonTitle} />
{resourcesListElement}
Expand Down

0 comments on commit cbb16e6

Please sign in to comment.