Skip to content

CWE-312: Cleartext Storage of Sensitive Information occurs when an application stores sensitive information like Personally Identifiable Information (PII) in cleartext. This storage can be in various locations such as databases, configuration files, or logs. Storing sensitive data without proper encryption exposes it to various risks, making it a critical security flaw to address. This article discusses the risks associated with PII exposure due to CWE-312 and provides coding examples in Java, JavaScript, and Python to demonstrate remediation techniques.

  1. Data Theft: Unauthorized access to storage systems can lead to direct theft of PII.

  2. Identity Theft: Exposed PII can be used to impersonate individuals.

  3. Financial Fraud: Sensitive data like credit card details or bank account information can be used for fraudulent transactions.

  1. Reputational Damage: Incidents of data exposure can lead to loss of customer trust and damage to the company’s reputation.

  2. Legal and Regulatory Penalties: Failure to protect data can result in fines and sanctions under laws like GDPR, HIPAA, or CCPA.

  3. Remediation Costs: The cost of addressing a data breach, including incident response and increased security measures, can be substantial.

To mitigate CWE-312, organizations should ensure sensitive data is never stored in cleartext. Techniques include omitting unnecessary sensitive data, masking data to hide true values, and encrypting data to protect its integrity and confidentiality.

Omit sensitive data when it’s not necessary for the application’s functionality.

Java
public class User {
private String username;
// Omit storing sensitive PII such as social security numbers unless absolutely necessary
}
Javascript
function createUser(username) {
// Only store essential information; omit sensitive PII like address or phone number
return { username };
}
Python
class User:
def __init__(self, username):
self.username = username
# Sensitive PII such as date of birth is omitted from storage

When storing data that might identify individuals, mask parts of the data.

Java
public String maskEmail(String email) {
int index = email.indexOf('@');
String localPart = email.substring(0, index);
return localPart.replaceAll(".", "*") + email.substring(index);
}
Javascript
function maskEmail(email) {
let [local, domain] = email.split('@');
local = local.replace(/./g, '*');
return `${local}@${domain}`;
}
Python
def mask_email(email):
local, domain = email.split('@')
masked_local = '*' * len(local)
return f'{masked_local}@{domain}'

Encrypt sensitive data before storage to ensure it is protected from unauthorized access.

Java
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
public class EncryptionUtility {
// secretKey is supplied by the caller, sourced from your KMS or secret store
public static String encrypt(String data, SecretKey secretKey) throws Exception {
byte[] iv = new byte[12];
new SecureRandom().nextBytes(iv);
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new GCMParameterSpec(128, iv));
byte[] encryptedData = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
byte[] payload = ByteBuffer.allocate(iv.length + encryptedData.length).put(iv).put(encryptedData).array();
return java.util.Base64.getEncoder().encodeToString(payload);
}
}
Javascript
const crypto = require('crypto');
// key: 32-byte Buffer loaded from your KMS or secret store
function encrypt(data, key) {
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
const encrypted = Buffer.concat([cipher.update(data, 'utf8'), cipher.final()]);
return iv.toString('hex') + ':' + encrypted.toString('hex');
}
Python
from cryptography.fernet import Fernet
# key: Fernet key loaded from your secret store, never generated per call
def encrypt(data, key):
cipher = Fernet(key)
encrypted_data = cipher.encrypt(data.encode())
return encrypted_data.decode()

Implementing these remediation strategies is crucial for protecting PII from exposure through cleartext storage (CWE-312). Properly handling sensitive information not only prevents data breaches but also ensures compliance with various data protection regulations, thereby maintaining trust and safeguarding against potential financial and reputational harm.