Security — Auth, Hashing & OWASP

Authentication vs authorization, sessions vs JWT, hashing vs encryption (bcrypt vs Fernet), and the OWASP bugs (XSS, CSRF, SQLi) with their mitigations — grounded in StockVision.

securityauthOWASPencryption

Authentication vs authorization

  • Authentication (authn): who are you? (login)
  • Authorization (authz): what may you do? (permissions/entitlements)

Keep authz server-side — StockVision enforces subscription tiers on the API; the client-side gate is only UX.

Sessions vs JWT

Session (server state)JWT (stateless)
Where state livesserver (Redis/DB)the signed token itself
Revocationeasy (delete session)hard (need a denylist / short TTL)
Scaleshared session storeno lookup, scales freely

StockVision uses JWTs in httpOnly cookies + short-lived access tokens + rotating refresh tokens — stateless scale with a revocation story via refresh rotation.

Hashing vs encryption (they're not the same)

  • Hashing (passwords): one-way. Use bcrypt/argon2 — deliberately slow, salted per-user, so leaked hashes resist brute force. Never reversible.
  • Encryption (PII you must read back): two-way. StockVision uses Fernet (authenticated symmetric encryption) for PAN/broker tokens so a DB dump yields ciphertext; the key lives outside the DB.
Passwords are hashed, not encrypted

If you can decrypt your users' passwords, you've already lost. Store a slow, salted hash (bcrypt) and compare hashes. Encryption is for data you need to recover (a PAN, an API token), not for passwords.

OWASP bugs & fixes

VulnWhatMitigation
XSSinjected script runs in the victim's pageescape/encode output, CSP, httpOnly cookies
CSRFa site rides the user's cookie to act as themSameSite cookies, CSRF tokens
SQL injectioninput becomes SQLparameterized queries / ORM, never string-concat
Broken authzaccessing others' dataenforce ownership checks server-side
Secrets in codekeys in the repoenv/secret manager, rotate

The httpOnly-cookie choice is a direct XSS mitigation (script can't read the token); pair it with SameSite + CSRF tokens to cover CSRF, and a CSP to reduce XSS impact. StockVision ships security headers + CSP middleware for this reason.