Skip to content
Putting technology to work.
Insights to guide decisions and action.

Search articles

Passing host:port to Guzzle's base_uri sends requests to another host

Table of contents · 7 items

In PHP code that calls external APIs, are you passing a path built from user input to a Guzzle client with base_uri set? For example, if a value like localhost:18082 goes into $code of $client->get("{$code}/detail"), the request is sent not to the base_uri host but to port 18082 on localhost.

On November 19, Guzzle documented this behavior in the 7.x documentation (quickstart.md) and added that path segments built from user input should be encoded with rawurlencode(). In 8.6.0, released on November 22, URIs starting with a host and port are deprecated, and the project has announced they will be rejected in 9.0. Meanwhile, 7.17.0 in the 7.x line, which Laravel 12 uses, neither changes the behavior nor gives a warning.

In this article, we read the documentation changes and CHANGELOG in the Guzzle repository and checked which server requests reach with 7.16.1, 7.17.0, 8.6.0, and Laravel's Http client.

Results in the test environment

We started PHP's built-in server on 127.0.0.1:18081 (the API we intend to call, A) and on 127.0.0.1:18082 (another server assumed to be internal, B). Both simply return the path of the request they receive. With a Guzzle client whose base_uri was set to http://127.0.0.1:18081/api/, we requested four URIs (PHP 8.3.6, curl 8.5.0).

guzzle 7.17.0
  get('users/1') -> A(18081) /api/users/1
  get('localhost:18082/secret') -> B(18082) /secret
  get('./localhost:18082/secret') -> A(18081) /api/localhost:18082/secret
  get('localhost%3A18082/secret') -> A(18081) /api/localhost%3A18082/secret
guzzle 8.6.0
    [deprecated] Since guzzlehttp/guzzle 8.6: Passing a URI that starts with a host and port, such as "example.com:8080", is deprecated and will be rejected in 9.0. ...
  get('localhost:18082/secret') -> B(18082) /secret
illuminate/http v12.74.0 + guzzle 7.17.0
  Http::baseUrl(...)->get('localhost:18082/secret') -> A(18081) /api/localhost:18082/secret

Diagram of the results of requesting localhost:18082/secret with Guzzle's base_uri set to the API server (port 18081). Guzzle 7.16.1, 7.17.0, and 8.6.0 all sent the request to the other server (port 18082), and only 8.6.0 gave a deprecation warning. Adding ./ at the beginning or encoding with rawurlencode() made the request reach port 18081. Laravel's Http::baseUrl() reached port 18081

The results show three things.

  • With 7.16.1, 7.17.0, and 8.6.0 alike, localhost:18082/secret reached B. 8.6.0 gives a deprecation warning but still sends the request. The 7.x line does not even give a warning
  • Adding ./ at the beginning or encoding with rawurlencode() made the request reach A. In both cases, localhost:18082 is treated as part of the path rather than as a host and port
  • baseUrl() in Laravel's Http client reached A. This is because Laravel joins baseUrl and the path as strings before passing them to Guzzle (ltrim(rtrim($this->baseUrl, '/').'/'.ltrim($url, '/'), '/') in PendingRequest)

Why requests reach another host

Guzzle parses a string URI with Uri of guzzlehttp/psr7 and then combines it with base_uri (UriResolver::resolve()). Uri uses PHP's parse_url() internally and reads localhost:18082/secret, which has no scheme, as host localhost, port 18082, and path /secret. In the test environment as well, the host of new Uri('localhost:18082/secret') was localhost and the port was 18082. When combined, a URI that has a host replaces the host of base_uri.

The table in Guzzle's documentation also says that when base_uri is http://foo.com/foo/ and the URI is baz.com:81/bar, the result is http://baz.com:81/bar. Rather than fixing this as a bug, the project documents it and deprecates it in 8.x. However, if you use a client with base_uri set as something that "only sends to this API," joining user input into the path can lead to requests to unintended hosts (SSRF). The November documentation addition and the deprecation in 8.6.0 appear to be meant to reduce this misreading (editorial interpretation).

Note that Guzzle's documentation describes how it combines URIs with base_uri as "the rules of RFC 3986, section 5.2," but reading them as a host and port does not follow RFC 3986. In RFC 3986, a string like baz.com that starts with a letter and contains . can also be treated as a scheme name. Section 4.2 also says that a segment containing : cannot be placed at the start of a relative path, and that when you do place one there, you prefix it with ./, as in ./this:that. Read according to RFC 3986, baz.com:81/bar is a URI whose scheme is baz.com, and it has no host or port. Because Guzzle reads it the same way as parse_url(), the host gets replaced. In the results above, adding ./ at the beginning made the request reach the base_uri host, which corresponds to this form in section 4.2. The upgrade guide (UPGRADING.md) on Guzzle's 9.0 development branch says URIs of this form will be rejected with InvalidArgumentException.

