JWT实现可能会受到不同的攻击,其中之一是alg:none
攻击(在此处查看更多详细信息(。
我在pom.xml文件中使用了spring-security-jwt
依赖项,并且无法确定此实现是否处理alg:none
攻击。
春季安全 JWT 实现是否缓解了此攻击?
如果您使用的是 spring-security-oauth/spring-security-jwt,那么是的,此攻击已缓解。根据您共享的链接,缓解此攻击的一种方法是在选择算法时将标头为无效或不依赖于alg
标头的 JWT 令牌"alg":"none"
。
在 spring-security-jwt 文件的源代码中,decode
方法中的 JwtHelper 在选择算法时不依赖于 alg
标头。
public static Jwt decode(String token) {
int firstPeriod = token.indexOf('.');
int lastPeriod = token.lastIndexOf('.');
if (firstPeriod <= 0 || lastPeriod <= firstPeriod) {
throw new IllegalArgumentException("JWT must have 3 tokens");
}
CharBuffer buffer = CharBuffer.wrap(token, 0, firstPeriod);
// TODO: Use a Reader which supports CharBuffer
JwtHeader header = JwtHeaderHelper.create(buffer.toString());
buffer.limit(lastPeriod).position(firstPeriod + 1);
byte[] claims = b64UrlDecode(buffer);
boolean emptyCrypto = lastPeriod == token.length() - 1;
byte[] crypto;
if (emptyCrypto) {
if (!"none".equals(header.parameters.alg)) {
throw new IllegalArgumentException(
"Signed or encrypted token must have non-empty crypto segment");
}
crypto = new byte[0];
}
else {
buffer.limit(token.length()).position(lastPeriod + 1);
crypto = b64UrlDecode(buffer);
}
return new JwtImpl(header, claims, crypto);
}
spring-security-jwt
中没有漏洞的文档或汇编,但您可以查看spring-security-jwt
下的问题部分,并报告您认为需要修补的任何漏洞。