[mv3] `uritransform` cannot be converted to DNR

This commit is contained in:
Raymond Hill 2024-01-21 14:32:14 -05:00
parent ec3f029b18
commit 3d576603c4
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 22 additions and 19 deletions

View File

@ -110,7 +110,7 @@ const urlToFileName = url => {
; ;
}; };
const fetchList = (url, cacheDir) => { const fetchText = (url, cacheDir) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const fname = urlToFileName(url); const fname = urlToFileName(url);
fs.readFile(`${cacheDir}/${fname}`, { encoding: 'utf8' }).then(content => { fs.readFile(`${cacheDir}/${fname}`, { encoding: 'utf8' }).then(content => {
@ -168,7 +168,7 @@ const requiredRedirectResources = new Set();
/******************************************************************************/ /******************************************************************************/
async function fetchAsset(assetDetails) { async function fetchList(assetDetails) {
// Remember fetched URLs // Remember fetched URLs
const fetchedURLs = new Set(); const fetchedURLs = new Set();
@ -190,7 +190,7 @@ async function fetchAsset(assetDetails) {
newParts.push(`!#trusted on ${assetDetails.secret}`); newParts.push(`!#trusted on ${assetDetails.secret}`);
} }
newParts.push( newParts.push(
fetchList(part.url, cacheDir).then(details => { fetchText(part.url, cacheDir).then(details => {
const { url } = details; const { url } = details;
const content = details.content.trim(); const content = details.content.trim();
if ( typeof content === 'string' && content !== '' ) { if ( typeof content === 'string' && content !== '' ) {
@ -227,10 +227,13 @@ const isRegex = rule =>
rule.condition !== undefined && rule.condition !== undefined &&
rule.condition.regexFilter !== undefined; rule.condition.regexFilter !== undefined;
const isRedirect = rule => const isRedirect = rule => {
rule.action !== undefined && if ( rule.action === undefined ) { return false; }
rule.action.type === 'redirect' && if ( rule.action.type !== 'redirect' ) { return false; }
rule.action.redirect.extensionPath !== undefined; if ( rule.action.redirect?.extensionPath !== undefined ) { return true; }
if ( rule.action.redirect?.transform?.path !== undefined ) { return true; }
return false;
};
const isModifyHeaders = rule => const isModifyHeaders = rule =>
rule.action !== undefined && rule.action !== undefined &&
@ -367,6 +370,14 @@ async function processNetworkFilters(assetDetails, network) {
} }
} }
// Add native DNR ruleset if present
if ( assetDetails.dnrURL ) {
const result = await fetchText(assetDetails.dnrURL, cacheDir);
for ( const rule of JSON.parse(result.content) ) {
rules.push(rule);
}
}
const plainGood = rules.filter(rule => isGood(rule) && isRegex(rule) === false); const plainGood = rules.filter(rule => isGood(rule) && isRegex(rule) === false);
log(`\tPlain good: ${plainGood.length}`); log(`\tPlain good: ${plainGood.length}`);
log(plainGood log(plainGood
@ -384,6 +395,7 @@ async function processNetworkFilters(assetDetails, network) {
isRedirect(rule) isRedirect(rule)
); );
redirects.forEach(rule => { redirects.forEach(rule => {
if ( rule.action.redirect.extensionPath === undefined ) { return; }
requiredRedirectResources.add( requiredRedirectResources.add(
rule.action.redirect.extensionPath.replace(/^\/+/, '') rule.action.redirect.extensionPath.replace(/^\/+/, '')
); );
@ -979,7 +991,7 @@ async function rulesetFromURLs(assetDetails) {
log(`Listset for '${assetDetails.id}':`); log(`Listset for '${assetDetails.id}':`);
if ( assetDetails.text === undefined ) { if ( assetDetails.text === undefined ) {
const text = await fetchAsset(assetDetails); const text = await fetchList(assetDetails);
if ( text === '' ) { return; } if ( text === '' ) { return; }
assetDetails.text = text; assetDetails.text = text;
} }
@ -1155,6 +1167,7 @@ async function main() {
enabled: true, enabled: true,
secret, secret,
urls: contentURLs, urls: contentURLs,
dnrURL: 'https://ublockorigin.github.io/uAssets/dnr/default.json',
homeURL: 'https://github.com/uBlockOrigin/uAssets', homeURL: 'https://github.com/uBlockOrigin/uAssets',
}); });

View File

@ -4571,17 +4571,7 @@ FilterContainer.prototype.dnrFromCompiled = function(op, context, ...args) {
} }
break; break;
case 'uritransform': { case 'uritransform': {
const path = rule.__modifierValue; dnrAddRuleError(rule, `Unsupported uritransform=${rule.__modifierValue}`);
let priority = rule.priority || 1;
if ( rule.__modifierAction !== ALLOW_REALM ) {
const transform = { path };
rule.action.type = 'redirect';
rule.action.redirect = { transform };
rule.priority = priority + 1;
} else {
rule.action.type = 'block';
rule.priority = priority + 2;
}
break; break;
} }
default: default: