Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check domains validity when parsing network filters #4525

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

seia-soto
Copy link
Member

From #4524, I found that NetworkFilter.prototype.toString is broken for network modifiers utilizing Domains.

This fixes it by replacing all , into | in Domains.parts.

Also, this moves invalid option value check which was used to prevent passing $domain=|a.com| forms into Domains.prototype.parse.

@seia-soto seia-soto added the PR: Bug Fix 🐛 Increment patch version when merged label Dec 12, 2024
@seia-soto seia-soto self-assigned this Dec 12, 2024
@seia-soto seia-soto requested a review from remusao as a code owner December 12, 2024 08:07
Copy link
Member

@chrmod chrmod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add some tests for those changes?

@seia-soto
Copy link
Member Author

@chrmod Added 96f51b8 (#4525)

@seia-soto seia-soto requested a review from chrmod December 13, 2024 06:35

describe('Domains', () => {
it('rejects to parse invalid pipes', () => {
for (const str of ['|foo.com', 'foo.com|', '|foo.com|']) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we just drop | and accept foo.com?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should support $to=example.com|||||ghostery.com as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrmod We have tests that defines those cases as an invalid. Please, choose the behavior on this. Personally, I don't feel it's good to accept those typos.

  1) #parseFilters
       with not supported filters
         returns invalid network filters:

      AssertionError: expected [] to deeply equal [ { filter: '$domain=|a', …(2) } ]
      + expected - actual

      -[]
      +[
      +  {
      +    "filter": "$domain=|a"
      +    "filterType": 1
      +    "lineNumber": 0
      +  }
      +]
      
      at Context.<anonymous> (test/lists.test.ts:210:24)
      at process.processImmediate (node:internal/timers:491:21)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverting this to keep existing tests: 071550b (#4525)

packages/adblocker/test/parsing.test.ts Outdated Show resolved Hide resolved
@seia-soto seia-soto requested a review from chrmod December 25, 2024 04:53
@seia-soto
Copy link
Member Author

@chrmod I realized that it was improper to add a check in the Domains.prototype.parse since it was also used by cosmetics. Instead, I created a separate function called #normalizeNetworkEntities to do this. Also, new change includes:

  • Tests for #normalizeNetworkEntities and NetworkFilter.prototype.toString

@seia-soto
Copy link
Member Author

Tests are currently expected to fail: see #4525 (comment)

@seia-soto seia-soto marked this pull request as draft December 30, 2024 05:54
@seia-soto seia-soto changed the title refactor: integrate emptiness check to Domains.prototype.parse fix: check domains validity when parsing network filters Dec 31, 2024
@seia-soto seia-soto marked this pull request as ready for review December 31, 2024 10:38
@seia-soto
Copy link
Member Author

One more idea to this would be having a Domains.prototype.toString method and accept isNetworkFilter = false as a first argument.

@@ -11,6 +11,24 @@ import { toASCII } from '../punycode.js';
import { StaticDataView, sizeOfUint32Array, sizeOfUTF8 } from '../data-view.js';
import { binLookup, hasUnicode, HASH_INTERNAL_MULT } from '../utils.js';

export function testNetworkParts(parts: string[]): boolean {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is a "network part"? Can we make the naming more specific here as I find it hard to understand without more context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an option called isNetworkEntities and integrated everything inside of Domains.prototype.parse. The new shape looks like the following. Would like you like to change the shape?

export class Domains {
  public static parse(
    parts: string[],
    {
      isNetworkEntities = false,
      debug = false,
    }: { isNetworkEntities?: boolean | undefined; debug?: boolean | undefined } = {},
  ): Domains | undefined

return true;
}

export function normalizeNetworkPartsLiteral(parts: string) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some comment and/or make naming more explicit. It's hard to understand what "normalizing" means, I think it's too generic.

Copy link
Member

@chrmod chrmod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to move the splitting to Domain.parse and pass a desired separator as an argument?

The logic for the trailing | may apply to trailing ,.

Or alternatively, we could ignore those trailing characters as filters that include them (if they exist) may still be useful. The question is how fault tolerant we want to be. Eg. Should we drop filter: test$domain=example.com|example.org||

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Bug Fix 🐛 Increment patch version when merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants