Defending AI Agents
עברית Join waitlist
Menu

On-ramp · Lesson 11

OAuth's own threat model: what RFC 9700 requires, and why an agent needs all of it

An agent connects to whatever MCP server its user names, and each server points the agent to its own authorization server (Module 3, lesson 10). So the agent is an OAuth client that talks to many authorization servers, and an attacker can run one of them simply by publishing a server. That is the setting in which OAuth's best-known protocol attacks work. RFC 9700 (Lodderstedt et al., 2025), the IETF's security best current practice for OAuth 2.0, writes the attacker down and derives its requirements from it. This lesson reads it as a threat model, and then adds the two operations an agent's tokens depend on: exchange and key rotation.

The attackers, made explicit

RFC 9700, section 3, names five attackers. The first two come from the formal analysis by Fett, Küsters and Schmitz (2016). The other three are there because real attacks used them.

Attacker Can In an agent deployment
A1, web attacker run any number of endpoints, including its own client, authorization server and resource server; send the user's browser to any URL anyone who publishes an MCP server or a page the agent will open
A2, network attacker read, change and block traffic that cryptography does not protect an untrusted network; TLS (lesson 7) answers it
A3 read the authorization response codes in redirect URLs that reach logs, history or an open redirector
A4 read the authorization request the same channels, one step earlier
A5 obtain an access token issued to someone else every resource server the agent calls: a malicious or compromised one receives the token by design

A5 is the row that matters most for agents. A browser user talks to a handful of known resource servers. An agent hands a token to every server it calls, and some of those servers are written by strangers. Any requirement that defends against A5 is one an agent deployment cannot drop.

Each requirement, against its attacker

