Skip to content

Commit

Permalink
fix: resolve alias with slashes before conversion
Browse files Browse the repository at this point in the history
Adguard dnr converter doesn't allow you to convert redirect filters with slash value to dnr. This PR solves the problem by replacing the slash value prior to the dnr conversion.
  • Loading branch information
seia-soto committed Oct 14, 2024
1 parent ab74223 commit 6f96fcb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/converters/adguard.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default async function convert(rules, { resourcesPath } = {}) {
const declarativeRules = await conversionResult.ruleSet.getDeclarativeRules();

return {
rules: declarativeRules.map(normalizeRule),
rules: declarativeRules.map(rule => normalizeRule(rule)),
errors: conversionResult.errors,
limitations: conversionResult.limitations,
};
Expand Down
53 changes: 35 additions & 18 deletions src/converters/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,27 @@ function getPathBasename(path) {
return path.slice(lastIndex + 1);
}

function getPathDirname(path) {
const lastIndex = path.lastIndexOf('/');
if (lastIndex === -1) {
return '.';
}
return path.slice(0, lastIndex);
}

export function generateResourcesMapping() {
const mappings = new Map();
const resourcesMapping = new Map();
for (const names of definition) {
for (const name of names.slice(1)) {
mappings.set(name, names[0]);
resourcesMapping.set(name, names[0]);
}
}
return mappings;
return resourcesMapping;
}

export const DEFAULT_PARAM_MAPPING = {
'3p': 'third-party',
'xhr': 'xmlhttprequest',
'frame': 'subdocument'
};
export const DEFAULT_RESOURCES_MAPPING = generateResourcesMapping();

export function normalizeFilter(filter, { mapping = DEFAULT_PARAM_MAPPING } = {}) {
export function normalizeFilter(filter, {
mapping = DEFAULT_PARAM_MAPPING,
resourcesMapping = DEFAULT_RESOURCES_MAPPING,
} = {}) {
let [front, ...back] = filter.split("$");
let params = back.join(',').split(',');

Expand All @@ -53,16 +49,34 @@ export function normalizeFilter(filter, { mapping = DEFAULT_PARAM_MAPPING } = {}
front = front.toLowerCase();
}

// adguard converter doesn't work with $redirect with slash value
// replace possible $redirect params including a slash
const indexOfRedirect = params.findIndex(p => p.startsWith('redirect=') && p.includes('/'));
if (indexOfRedirect !== -1) {
const name = resourcesMapping.get(params[indexOfRedirect].slice(9));
if (name !== undefined) {
params[indexOfRedirect] = 'redirect=' + name;
}
}

const indexOfRedirectRule = params.findIndex(p => p.startsWith('redirect-rule=') && p.includes('/'));
if (indexOfRedirectRule !== -1) {
const name = resourcesMapping.get(params[indexOfRedirectRule].slice(14));
if (name !== undefined) {
params[indexOfRedirectRule] = 'redirect-rule=' + name;
}
}

if (back.length === 0) {
return front;
}

return `${front}$${params.join(',')}`;
}

export const DEFAULT_RESOURCE_MAPPING = generateResourcesMapping();

export function normalizeRule(rule, { resourcesMapping = DEFAULT_RESOURCE_MAPPING } = {}) {
export function normalizeRule(rule, {
resourcesMapping = DEFAULT_RESOURCES_MAPPING
} = {}) {
if (!rule) {
return;
}
Expand Down Expand Up @@ -101,11 +115,14 @@ export function normalizeRule(rule, { resourcesMapping = DEFAULT_RESOURCE_MAPPIN

if (newRule.action && newRule.action.type === 'redirect') {
const filename = getPathBasename(newRule.action.redirect.extensionPath);
const preferredFilename = resourcesMapping.get(filename);

const preferredFilename =
resourcesMapping.get(filename) ??
// try searching without an extension
// adguard converter attaches an file extension at the end
resourcesMapping.get(filename.slice(0, filename.lastIndexOf('.')));
if (preferredFilename !== undefined) {
newRule.action.redirect.extensionPath =
getPathDirname(newRule.action.redirect.extensionPath) + '/' + preferredFilename;
newRule.action.redirect.extensionPath.slice(0, -filename.length) + preferredFilename;
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/unit/converters/helpers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ describe("normalizeFilter", () => {
).toEqual("TEST$match-case");
});
});

describe("with redirect param", () => {
it("replaces values with slashes", () => {
expect(
normalizeFilter("test$redirect=scorecardresearch.com/beacon.js")
).toEqual("test$redirect=scorecardresearch_beacon.js");
expect(
normalizeFilter("test$redirect-rule=scorecardresearch.com/beacon.js")
).toEqual("test$redirect-rule=scorecardresearch_beacon.js");
});
});
});

describe('normalizeRule', () => {
Expand Down

0 comments on commit 6f96fcb

Please sign in to comment.