CWE-212: Improper Removal of Sensitive Information Before Storage or Transfer
Learn about CWE-212 (Improper Removal of Sensitive Information Before Storage or Transfer), its security impact, exploitation methods, and prevention guidelines.
What is Improper Removal of Sensitive Information Before Storage or Transfer?
• Overview: Improper Removal of Sensitive Information Before Storage or Transfer (CWE-212) occurs when a software product fails to adequately cleanse or scrub sensitive information from resources like documents, messages, or databases before they are stored, transferred, or shared with unauthorized parties.
• Exploitation Methods:
- Attackers can exploit this vulnerability by intercepting or accessing resources that still contain sensitive data, such as comments, metadata, or internal network addresses.
- Common attack patterns and techniques include analyzing document metadata, intercepting network traffic, or accessing exposed databases to extract sensitive information.
• Security Impact:
- Direct consequences of successful exploitation include unauthorized access to sensitive information such as personal data, business secrets, or internal system details.
- Potential cascading effects include data breaches, identity theft, or further exploitation of internal systems due to leaked information.
- Business impact may involve financial loss, legal liabilities, reputational damage, and loss of customer trust.
• Prevention Guidelines:
- Specific code-level fixes include implementing data scrubbing functions that remove or obscure sensitive information before storage or transfer.
- Security best practices involve adopting a "least privilege" approach, ensuring only necessary data is shared, and using encryption for data in transit and at rest.
- Recommended tools and frameworks include data loss prevention (DLP) solutions, secure coding libraries, and regular security audits to identify and mitigate sensitive data exposure.
Technical Details
Likelihood of Exploit: Not specified
Affected Languages: Not Language-Specific
Affected Technologies: Not specified
Vulnerable Code Example
const fs = require('fs');
// Function that writes sensitive information to a file without removing it
function saveUserData(user) {
// Saving user data with sensitive information like email and password
fs.writeFileSync('user_data.txt', `User: \${user.name}, Email: \${user.email}, Password: \${user.password}`); // {5-7}
}
Explanation:
- The code writes sensitive information such as passwords directly to a file without any form of protection.
- This can lead to security risks if the file is accessed by unauthorized individuals, leading to potential data breaches.
How to fix Improper Removal of Sensitive Information Before Storage or Transfer?
To fix this issue, ensure that sensitive information is protected:
- Remove Sensitive Data: Avoid storing sensitive information directly; remove it or replace it with non-sensitive placeholders.
- Encrypt Sensitive Data: If necessary to store, encrypt sensitive data before storage.
- Access Controls: Ensure files containing sensitive data are protected with proper access controls.
Fixed Code Example
const fs = require('fs');
const crypto = require('crypto');
// Function to encrypt sensitive data
function encrypt(text) {
const cipher = crypto.createCipher('aes-256-cbc', 'a_secure_key');
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
// Function that writes non-sensitive information to a file
function saveUserData(user) {
// Only save non-sensitive user data or mask sensitive details
const encryptedEmail = encrypt(user.email);
const encryptedPassword = encrypt(user.password);
fs.writeFileSync('user_data.txt', `User: \${user.name}, Email: \${encryptedEmail}, Password: \${encryptedPassword}`); // {11-12}
}
Explanation:
- Sensitive information is encrypted before being stored in the file, which protects it from unauthorized access.
- The use of encryption ensures that even if the file is accessed, the sensitive data is not easily readable.
- This approach demonstrates best practices by using encryption for sensitive data, ensuring that it is not stored in plain text.