Nothing Worth Stealing, Part 2: The key does not belong in the PHP process
For anyone running TYPO3 or another PHP application with FrankenPHP on Kubernetes and securing the database and cache with client certificates. The certificate protects the connection. Its key, however, sits in the same process as every line of PHP an attacker might manage to run.
Here is how to get it out of there without rebuilding the application.
Part 2 of 4 of the series “Nothing Worth Stealing”. How the database and cache manage without a password is shown in Part 1: Certificate instead of password.
01 — The starting point
In a cluster with mTLS on every connection, each pod gets its own certificate. The application uses it in both directions. Inbound, it identifies itself to the reverse proxy. Outbound, it uses it to log in to MariaDB and Valkey, as described in The whole network on pod certificates. Since the database and cache no longer ask for a password, the certificate is the login. Whoever holds the key is the application.
With FrankenPHP, that is a problem. Caddy and PHP run in one process, as one user, with the same files. Caddy needs the key to speak TLS. So PHP can read it. A flaw in an extension, an unsafe upload, an injected template line, and file_get_contents('/run/tls/app.key') returns the key. open_basedir does not help here: the path has to be readable, or Caddy will not start. More on that in Part 3: What really protects secrets in a container.
The danger is not that the attacker can do something inside the pod. The danger is that they can take the key with them. With a copy, they log in as the application from anywhere, for as long as the certificate is valid. And nobody in the pod notices that anything is missing.
02 — The idea: a gatekeeper in the pod
The key belongs in a process that runs no foreign code. So the pod gets a second container, the TLS gateway. Only it mounts the certificate. The PHP container no longer has /run/tls.
Pod
┌──────────────────────────────────────────────────┐
│ │
│ tls-gateway (holds certificate + key) │
│ ├─ :443 mTLS in ──► PROXY v2 ──► 127.0.0.1:8444 FrankenPHP
│ ├─ 127.0.0.1:3306 MySQL plain ──► TLS ──► MariaDB
│ ├─ 127.0.0.1:6379 plain ─────────► TLS ──► Valkey
│ └─ 127.0.0.1:8445 plain ─────────► TLS ──► reverse proxy (purge)
│ │
│ app (FrankenPHP: Caddy + PHP, no /run/tls) │
└──────────────────────────────────────────────────┘
The application now only talks to 127.0.0.1, in plain text. The gateway speaks TLS to the outside with the pod certificate. For TYPO3, only host and port change in the configuration, not the code.
The gateway is a small Go program, just under 700 lines without tests. It knows three kinds of route, given as a JSON list in an environment variable:
[
{"kind": "inbound", "listen": "0.0.0.0:443", "upstream": "127.0.0.1:8444", "upstreamTLS": true},
{"kind": "mysql", "listen": "127.0.0.1:3306", "upstream": "db:3306"},
{"kind": "tunnel", "listen": "127.0.0.1:6379", "upstream": "cache:6379"}
]
A plain-text listener that is not on loopback is rejected at start-up. That way, a configuration typo cannot open plain text to the network.
03 — Inbound: mTLS, then a PROXY header
The reverse proxy sets up mTLS to the pod as before. Now the gateway accepts the connection and checks the client certificate against the CA. Without a certificate, or with one from a different CA, the connection ends in the handshake.
It then hands the connection on to FrankenPHP on 127.0.0.1. Before that, it writes a PROXY header (version 2) with the client's address. Without it, the application would see every request coming from 127.0.0.1. The IP checks for reverse proxies and all the logs would then be wrong.
FrankenPHP still speaks TLS on loopback, with tls internal. That sounds redundant, but there is a reason. The application keeps seeing an HTTPS request, with HTTPS=on, the original host and HTTP/2. The key for it comes from Caddy's local CA. Nobody trusts that CA, and outside the pod the key is worthless. PHP may read it. It opens nothing.
The Caddy configuration for this is short:
{
servers 127.0.0.1:{$SERVER_HTTPS_PORT:443} {
listener_wrappers {
proxy_protocol {
allow 127.0.0.1/32
}
tls
}
}
auto_https disable_redirects
skip_install_trust
}
:{$SERVER_HTTPS_PORT} {
bind 127.0.0.1
tls internal
}
Caddy only accepts the PROXY header from 127.0.0.1. Anyone sending a header from the network is rejected.
04 — Outbound to MySQL: TLS halfway through the protocol
For Valkey and HTTPS, a simple tunnel is enough: plain text in, TLS out. MySQL does not work that way. The protocol starts in plain text. The server sends its greeting and capabilities. Only then does the client ask for TLS with an SSLRequest, and the connection switches midway.
The gateway takes over this switch:
- It passes the server's greeting on to the client, but without the
CLIENT_SSLflag. The client therefore answers in plain text, over loopback. - From the first 32 bytes of the client's answer, it builds the
SSLRequestto the server itself, with the flag set. The TLS handshake with the pod certificate follows. - The full client answer follows over TLS.
- Because of the extra
SSLRequest, the server's packet count runs one ahead of the client's. Until the login ends with OK or ERR, the gateway rewrites the sequence numbers in both directions. After that, every command starts at 0 again, and the gateway just copies bytes.
If the server offers no TLS, the gateway aborts. Otherwise the login would cross the network in plain text.
On the database, the application's account requires a certificate with a fixed subject and issuer, and no longer a password. How that works, and which traps MariaDB sets along the way, is covered in Part 1 of this series: Certificate instead of password.
In the lab, connecting plus one query through the gateway took 2.6 ms on average, TLS handshake included. Valkey through the tunnel, connecting plus PING, took 2.0 ms. The application keeps its connections open per worker anyway. So this only applies when a connection is set up.
05 — Certificate rotation without restarts
Pod certificates are short-lived. After about two thirds of their lifetime, the kubelet writes a new certificate into the volume. The gateway therefore holds no path and no loaded certificate in memory. It reads certificate, key and CA afresh on every handshake. From the next connection on, the new certificate applies.
With a lifetime of 24 hours, the first rotation comes after 16 hours at the earliest. I did not want to wait for that. The lifetime can be capped per volume (maxExpirationSeconds; the API server's minimum is one hour). With one-hour certificates, the rotation came after two thirds of the hour, in every pod, without a restart and without errors.
What mattered was the time after that. Even once the old certificates had expired, new connections to the database and cache got through. So the gateway really was presenting the new certificate.
One pitfall when observing this: Kubernetes cleans up PodCertificateRequest objects shortly after issuance. Anyone counting rotations by them never sees one. The signer's logs are more reliable, as it records every issuance with the pod name. And every pod restart resets the clock.
06 — What PHP can still do
Honestly: quite a lot. Code in the PHP process can still talk to the database and cache through the gateway, as the application, for as long as the pod lives. The gateway is not meant to do more than that. The application needs those connections, after all.
What changes:
- The key can no longer be taken away. There is no file PHP could read. A login from outside the pod is therefore no longer possible.
- An attack stays tied to the pod. Once the pod is gone, the access is gone. The next rollout cleans it up.
- An attack becomes visible. Everything runs through the gateway and therefore through the logs and metrics you already have.
Then there is the worker without inbound traffic. A queue consumer only needs the outbound routes. Its gateway gets no inbound route, and its PHP container gets no certificate either.
07 — What it costs
- One more container per pod. The gateway runs as a native sidecar, that is, as an init container with
restartPolicy: Always. Its startup probe holds the app container back until every listener accepts connections. It needs little: the requests are 10 millicores and 16 MiB. - Short-lived jobs keep their certificate. Init containers, migrations during a deploy and CronJobs run briefly, execute fixed code and handle no requests from outside. They still mount the certificate directly. That is a deliberate trade-off, not an overlooked gap.
- Anything that reaches into the app container has to move. In my case it was a hook that, after each deploy, ran a
curlwith--cacert /run/tls/ca.crtin the app container viakubectl exec. After the switch it could no longer find the file. It now uses the gateway's tunnel. Before switching, look for every script that expects certificate paths in the app container. - Restarts hit both containers. If the gateway dies, all of the application's connections drop until it is back. There is no warm-up: the gateway only opens a connection when the application opens one. If the upstream cannot yet be resolved at start-up, only that one attempt fails.
08 — Three Caddy pitfalls
Writing the gateway was the smaller part. Getting Caddy to work on loopback with a PROXY header took more time.
- The
serverskey must match the bind address.servers :8444 { … }does not apply when the site listens on127.0.0.1:8444withbind 127.0.0.1. Caddy then finds no matching server, and thelistener_wrappershave no effect, without any error message. Onlyservers 127.0.0.1:{$SERVER_HTTPS_PORT}matches. - Automatic HTTPS opens a port 80. Even with
tls internal, Caddy starts its HTTP-to-HTTPS redirect server, here on127.0.0.1:80. It is only reachable from inside and does no harm, but it is not needed either.auto_https disable_redirectsswitches it off. The adapted JSON then contains"disable_redirects": true, and the build checks for it. - Nested placeholder defaults do not exist. Caddy does not evaluate
{$A:{$B:443}}. Each variable gets exactly one default.
A fourth point is not a pitfall but a recommendation. Keep the new mode alongside the old one and switch between them with an environment variable. In my set-up, TLS_MODE selects which snippets Caddy imports. In the previous mode, the adapted configuration is byte-identical to before. That let the new image roll out to every site before a single one used the gateway.
Frequently asked questions
Why not simply set file permissions so that only Caddy can read the key?+
With FrankenPHP, Caddy and PHP are the same process with the same user. Whatever Caddy may read, PHP may read too. Only a second process in its own container separates the two.
Isn't this just a service mesh?+
At its core, it is the same principle: a sidecar holds the identity. The difference is scope. No control plane, no CRDs, no injection. Just the three connections the application really needs, plus the MySQL protocol, which a plain TLS proxy cannot handle.
Does the detour cost noticeable time?+
In the lab, connecting plus one query took 2.6 ms, TLS included. Because the application keeps its connections open per worker, this only applies when a connection is set up, not per request.
What happens when the certificate is renewed?+
Nothing visible. The gateway reads certificate and key afresh on every handshake. The next connection uses the new certificate, and open connections carry on.
Can't an attacker in the pod simply use the gateway?+
Yes, for as long as the pod lives. The gateway promises no more than that. It stops the key from leaving the pod. Together with short certificate lifetimes and regular rollouts, that limits how long and from where an attacker can use the identity.
Conclusion
A certificate is a good login, but only if its key sits where no foreign code can reach it. With FrankenPHP, that is not the app container. A sidecar with a few hundred lines of Go is enough to take it out. The switch in the MySQL protocol is the only tricky part. The application notices nothing, apart from 127.0.0.1 in its configuration.
Anyone running code in the PHP process can still talk to the database. No gateway can prevent that. What they can no longer do is take the key away and log in later from somewhere else. What else is left in the container after that is the subject of Part 3 of this series: What really protects secrets in a container.
The series “Nothing Worth Stealing”
Four parts on how credentials and keys in a TYPO3 cluster end up where injected PHP code can no longer take them away.
- Part 1: Certificate instead of password
- Part 2: The key does not belong in the PHP process (this post)
- Part 3: What really protects secrets in a container
- Part 4: Egress by hostname instead of by IP
The series builds on the TYPO3 series “TYPO3 on Kubernetes under IT-Grundschutz”, above all on its parts 5 (mTLS on every connection), 6 (network and tenant isolation) and 7 (secrets without Git).
I take the key out of your PHP process. Without rebuilding the application.
A TLS gateway as a sidecar: mTLS inbound, MySQL, Valkey and purge outbound, certificate rotation without restarts. Your application only changes host and port.
Platform operations rather than advice on paper: if you wish, I run your TYPO3 with you for the long term and provide the evidence for your audit.
About the author

Kai Ole Hartwig
Programming since 2002 – self-taught, set up my own business with KO-Web in 2012. Over 100 projects, with a focus on security, performance, automation and quality. Today freelance: DevSecOps consulting, training and software development.
Newsletter
The week, in one email.
The new advisories and posts, once a week and briefly put in context. You will get a confirmation email first, and opens and clicks are only counted anonymously.
Almost there – please confirm your subscription via the link in the email I have just sent you.
