1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-22 18:32:45 +01:00

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.
This commit is contained in:
Raymond Hill 2021-01-04 07:54:24 -05:00
parent ced4330d74
commit b28acfccbc
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
5 changed files with 41 additions and 9 deletions

View File

@ -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"
}
}
}
}

View File

@ -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);

View File

@ -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`);

View File

@ -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 ) {

View File

@ -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;
}
};
});