Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

SEC
Authentication & Security
0/10 completed
Beginner
Authentication vs authorizationHashing passwords
Intermediate
Sessions and cookiesJSON Web TokensOAuth 2.0 and OpenID ConnectMulti-factor authentication and passkeys
Advanced
The OWASP Top 10XSS, CSRF and CORSSecrets management and encryption
Pro
Security in practice
Tracks / Authentication & Security / Pro

Security in practice

Knowing individual attacks isn't enough. Secure software comes from habits: thinking about threats before writing code, defending in layers, watching what happens in production, and knowing what to do when something goes wrong. This final lesson ties the track together.

Threat modelling

Before building a feature, spend 30 minutes asking four questions (from Adam Shostack's threat-modelling framework):

  1. What are we building? Sketch the data flow: users, services, databases, third parties, and the trust boundaries between them.
  2. What can go wrong? Walk through each boundary with STRIDE.
  3. What are we going to do about it? A mitigation, a test, or a conscious decision to accept the risk.
  4. Did we do a good job? Review after shipping.
STRIDE Threat Violates Typical mitigation
Spoofing Pretending to be someone else Authentication Strong auth, MFA, signed tokens
Tampering Changing data or code Integrity Validation, signatures, access control
Repudiation "I never did that" Accountability Audit logs
Information disclosure Leaking data Confidentiality Authorization, encryption, minimal responses
Denial of service Making it unavailable Availability Rate limits, timeouts, quotas
Elevation of privilege Gaining more rights Authorization Least privilege, permission checks

Example: "forgot password"

What can go wrong Mitigation
Reveals which emails have accounts Always reply "If that account exists, we've sent an email"
Reset tokens guessable or reusable 256 random bits, single use, stored hashed, expire in 30 minutes
Token leaks via the Referer header to third-party scripts Referrer-Policy: no-referrer on the reset page
Attacker floods a victim with emails Rate limit per email and per IP
Old sessions stay valid after the reset Invalidate all sessions on password change
Account taken over silently Notify the user by email after any change
Node.js · editable

Defence in depth

Assume every single control will fail one day, and layer them so one failure isn't a breach:

WAF / rate limits → TLS → authentication → authorization → input validation
→ parameterised queries → least-privilege database user → encryption at rest → monitoring and alerts

If an SQL injection slips through, a database user that can only SELECT from three tables limits the damage; encryption protects the most sensitive columns; and alerts on unusual query volume catch it early.

Secure defaults and least privilege

  • New routes require authentication unless explicitly marked public.
  • New users get the smallest role; services get only the permissions they need (separate database users per service, read-only where possible).
  • Containers run as non-root with read-only filesystems; cloud roles are scoped to specific resources.
  • Features that are rarely needed (admin panels, debug endpoints) are off or behind extra protection.

Brute force and abuse

Combine several measures on login and other sensitive endpoints:

  • Rate limiting per account and per IP (with backoff that grows).
  • Temporary lockout or extra friction (a CAPTCHA) after repeated failures. Avoid permanent lockouts, which let attackers lock out real users.
  • MFA, and breached-password checks.
  • Bot detection for high-value actions (sign-ups, checkouts, gift cards).
Node.js · editable

Dependencies and the supply chain

Most of your application's code was written by other people. Keep it under control:

  • Commit lock files; review what a new dependency pulls in before adding it.
  • Run npm audit / pip-audit / OWASP Dependency-Check in CI, and enable Dependabot or Renovate.
  • Prefer well-maintained packages; remove unused ones.
  • Protect the pipeline: required code review, protected branches, short-lived deploy credentials, MFA on npm/PyPI/GitHub accounts, and pinned (by commit hash) third-party CI actions.

Logging, monitoring and incident response

Log security events (with a timestamp, user ID, IP and request ID, but never passwords, tokens or full card numbers): logins and failures, MFA changes, password resets, permission denials, admin actions, and input validation failures in bulk.

Alert on patterns: many failed logins across accounts from one IP (credential stuffing), a user downloading thousands of records, admin actions at 3 a.m.

Have an incident plan before you need it:

  1. Detect and triage: what happened, how bad is it?
  2. Contain: revoke keys, disable accounts, block IPs, take a feature offline.
  3. Eradicate and recover: fix the vulnerability, restore from clean backups.
  4. Notify: users and regulators where required (GDPR gives 72 hours).
  5. Learn: a blameless post-mortem, then fix the root cause and the detection gap.

Building security into the process

  • Security requirements in every feature's definition (ASVS gives ready-made ones).
  • Code review with security in mind; a checklist helps.
  • Automated tools in CI: SAST (Semgrep, CodeQL), dependency scanning, secret scanning, and DAST (OWASP ZAP) against staging.
  • Tests for the "no" cases: wrong user, expired token, malicious input.
  • Penetration tests and a vulnerability disclosure policy (/.well-known/security.txt) so researchers can report issues to you.

🎓 Authentication & Security track complete!

You've learned the difference between authentication and authorization, how to store passwords, sessions and cookies, JWTs, OAuth 2.0 and OpenID Connect, MFA and passkeys, the OWASP Top 10, XSS, CSRF and CORS, secrets and encryption, and how to design, run and respond securely.

Capstone project: a secure notes app

Build a small notes API (any stack), then attack it yourself:

  1. Users register and log in; passwords are hashed with Argon2id or scrypt; breached passwords are refused.
  2. Sessions in HttpOnly; Secure; SameSite=Lax cookies, rotated at login, expired after 30 idle minutes; logout everywhere.
  3. TOTP-based MFA with recovery codes; a re-prompt before changing the email address.
  4. Notes are private by default and can be shared with specific users: test that nobody else can read them (IDOR).
  5. Note bodies are encrypted with AES-256-GCM, bound to their owner with AAD, keys from environment variables.
  6. Rate limits on login and password reset; security headers via helmet or equivalent; strict CORS.
  7. Structured security logs and an alert rule for five failed logins in a minute.
  8. Write a STRIDE table for the app, then try to break your own app: SQL injection, XSS in a note, CSRF on delete, reading another user's note, reusing a reset link. Fix what you find.
✎ Check your understanding

1.What does the "E" in STRIDE stand for?

2.Why should a "forgot password" form give the same answer for unknown emails?

3.What is defence in depth?

← Secrets management and encryption