Skip to content

Fix regex filter parsing when pattern contains $ end-anchor#608

Open
0b0d3 wants to merge 2 commits intobrave:masterfrom
0b0d3:fix/regex-end-anchor-parsing
Open

Fix regex filter parsing when pattern contains $ end-anchor#608
0b0d3 wants to merge 2 commits intobrave:masterfrom
0b0d3:fix/regex-end-anchor-parsing

Conversation

@0b0d3
Copy link
Copy Markdown

@0b0d3 0b0d3 commented Mar 16, 2026

Summary

Fixes #257

  • Regex filters like /pattern$/ use $ as a regex end-of-string anchor, but the parser always treated the rightmost $ as the options separator
  • This caused filters like /\.json$/ to fail with UnrecognisedOption, and filters like /^https?:\/\/[a-z]{8,15}\.top\/[a-z]{4,}\.json$/$xhr,3p,match-case to lose the $ anchor from their pattern
  • The fix tries each $ from right to left, but only falls back to the next candidate when the pattern to the left looks like a regex (delimited by /). Non-regex filters still propagate option parse errors immediately

Changes

  • src/filters/abstract_network.rs: Replace single find_char_reverse with a loop that tries $ candidates right-to-left, with regex-awareness to avoid false fallbacks
  • tests/unit/filters/network.rs: Added 3 test cases covering regex with $ anchor + options, regex with $ anchor alone, and regex without $ anchor
  • tests/unit/blocker.rs: Updated easylist filter count (4 more filters now correctly parsed)
  • tests/unit/engine.rs: Updated serialization hashes to reflect the new parsing behavior

Test plan

  • All 262 existing tests pass (including with css-validation feature)
  • New test: regex filter with $ end-anchor and options parses correctly
  • New test: regex filter with $ end-anchor and no options parses correctly
  • New test: regex filter without $ anchor and options still works
  • Non-regex filters with invalid options still return proper errors
  • Real-world filter lists (easylist, easyprivacy, ublock-unbreak) parse with correct counts

🤖 Generated with Claude Code

Regex filters like `/pattern$/` use `$` as a regex end-of-string
anchor, but the parser always treated the last `$` as the options
separator. This caused filters like `/\.json$/` to fail with
UnrecognisedOption, and filters like `/regex$/$xhr,match-case` to
lose the `$` anchor from their pattern.

The fix tries each `$` from right to left, but only falls back to
the next candidate when the pattern to the left looks like a regex
(delimited by `/`). Non-regex filters still propagate option parse
errors immediately.

Fixes brave#257

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ImpulseB23
Copy link
Copy Markdown

approach looks good, this is basically what i described in my comment above. logic is clean and the tests cover the key cases.

one small thing: the pattern_before.ends_with("$/") branch in the looks_like_regex check might be unreachable. when you're at a given $, pattern_before is everything to its left, so it can't end with $/ unless there's a consecutive $$ in the pattern. not a bug, just potentially dead code. might be worth dropping it or adding a test that hits it if you want to keep it.

otherwise lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support regex rules with an end-anchor ($)

2 participants