← 返回首页
Cleartext storage of sensitive information in cookie — CodeQL query help documentation CodeQL docs
CodeQL documentation
CodeQL resources

Cleartext storage of sensitive information in 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

Click to see the query in the CodeQL repository

Sensitive information that is stored unencrypted is accessible to an attacker who gains access to the storage.

Recommendation

Ensure that sensitive information is always encrypted before being stored. It may be wise to encrypt information before it is put into a heap data structure (such as Java.util.Properties) that may be written to disk later. Objects that are serializable or marshallable should also always contain encrypted information unless you are certain that they are not ever going to be serialized.

In general, decrypt sensitive information only at the point where it is necessary for it to be used in cleartext.

Example

The following example shows two ways of storing user credentials in a cookie. In the ‘BAD’ case, the credentials are simply stored in cleartext. In the ‘GOOD’ case, the credentials are hashed before storing them.

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)); } }

References