API Design
Your API Key Is in Your Stack Trace
With one PHP setting off, every frame of an exception's trace keeps its arguments, and an SDK's authenticated request goes with them. Where keys leak, why production hides it, and how to stop it.
Nov 202610 min read
There’s a PHP setting most of us have never actually looked at, and it decides whether your API keys end up in your stack traces. It’s called zend.exception_ignore_args, and when it’s switched off, every frame of an exception’s trace keeps the arguments that were passed to it. So if one of those frames happened to be handed an authenticated HTTP request, the bearer token in its Authorization header is sitting right there in the trace, waiting for anything that prints it.
PHP’s own php.ini-production switches it on, and the comment above the line even tells you why, “to prohibit the output of sensitive information in stack traces”. That means on a properly configured production server, you’ll never see the problem. Everywhere else, it’s off. It’s PHP’s built-in default, it’s what php.ini-development sets, and it’s what you get in any container that never had a php.ini copied into it.
I found this in my own SDK framework, sdk-toolbox, and I want to tell you how, because the way it hid from me is exactly why nobody notices it.
The test that only failed in Docker
For a while, one test in sdk-toolbox failed on PHP 8.3, but only when the suite ran inside a stock php:8.3-cli Docker container. It passed on my machine, and it passed in CI. That’s the kind of failure that’s very easy to write off as an environment quirk, and for a while that’s exactly what I did, noting it down as pre-existing and moving on to something else.
When I finally dug into it, it turned out to have nothing to do with PHP 8.3 at all. My machine and the CI runners both load a production style ini, which hides trace arguments. The stock Docker image doesn’t load an ini at all, so it was running on PHP’s built-in defaults, with trace arguments kept. The test was checking that an API key never appears anywhere in an exception’s chain, and inside that container, it did.
Once I knew what I was looking for, I found it all the way along the send path. When a request failed in transit, the exception’s trace held the authenticated request, the HTTP client’s own exception, and on the OAuth path, the grant’s parameters, which can include a refresh token. I wrote a test to check every one of those paths, and it failed on nine of its ten cases.
What actually leaks, and where
So how much of this was my code, and how much of it is just PHP? To find out, I built the smallest SDK I could think of, a client that adds a bearer token in its own send() method and then hands the request to Guzzle. I pointed it at a host that doesn’t exist, and looked at where the token, and an API key sent in the query string, ended up.
| Where the secret ends up | Guzzle 7 | Guzzle 8 |
|---|---|---|
| The exception message, so every log line | query key, with any ini | clean |
Trace arguments, with exception_ignore_args off | both | both |
print_r() of the exception itself | bearer token | bearer token |
That’s three different leaks, and it’s worth taking them one at a time, because they don’t all have the same cause.
Let’s start with the message. Guzzle 7 ends a connection error with “for” followed by the full URL, query string and all, so your ?api_key=... lands right there in the exception message. That message then goes into every log line Laravel and Monolog write for the exception, and it doesn’t matter what your ini says. Guzzle 8 redacts the URL in its messages now, which is great, but Laravel 13 will happily accept either major version, and plenty of apps are still running on 7.
Then there are the trace arguments. Guzzle 8 also marked its own credential-bearing parameters with PHP 8.2’s #[\SensitiveParameter] attribute, so its own frames are clean. What it can’t do is anything about yours. Our SDK’s send() method received the authenticated request, so with trace arguments kept, that frame holds the token, whichever version of Guzzle you happen to be on.
And finally, the exception object itself. Guzzle’s exceptions keep hold of the request they failed on, so if you dump the exception, you’ll see the Authorization header. This one doesn’t care about the ini setting at all.
Where those traces actually go matters too. print_r() and var_dump() are the obvious ones, but error trackers read frames as well. Sentry’s PHP SDK, for one, turns the arguments of each frame into the variables it shows you beside that frame.
Fixing it in sdk-toolbox
The fix in sdk-toolbox 0.1.0 was to put #[\SensitiveParameter] on every parameter along the send path that could hold the authenticated request, a response to it, a pre-signed URL, or an OAuth grant’s parameters. That came to 28 files, closures included. A parameter marked this way shows up in a trace as a SensitiveParameterValue object, instead of the value itself.
The other two leaks had already been closed in earlier releases. An sdk-toolbox exception never holds the PSR-7 request, only a snapshot of it with the sensitive headers and query parameters masked. And the HTTP client’s exception doesn’t survive as getPrevious() either. It gets replaced with a RedactedClientException, which keeps the class, code, file and line, and masks any credentials in the message.
What that means for an SDK built on top of it is that the only thing you need to write is the attribute on your own constructor:
use JustSteveKing\SdkToolbox\Auth\BearerToken;use JustSteveKing\SdkToolbox\Connector;use Psr\Http\Client\ClientInterface;
final class Acme extends Connector{ public function __construct(#[\SensitiveParameter] string $token, ?ClientInterface $http = null) { parent::__construct( baseUri: 'https://api.acme.test/v2', auth: new BearerToken($token), http: $http, ); }}It might not look like much, but that attribute is doing a lot. The constructor is the first frame in the SDK that sees the token, and if anything throws while the connector is being built, that frame is going to be in the trace.
I ran the same experiment against it, on both versions of Guzzle, with trace arguments on and off, using a bearer token and a query key. The token didn’t show up anywhere, not in the message, not in the trace, not in the exception chain, and not in a dump of the connector. The Guzzle 7 query key comes through in the message as api_key=[redacted], URL-encoded. I also ran it against 0.0.9, the release before the fix, and it was clean on production settings and leaked through trace arguments with them off, which is exactly the failure that Docker container caught.
Fixing it by hand
If your SDK isn’t built on sdk-toolbox, the same three fixes still apply, and you don’t need a package for any of them. Here’s a client that takes any PSR-18 client, Guzzle included:
final class AcmeClient{ public function __construct( #[\SensitiveParameter] private string $token, private ClientInterface $http, ) {}
public function __debugInfo(): array { return ['token' => '[redacted]']; }
private function send(#[\SensitiveParameter] RequestInterface $request): array { $request = $request->withHeader('Authorization', "Bearer {$this->token}");
try { return json_decode((string) $this->http->sendRequest($request)->getBody(), true); } catch (ClientExceptionInterface $e) { throw new AcmeException(sprintf( 'Could not complete %s %s: %s', $request->getMethod(), $request->getUri()->getPath(), $e::class, )); } }}The #[\SensitiveParameter] attribute goes on every parameter that can hold a credential or the authenticated request, and I really do mean every one, all the way down the path, because a single frame you missed is all it takes. You’ll need PHP 8.2 for it, and it only covers trace arguments, nothing else.
The catch block is the part I think people skip. It throws our own exception with no previous, built from the method and the path, and never from the full URL. The client’s exception, with the request still inside it, goes no further than this method.
Then there’s __debugInfo(), which covers print_r() and var_dump() of the client itself. You need it because #[\SensitiveParameter] on a promoted constructor parameter hides the argument in a trace, but it does nothing for the property on the object. It isn’t a complete answer either. var_export(), serialize() and an (array) cast all ignore __debugInfo() and will still show you the token, so the real rule here is to not hand the client to anything that dumps objects.
I’d keep the getPath() in that message rather than the full URI, too. The moment an API takes its key in the query string, a message built from the full URL is just one more place for it to leak.
A test that would have caught it
This went unseen in sdk-toolbox because every environment I ran the tests in was hiding the problem from me. So the test needs to switch the setting off itself, rather than trusting whatever ini the machine it runs on happens to have:
beforeEach(function (): void { $this->ignoreArgs = ini_set('zend.exception_ignore_args', '0');});
afterEach(function (): void { ini_set('zend.exception_ignore_args', $this->ignoreArgs);});
it('never puts the token in a transport failure', function (): void { $token = implode('', ['sk-', 'sh0uld-n0t-l3ak']);
try { (new Acme($token, http: new FailingClient()))->send(new ListOrders()); $this->fail('The request should have failed.'); } catch (TransportException $e) { for ($x = $e; $x !== null; $x = $x->getPrevious()) { expect(print_r($x, true))->not->toContain($token) ->and(print_r($x->getTrace(), true))->not->toContain($token) ->and($x->getMessage())->not->toContain($token); } }});There are two details in there that come straight from getting this wrong the first time. The token is built at runtime, so that no frame of the test itself holds it as a literal argument and trips the check. And it walks the whole chain of previous exceptions, because that’s exactly where the HTTP client’s exception likes to hide. The version in sdk-toolbox also checks Basic auth credentials in their base64 form, and its CI runs the entire suite with trace arguments kept, not just this one test.
FailingClient is whatever PSR-18 client you have in your tests that throws a network exception. What we’re after is a request that fails in transit, because that’s the path where the authenticated request is most likely to be sitting in a frame somewhere.
What I’d do with this
Run that experiment against your own SDK, or the SDKs your app depends on. It’ll take you about five minutes: switch zend.exception_ignore_args off, point the client at a host that doesn’t exist, and search everything the exception gives you back for the key.
Then go and check your Docker images. If your Dockerfile starts from an official PHP image and never copies php.ini-production into place, your containers are running with trace arguments kept, and any error tracker that reads frame arguments is getting them. That’s one line in your Dockerfile, and it’s worth adding today, even if every SDK you use turns out to be clean.
Related
More in API Design
Laravel Rate Limiting Guide
Rate limiting a Laravel 13 API from both ends: switching it on, keying and layering limits, a 429 a client can act on, the IETF RateLimit headers, and a client that waits instead of hammering.
Nov 2026 · 13 min read
API DesignRebuilding Resend's PHP SDK
I rebuilt part of Resend's official PHP SDK on sdk-toolbox and ran both against the same failures. The rebuild handles what goes wrong better. The official one is still better to use.
Nov 2026 · 15 min read
API DesignYou Can't Deprecate a Field With a Header
RFC 9745's Deprecation header covers whole resources, so a field changing shape has no HTTP signal. Where that deprecation goes instead, and where the header is exactly right.
Oct 2026 · 7 min read
Same subject
In other formats
laravel-bastion
Stripe-inspired API authentication with environment isolation, granular scopes, and built-in security.
API Design
VideoBuild a CLI app with Laravel Zero - Todoist API Integration
🚀 Join me LIVE as we dive into the world of command-line interface (CLI) applications using Laravel Zero! In this session, we'll craft a sleek, functional CLI app designed to work seamlessly with the
API Design
TalkBuilding & Testing Robust APIs in Laravel
API Design