Safe Data Handling
in Scripts
Learn how to protect sensitive data like passwords, API keys, and personal information when writing and deploying scripts.
What is Sensitive Data?
Sensitive data is any information that, if exposed, could cause harm to individuals or organizations. In the context of scripting, this includes credentials, tokens, and personally identifiable information (PII) that your code may process or store.
Why Data Leaks Happen in Scripts
The Attack Surface of a Script
Every script interacts with its environment in multiple ways. Each interaction point is a potential data exposure vector: a place where sensitive data could leak to unauthorized parties.
The Golden Rule of Data Safety
Principle of Least Privilege
Every script should request and receive only the minimum permissions it needs to accomplish its task. If a script only reads from a database, it should not have write or admin access.
Hardcoded vs. Externalized Secrets
| Hardcoded | Externalized | |
|---|---|---|
| Location | Inside the script file | Environment variable or vault |
| Version Control | Committed to repo | Never in repo |
| Rotation | Requires code change + redeploy | Change variable, no code edit |
| Audit Trail | None: who read the file? | Vault logs every access |
| Risk Level | High | Low |
Safe Storage Methods
How to Secure Your Script
Example 1: The Unsafe Script
Example 2: The Fixed Script
Example 3: The .gitignore Guard
Sanitizing Log Output
Scripts often use print() or logging for debugging. If a variable containing a secret is accidentally logged, it becomes visible in console output, log files, and monitoring dashboards.
Encryption: At Rest and In Transit
Common Mistakes to Avoid
Summary: Safe vs. Unsafe Practices
| Practice | Unsafe | Safe |
|---|---|---|
| Secret Storage | Hardcoded in script | Environment variable / vault |
| Version Control | Secrets in repo | .gitignore excludes secrets |
| Logging | Print secret values | Generic messages only |
| Network | HTTP (plain text) | HTTPS / TLS |
| Permissions | Full admin access | Least privilege |
| Key Rotation | Never rotated | Regular rotation schedule |
Question 1 of 5
A developer writes API_KEY = "sk-abc123" directly in their Python script. What is the primary security risk?
Question 2 of 5
Which of the following is the safest way to provide a database password to a script?
Question 3 of 5
A script contains print(f"Token: {auth_token}"). Why is this a security concern?
Question 4 of 5
What does the Principle of Least Privilege mean for a script that only needs to read data from a database?
Question 5 of 5
A developer accidentally committed an API key to a public GitHub repository last week. They deleted the key from the file in a new commit. Is the key still at risk?