Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/compat/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export function toContentRules(rules: Rule[], host: string, now: number): Conten
}

export interface ParsedImport {
accepted: RuleInput[];
/** Each valid rule with its position in the file, so a rule skipped later can be named. */
accepted: { index: number; rule: RuleInput }[];
rejected: { index: number; reason: string }[];
}

Expand All @@ -88,7 +89,7 @@ export function parseRulesImport(payload: unknown): ParsedImport {
],
};
}
const accepted: RuleInput[] = [];
const accepted: ParsedImport['accepted'] = [];
const rejected: ParsedImport['rejected'] = [];
envelope.data.rules.slice(0, LIMITS.maxRules).forEach((raw, index) => {
const candidate = z
Expand All @@ -106,7 +107,7 @@ export function parseRulesImport(payload: unknown): ParsedImport {
return;
}
const result = RuleInputSchema.safeParse({ ...candidate.data, enabled: false });
if (result.success) accepted.push(result.data);
if (result.success) accepted.push({ index, rule: result.data });
else rejected.push({ index, reason: result.error.issues.map((i) => i.message).join('; ') });
});
return { accepted, rejected };
Expand Down
16 changes: 13 additions & 3 deletions src/storage/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,28 @@ export class Repository {
const now = this.now();
let imported = 0;
let duplicates = 0;
for (const input of parsed.accepted) {
const rejected = [...parsed.rejected];
for (const { index, rule: input } of parsed.accepted) {
const rule = createRule(input, 'community', now);
if (rules.some((r) => r.domain === rule.domain && r.selector === rule.selector)) {
duplicates++;
continue;
}
if (rules.length >= LIMITS.maxRules) break;
// Past the limit each remaining rule is reported, so the outcome
// accounts for every rule in the file instead of dropping the rest.
if (rules.length >= LIMITS.maxRules) {
rejected.push({
index,
reason: `Not imported: you can store at most ${LIMITS.maxRules} rules.`,
});
continue;
}
rules.push(rule);
imported++;
}
await this.area.set({ [KEYS.rules]: rules });
return { imported, duplicates, rejected: parsed.rejected };
rejected.sort((a, b) => a.index - b.index);
return { imported, duplicates, rejected };
});
}

Expand Down
28 changes: 28 additions & 0 deletions tests/unit/repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { MemoryArea } from '../../src/storage/area';
import { Repository, SCHEMA_VERSION } from '../../src/storage/repository';
import { DEFAULT_SETTINGS } from '../../src/shared/schemas';
import { ShieldFixError } from '../../src/shared/messages';
import { LIMITS } from '../../src/shared/constants';
import { createRule } from '../../src/compat/rules';

let area: MemoryArea;
let now: number;
Expand Down Expand Up @@ -142,6 +144,32 @@ describe('rules', () => {
expect(imported).toMatchObject({ enabled: false, source: 'community' });
});

it('reports the rules it skips once the rule limit is reached', async () => {
await area.set({
rules: Array.from({ length: LIMITS.maxRules - 2 }, (_, i) =>
createRule({ domain: 'example.com', selector: `.r${i}`, enabled: true }, 'user', now),
),
});
const result = await repo.importRules({
format: 'shieldfix-rules',
formatVersion: 1,
rules: [
{ domain: 'example.com', selector: '.r0' },
{ domain: 'example.com', selector: '.a' },
{ domain: 'example.com', selector: '.b' },
{ domain: 'example.com', selector: '.c' },
{ domain: 'example.com', selector: '.r1' },
],
});
// Two fit, ".c" does not: it must be reported, not silently dropped. A
// duplicate after the limit still counts as a duplicate.
expect(result).toMatchObject({ imported: 2, duplicates: 2 });
expect(result.rejected).toEqual([
{ index: 3, reason: `Not imported: you can store at most ${LIMITS.maxRules} rules.` },
]);
expect(await repo.getRules()).toHaveLength(LIMITS.maxRules);
});

it('round-trips through export and import', async () => {
await repo.upsertRule({ domain: 'example.com', selector: '.a', enabled: true });
const exported = await repo.exportRules();
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/rules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ describe('import', () => {
],
});
expect(parsed.accepted).toHaveLength(1);
expect(parsed.accepted[0]).toMatchObject({ enabled: false, selector: '.adblock-wall' });
expect(parsed.accepted[0]).not.toHaveProperty('id');
expect(parsed.accepted[0]!.index).toBe(0);
expect(parsed.accepted[0]!.rule).toMatchObject({ enabled: false, selector: '.adblock-wall' });
expect(parsed.accepted[0]!.rule).not.toHaveProperty('id');
expect(parsed.rejected.map((r) => r.index)).toEqual([1, 2, 3]);
});

Expand Down