Dependencies, secrets and the supply chain

Protect the code you did not write and the credentials your application depends on.

Intermediate30 min

Learning objectives

  • Manage npm dependencies with lockfiles and audits
  • Keep secrets out of source control and out of client bundles
  • Add integrity checks to third-party scripts

Your code is mostly someone else’s code

A typical web app pulls in hundreds of packages. Each one runs with the same privileges as your own code. Supply-chain attacks, where a dependency is hijacked or typosquatted, are now a routine threat.

Dependency hygiene

  • Commit the lockfile (package-lock.json, pnpm-lock.yaml) so every build installs the same versions.
  • Install exactly from the lockfile in CI with npm ci, not npm install.
  • Audit regularly with npm audit, and review what it reports rather than blindly upgrading.
  • Automate updates with Dependabot or Renovate, and read the changelog before merging.
  • Keep dependencies few. Every package is attack surface. Prefer platform APIs when they do the job.
  • Check names carefully before installing. Typosquatted packages often differ by a single character.

Keep secrets out of the codebase

API keys, database passwords and signing keys must never be committed.

# .gitignore
.env
.env.*
!.env.example

Read secrets from environment variables or a secret manager at runtime, and keep a .env.example file with placeholder values so others know what is needed.

If a secret is committed, treat it as compromised. Removing it in a later commit does not remove it from history. Rotate the credential first, then clean up. Secret scanning tools such as gitleaks or GitHub’s secret scanning can catch these before they are pushed.

Frameworks can expose secrets to the browser

Some frameworks expose environment variables to client-side code when they carry a public prefix. In Astro, variables prefixed with PUBLIC_ are embedded in the client bundle, so never give a secret that name:

# .env
PUBLIC_SITE_NAME="GuardCoder"   # safe: intended for the browser
DATABASE_URL="postgres://..."   # server-only: never sent to the browser

Check your built output for secrets before you deploy. Searching dist/ for a known key prefix is a quick sanity check.

Subresource Integrity for third-party scripts

If you load a script from a CDN, pin its content with an integrity hash. The browser refuses the file if it has been changed:

<script
  src="https://cdn.example.com/library.js"
  integrity="sha384-BASE64_HASH_OF_THE_EXACT_FILE"
  crossorigin="anonymous"></script>

Even better, self-host critical scripts so you control the exact bytes.

Protect your publishing pipeline

  • Turn on two-factor authentication for your npm, Git hosting and cloud accounts.
  • Give CI tokens the smallest scope and shortest lifetime that work.
  • Review who can publish packages or deploy to production.

Lab: audit your project

In a project you own, run:

npm audit
git log -p --all -S "API_KEY" --oneline

The second command searches your history for a key name. Replace it with the names of your own secret types, and rotate anything you find.

Key takeaways

  • Commit lockfiles, install with npm ci, and audit and update dependencies regularly.
  • Keep secrets in environment variables or a secret manager, never in source control or PUBLIC_ variables.
  • Pin third-party scripts with Subresource Integrity, or self-host them.