Skip to content

Commit

Permalink
[mv3] Implement strict blocking
Browse files Browse the repository at this point in the history
Related issue:
uBlockOrigin/uBOL-home#214

This implements basic functionality for strict blocking, i.e. the
ability to block navigation to undesirable websites. This is a
first implementation, which converts only filters that are plain
hostnames.

Unlike with uBO, it is not possible to know from which ruleset a
blocking rule originates. Nonetheless, users will have to make a
choice as to whether navigation should proceed or not.

A setting has been added to the dashboard to wholly enable/disable
strict blocking. It is enabled by default.

Potential future improvements, pending investigation on feasability
in an MV3 framework:
- Extend coverage to explicit `document` filters
- Leverage and use `urlskip=` filters in the blocking page in order
  to proceed while bypassing unwanted redirects.
  • Loading branch information
gorhill committed Dec 3, 2024
1 parent d7df6cd commit aa05cb3
Show file tree
Hide file tree
Showing 18 changed files with 879 additions and 365 deletions.
5 changes: 2 additions & 3 deletions platform/mv3/chromium/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"128": "img/icon_128.png"
},
"manifest_version": 3,
"minimum_chrome_version": "119.0",
"minimum_chrome_version": "122.0",
"name": "__MSG_extName__",
"options_page": "dashboard.html",
"optional_host_permissions": [
Expand All @@ -41,6 +41,5 @@
"storage": {
"managed_schema": "managed_storage.json"
},
"version": "1.0",
"web_accessible_resources": []
"version": "1.0"
}
36 changes: 36 additions & 0 deletions platform/mv3/extension/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,44 @@
"message": "Show the number of blocked requests on the toolbar icon",
"description": "Label for a checkbox in the options page"
},
"enableStrictBlockLabel": {
"message": "Enable strict blocking",
"description": "Label for a checkbox in the options page"
},
"enableStrictBlockLegend": {
"message": "Navigation to potentially undesirable sites will be blocked, and you will be offered the option to proceed.",
"description": "Label for a checkbox in the options page"
},
"findListsPlaceholder": {
"message": "Find lists",
"description": "Placeholder for the input field used to find lists"
},
"strictblockTitle": {
"message": "Page blocked",
"description": "Webpage title for the strict-blocked page"
},
"strictblockSentence1": {
"message": "uBO Lite has prevented the following page from loading:",
"description": "Sentence used in the strict-blocked page"
},
"strictblockNoParamsPrompt": {
"message": "without parameters",
"description": "Label to be used for the parameter-less URL"
},
"strictblockBack": {
"message": "Go back",
"description": "A button to go back to the previous webpage"
},
"strictblockClose": {
"message": "Close this window",
"description": "A button to close the current tab"
},
"strictblockDontWarn": {
"message": "Don't warn me again about this site",
"description": "Label for checkbox in document-blocked page"
},
"strictblockProceed": {
"message": "Proceed",
"description": "A button to navigate to the blocked page"
}
}
7 changes: 5 additions & 2 deletions platform/mv3/extension/css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ body.firstRun .firstRun {
h3 {
margin: 1em 0;
}
p {
white-space: pre-line;

label + legend {
color: color-mix(in srgb, currentColor 69%, transparent);
font-size: small;
margin-inline-start: var(--default-gap-large);
}

body[data-forbid~="dashboard"] #dashboard-nav [data-pane="settings"],
Expand Down
147 changes: 147 additions & 0 deletions platform/mv3/extension/css/strictblock.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
uBlock Origin - a browser extension to block requests.
Copyright (C) 2018-present Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/

body {
display: flex;
padding: var(--default-gap-xxlarge) var(--default-gap-small);
justify-content: center;
}
:root.mobile body {
padding: var(--default-gap-small);
}

#rootContainer {
width: min(100%, 640px);
}
#rootContainer > * {
margin: 0 0 var(--default-gap-xxlarge) 0;
}
:root.mobile #rootContainer > * {
margin-bottom: var(--default-gap-xlarge);
}

