Skip to content

Commit

Permalink
Add "extraTrustedSiteDirectives" as new admin policy
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-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.
  • Loading branch information
gorhill committed Jan 4, 2021
1 parent ced4330 commit b28acfc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
10 changes: 9 additions & 1 deletion platform/chromium/managed_storage.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
4 changes: 2 additions & 2 deletions platform/chromium/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 22 additions & 4 deletions src/js/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};
Expand Down Expand Up @@ -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.
Expand All @@ -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);
};

Expand Down Expand Up @@ -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([
Expand 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`);
Expand Down
2 changes: 1 addition & 1 deletion src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
8 changes: 7 additions & 1 deletion src/js/whitelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
});
Expand Down

0 comments on commit b28acfc

Please sign in to comment.