Skip to main content

Authentication System

Sunschool uses JWT (JSON Web Token) authentication with scrypt password hashing. From server/middleware/auth.ts:

JWT Configuration

JWT tokens provide stateless authentication, allowing the server to verify user identity without session storage.

Token Generation

From server/middleware/auth.ts:
Configuration:
From server/config/env.ts:

Token Verification

From server/middleware/auth.ts:

Generate Secure Secrets

Never use default secrets in production. Generate cryptographically secure random values.
Generate JWT secret:
Add to .env:
If JWT_SECRET is not set, it defaults to SESSION_SECRET. Set both separately for better security.

Password Security

scrypt Hashing

Sunschool uses scrypt for password hashing - a memory-hard function resistant to brute-force attacks. From server/middleware/auth.ts:
Security features:
  • Random 16-byte salt per password
  • 64-byte derived key using scrypt
  • Timing-safe comparison to prevent timing attacks
  • Format: {hash_hex}.{salt_hex}

Password Requirements

Implement password requirements in your client application. The server accepts any non-empty password.
Recommended client-side validation:

Role-Based Access Control

From shared/schema.ts:

User Roles

Permissions:
  • Full system access
  • View all users and data
  • Manage parents and learners
  • Access admin-only endpoints
  • Export all data
Use case: System administrators, school administratorsFrom server/auth.ts:

Role Middleware

From server/middleware/auth.ts:
Usage in routes:

First User Admin Promotion

From server/auth.ts and server/routes.ts:
First registered user becomes ADMIN automatically. This ensures you can bootstrap the system without manual database access.
Registration response includes promotion notice:

Session Security

From server/config/env.ts:
Session configuration:
Use different secrets for JWT and sessions to limit the impact of a potential key compromise.

Session Storage

From shared/schema.ts, sessions are stored in PostgreSQL:
Automatic cleanup: Expired sessions are periodically removed via the expire index.

CORS Configuration

From server/auth.ts:
SAST finding (MEDIUM priority): CORS origin uses substring match. For production, tighten to exact domain match.
Recommended production CORS:

API Security Best Practices

Rate Limiting

Not implemented by default. Add rate limiting for production deployments.
Recommended: express-rate-limit

Input Validation

Current validation:
Recommended: Add schema validation with Zod

SQL Injection Protection

Already protected. Sunschool uses Drizzle ORM with parameterized queries.
From server/routes.ts:
Avoid raw SQL unless necessary:

XSS Protection

From ENGINEERING.md:
SVG content is sanitized via a lightweight regex-based sanitizer in server/services/svg-llm-service.ts
SVG sanitization:
  • Strips <script>, <style>, <iframe> tags
  • Removes event handlers (onclick, onload, etc.)
  • Blocks javascript: and data: URIs
  • LLM prompts prohibit scripts and external references
SAST finding (LOW priority): SVG innerHTML rendering. Server-side DOMPurify mitigates; consider client-side pass.

Environment-Specific Security

Development

Relaxed settings:
  • Detailed error messages
  • No SSL for local database
  • Shorter JWT expiry for testing

Production

Hardened settings:
  • Generic error messages
  • SSL required for all connections
  • Long random secrets
  • Extended token expiry for user convenience

Security Checklist

1

Generate Secure Secrets

2

Enable SSL for Database

3

Set Strong Password Policy

Implement client-side validation:
  • Minimum 8 characters
  • Mixed case, numbers, symbols
4

Configure CORS Properly

Use exact origin matching, not substrings
5

Add Rate Limiting

Install and configure express-rate-limit
6

Set NODE_ENV=production

7

Keep Dependencies Updated

Audit Logging

Not implemented by default. Add audit logging for production.
Recommended approach:

Next Steps

Database Setup

Secure your database with SSL and access controls

Monitoring

Monitor authentication failures and security events

Troubleshooting

Debug authentication issues