From b28acfccbc9c3c694c1af65520e3e25ce675a3b8 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 4 Jan 2021 07:54:24 -0500 Subject: [PATCH] Add "extraTrustedSiteDirectives" as new admin policy Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/1433 The new "extraTrustedSiteDirectives" policy is an array of strings, each of which is parsed as a trusted-site directive to append to a user's own set of trusted-site directives at launch time. The added trusted-site directives will be considered as part of the default set of directives by uBO. --- platform/chromium/managed_storage.json | 10 +++++++++- platform/chromium/vapi-background.js | 4 ++-- src/js/start.js | 26 ++++++++++++++++++++++---- src/js/storage.js | 2 +- src/js/whitelist.js | 8 +++++++- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/platform/chromium/managed_storage.json b/platform/chromium/managed_storage.json index d3f2c864f..44737cfbd 100644 --- a/platform/chromium/managed_storage.json +++ b/platform/chromium/managed_storage.json @@ -3,9 +3,17 @@ "type": "object", "properties": { "adminSettings": { - "title": "A valid JSON string compliant with uBO's backup format.", + "title": "A valid JSON string compliant with uBO's backup format", "description": "All entries present will overwrite local settings.", "type": "string" + }, + "extraTrustedSiteDirectives": { + "title": "A list of trusted-site directives", + "description": "Trusted-site directives to always add at launch time.", + "type": "array", + "items": { + "type": "string" + } } } } diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index 9a5f4f0b3..871de996b 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -1417,13 +1417,13 @@ vAPI.commands = browser.commands; vAPI.adminStorage = (( ) => { if ( webext.storage.managed instanceof Object === false ) { return { - getItem: function() { + get: function() { return Promise.resolve(); }, }; } return { - getItem: async function(key) { + get: async function(key) { let bin; try { bin = await webext.storage.managed.get(key); diff --git a/src/js/start.js b/src/js/start.js index 79a1a441a..d814750ba 100644 --- a/src/js/start.js +++ b/src/js/start.js @@ -133,11 +133,24 @@ const onVersionReady = function(lastVersion) { // Whitelist in memory. // Whitelist parser needs PSL to be ready. // gorhill 2014-12-15: not anymore +// +// https://github.com/uBlockOrigin/uBlock-issues/issues/1433 +// Allow admins to add their own trusted-site directives. -const onNetWhitelistReady = function(netWhitelistRaw) { +const onNetWhitelistReady = function(netWhitelistRaw, adminExtra) { if ( typeof netWhitelistRaw === 'string' ) { netWhitelistRaw = netWhitelistRaw.split('\n'); } + // Append admin-controlled trusted-site directives + if ( + Array.isArray(adminExtra.trustedSites) && + adminExtra.trustedSites.length !== 0 + ) { + for ( const directive of adminExtra.trustedSites ) { + µb.netWhitelistDefault.push(directive); + netWhitelistRaw.push(directive); + } + } µb.netWhitelist = µb.whitelistFromArray(netWhitelistRaw); µb.netWhitelistModifyTime = Date.now(); }; @@ -190,7 +203,7 @@ const onCacheSettingsReady = async function(fetched) { /******************************************************************************/ -const onFirstFetchReady = function(fetched) { +const onFirstFetchReady = function(fetched, adminExtra) { // https://github.com/uBlockOrigin/uBlock-issues/issues/507 // Firefox-specific: somehow `fetched` is undefined under certain // circumstances even though we asked to load with default values. @@ -202,7 +215,7 @@ const onFirstFetchReady = function(fetched) { fromFetch(µb.localSettings, fetched); onUserSettingsReady(fetched); fromFetch(µb.restoreBackupSettings, fetched); - onNetWhitelistReady(fetched.netWhitelist); + onNetWhitelistReady(fetched.netWhitelist, adminExtra); onVersionReady(fetched.version); }; @@ -283,6 +296,11 @@ try { ); log.info(`Backend storage for cache will be ${cacheBackend}`); + const adminExtra = {}; + adminExtra.trustedSites = + await vAPI.adminStorage.get('extraTrustedSiteDirectives') || []; + log.info(`Extra admin settings ready ${Date.now()-vAPI.T0} ms after launch`); + // https://github.com/uBlockOrigin/uBlock-issues/issues/1365 // Wait for onCacheSettingsReady() to be fully ready. await Promise.all([ @@ -297,7 +315,7 @@ try { }), vAPI.storage.get(createDefaultProps()).then(fetched => { log.info(`First fetch ready ${Date.now()-vAPI.T0} ms after launch`); - onFirstFetchReady(fetched); + onFirstFetchReady(fetched, adminExtra); }), µb.loadPublicSuffixList().then(( ) => { log.info(`PSL ready ${Date.now()-vAPI.T0} ms after launch`); diff --git a/src/js/storage.js b/src/js/storage.js index e86d02cd9..f22a72966 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -1224,7 +1224,7 @@ self.addEventListener('hiddenSettingsChanged', ( ) => { µBlock.restoreAdminSettings = async function() { let data; try { - const json = await vAPI.adminStorage.getItem('adminSettings'); + const json = await vAPI.adminStorage.get('adminSettings'); if ( typeof json === 'string' && json !== '' ) { data = JSON.parse(json); } else if ( json instanceof Object ) { diff --git a/src/js/whitelist.js b/src/js/whitelist.js index 0ff38cd10..afd07bec8 100644 --- a/src/js/whitelist.js +++ b/src/js/whitelist.js @@ -70,7 +70,13 @@ CodeMirror.defineMode("ubo-whitelist-directives", function() { } return null; } - return reHostnameExtractor.test(line) ? null : 'error'; + if ( reHostnameExtractor.test(line) === false ) { + return 'error'; + } + if ( whitelistDefaultSet.has(line.trim()) ) { + return 'keyword'; + } + return null; } }; });