## Authentication & Session Management Defects (High)

### Trigger Signals

- Password/Token comparison operations (`==`, `===`, `equals`, `compare`)
- Session management code (`session`, `cookie`, `jwt`, `token`, `refreshToken`)
- Password handling (`hash`, `bcrypt`, `pbkdf2`, `scrypt`)
- Login/register/password reset flows

### Judgment Process

1. **Check password comparison**: `Read` confirms whether password/Token comparison uses constant-time comparison:
   - Using `==`/`===` → timing attack risk
   - Should use `crypto.timingSafeEqual`, `bcrypt.compare`, `hmac.compare`
2. **Check password storage**: `Read` confirms whether passwords use secure hashing algorithms:
   - Should use `bcrypt`/`scrypt`/`argon2` (built-in salt and adaptive computational cost)
   - Using `md5`/`sha1`/`sha256` + fixed salt → insecure
   - Plaintext storage → critical defect
3. **Check session management**: `Read` confirms session security:
   - Whether session ID generation uses cryptographically secure random numbers (`crypto.randomBytes`, not `Math.random`)
   - Whether cookies set `HttpOnly` (prevents XSS theft), `Secure` (HTTPS only), `SameSite`
   - Whether session expiration time is reasonable
4. **Check JWT security**: `Read` confirms JWT implementation:
   - Signing key strength (at least 256-bit random key)
   - Whether signature is verified (not just `decode` without `verify`)
   - Token expiration time (`exp`) whether set and reasonable
   - Whether sensitive data is stored in payload (payload is only Base64 encoded, not encrypted)
5. **Check password reset flow**: `Read` confirms reset Token:
   - Whether Token has expiration and single-use restriction
   - Whether Token is bound to user (preventing using A's Token to reset B's password)

### Evidence Construction

After confirming a defect, the output must include:
- Defect location (insecure comparison/storage/session operation, with file:line number)
- Attack vector (timing attack, brute force, session hijacking, Token forgery)
- Business impact (account compromise, session hijacking, lateral privilege escalation)

### Exclusions

- Already using `bcrypt.compare`/`crypto.timingSafeEqual` for password comparison
- Password already stored with `bcrypt`/`argon2`
- Session cookies already set with `HttpOnly` + `Secure` + `SameSite`
- JWT signature correctly verified with reasonable expiration time
