SV.SESSION.FIXATION.COOKIE

Cookie 应该能够有效抵御会话固定攻击

当使用受污染的值设置 JSESSIONID Cookie 时,Klocwork 会报告 SV.SESSION.FIXATION.COOKIE 缺陷。

漏洞与风险

通过在 Cookie 中使用受污染数据,攻击者可以将会话标识符设置为已知值,从而允许攻击者与受害者共享会话。随后攻击者无需授权便可访问敏感信息。

缓解与预防

避免在 JSESSIONID Cookie 中使用受污染数据。

漏洞代码示例

复制
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.*;
import java.io.IOException;
 
public class Test {
 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String value = req.getParameter("value");
        Cookie cookie = new Cookie("jsessionid", value);  // SV.SESSION.FIXATION.COOKIE
    }
}

Klocwork 在第 9 行报告了 SV.SESSION.FIXATION.COOKIE 缺陷,因为在 Cookie 中使用来自请求的受污染数据设置会话 ID。

修正代码示例

复制
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.*;
import java.io.IOException;
 
public class Test {
 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        SecureRandom secureRandom = SecureRandom.getInstance("NativePRNG");
        long value = secureRandom.nextLong();
        Cookie cookie = new Cookie("jsessionid", value);
    }
}

Klocwork 不再报告 SV.SESSION.FIXATION.COOKIE 缺陷,因为 JSESSIONID Cookie 设置为随机值。

相关检查器

安全培训

应用程序安全培训材料由 Secure Code Warrior 提供。