Kai Ole Hartwig
12 min read
Low
By

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:

  1. It passes the server's greeting on to the client, but without the CLIENT_SSL flag. The client therefore answers in plain text, over loopback.
  2. From the first 32 bytes of the client's answer, it builds the SSLRequest to the server itself, with the flag set. The TLS handshake with the pod certificate follows.
  3. The full client answer follows over TLS.
  4. 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:

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

08 — Three Caddy pitfalls

Writing the gateway was the smaller part. Getting Caddy to work on loopback with a PROXY header took more time.

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.

  1. Part 1: Certificate instead of password
  2. Part 2: The key does not belong in the PHP process (this post)
  3. Part 3: What really protects secrets in a container
  4. 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.

Book a call →

About the author

Photo of Kai Ole Hartwig.

Kai Ole Hartwig

Freelance DevSecOps consultant · OnlyOle Consulting

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.

Language