Secrets, dependencies and headers
Keep keys out of code, update packages and set safe defaults.
Secrets
- Keep API keys, tokens and passwords in environment variables or a secrets manager, never in the source code.
- Add
.envto.gitignorebefore the first commit. - If a secret reaches a public repository, rotate it immediately. Deleting the commit is not enough: bots scan for keys within minutes.
- Give each key the smallest permissions it needs, and use separate keys per environment.
Dependencies
Most of your code is other people's code. Vulnerabilities in packages are a leading cause of breaches.
npm audit # known vulnerabilities in Node dependencies
pip-audit # same idea for Python (install with pip)
- Commit the lock file and install from it in CI.
- Update regularly. Tools such as Dependabot open pull requests for you.
- Remove packages you no longer use.
Safe defaults
| Header | Purpose |
|---|---|
Strict-Transport-Security | Force HTTPS for future visits |
Content-Security-Policy | Limit script and resource sources |
X-Content-Type-Options: nosniff | Stop the browser guessing content types |
Referrer-Policy | Control how much URL is sent to other sites |
Permissions-Policy | Disable browser features you do not use |
NoteShow generic error messages to users and log the details on the server. Stack traces help attackers.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.