Requirement (RFC 9700) Defends against Mechanism
Redirect URIs compared by exact string match (section 4.1.3; a port may vary only for a native app's localhost) A3, through a redirect the attacker can also satisfy a pattern such as https://*.client.example/* is a parser of URLs, and lesson 10 says what that costs; equality has no differential set
PKCE for every public client (section 2.1.1) A3 and A4 redeeming a stolen code; code injection the code is redeemable only with a verifier that never left the client (RFC 7636)
The issuer in the authorization response (RFC 9207), or a distinct redirect URI per issuer (section 4.4.2) mix-up, below the client learns which server actually answered
Audience-restricted access tokens (sections 2.3 and 4.10.2) A5 replaying a token at another resource server the audience must be the URL the client actually sends requests to, or a counterfeit server's token works at the real one
Sender-constrained access tokens (sections 2.2.1 and 4.10.1; mutual TLS or DPoP) A5 and any other leak the token is useless without the client's private key (lesson 4)
Refresh tokens of public clients sender-constrained or rotated (sections 2.2.2 and 4.14) a stolen long-lived refresh token see below
No implicit grant (SHOULD NOT); no password grant (MUST NOT) (sections 2.1.2 and 2.4) tokens in URLs; a client that sees the user's password tokens travel only in back-channel responses, and the password only to the authorization server

Rotation as detection. With rotation, every refresh returns a new refresh token and invalidates the old one, and the server remembers which token replaced which. If a stolen refresh token is used, either the thief or the legitimate client will later present a token that has already been replaced. The server cannot tell which of the two is legitimate, so it revokes the whole chain (section 4.14.2). The legitimate client pays with a fresh authorization. In exchange, theft becomes visible the first time both parties refresh, instead of lasting as long as the token does. It is a detector with a known false-positive cost, which makes it an expected-cost decision of the kind in Module 2, lesson 6.

Mix-up, as a mechanism

The precondition is a client that uses one redirect URI for several authorization servers and remembers, per browser session, which one the user picked. If the user picks the attacker's server, that server can pass the browser on to the honest server, and the honest server's code arrives at the shared redirect URI. The client still believes it is talking to the attacker's server, so it sends the code to the attacker's token endpoint. No signature fails and no TLS check fails. The client has misattributed a response, and it is the same error as believing a forwarded header (lesson 6). The defence gives the response its provenance: the authorization server puts its issuer identifier in the response (RFC 9207), and the client compares it with the issuer it started with before doing anything else. An agent that discovers authorization servers at run time from resource metadata meets the precondition by default. Checking the issuer is therefore not optional for it.

Token exchange: narrowing as an invariant

When the gateway trades the agent's token for one meant for the CRM (RFC 8693; lesson 9), every exchange should satisfy

\text{scope}_{\text{out}} \subseteq \text{scope}_{\text{in}} \cap \text{policy}, \quad \text{aud}_{\text{out}} = \text{one resource server}, \quad \text{exp}_{\text{out}} \le \text{exp}_{\text{in}}, \quad \text{act}_{\text{out}} = \text{act}_{\text{in}} \text{ extended by one hop.}

The RFC defines the request and the response. Whether the output is narrower than the input is left to the authorization server's policy. An exchange that can widen any of the four turns the gateway into Hardy's confused deputy (Module 2, lesson 3), with a valid signature on the result. Because the property is a universally quantified invariant over pairs (input, output), it can be checked on every exchange the server makes. Module 12 builds that check and tests it.

Keys change: rotation without an outage

A resource server verifies signatures with the issuer's public keys. It finds them as a JSON Web Key Set (RFC 7517) at the jwks_uri given in the issuer's metadata (RFC 8414), and selects one by the token's kid. Two rules follow from lesson 8. The key set comes from the configured issuer's metadata, never from a URL in the token: RFC 8725, section 3.10, warns that following jku or x5u invites server-side request forgery. And the algorithm comes from the key (a JWK's alg member), not from the token's header.

Rotation is then arithmetic. Let verifiers cache the key set for up to C, tokens live up to L, and clocks differ by up to s:

  • publish the new key at least C before the first token is signed with it, so that every cache holds it in time;
  • keep the old key published for at least L + s after the last token is signed with it.

With C = 1 hour, L = 5 minutes and s = 30 seconds, the new key appears at least an hour ahead, and the old one goes 5.5 minutes after the switch. Get the first rule wrong and valid tokens fail for up to C; that is an outage, not a breach.

Rotation after a compromise runs the other way. The key is withdrawn at once, and tokens signed with it fail. Verifiers must refetch the key set when they see an unknown kid, or they keep trusting their cache until it expires. Refetches must be rate-limited, though, or tokens with random kid values turn every request into a fetch from the issuer: a denial of service that needs no valid key. The two failure modes are held apart by a refetch budget, which is a choice between failing open and failing closed (Module 2, lesson 6).

What is open

RFC 9700 assumes a person, a browser and a few servers. An agent changes all three. One consent may cover a long task, tokens go to many servers, some of them unknown in advance, and authority passes along chains of agents. No RFC yet says how a person's consent should map onto the scopes of a task that is planned after the consent is given. Nor does one say how long an act chain may grow, or whether a resource server should accept a token exchanged through n intermediaries, and on whose policy. Module 3, lesson 10, and Module 12 build the parts that can be built today. The rest is standards work in progress.

Check yourself

  1. Which of A1 to A5 does a malicious MCP server, called by your agent with a valid token, belong to? Which two requirements in the table limit what it can do with that token, and which of them still protects you if it persuaded the client to put the real CRM's URL in the token's audience?
  2. An authorization server allows the redirect pattern https://agent.example/callback*. Using lesson 10's terms, what is the check, what is the target, and why is exact matching the only design with an empty differential set?
  3. Rotate a key with C = 24 hours, L = 1 hour and s = 2 minutes. When must the new key be published, when may the old one go, and what changes if the old key leaked?
Answers
  1. A5: it obtains an access token by design (and as its publisher it is also A1). Audience restriction and sender-constrained tokens limit what it can do with the token. If it got the real CRM's URL into the audience, audience restriction no longer helps, and only sender-constraining (DPoP or mutual TLS) still protects you: without the client's private key the token is useless at the CRM.
  2. The check is the authorization server's pattern match, P_C, a parser of URLs; the target, P_T, is the browser and the client, which resolve the redirect and decide where the code goes. A pattern accepts strings such as https://agent.example/callback/../open-redirect?to=... that P_T normalises to another destination. Exact matching parses nothing: \varphi is byte equality with a registered string, so the check and the target read the same thing and D is empty.
  3. Publish the new key at least 24 hours before the first token signed with it; keep the old key published for at least L + s = 62 minutes after the last token it signed. If the old key leaked, withdraw it at once and accept that tokens signed in the last hour fail; and caches keep trusting the leaked key for up to 24 hours unless verifiers are told to refetch, so the cache lifetime, not L, becomes the exposure window.