我们是否应该始终忽略 JWT header 中的 alg 参数?

Should we always ignore the alg parameter in the JWT header?

this article 中,作者描述了一个场景,其中黑客利用以下事实构建有效的 JWT 令牌:为了验证签名,验证者首先必须查看 header。如果我们总是使用带有 public 个私钥对的 RSA,是否完全避免了这种黑客攻击发生的可能性?

引述如下:

Unfortunately, an attacker can abuse this. If a server is expecting a token signed with RSA, but actually receives a token signed with HMAC, it will think the public key is actually an HMAC secret key.

我假设此语句意味着如果 header 中的 alg 键表示令牌正在使用 HMAC,检查令牌的代码将切换到 HMAC。因此,如果我们始终使用 RSA 而从不使用 HMAC,因此从不检查 header,我们只是使用我们的 public 密钥验证令牌,如果验证失败,我们拒绝访问,是否解决了这个问题?

这种方法会引入任何其他安全问题吗?

是的,只要您知道 Algo 的期望并且您使用的是最新更新的库,您就应该很好。如果您可以访问该站点 https://jwt.io,您会发现他们也强调了此漏洞,并且对于受影响的库,他们提到了要使用的最低版本,该版本修复了问题,即 Algo 验证未通过-通过了。

    verify(string token, string algorithm, string verificationKey)

这是我的理解。