p {
margin: 0.5em 0;
}
a {
text-decoration: none;
}
.code {
font-size: 13px;
word-break: break-all;
}
#warningSign {
color: var(--accent-surface-1);
fill: var(--accent-surface-1);
font-size: 96px;
line-height: 1;
width: 100%;
}
:root.mobile #warningSign {
font-size: 64px;
}
#theURL {
color: var(--ink-2);
padding: 0;
}
#theURL > * {
margin: 0;
}
#theURL > p {
position: relative;
z-index: 10;
}
#theURL > p > span:first-of-type {
display: block;
max-height: 6lh;
overflow-y: auto;
}
:root.mobile #theURL > p > span:first-of-type {
max-height: 3lh;
}
#theURL #toggleParse {
background-color: transparent;
top: 100%;
box-sizing: border-box;
color: var(--ink-3);
fill: var(--ink-3);
cursor: pointer;
font-size: 1.2rem;
padding: var(--default-gap-xxsmall);
position: absolute;
transform: translate(0, -50%);
}
#theURL:not(.collapsed) #toggleParse > span:first-of-type {
display: none;
}
#theURL.collapsed #toggleParse > span:last-of-type {
display: none;
}
body[dir="ltr"] #toggleParse {
right: 0;
}
body[dir="rtl"] #toggleParse {
left: 0;
}
#theURL > p:hover #toggleParse {
transform: translate(0, -50%) scale(1.15);
}
#parsed {
background-color: var(--surface-1);
border: 4px solid var(--surface-2);
font-size: small;
overflow-x: auto;
padding: var(--default-gap-xxsmall);
text-align: initial;
text-overflow: ellipsis;
}
#theURL.collapsed > #parsed {
display: none;
}
#parsed ul, #parsed li {
list-style-type: none;
}
#parsed li {
white-space: nowrap;
}
#parsed span {
display: inline-block;
}
#parsed span:first-of-type {
font-weight: bold;
}

#actionContainer {
display: flex;
justify-content: space-between;
}
:root.mobile #actionContainer {
justify-content: center;
display: flex;
flex-direction: column;
}
#actionContainer > button {
margin-bottom: 2rem
}

/* Small-screen devices */
:root.mobile button {
width: 100%;
}
1 change: 1 addition & 0 deletions platform/mv3/extension/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ <h3 data-i18n="behaviorSectionLabel"></h3>
<p><label id="autoReload" data-i18n="autoReloadLabel"><span class="input checkbox"><input type="checkbox"><svg viewBox="0 0 24 24"><path d="M1.73,12.91 8.1,19.28 22.79,4.59"/></svg></span>_</label>
</p>
<p><label id="showBlockedCount" data-i18n="showBlockedCountLabel"><span class="input checkbox"><input type="checkbox"><svg viewBox="0 0 24 24"><path d="M1.73,12.91 8.1,19.28 22.79,4.59"/></svg></span>_</label>
<p><label id="strictBlockMode" data-i18n="enableStrictBlockLabel"><span class="input checkbox"><input type="checkbox"><svg viewBox="0 0 24 24"><path d="M1.73,12.91 8.1,19.28 22.79,4.59"/></svg></span>_</label><legend data-i18n="enableStrictBlockLegend"></legend>
<p id="developerMode" hidden><label><span class="input checkbox"><input type="checkbox"><svg viewBox="0 0 24 24"><path d="M1.73,12.91 8.1,19.28 22.79,4.59"/></svg></span>Developer mode</label>
</div>

