Package · cast

Redirects and security

Cast follows redirects itself, not through cURL: that is how it decides what may be passed on to the new address and what may not.

Redirects

By default redirects are followed, up to 10 hops.

php
use Flytachi\Winter\Cast\Client\Redirects;

$client->withRedirects(Redirects::follow(max: 5));
$client->withRedirects(Redirects::none());        // return the 3xx as it is

Method and body

Status The new request
303 See Other GET without a body (except HEAD)
301, 302 after a POST GET without a body — as browsers do
301, 302 after other methods the same method and body
307, 308 the same method and the same body

A stream body that cannot be rewound cannot be sent again: such a 307/308 is returned as a response, without following it.

The target address

Location is resolved against the response’s address per RFC 3986: from the page /v1/dir/file, a redirect to other leads to /v1/dir/other. This rule differs from joining with the client’s base URL, where the path is appended.

What does not go to another host

If the scheme, host or port changes, these are removed from the request:

  • Authorization, Proxy-Authorization, Cookie, X-Api-Key;
  • the key header set through AuthMiddleware::header() — for example, X-Token.

A typical case: an API answers 302 with a link to a CDN or a third-party store. Without dropping the headers, your key would go to a third party.

Downgrading to http

A redirect from https:// to http:// is not followed: the 3xx response is returned as it is. If you really need such a redirect, use Redirects::follow(allowDowngrade: true).

Count and final address

php
$response->info()->redirects;      // number of hops
$response->info()->effectiveUri;   // the final address

More than max hops gives a RequestException “Too many redirects”. Response headers are those of the final response only; intermediate 3xx headers are not mixed in.

What else is protected

TLS. Certificate verification is always on. It can be turned off only explicitly, through withCurlOptions([CURLOPT_SSL_VERIFYPEER => false]), and only for development.

http and https only. An address with any other scheme (file://, ftp://, gopher://) is not sent — RequestException. At the cURL level, too, only these two protocols are allowed.

Header injection. \r, \n and NUL in a header name or value are refused. A value taken from user input cannot add a header of its own to the request.

Reserved cURL options. withCurlOptions() cannot override the address, method, headers, body, callbacks or redirects — the things the other guarantees rely on.

Logs. The client’s logger writes the method, the address without the query, the status, the time and the number of attempts — no headers and no bodies. Exception messages also leave out the query and user:password@.

Dumps. var_dump() of a client or a request shows Authorization, Proxy-Authorization, Cookie, X-Api-Key and headers marked as secret as ***.

Response size. The body kept in memory is capped at 10 MiB — see Configuring the client.