diff --git a/browser/brave_shields/brave_shields_web_contents_observer.cc b/browser/brave_shields/brave_shields_web_contents_observer.cc index 670aa88f8e65..d07bb0fa3f06 100644 --- a/browser/brave_shields/brave_shields_web_contents_observer.cc +++ b/browser/brave_shields/brave_shields_web_contents_observer.cc @@ -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()) { @@ -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 @@ -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( @@ -257,16 +257,23 @@ void BraveShieldsWebContentsObserver::ReadyToCommitNavigation( } void BraveShieldsWebContentsObserver::BlockAllowedScripts( - const std::vector& origins) { - for (const auto& origin : origins) { - base::Erase(allowed_script_origins_, origin); + const std::vector& 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& 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 diff --git a/browser/brave_shields/brave_shields_web_contents_observer.h b/browser/brave_shields/brave_shields_web_contents_observer.h index 791ee1278530..7901c1423827 100644 --- a/browser/brave_shields/brave_shields_web_contents_observer.h +++ b/browser/brave_shields/brave_shields_web_contents_observer.h @@ -98,7 +98,7 @@ class BraveShieldsWebContentsObserver mojo::AssociatedRemote& GetBraveShieldsRemote(content::RenderFrameHost* rfh); - std::vector allowed_script_origins_; + std::vector 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 blocked_url_paths_; diff --git a/browser/brave_shields/brave_shields_web_contents_observer_browsertest.cc b/browser/brave_shields/brave_shields_web_contents_observer_browsertest.cc index 226cf3a133a4..54f6058ffd96 100644 --- a/browser/brave_shields/brave_shields_web_contents_observer_browsertest.cc +++ b/browser/brave_shields/brave_shields_web_contents_observer_browsertest.cc @@ -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(), diff --git a/components/brave_shields/resources/panel/components/tree-list/index.tsx b/components/brave_shields/resources/panel/components/tree-list/index.tsx index 9b9e322eff47..363dfcc40f26 100644 --- a/components/brave_shields/resources/panel/components/tree-list/index.tsx +++ b/components/brave_shields/resources/panel/components/tree-list/index.tsx @@ -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 ( @@ -105,9 +105,9 @@ function TreeList (props: Props) { return () })} @@ -116,7 +116,7 @@ function TreeList (props: Props) { {props.blockedList.length} {props.totalBlockedTitle} - {allowedList.length > 0 && ( + {props.allowedList && ( { {getLocale('braveShieldsAllowScriptsAll')} @@ -126,11 +126,11 @@ function TreeList (props: Props) { {[...blockedScriptsByOrigin.keys()].map((origin, idx) => { return () })} diff --git a/components/brave_shields/resources/panel/components/tree-list/resource-element.tsx b/components/brave_shields/resources/panel/components/tree-list/resource-element.tsx index 474afc4e761c..2621b8cdb66e 100644 --- a/components/brave_shields/resources/panel/components/tree-list/resource-element.tsx +++ b/components/brave_shields/resources/panel/components/tree-list/resource-element.tsx @@ -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) { @@ -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({ @@ -47,7 +48,7 @@ function ResourceElement (props: UrlElementProps) { return (
- {props.name} + {props.path ? props.path : props.host}
{props.onPermissionButtonClick && (