[在常规网络应用中实施授权代码流时,可以将状态参数存储在cookie中。请参见here和[此处](https://auth0.com/docs/protocols/oauth2/mitigate-csrf-attacks)
为什么将状态(aka随机数)存储在cookie中是可以接受的解决方案?例如,如果我们在nodejs中执行此操作,则代码将如下所示。
app.use('/login', (_req, res) => {
state = randomString(32)
const authorizationEndpointUrl = new URL(`${activeConfig.authorization.domain}/authorize`);
authorizationEndpointUrl.search = new URLSearchParams({
audience: activeConfig.authorization.audience,
response_type: 'code',
redirect_uri: 'http://localhost:8443/callback',
client_id: activeConfig.authorization.clientId,
scope: activeConfig.authorization.scope,
state,
}).toString();
res.cookie('state', state, { httpOnly: true }); // cookie set here
res.redirect(authorizationEndpointUrl.toString());
});
app.use('/callback', req => {
// check that url state matches req.cookie.state???
})
但是,如果这样做,阻止了攻击者仅在响应URL和cookie中设置相同的“伪”状态?我在这里想念什么?
用于跟踪状态的Cookie不应是可伪造的。客户端应至少对其进行签名,以便客户端本身无法生成它。届时,如果攻击者恰好在请求生成后,返回合法响应之前恰好可以访问设备,则只能重播此类cookie,但不能伪造来自任意设备的无提示授权响应。