Overview
OdontologyApp implements multiple layers of security to protect sensitive medical data and ensure proper access control. The security architecture includes authentication, role-based access control (RBAC), session management, and protection against common web vulnerabilities.Authentication
Password Hashing
Passwords are hashed using bcryptjs with salt rounds before storage:- Admin:
admin/admin123 - Doctor:
doctor/doctor123 - Secretary:
secretaria/secretaria123
Session Management
Sessions are stored in HTTP-only cookies using SvelteKit’s built-in cookie handling:Global Authentication Guard
Thehooks.server.js (src/hooks.server.js:1-68) file implements global authentication:
- Check for session cookie
- Parse user data from session
- Store user in
event.locals.user - Validate route access based on role
- Redirect if unauthorized
Authorization (RBAC)
Role-Based Access Control
OdontologyApp implements a granular permission system with three primary roles: 1. Admin - Full system access 2. Doctor - Clinical operations and patient care 3. Secretary - Administrative and scheduling tasksPermission System
Permissions are defined insrc/lib/permissions.js (src/lib/permissions.js:1-109):
Role Permissions Mapping
Permission Checking
ThecheckPermission function (src/lib/server/checkPermission.js:17-41) validates user permissions:
- Admin check: Admins always return
true - Database lookup: Check
user_permissionstable for individual grants - Role fallback: Use
ROLE_PERMISSIONSmapping if no DB entry
Using Permissions in API Endpoints
Example: Patient API (src/routes/api/patients/+server.js:10-17):Frontend Permission Checks
Components can check permissions to show/hide UI elements:Database-Backed Permissions
Admins can grant/revoke individual permissions via theuser_permissions table:
CSRF Protection
SvelteKit CSRF Configuration
CSRF protection is configured insvelte.config.js (src/routes/svelte.config.js:10-12):
checkOrigin: true to validate request origins.
Same-Site Cookie Policy
Session cookies usesameSite: "strict" to prevent CSRF:
SQL Injection Prevention
Prepared Statements
All database queries use parameterized statements viamysql2/promise:
Input Validation
All inputs are validated using Zod schemas before database operations:Audit Logging
All sensitive operations are logged to thelogs table:
Error Handling
Global Error Handler
ThehandleError function (src/hooks.server.js:56-67) catches unhandled errors:
Security Best Practices
Implemented
✅ Password hashing with bcrypt (10 rounds)✅ HTTP-only session cookies
✅ Same-site cookie policy (strict)
✅ Prepared SQL statements (no string concatenation)
✅ Input validation with Zod schemas
✅ Role-based access control (RBAC)
✅ Granular permission system
✅ Audit logging for sensitive operations
✅ Authentication guard on all routes
✅ Permission checks on API endpoints
Recommended Enhancements
⚠️ Enable CSRF origin checking in production⚠️ Implement rate limiting for login attempts
⚠️ Add password complexity requirements
⚠️ Implement session timeout/idle detection
⚠️ Add two-factor authentication (2FA)
⚠️ Use HTTPS in production (secure cookies)
⚠️ Implement Content Security Policy (CSP)
⚠️ Add security headers (X-Frame-Options, etc.)
⚠️ Regular security audits and dependency updates
Related Documentation
- Database Schema - Permission tables and stored procedures
- Architecture - Overall system design
- API Reference - Endpoint authentication requirements
