feat(security): implement comprehensive security hardening

- Add CSRF protection with cookie-based tokens
  - Add cookie-parser and csurf middleware
  - Create GET /api/csrf-token endpoint
  - Frontend automatically includes CSRF token in POST/PUT/DELETE requests
  - Add retry logic for expired CSRF tokens

- Implement account lockout mechanism
  - Add database fields: failedLoginAttempts, lockedUntil
  - Track failed login attempts and lock accounts after max attempts (configurable)
  - Auto-unlock after lockout duration expires
  - Return helpful error messages with remaining time

- Add comprehensive security environment variables
  - Rate limiting configuration (API, auth, email endpoints)
  - CSRF protection toggle
  - Password policy requirements
  - Account lockout settings
  - Logging levels

- Add comprehensive test coverage
  - 6 new tests for account lockout functionality
  - 11 new tests for CSRF protection
  - All tests handle enabled/disabled states gracefully

- Update documentation
  - Add Phase 3 security hardening to SESSION_CONTEXT.md
  - Document new database fields and migration
  - Update progress to 85%

Files changed:
- Backend: app.js, auth controller, security config, new migration
- Frontend: api.js with CSRF token handling
- Tests: auth.test.js (extended), csrf.test.js (new)
- Config: .env examples with security variables
- Docs: SESSION_CONTEXT.md updated
This commit is contained in:
Radosław Gierwiało
2025-11-19 20:16:05 +01:00
parent cbc970f60b
commit 44df50362a
10 changed files with 687 additions and 12 deletions

View File

