Credential Injection Patterns for AI Agents: Part 4, Eliminating Plaintext A four-part blog series on credential injection for AI agents concludes by moving the final AES-GCM unwrap of upstream tokens from the controller to the gateway, so the controller holds only ciphertext and never a plaintext credential. Under the revised design, the gateway receives the ciphertext from the controller and the plaintext data encryption key (DEK) from the broker, and only the gateway can decrypt; the controller is reduced to a store and state machine tracking elicitation state, row metadata and expiry, with no decrypt path. The author argues the gateway already terminated the agent's MCP/API/LLM connection and saw plaintext on every live request, so the change adds no new exposure, and points to attaching the credential inside a trusted execution environment (TEE) as the next step so the gateway never sees it. This is the last part in the Credential Injection Patterns for AI Agents series https://agentgateway.dev/blog/2026-07-27-credential-injection-ai-agent-egress-cb4a/ . The series reviewed patterns for securing the credentials used by AI agents with the premise that: you can’t trust what an AI agent will do with credentials you give to it. They WILL eventually leak. Littering API keys/OAuth tokens/client secrets all over your AI agents creates a governance nightmare, with no way to expire/revoke/audit/control. If we adopted a credential brokering pattern, we can alleviate this pain but introduce a new potential issue. What happens to the token vault if the process that reads it is compromised? Links to the previous posts: The responses for each of these posts have been absolutely incredible, with a number of them going viral https://www.linkedin.com/posts/ceposta you-should-not-give-ai-agents-any-share-7518426181592113152-rEot/ Both part 2.5 https://agentgateway.dev/blog/2026-10-20-credential-brokering-mode-3-at-scale/ and part 3 https://agentgateway.dev/blog/2026-11-02-protecting-token-vault-openbao-credential-injection/ reactions got some version of “you didn’t remove the risk you just moved it to the gateway” on LinkedIn https://lnkd.in/p/ggyMGWn5 . In the part 3 thread https://www.linkedin.com/feed/update/urn:li:activity:7523131357552762882/ someone pointed out that a compromised gateway can just log credentials after each authorized use, it only takes longer. That’s all true, and it’s the dwell/trickle blast radius I mentioned at the end of part 3. But the gateway was always going to see those tokens. So we didn’t actually “move the risk to the gateway”. It was already there. The gateway terminates the agent’s MCP/API/LLM connection and puts the upstream token on the wire the agent never holds one, which was the whole point of part 1 https://agentgateway.dev/blog/2026-07-27-credential-injection-ai-agent-egress-cb4a/ , so it sees plaintext on every live request. So does every TLS-intercepting egress proxy you already run Zscaler, Netskope, Squid with bumping, etc , for every script, CI job, and SaaS integration, not just agents. So what did brokering add? It put every user’s upstream tokens in one logical store. And then we discussed how to prevent the component with access to the store from total melt down if compromised. But we ended up with some “control plane” component still seeing plaintext credentials at some point in time. And this motivates part 4: Why should we have another component get exposed to plaintext beside the gateway when doing the injection? In this post I want to move the final unwrap to the gateway, so the controller only ever holds ciphertext and the gateway is exactly as exposed as it already was. Then I’ll look at where this could go next to make the gateway unwrap safer including attaching the credential inside a trusted execution environment TEE so the gateway never sees it. Let’s get into it In the 2.5 post, the controller sends the subject token, a freshness proof, and the row’s wrapped DEK to the broker, gets the plaintext DEK back, opens the inner AES-GCM layer, and hands the upstream token to the gateway. It does all of that because it’s the STS, and the RFC 8693 https://www.rfc-editor.org/rfc/rfc8693 token exchange contract is subject token in, upstream token out. That’s a perfectly reasonable API, but it means the controller passes the plaintext token back to the gateway. The controller needs to find the right row, but does it really need to open it? What if we want ONLY the gateway to see the plaintext? If we change the API, the gateway gets the ciphertext from the controller, the plaintext DEK from the broker, and only it can decrypt. The gateway was going to hold the plaintext token a few milliseconds later anyway, so the only thing that changes on the gateway is where the AES-GCM decrypt happens. The controller becomes just a store and a state machine elicitation state, row metadata, expiry with no decrypt path and no plaintext credential. The controller loses encrypt permission too, which it can only do if we adopt the “refresher” component from 2.5. That refresher component became the OAuth client that completes the code exchange and wraps the rows it writes, so with the read path moved to the gateway there’s nothing left for the controller to call KMS or Transit for. The broker only accepts the gateway’s SVID, so a compromised controller can’t ask for an unwrap at all, no matter how many subject tokens it has harvested. In parts 2 and 3 the gateway-signed freshness proof did that job. The broker still verifies the subject token against the IdP JWKS on every unwrap, which limits the gateway to users who are actually active. The controller only releases ciphertext to the gateway for the row’s owner, so nobody else can bulk-pull the store. The grant-shaped versions work the same way with a different party calling Decrypt . On KMS, the broker mints a grant https://docs.aws.amazon.com/kms/latest/developerguide/grants.html with the gateway’s role as grantee instead of the controller’s. In Mode 3b on OpenBao, the broker mints the batch token with token bound cidrs set to the gateway’s pod CIDR, and the gateway calls transit/decrypt itself. Part 3’s vault-login design is the smallest change of all, since the gateway already logs in as the user and holds the OpenBao token, so it would call Transit directly instead of handing the token to the controller. That takes the controller out, and the gateway ends up where it is in the other designs, since a live request already opened all of that user’s rows. Only the access token. In the store from part 2, each row’s payload is the whole token response access token, refresh token, id token under one DEK, which is fine when the controller opens it and only hands the access token on. If the gateway opened that same row it would get the refresh token too, so the refresh token has to live under its own DEK in the refresher’s store from 2.5 that the gateway never gets. When an access token expires, the refresher writes a new wrapped access-token row and the gateway unwraps it like any other, so neither the gateway nor the controller ever sees a refresh token. “So now the most exposed component on the network does all of the decrypting. How is that better?” The gateway already held the plaintext for every in-use token, in every mode, because it’s the thing putting the token on the wire. What this change does is stop a second component from holding it too. An attacker who sits on the gateway harvests in-use tokens at the rate of use whether or not the gateway does the AES-GCM open, and that was true in part 2. An attacker who sits on the controller, which in 2.5 harvested exactly the same tokens, now gets ciphertext. For tokens in flight, a compromised brokering gateway sees exactly what a compromised intercepting proxy would. The one thing it can do that a proxy can’t is ask for credentials. With alice’s live subject token it can call the broker for her Slack and Jira rows even if her agents only touched GitHub that day, because the gateway is the one naming the resource a confused deputy . That works for as long as her subject token is valid typically about an hour after her last request , and it’s capped at the rows of users who are active during the compromise. Nothing works offline, dormant users are out of reach, and every pull shows up in the broker’s decision log. Part 3’s comparison table says the same for both OpenBao designs, so moving the unwrap doesn’t change it. The trickle doesn’t stop when you evict the attacker, either. The gateway collects at the rate of use while the attacker sits on it, but whatever it logged stays usable until the upstream expires it, so the lifetime of the upstream access tokens is part of the gateway’s blast radius, and that varies a lot by provider some issue tokens that don’t expire unless you turn expiration on . Here’s what each single compromise gets you after the change. The one thing a ciphertext-only controller can still do is let an attacker steal ciphertext now and hope to get the KEK later root on the OpenBao node, a key policy mistake during a migration, etc . On OpenBao the answer is to rewrap https://openbao.org/api-docs/secret/transit/ on a schedule and then raise min decryption version on the Transit key, so ciphertext stolen before the rewrap goes stale. Rewrap on its own doesn’t help, since the old key version still decrypts whatever the attacker took. Everything above leaves the gateway with the same two problems every earlier design had. The plaintext token is in the gateway, since it’s the thing putting the token on the wire, so whatever a compromised gateway logs/captures keeps working after you evict it. And the gateway decides which credential to fetch, so with a live subject token it can ask for an active user’s other rows. An agent-signed proof takes that choice away from the gateway, and a TEE takes the plaintext off the gateway host. To take that choice away from the gateway, the agent has to sign a proof bound to what it’s trying to reach, which the gateway can carry to the broker but can’t make on its own. The IETF WIMSE working group has two drafts that do this, a Workload Proof Token and an HTTP Message Signatures profile. The agent holds a Workload Identity Token WIT from SPIRE https://spiffe.io/docs/latest/spire-about/ or equivalent, with its public key in cnf . With the Workload Proof Token https://datatracker.ietf.org/doc/draft-ietf-wimse-wpt/ WPT , it signs a short-lived JWT with that key on every request, with the request’s target URI as the aud . { "aud": "https://agw.internal/mcp/github", "exp": 1793260830, "jti": "7f3a9c1e-2b8d-4c51-9e0a-6d4f1b2a8c33", "wth": "