Cookie 中敏感信息的明文存储¶
ID: java/cleartext-storage-in-cookie
Kind: problem
Security severity: 5.0
Severity: error
Precision: high
Tags:
- security
- external/cwe/cwe-315
Query suites:
- java-code-scanning.qls
- java-security-extended.qls
- java-security-and-quality.qls
未加密存储的敏感信息可供获得存储访问权限的攻击者访问。
建议¶
确保敏感信息在存储前始终加密。最好在将信息放入堆数据结构(例如 Java.util.Properties
)之前对其进行加密,该数据结构可能稍后写入磁盘。可序列化或可编组的对象也应始终包含加密信息,除非您确定它们永远不会被序列化。
通常,仅在需要以明文形式使用敏感信息时对其进行解密。
示例¶
以下示例展示了在 Cookie 中存储用户凭据的两种方法。在“BAD”情况下,凭据只是以明文形式存储。在“GOOD”情况下,凭据在存储之前已进行哈希处理。
public static void main(String[] args) {
{
String data;
PasswordAuthentication credentials =
new PasswordAuthentication("user", "BP@ssw0rd".toCharArray());
data = credentials.getUserName() + ":" + new String(credentials.getPassword());
// BAD: store data in a cookie in cleartext form
response.addCookie(new Cookie("auth", data));
}
{
String data;
PasswordAuthentication credentials =
new PasswordAuthentication("user", "GP@ssw0rd".toCharArray());
String salt = "ThisIsMySalt";
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
messageDigest.reset();
String credentialsToHash =
credentials.getUserName() + ":" + credentials.getPassword();
byte[] hashedCredsAsBytes =
messageDigest.digest((salt+credentialsToHash).getBytes("UTF-8"));
data = bytesToString(hashedCredsAsBytes);
// GOOD: store data in a cookie in encrypted form
response.addCookie(new Cookie("auth", data));
}
}
参考¶
SEI CERT Oracle Java 编码标准:SER03-J. 不要序列化未加密的敏感数据。
M. Dowd、J. McDonald 和 J. Schuhm 著,软件安全评估艺术,第 1 版,第 2 章 - “加密的常见漏洞”,第 43 页。Addison Wesley,2006 年。
M. Howard 和 D. LeBlanc 著,编写安全代码,第 2 版,第 9 章 - “保护机密数据”,第 299 页。Microsoft,2002 年。
常见弱点枚举:CWE-315。