@@ -21,8 +21,8 @@
- Phase 2 (Matches & Ratings API) - ✅ COMPLETED
- Phase 1.6 (Competition Heats) - ✅ COMPLETED
- Phase 1.5 (Email & WSDC & Profiles & Security & QR Check-in) - ✅ COMPLETED
**Progress:** ~80% overall
**Next Goal:** Security hardening, PWA features, Production deployment
**Progress:** ~85% overall
**Next Goal:** PWA features, Production deployment
### What Works Now
- ✅ Docker Compose (nginx:8080 + frontend + backend + PostgreSQL)
@@ -47,10 +47,10 @@
-**STUN servers for NAT traversal (production-ready) - Phase 2.5**
-**Landing page with hero section and features showcase - Phase 3**
-**WebRTC test suite (7 backend tests passing) - Phase 3**
-**Security hardening (CSRF protection, Account Lockout, Rate Limiting) - Phase 3**
- ✅ Real-time chat (Socket.IO for event & match rooms)
### What's Missing
- ⏳ Security hardening (CORS, CSRF, Helmet, CSP)
- ⏳ PWA features (manifest, service worker, offline support)
- ⏳ Production deployment & monitoring
- ⏳ Competition heats UI integration improvements
@@ -141,16 +141,18 @@
- `frontend/src/components/common/PasswordStrengthIndicator.jsx` - Password strength indicator
- `frontend/src/components/common/VerificationBanner.jsx` - Email verification banner
- `frontend/src/contexts/AuthContext.jsx` - JWT authentication integration
- `frontend/src/services/api.js` - **UPDATED: Heats API (divisionsAPI, competitionTypesAPI, heatsAPI) - Phase 1.6**
- `frontend/src/services/api.js` - **UPDATED: Heats API, CSRF token handling - Phase 1.6 & Phase 3**
- `frontend/src/services/socket.js` - Socket.IO client connection manager
- `frontend/src/data/countries.js` - **NEW: List of 195 countries - Phase 1.5**
- `frontend/src/utils/__tests__/webrtcDetection.test.js` - **NEW: WebRTC detection tests - Phase 3**
- `frontend/src/components/__tests__/WebRTCWarning.test.jsx` - **NEW: WebRTC warning tests - Phase 3**
**Backend:**
- `backend/src/controllers/auth.js` - Register, login, email verification, password reset
- `backend/src/app.js` - **UPDATED: CSRF protection, cookie-parser middleware - Phase 3**
- `backend/src/controllers/auth.js` - **UPDATED: Account lockout logic in login - Phase 3**
- `backend/src/controllers/user.js` - **UPDATED: Profile updates (social, location) - Phase 1.5**
- `backend/src/controllers/wsdc.js` - WSDC API proxy for dancer lookup
- `backend/src/config/security.js` - **Security configuration (CSRF, rate limiting, account lockout)**
- `backend/src/routes/events.js` - **UPDATED: Heats management endpoints (POST/GET/DELETE /heats) - Phase 1.6**
- `backend/src/routes/divisions.js` - **NEW: List all divisions - Phase 1.6**
- `backend/src/routes/competitionTypes.js` - **NEW: List all competition types - Phase 1.6**
@@ -159,34 +161,39 @@
- `backend/src/utils/email.js` - AWS SES email service with HTML templates
- `backend/src/utils/auth.js` - Token generation utilities
- `backend/src/middleware/auth.js` - Email verification middleware
- `backend/src/middleware/rateLimiter.js` - Rate limiting middleware (API, auth, email)
- `backend/src/middleware/validators.js` - **UPDATED: Social media URL validation - Phase 1.5**
- `backend/src/server.js` - Express server with Socket.IO integration
- `backend/src/__tests__/socket-webrtc.test.js` - **NEW: WebRTC signaling tests (7 tests) - Phase 3**
- `backend/src/__tests__/auth.test.js` - Authentication tests
- `backend/src/__tests__/auth.test.js` - **UPDATED: Account lockout tests (6 new tests) - Phase 3**
- `backend/src/__tests__/csrf.test.js` - **NEW: CSRF protection tests (11 tests) - Phase 3**
- `backend/src/__tests__/events.test.js` - Events API tests
- `backend/src/__tests__/matches.test.js` - Matches API tests
- `backend/prisma/schema.prisma` - **UPDATED: 8 tables (EventCheckinToken added) - Phase 1.5**
- `backend/prisma/schema.prisma` - **UPDATED: Account lockout fields (failedLoginAttempts, lockedUntil) - Phase 3**
- `backend/prisma/migrations/20251113151534_add_wsdc_and_email_verification/` - Phase 1.5 migration
- `backend/prisma/migrations/20251113202500_add_event_slug/` - **NEW: Event slugs migration - Phase 1.5**
- `backend/prisma/migrations/20251114125544_add_event_checkin_tokens/` - **NEW: QR check-in tokens - Phase 1.5**
- `backend/prisma/migrations/20251119_add_account_lockout_fields/` - **NEW: Account lockout migration - Phase 3**
**Config:**
- `docker-compose.yml` - nginx, frontend, backend, PostgreSQL
- `nginx/conf.d/default.conf` - Proxy for /api and /socket.io
- `backend/.env` - **UPDATED: AWS SES credentials, email settings - Phase 1.5**
- `backend/.env.production` - **UPDATED: Security env variables - Phase 3**
- `backend/.env.development` - **UPDATED: Security env variables - Phase 3**
---
## Database Schema (Implemented - Prisma)
11 tables with relations:
- `users` - **EXTENDED in Phase 1.5:**
- `users` - **EXTENDED in Phase 1.5 & Phase 3:**
- Base: id, username, email, password_hash, avatar, created_at, updated_at
- **WSDC:** first_name, last_name, wsdc_id
- **Email Verification:** email_verified, verification_token, verification_code, verification_token_expiry
- **Password Reset:** reset_token, reset_token_expiry
- **Social Media:** youtube_url, instagram_url, facebook_url, tiktok_url
- **Location:** country, city
- **Account Lockout (Phase 3):** failed_login_attempts, locked_until
- `events` - id, **slug (unique)**, name, location, start_date, end_date, description, worldsdc_id, participants_count
- `event_participants` - **NEW in Phase 1.5:** id, user_id, event_id, joined_at (many-to-many)
- `event_checkin_tokens` - **NEW in Phase 1.5:** id, event_id (unique), token (cuid, unique), created_at
@@ -204,6 +211,7 @@
- `20251113202500_add_event_slug` - **Phase 1.5 (event security - unique slugs)**
- `20251114125544_add_event_checkin_tokens` - **Phase 1.5 (QR code check-in system)**
- `20251114142504_add_competition_heats_system` - **Phase 1.6 (competition heats for matchmaking)**
- `20251119_add_account_lockout_fields` - **Phase 3 (account lockout security)**
**Seed data:** 4 events, 6 divisions, 2 competition types, event chat rooms
@@ -455,7 +463,7 @@ RUN apk add --no-cache openssl
**Phase 3 Status:** ⏳ IN PROGRESS - MVP Finalization
- ✅ Landing page with hero section
- ✅ WebRTC test suite (7 backend tests passing)
- Security hardening (CORS, CSRF, Helmet, CSP)
- Security hardening (CSRF, Account Lockout, env variables, comprehensive tests)
- ⏳ PWA features (manifest, service worker)
- ⏳ Production deployment
**Next Goal:** Security hardening, PWA features, Production deployment
**Next Goal:** PWA features, Production deployment