Skip to content

Validate statistics visitor source - #923

Merged
giuscris merged 2 commits into
2.xfrom
fix/validate-visitor-source
Aug 12, 2026
Merged

Validate statistics visitor source#923
giuscris merged 2 commits into
2.xfrom
fix/validate-visitor-source

Conversation

@giuscris

Copy link
Copy Markdown
Member

This pull request refactors how visitor source detection is handled and improves the accuracy and security of source tracking in statistics. The main changes include introducing a new Visitor::getSource() method for determining the source of a visit, updating statistics tracking to use this method, and enhancing output escaping for source display in the statistics panel.

Visitor source detection and tracking:

  • Added a new static method Visitor::getSource(Request $request) in Visitor.php to centralize and improve logic for determining the visitor's source, returning an empty string for direct visits, null for invalid or same-host sources, and the source host otherwise.
  • Updated Statistics::trackVisit() to use Visitor::getSource() for source tracking, ensuring consistent and accurate detection of external referrers.

Security and display improvements:

  • Escaped the $source value in the statistics panel (statistics/index.php) before outputting, preventing potential XSS vulnerabilities and ensuring safe display of source names.

Dependency updates:

  • Added use Formwork\Utils\Uri to Visitor.php to support host extraction from referer URLs.

@giuscris
giuscris requested a lite review from Copilot August 12, 2026 09:56
@giuscris giuscris self-assigned this Aug 12, 2026
@giuscris giuscris added the bug Something isn't working label Aug 12, 2026
@giuscris
giuscris requested a lite review from Copilot and removed request for Copilot August 12, 2026 12:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This pull request centralizes visitor source detection into Visitor::getSource() and updates statistics tracking and panel rendering to use the new source logic, with additional output escaping to improve safety of source display.

Changes:

  • Added Visitor::getSource(Request $request): ?string to compute and validate the visit source host.
  • Updated Statistics::trackVisit() to use Visitor::getSource() for source tracking.
  • Escaped the source value in the statistics panel sources table output.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
panel/views/statistics/index.php Escapes source values before rendering them in the statistics UI.
formwork/src/Statistics/Statistics.php Switches source tracking to the new centralized visitor-source method.
formwork/src/Http/Utils/Visitor.php Introduces getSource() to extract/validate a referrer host as the visit source.

馃挕 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread formwork/src/Http/Utils/Visitor.php
Comment thread formwork/src/Statistics/Statistics.php Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (2)

formwork/src/Http/Utils/Visitor.php:99

  • Same as above for the request host: requiring FILTER_VALIDATE_DOMAIN means any IP-based Host header will make the method return null, dropping all non-direct sources. If the goal is to validate hosts, consider allowing IPs here too so source-vs-host comparisons still work for IP-hosted instances.
        if (
            $host === null || filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false
            || $source === $host // Source and host are lowercased by `Uri::host()`
        ) {
            return null;

formwork/src/Http/Utils/Visitor.php:80

  • getSource() currently rejects IP-address sources (and will also refuse to compare against an IP Host header), because it only allows FILTER_VALIDATE_DOMAIN. This can cause referrer sources to be dropped entirely when the site is accessed via an IP (common in staging/internal deployments), even though the referer host parsing succeeded.

This issue also appears on line 95 of the same file.

        if ($source === null || filter_var($source, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false) {
            return null;
        }

@giuscris
giuscris merged commit 5f8889d into 2.x Aug 12, 2026
2 checks passed
@giuscris
giuscris deleted the fix/validate-visitor-source branch August 12, 2026 14:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants