Get a Pentest and security assessment of your IT network.

Cyber Security

Secure Data Storage in Web Apps

TL;DR

Never store sensitive information (passwords, API keys, credit card details) directly in your application code or database in plain text. Use strong encryption, hashing algorithms with salting, and secure key management practices. Consider using dedicated secrets management tools.

1. Understand the Risks

Storing sensitive data insecurely is a major security vulnerability. If compromised, it can lead to:

  • Data breaches: Exposure of customer or business-critical information.
  • Financial loss: Fines, legal costs, and reputational damage.
  • Compliance issues: Violations of data protection regulations (e.g., GDPR).

2. Never Store Plain Text

This seems obvious, but it’s worth repeating. Avoid storing sensitive information as is. This includes:

  • Passwords
  • API keys
  • Credit card numbers
  • Personal Identifiable Information (PII)

3. Password Storage: Hashing with Salting

Instead of storing passwords directly, store their hashes. A hash is a one-way function – you can’t easily get the original password back from the hash.

  • Hashing Algorithm: Use strong hashing algorithms like bcrypt, Argon2, or scrypt. Avoid older algorithms like MD5 and SHA1 which are vulnerable to attacks.
  • Salting: Add a unique random string (the salt) to each password before hashing. This prevents attackers from using pre-computed hash tables (rainbow tables).
# Example Python with bcrypt
pw_hash = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())

4. Encryption for Data at Rest

For sensitive data that *must* be stored, encrypt it before saving to the database.

  • Encryption Algorithm: Use strong encryption algorithms like AES (Advanced Encryption Standard).
  • Key Management: This is crucial! Never store the encryption key in your application code or alongside the encrypted data. See section 6 for more details.
# Example Python with Fernet
from cryptography.fernet import Fernet
key = Fernet.generate_key()
f = Fernet(key)
token = f.encrypt(b'my secret data')
decrypted_data = f.decrypt(token).decode() # Remember to decode bytes!

5. Database Security

Even with encryption and hashing, secure your database:

  • Access Control: Limit access to the database to only necessary users and applications.
  • Regular Backups: Ensure regular backups are performed securely.
  • Database Encryption: Consider using database-level encryption features if available.

6. Secure Key Management

Protecting your encryption keys is paramount.

  • Dedicated Secrets Management Tools: Use tools like HashiCorp Vault, AWS KMS (Key Management Service), Azure Key Vault, or Google Cloud KMS. These provide secure storage and access control for secrets.
  • Environment Variables: For development environments, store keys in environment variables. Do not commit them to version control!
  • Rotate Keys Regularly: Change your encryption keys periodically to minimize the impact of a potential compromise.

7. Tokenization

Replace sensitive data with non-sensitive substitutes called tokens.

  • Payment Card Industry (PCI) Compliance: Commonly used for credit card information to reduce PCI scope.
  • Third-Party Providers: Use tokenization services from reputable providers.

8. Regular Security Audits

Regularly review your security practices and code for vulnerabilities.

  • Penetration Testing: Hire a professional to test your application’s security.
  • Code Reviews: Have other developers review your code for potential issues.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation