← 中文资源

简体中文版本

如何实现 DPoP 发送方约束令牌

用 DPoP 把访问令牌绑定到客户端持有的密钥,使被盗的 bearer 令牌无法被攻击者重放。

阅读英文原文

引言

Bearer 令牌像现金:谁持有谁就能花。若访问令牌通过日志、代理或被攻陷的客户端泄漏,攻击者可从任何地方使用它直到过期。DPoP(Demonstrating Proof-of-Possession,RFC 9449)通过把令牌绑定到合法客户端持有的密钥来改变这一点。客户端生成密钥对、每请求发送签名的证明,资源服务器验证证明与令牌所绑定的密钥匹配。于是被盗令牌没有私钥就无用。

Bearer 令牌的问题

常规访问令牌不携带“谁在出示它”的证明。任何持有方可重放它,这正是令牌泄漏危害巨大的原因:明文记录或拦截一次的令牌在过期前都是可用凭据。发送方约束通过要求出示方证明持有密钥来移除重放窗口。

DPoP 是发送方约束的一种,与 mTLS 绑定令牌并列。它有吸引力是因为无需客户端证书基础设施:客户端生成临时密钥对并以每请求签名证明持有。公钥嵌入证明中,令牌的确认声明把令牌绑定到它。

密钥与 DPoP 证明

客户端生成密钥对并保留私钥。对资源服务器的每个请求,它创建 DPoP 证明:用该私钥签名的 JWT,在 htm 与 htu 声明中携带 HTTP 方法与 URL,唯一 jti、签发时间 iat,公钥在 jwk 头中。证明放在 DPoP 头里与访问令牌并列。

证明是每请求的:每次调用创建新的,带全新 jti 与当前 iat,使捕获的证明也无法重放。htu 声明必须在规范化后与 URL 精确匹配,把证明固定到该端点;htm 必须与 HTTP 方法匹配,使 GET 的证明不能授权 POST。

在授权服务器把令牌绑定到密钥

令牌请求期间,客户端发送 DPoP 证明,授权服务器返回的令牌其确认声明(cnf)携带客户端公钥的 JWK 指纹。该指纹就是绑定:令牌仅在与哈希到同指纹的密钥的证明一起出示时有效。

响应中令牌类型变为 DPoP 而非 Bearer,令牌本身不再是 bearer 凭据。一些服务器还支持刷新令牌绑定,使被盗刷新令牌无密钥无法铸造新访问令牌。两者都绑定,否则刷新路径成为薄弱环节。

在资源服务器验证证明

资源服务器提取令牌,从其 cnf 声明读 jkt,并验证 DPoP 证明:对证明头中 jwk 的签名、htm 与 htu 对实际请求、iat 在小窗口内、jti 此前未见(阻重放)。然后检查证明公钥的指纹等于令牌的 jkt。任一失败则拒绝请求。

jti 重放检查需要共享状态或短接受窗口。以 TTL 等于允许时钟偏移加令牌生存期的缓存记录已见 jti 值,阻止捕获的证明在窗口内被复用。没有它,证明可被任何拦截它的人重放。

Nonce 挑战

资源服务器可要求客户端在证明中包含服务器提供的 nonce,经 DPoP-Nonce 头返回。客户端带着含 nonce 的证明重发请求。这进一步提高门槛:捕获的证明即使对否则会接受的服务器也无法重放,因为 nonce 会变。

优雅处理 nonce 握手。收到错误为 use_dpop_nonce 且带 DPoP-Nonce 头的 401 时,客户端应用含该 nonce 的证明重试一次。服务器应周期性轮换 nonce 并接受少量近期值,以容忍轮换期间在途的请求。

实际取舍

DPoP 防令牌重放,但不防同时拥有令牌与私钥的完全被攻陷客户端。其价值在于经客户端之外的渠道(如日志或代理)泄漏的令牌不能直接使用。与短令牌生存期和安全存储结合,真正降低风险。

实现有成本:每个客户端须每请求签名,每个资源服务器须验证,增加延迟并依赖证明格式。在令牌泄漏是现实风险处(如公共客户端与高价值 API)采用 DPoP,对可信网络内的内部服务权衡复杂度与威胁。

常见陷阱与小结

不要只验证签名而不查 htm、htu、iat 与 jti;针对别的请求的旧证明就是重放。不要宽松比较 URL,否则路径混淆破坏绑定。不要跳过指纹比较,那才是令牌与密钥的实际绑定。不要忘记也保护刷新令牌。也不要假定 DPoP 消除了短令牌生存期的需要。

DPoP 通过把令牌绑定到密钥并要求签名的每请求证明,使 bearer 令牌变成只有其 rightful 持有人能用的令牌。签发带 cnf 指纹的令牌,生成带 htm、htu、jti、iat 的证明,全部对照令牌绑定验证,并加入 nonce 挑战以提供更强重放保护。结果是被盗令牌不再是可用凭据。

代码示例

# bearer token: replay from anywhere
attacker steals token  ->  sends it  ->  accepted

# DPoP: token is bound to a key
attacker steals token  ->  has no private key
                       ->  cannot produce a valid proof
                       ->  rejected at the resource server
import { SignJWT, exportJWK, generateKeyPair } from 'jose';

const { privateKey, publicKey } = await generateKeyPair('ES256');
const jwk = await exportJWK(publicKey);

export async function dpopProof(method: string, url: string): Promise<string> {
  return new SignJWT({ htm: method, htu: url, jti: crypto.randomUUID() })
    .setProtectedHeader({ alg: 'ES256', typ: 'dpop+jwt', jwk }) // public key in header
    .setIssuedAt()
    .sign(privateKey);                                           // per-request signature
}

// Authorization: DPoP <access_token>
// DPoP: <proof>
// Access token issued with a DPoP binding (cnf.jkt = JWK thumbprint)
{
  "iss": "https://auth.example.com",
  "sub": "user_123",
  "aud": "https://api.example.com",
  "exp": 1698249740,
  "cnf": { "jkt": "0ZcOCORZNYy-DWpqq30jZyJGHTN0d2HglBV3uiguA4I" },
  "token_type": "DPoP"
}
export async function verifyDpop(token: string, proof: string, req: Request) {
  const { payload, header } = await jwtVerify(proof, async () => await importJwk(header.jwk));
  if (header.typ !== 'dpop+jwt') throw new Error('bad_typ');
  if (header.jwk?.kty !== 'EC') throw new Error('bad_key_type');
  if (payload.htm !== req.method) throw new Error('method_mismatch');
  if (normalise(payload.htu) !== normalise(req.url)) throw new Error('url_mismatch');
  if (Date.now() / 1000 - payload.iat > 300) throw new Error('proof_expired');
  if (await seenJti(payload.jti)) throw new Error('replay');         // shared cache
  const tokenJkt = decodeTokenCnf(token).jkt;
  if (thumbprint(header.jwk) !== tokenJkt) throw new Error('binding_mismatch');
  return true;
}
# nonce handshake
client -> request without nonce
server -> 401 WWW-Authenticate: DPoP error="use_dpop_nonce", DPoP-Nonce: "abc123"
client -> request with proof carrying nonce "abc123"
server -> 200
# purpose: a captured proof is not enough; the nonce must be fresh
# DPoP helps when the leak channel is not the client
[+] token logged / proxied / cached  -> useless without the key
[-] client fully compromised          -> attacker has the key too
# pair with: short lifetimes, per-request proofs, jti/nonce replay checks