appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

Secrets, dependencies and headers

Lesson 3 of 39 minIntermediate

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 .env to .gitignore before 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

HeaderPurpose
Strict-Transport-SecurityForce HTTPS for future visits
Content-Security-PolicyLimit script and resource sources
X-Content-Type-Options: nosniffStop the browser guessing content types
Referrer-PolicyControl how much URL is sent to other sites
Permissions-PolicyDisable 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.

1. An API key was committed to a public repo. What is the first step?
HintAn npm subcommand.
3. Which header forces browsers to use HTTPS on future visits?
4. What should users see when an unexpected error happens?

Key terms