Skip to content

Commit

Permalink
Support $redirect in DNR (#1750)
Browse files Browse the repository at this point in the history
  • Loading branch information
seia-soto authored Aug 8, 2024
1 parent 64097d1 commit 20eab7f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
32 changes: 32 additions & 0 deletions extension-manifest-v3/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,35 @@ if (manifest.declarative_net_request?.rule_resources) {
}
}

// copy redirect rule resources
shelljs.mkdir('-p', resolve(options.outDir, 'rule_resources/redirects'));
shelljs.cp(
'-r',
resolve(options.srcDir, 'rule_resources/redirects', '*'),
resolve(options.outDir, 'rule_resources/redirects'),
);

// append web_accessible_resources
const redirectResources = readdirSync(
resolve(options.srcDir, 'rule_resources/redirects'),
);

if (manifest.manifest_version === 3) {
manifest.web_accessible_resources.push({
resources: redirectResources.map((filename) =>
join('rule_resources/redirects', filename),
),
all_frames: true,
matches: ['<all_urls>'],
});
} else {
redirectResources.forEach((filename) =>
manifest.web_accessible_resources.push(
join('rule_resources/redirects', filename),
),
);
}

// generate license file
execSync('npm run licenses', { stdio: 'inherit' });

Expand Down Expand Up @@ -227,6 +256,9 @@ manifest.web_accessible_resources?.forEach((entry) => {
}

paths.forEach((path) => {
if (path.includes('/redirects/')) {
return;
}
if (!path.match(/\.(js|css|html)$/)) {
const dir = dirname(path);
shelljs.mkdir('-p', resolve(options.outDir, dir));
Expand Down
52 changes: 51 additions & 1 deletion extension-manifest-v3/scripts/download-engines/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createHash } from 'crypto';
import { resolve } from 'path';
import shelljs from 'shelljs';

import { ENGINE_VERSION } from '@cliqz/adblocker';
import { ENGINE_VERSION, FiltersEngine } from '@cliqz/adblocker';

function checksum(content) {
return createHash('sha256').update(content).digest('hex');
Expand Down Expand Up @@ -140,3 +140,53 @@ for (const [name, target] of Object.entries(DNR)) {
writeFileSync(outputPath, dnr);
}
}

// Extract resources from ads engine
console.log('Extracting resources...');

shelljs.mkdir('-p', resolve(TARGET_PATH, 'redirects'));

const seenResource = new Set();
const allowedResourceExtensions = [
'html',
'js',
'css',
'mp4',
'mp3',
'xml',
'txt',
'json',
'empty',
];

FiltersEngine.deserialize(
readFileSync(`${TARGET_PATH}/engine-ads.dat`),
).resources.resources.forEach((value, key) => {
// refs https://github.com/gorhill/uBlock/tree/master/src/web_accessible_resources
if (
value.contentType === 'application/javascript' &&
(value.body.includes('scriptletGlobals') || // Drop scriptlets
key.includes('/')) // Drop resources within a directory
) {
return;
}

if (
!allowedResourceExtensions.includes(key.split('.').pop()) // Drop resources with an unknown file extension
) {
return;
}

if (seenResource.has(value.body)) {
return;
}

seenResource.add(value.body);

// Decode base64
if (value.contentType.endsWith(';base64')) {
value.body = Buffer.from(value.body, 'base64').toString('binary');
}

writeFileSync(resolve(TARGET_PATH, 'redirects', key), value.body);
});

0 comments on commit 20eab7f

Please sign in to comment.