Skip to content

Validate and compile the user agent filter pattern once - #40

Merged
loevgaard merged 1 commit into
masterfrom
fix/18-user-agent-regex
Sep 7, 2026
Merged

Validate and compile the user agent filter pattern once#40
loevgaard merged 1 commit into
masterfrom
fix/18-user-agent-regex

Conversation

@loevgaard

Copy link
Copy Markdown
Member

Fixes #18

Stacked on #39.

Problem

FilterConfiguredUserAgentsSubscriber built '#' . implode('|', $userAgents) . '#' on every event and compared the result of preg_match() with === 1.

An invalid fragment, or one containing a #, makes preg_match() emit a warning and return false, which is not 1, so the filter silently stops filtering. foo#bar is the nastiest case: it closes the delimiter and turns bar into modifiers. Nothing failed, nothing was logged, bots simply started being tracked again.

Matching was also case sensitive, while the same crawler shows up in different casings.

Change

  • The pattern is compiled once in the constructor. An invalid pattern now throws \InvalidArgumentException at container build time instead of failing open at runtime.
  • Each fragment is validated in Configuration, so the error surfaces as a normal configuration error naming the offending fragment and reminding you to escape #.
  • The i modifier is added, and the option is documented as case insensitive.
  • The no-fragments case is decided once in the constructor rather than on every event.

Tests

Six unit tests (match, no match, case insensitive match, no user agent, no fragments configured, and both invalid-pattern constructor throws) plus two extension tests asserting the configuration is rejected.

@codecov

codecov Bot commented Sep 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.99%. Comparing base (985e748) to head (1b06004).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master      #40      +/-   ##
============================================
+ Coverage     62.40%   67.99%   +5.59%     
- Complexity      129      134       +5     
============================================
  Files            30       30              
  Lines           375      403      +28     
============================================
+ Hits            234      274      +40     
+ Misses          141      129      -12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

An invalid fragment, or one containing an unescaped delimiter, made
preg_match() return false instead of 1, so the filter silently stopped
filtering. Compile the pattern once in the constructor, validate each
fragment at compile time, and match case insensitively.

Fixes #18
@loevgaard
loevgaard force-pushed the fix/18-user-agent-regex branch from 2809b78 to 1b06004 Compare September 7, 2026 12:18
@loevgaard
loevgaard changed the base branch from fix/20-fbc-cookie-format to master September 7, 2026 12:18
@loevgaard
loevgaard merged commit 31338c4 into master Sep 7, 2026
45 checks passed
@loevgaard
loevgaard deleted the fix/18-user-agent-regex branch September 7, 2026 12:19
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.

User agent filter builds an unescaped, unvalidated regex from configuration

1 participant