Expand Down
29 changes: 23 additions & 6 deletions platform/mv3/extension/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ import {

import {
enableRulesets,
excludeFromStrictBlock,
getEnabledRulesetsDetails,
getRulesetDetails,
setStrictBlockMode,
updateDynamicRules,
} from './ruleset-manager.js';

Expand Down Expand Up @@ -213,6 +215,7 @@ function onMessage(request, sender, callback) {
autoReload: rulesetConfig.autoReload,
showBlockedCount: rulesetConfig.showBlockedCount,
canShowBlockedCount,
strictBlockMode: rulesetConfig.strictBlockMode,
firstRun: process.firstRun,
isSideloaded,
developerMode: rulesetConfig.developerMode,
Expand Down Expand Up @@ -244,6 +247,13 @@ function onMessage(request, sender, callback) {
});
return true;

case 'setStrictBlockMode':
setStrictBlockMode(request.state).then(( ) => {
callback();
broadcastMessage({ strictBlockMode: rulesetConfig.strictBlockMode });
});
return true;

case 'setDeveloperMode':
rulesetConfig.developerMode = request.state;
toggleDeveloperMode(rulesetConfig.developerMode);
Expand Down Expand Up @@ -335,6 +345,13 @@ function onMessage(request, sender, callback) {
});
return true;

case 'excludeFromStrictBlock': {
excludeFromStrictBlock(request.hostname, request.permanent).then(( ) => {
callback();
});
return true;
}

case 'getMatchedRules':
getMatchedRules(request.tabId).then(entries => {
callback(entries);
Expand All @@ -360,19 +377,19 @@ function onMessage(request, sender, callback) {
async function start() {
await loadRulesetConfig();

if ( process.wakeupRun === false ) {
const rulesetsUpdated = process.wakeupRun === false &&
await enableRulesets(rulesetConfig.enabledRulesets);
}

// We need to update the regex rules only when ruleset version changes.
if ( process.wakeupRun === false ) {
const currentVersion = getCurrentVersion();
if ( currentVersion !== rulesetConfig.version ) {
ubolLog(`Version change: ${rulesetConfig.version} => ${currentVersion}`);
updateDynamicRules().then(( ) => {
rulesetConfig.version = currentVersion;
saveRulesetConfig();
});
rulesetConfig.version = currentVersion;
saveRulesetConfig();
if ( rulesetsUpdated === false ) {
updateDynamicRules();
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions platform/mv3/extension/js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const rulesetConfig = {
enabledRulesets: [ 'default' ],
autoReload: true,
showBlockedCount: true,
strictBlockMode: true,
developerMode: false,
};

Expand All @@ -50,6 +51,7 @@ export async function loadRulesetConfig() {
rulesetConfig.enabledRulesets = sessionData.enabledRulesets;
rulesetConfig.autoReload = sessionData.autoReload ?? true;
rulesetConfig.showBlockedCount = sessionData.showBlockedCount ?? true;
rulesetConfig.strictBlockMode = sessionData.strictBlockMode ?? true;
rulesetConfig.developerMode = sessionData.developerMode ?? false;
process.wakeupRun = true;
return;
Expand All @@ -60,6 +62,7 @@ export async function loadRulesetConfig() {
rulesetConfig.enabledRulesets = localData.enabledRulesets;
rulesetConfig.autoReload = localData.autoReload ?? true;
rulesetConfig.showBlockedCount = localData.showBlockedCount ?? true;
rulesetConfig.strictBlockMode = localData.strictBlockMode ?? true;
rulesetConfig.developerMode = localData.developerMode ?? false;
sessionWrite('rulesetConfig', rulesetConfig);
return;
Expand Down
6 changes: 6 additions & 0 deletions platform/mv3/extension/js/ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ export async function sessionWrite(key, value) {
return browser.storage.session.set({ [key]: value });
}

export async function sessionRemove(key) {
if ( browser.storage instanceof Object === false ) { return; }
if ( browser.storage.session instanceof Object === false ) { return; }
return browser.storage.session.remove(key);
}

/******************************************************************************/

export async function adminRead(key) {
Expand Down
Loading

0 comments on commit aa05cb3

Please sign in to comment.