Skip to content

FIX Director::hostName() returning null for bare hostnames#11979

Open
erikfrerejean wants to merge 1 commit into
silverstripe:6.2from
erikfrerejean:pulls/6.2/fix-11081-hostname-bare-host
Open

FIX Director::hostName() returning null for bare hostnames#11979
erikfrerejean wants to merge 1 commit into
silverstripe:6.2from
erikfrerejean:pulls/6.2/fix-11081-hostname-bare-host

Conversation

@erikfrerejean

@erikfrerejean erikfrerejean commented Apr 28, 2026

Copy link
Copy Markdown

Description

Director::hostName() returns null when the resolved host is a bare hostname (no port, no protocol) — for example, the RFC 7230-compliant Host header value example.com. PHP's parse_url() interprets such a string as a path and returns null for PHP_URL_HOST, so the wrapping ?: null propagates that null upwards and consumers receive null instead of the expected hostname.

The fix delegates to protocolAndHost() so parse_url() always receives a scheme-prefixed URL, which it can parse reliably regardless of whether the host carries a port or not.

The bug was reported (#11081) and reproduces verbatim against the current 6.2 branch.

Manual testing steps

  1. Configure a Silverstripe project so that Director::host() resolves via $_SERVER['HTTP_HOST'] (i.e. unset alternate_base_url / no trusted-proxy override).
  2. Send a request where the Host header is a bare hostname without a port, e.g. Host: example.com.
  3. Before this PR: Director::hostName() returns null. After this PR: it returns "example.com".

The included unit test (DirectorTest::testHostNameWithoutPort) reproduces this without needing a live server.

Issues

Pull request checklist

  • The target branch is correct
  • All commits are relevant to the purpose of the PR
  • The commit messages follow our commit message guidelines
  • The PR follows our contribution guidelines
  • Code changes follow our coding conventions
  • This change is covered with tests
  • Any relevant User Help/Developer documentation is updated; for impactful changes, information is added to the changelog for the intended release
  • CI is green

When host() returns a hostname without a port and without a protocol
(e.g. the RFC 7230 compliant Host header value "example.com"),
parse_url() interprets it as a path and returns null for PHP_URL_HOST.

Delegate to protocolAndHost() so parse_url() always receives a
scheme-prefixed string, and explicitly narrow the result so the
documented string|null return type holds.

Fixes silverstripe#11081
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