[mv3] Fix overwriting valid DNR rules with invalid entries

This fixes a flaw which has existed since the first version of
uBO Lite.

Related issue:
- https://github.com/uBlockOrigin/uBOL-issues/issues/11

Related commit:
- a559f5f271
This commit is contained in:
Raymond Hill 2022-10-18 16:06:27 -04:00
parent d41b5ebe04
commit 9879b7d03c
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 8 additions and 8 deletions

View File

@ -304,27 +304,27 @@ async function processNetworkFilters(assetDetails, network) {
writeFile( writeFile(
`${rulesetDir}/main/${assetDetails.id}.json`, `${rulesetDir}/main/${assetDetails.id}.json`,
`${JSON.stringify(plainGood, replacer)}\n` `${JSON.stringify(plainGood, replacer, 1)}\n`
); );
if ( regexes.length !== 0 ) { if ( regexes.length !== 0 ) {
writeFile( writeFile(
`${rulesetDir}/regex/${assetDetails.id}.json`, `${rulesetDir}/regex/${assetDetails.id}.json`,
`${JSON.stringify(regexes, replacer)}\n` `${JSON.stringify(regexes, replacer, 1)}\n`
); );
} }
if ( removeparamsGood.length !== 0 ) { if ( removeparamsGood.length !== 0 ) {
writeFile( writeFile(
`${rulesetDir}/removeparam/${assetDetails.id}.json`, `${rulesetDir}/removeparam/${assetDetails.id}.json`,
`${JSON.stringify(removeparamsGood, replacer)}\n` `${JSON.stringify(removeparamsGood, replacer, 1)}\n`
); );
} }
if ( redirects.length !== 0 ) { if ( redirects.length !== 0 ) {
writeFile( writeFile(
`${rulesetDir}/redirect/${assetDetails.id}.json`, `${rulesetDir}/redirect/${assetDetails.id}.json`,
`${JSON.stringify(redirects, replacer)}\n` `${JSON.stringify(redirects, replacer, 1)}\n`
); );
} }

View File

@ -4277,6 +4277,7 @@ FilterContainer.prototype.dnrFromCompiled = function(op, context, ...args) {
} }
// Patch id // Patch id
const rulesetFinal = [];
{ {
let ruleId = 1; let ruleId = 1;
for ( const rule of rulesetMap.values() ) { for ( const rule of rulesetMap.values() ) {
@ -4285,16 +4286,15 @@ FilterContainer.prototype.dnrFromCompiled = function(op, context, ...args) {
} else { } else {
rule.id = 0; rule.id = 0;
} }
rulesetFinal.push(rule);
} }
for ( const invalid of context.invalid ) { for ( const invalid of context.invalid ) {
rulesetMap.set(ruleId++, { rulesetFinal.push({ _error: [ invalid ] });
_error: [ invalid ],
});
} }
} }
return { return {
ruleset: Array.from(rulesetMap.values()), ruleset: rulesetFinal,
filterCount: context.filterCount, filterCount: context.filterCount,
acceptedFilterCount: context.acceptedFilterCount, acceptedFilterCount: context.acceptedFilterCount,
rejectedFilterCount: context.rejectedFilterCount, rejectedFilterCount: context.rejectedFilterCount,