You are affected when all of the following conditions apply.

  • You use a Guzzle client directly with base_uri set (as the results above show, baseUrl() in Laravel's Http client is not affected. Passing it to Guzzle with withOptions(['base_uri' => ...]) is likely affected, but we have not tested it)
  • User input or a database value comes at the start of the requested URI (as in "{$code}/detail", where everything up to the first separator is input)
  • That value can contain : and digits

When input comes in the middle ($code of "items/{$code}"), the start is items/, so it is not read as a host and port.

How to Fix

The documentation recommends encoding path segments built from user input with rawurlencode().

// 当たる書き方: 入力が先頭に来る
$client->get("{$code}/detail");

// 直し方1: 入力の部分をエンコードする(ドキュメントの勧め)
$client->get(rawurlencode($code) . '/detail');

// 直し方2: 先頭を ./ にする
$client->get("./{$code}/detail");

However, rawurlencode() also encodes /. If you want the input to include / (when accepting hierarchical paths), encode each segment separately or validate the format of the input in advance. With either fix, it is safer to validate the format of accepted values (alphanumeric only, for example).

The 8.6.0 warning message advises you to "start with a scheme or //." Since this form is scheduled to be rejected in 9.0, you also need to find such code when moving to 8.x.

Other changes in the 7.x line in November that cause exceptions

From 7.16.2 (November 17) to 7.17.0 (November 22), the following changes went into the 7.x line (from the CHANGELOG; we did not test them in the test environment). Requests that used to be sent may now stop with RequestException or similar, so check with tests after updating.

  • Reject request header values and methods that contain line breaks (CR or LF) or NUL (7.16.2)
  • Reject hosts enclosed in [ and ] unless they are IP addresses (7.16.3, GHSA-hpvq-6rrx-pc47)
  • In the stream handler, do not include the part of the URI from # onward in the request (7.16.3, GHSA-gqx6-m769-3x35)
  • Reject proxy credentials that contain encoded control characters and proxy URLs that older libcurl versions misread (7.16.5, GHSA-q8h8-pjjq-cq2h)
  • Reject header names that are not HTTP tokens and custom URI objects whose string form does not match their contents (7.17.0)

The three advisories listed in the CHANGELOG were published in the Guzzle repository on November 17 (GHSA-gqx6-m769-3x35, low, and GHSA-hpvq-6rrx-pc47, medium) and November 20 (GHSA-q8h8-pjjq-cq2h, medium). As of November 30, they are listed neither in the GitHub Advisory Database nor in the Packagist advisory API, so we expect that composer audit does not warn about them yet (we did not check with composer audit). Around the same time, a host misreading advisory for guzzlehttp/psr7 (GHSA-jwvc-hj95-82hh, fixed in 2.13.4, November 17) and an advisory for the 8.x line concerning the use of URIs from other PSR-7 implementations (GHSA-2g6w-c7gx-fxj8, fixed in 8.5.5, November 21) were also published in the respective repositories. Neither is listed in the databases.

November fixes to the Symfony components used internally by Laravel and EC-CUBE were covered in our article on Symfony's 23 November fixes.

Steps to check (editorial suggestion)

  1. Check the version. Run composer show guzzlehttp/guzzle guzzlehttp/psr7. According to Packagist records, illuminate/http in Laravel 12 (12.74.0) requires guzzlehttp/guzzle with ^7.8.2, so it stays on 7.x. Laravel 13 (13.44.1) uses ^7.8.2 || ^8.0, and a deprecation warning appears if 8.x is installed
  2. Find the places that use base_uri.
grep -rn "base_uri" app/ src/ --include=*.php
  1. Among the client calls you find, look for ones where a variable comes at the start of the URI. These are forms like ->get("{$ and ->request('GET', $. API SDKs sometimes use Guzzle internally, so also look through packages in vendor that use base_uri
  2. Test with values containing :. Put localhost:1/x into the input and confirm with a mock handler (MockHandler) that the request target remains the API host
  3. Upgrade to 7.17.0 and confirm that no exceptions occur. Use tests to check that you are not sending requests that fall under "Other changes that cause exceptions" above

Points to watch when upgrading dependencies with Composer are summarized in our article on Composer 2.11.3.

Scope of verification

On November 28, 2026, we reviewed the guzzle/guzzle repository (the CHANGELOG for 7.16.1 to 7.17.0 and 8.6.0, the docs/quickstart.md changes 5f2eb41e and 256e0592, and the src/Handler/HostValidator.php change 7cd949d2), PendingRequest.php and composer.json in Laravel's illuminate/http 12.74.0, the Packagist advisory API, and github/advisory-database (document research). On November 30, we rechecked the advisories (API) in the guzzle/guzzle and guzzle/psr7 repositories, whether they are listed in GitHub's advisory database, the newer releases on Packagist, docs/quick-start.md in 8.6.0, UPGRADING.md on the 9.0 branch, and RFC 3986. We confirmed where requests are sent by installing guzzle 7.16.1, 7.17.0 and 8.6.0 and illuminate/http 12.74.0 on PHP 8.3.6 and curl 8.5.0 and testing against two servers in a test environment (hands-on testing). We have not reproduced the requests that are rejected starting in 7.16.2 to 7.17.0, and we have not checked the output of composer audit or Laravel's withOptions(['base_uri' => ...]).

For reviews of business systems that integrate with external APIs, or for updating PHP and Laravel dependencies, please contact us through consultation on development, AI, and automation.

References

Share this articleXFacebook
Kakeru Suzuki

Fascinated by the possibilities of technology, has had a deep interest in programming and digital art since student days

Turn this article's theme into your company's next step

Concrete steps forward for your organization.

We organize your desired architecture, legacy systems, and operational requirements to formulate your next steps toward execution.

  • Desired architecture
  • Integration with existing environments
  • Operational requirements
Consult on development & operations initiatives

You can consult with us from the initial conceptual stage. Details from this article will be carried over to the inquiry form.

Receive the latest articles by email