Skip to content

fix!: stop trusting client-supplied IP headers and GET _method, harden health endpoints - #11

Merged
muhammetsafak merged 4 commits into
mainfrom
fix/router-ip-trust-method-health
Nov 24, 2026
Merged

muhammetsafak merged 4 commits into
mainfrom
fix/router-ip-trust-method-health

Conversation

@muhammetsafak

Copy link
Copy Markdown
Contributor

What

  • Client IP is REMOTE_ADDR by default. X-Forwarded-For is read only when REMOTE_ADDR is in the new trusted_proxies option (IPs and CIDR ranges, IPv4/IPv6); the chain is resolved right to left, skipping trusted proxies. Client-IP is never read. Entries that are not IP addresses yield no client address; IPv4-mapped IPv6 is normalised; an invalid trusted_proxies entry throws InvalidArgumentException.
  • _method override works only on a real POST and only from the POST parameters (PSR-7 parsed body, otherwise $_POST); $_REQUEST and the query string are no longer read.
  • Health: numeric check names no longer raise TypeError; invalid UTF-8 in a check detail no longer produces an empty 503 body; an empty checks map is encoded as {}.
  • port() still depends on the Host header; unchanged in code, documented.
  • Docs, README and CHANGELOG.md (Unreleased, breaking items and upgrade notes) updated. Two stale PHPStan baseline entries for RequestContext removed.

Breaking

Behind a reverse proxy, set trusted_proxies; otherwise ip() groups see the proxy's address. Clients that sent _method via GET or the query string must use a POST body.

Testing

composer validate --strict, php-cs-fixer fix --dry-run --diff, phpstan analyse, phpunit (207 tests) and wiki-sync.sh --check docs pass locally. New regression tests fail on the previous code.

…n health endpoints

- Client IP is REMOTE_ADDR by default. X-Forwarded-For is read only when
  REMOTE_ADDR is listed in the new trusted_proxies option (IPs and CIDR
  ranges), resolved right to left skipping trusted proxies. Client-IP is
  never read, invalid entries are rejected and IPv4-mapped IPv6 is handled.
- The _method override requires a real POST and reads only the POST
  parameters (PSR-7 parsed body, otherwise $_POST).
- HealthRegistry accepts numeric check names, HealthEndpoints keeps the
  body for invalid UTF-8 details and encodes an empty checks map as {}.
- Document the new behaviour and update the changelog.
…e PSR-7 request data

- A non-array trusted_proxies option now throws instead of silently
  trusting nothing, and a missing key no longer warns in subclasses that
  redefine $configs.
- ip() allow-list entries are compared with the client address in
  canonical form, so IPv4-mapped and upper-case or long IPv6 entries match.
- With a PSR-7 server request, RequestContext reads server parameters
  from getServerParams() when present and the method override only from
  the parsed body (arrays and objects), never from $_POST.
- Clarify the upgrade notes (open ip() group behind a same-host proxy),
  the right-to-left chain wording and the exception list.
Server parameters of some runtimes (ReactPHP) carry no HTTP_* entries, so
a trusted proxy's X-Forwarded-For header was never seen and every visitor
appeared as the proxy address. With a PSR-7 server request the header lines
are now read through getHeader() (joined in order), falling back to
HTTP_X_FORWARDED_FOR.

Add a regression matrix over superglobal, FPM-style, ReactPHP-style,
empty-bag and header-less PSR-7 requests for the client address and the
_method override, narrow the trusted_proxies example to proxy addresses and
list the new public API in the changelog.
Read the PSR-7 X-Forwarded-For header only when RequestContext derives the
server parameters from the request itself; explicitly injected server
parameters are used as given.
@muhammetsafak
muhammetsafak merged commit d866369 into main Nov 24, 2026
8 checks passed
@muhammetsafak
muhammetsafak deleted the fix/router-ip-trust-method-health branch November 24, 2026 07:13
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.

1 participant