Security Features
Gojang Framework Security Implementation Summary
This document outlines the security features currently implemented in the Gojang framework.
๐ Authentication & Password Securityโ
Password Hashingโ
- Argon2id algorithm - Industry-standard password hashing (superior to bcrypt)
- Parameters: 64MB memory, 3 iterations, 2 parallelism, 16-byte salt, 32-byte key
- Location:
app/gojang/utils/password.go
Featuresโ
- โ Constant-time password comparison (prevents timing attacks)
- โ Generic error messages (prevents user enumeration)
- โ Password field marked as sensitive in database schema
- โ Comprehensive test coverage
๐ซ Session Managementโ
Configurationโ
- HttpOnly cookies - Prevents XSS attacks from stealing session tokens
- Secure flag in production - Ensures cookies only sent over HTTPS
- SameSite: Lax - CSRF protection for navigation requests
- Idle timeout: 30 minutes - Auto-logout after inactivity
- Session lifetime: 12 hours (configurable)
- Location:
app/gojang/http/middleware/session.go
Featuresโ
- โ Session token renewal after login (prevents session fixation)
- โ Session destruction on logout
- โ User active status validation on every request
- โ Session data cleared on inactive/deleted accounts
๐ก๏ธ CSRF Protectionโ
Implementationโ
- Library: github.com/justinas/nosurf
- Coverage: All authentication, post, user, and admin routes
- Method: Double-submit cookie pattern
Protected Routesโ
- โ Login and registration forms
- โ All POST/PUT/DELETE requests
- โ Admin panel operations
- โ User management endpoints
Locationsโ
app/cmd/web/main.go:109(auth routes)app/posts/posts.route.go:14(post routes)app/gojang/http/routes/users.go:14(user routes)app/gojang/admin/admin_routes.go:13(admin routes)
โฑ๏ธ Rate Limitingโ
Featuresโ
- Per-IP rate limiting - Prevents brute force attacks
- Authentication endpoints: 5 requests per minute, burst of 10
- Proper IP extraction - Handles X-Forwarded-For securely
- Memory cleanup - Periodic cleanup of inactive limiters
Implementationโ
- Location:
app/gojang/http/middleware/ratelimit.go - Applied to: Login and registration endpoints
- IP validation: Extracts real client IP from X-Forwarded-For (first/leftmost IP)
๐ Security Headersโ
Headers Configuredโ
Content-Security-Policy (CSP)
default-src 'self'- Only load resources from same originscript-src 'self' 'unsafe-inline' https://unpkg.com- Script sourcesstyle-src 'self' 'unsafe-inline'- Style sourcesimg-src 'self' data: https:- Image sourcesframe-ancestors 'none'- Clickjacking protection
X-Frame-Options: DENY
- Prevents clickjacking attacks
X-Content-Type-Options: nosniff
- Prevents MIME type sniffing attacks
Referrer-Policy: strict-origin-when-cross-origin
- Controls referrer information leakage
Permissions-Policy
- Restricts geolocation, microphone, and camera access
Strict-Transport-Security (HSTS)
- Enforces HTTPS connections (production only)
max-age=31536000; includeSubDomains
Locationโ
app/gojang/http/middleware/security.go
๐ HTTPS Enforcementโ
Featuresโ
- Automatic HTTPS redirect in production
- X-Forwarded-Proto support - Works with reverse proxies
- Debug mode bypass - Development remains on HTTP
Implementationโ
- Location:
app/gojang/http/middleware/security.go(EnforceHTTPS) - Applied: Globally before all other middleware
โ Input Validationโ
Server-Side Validationโ
- Library: github.com/go-playground/validator/v10
- Location:
app/views/forms/forms.go
Validated Fieldsโ
- โ Email format validation
- โ Password minimum length (8 characters)
- โ Required field validation
- โ Password confirmation matching
- โ Field length limits
Featuresโ
- User-friendly error messages
- Form-specific validation structs
- Type-safe validation
๐พ Database Securityโ
ORM Protectionโ
- Ent ORM - Prevents SQL injection through parameterized queries
- Location:
app/gojang/models/ - No raw SQL queries - All database access through ORM
Featuresโ
- โ Automatic query parameterization
- โ Type-safe database operations
- โ Foreign key constraints enabled
- โ Schema migrations managed automatically
๐ค Authorization & Access Controlโ
Role-Based Access Control (RBAC)โ
- Roles: regular user, staff, superuser
- Fields:
is_active,is_staff,is_superuser - Location:
app/schema/user.go
Middleware Protectionโ
- RequireAuth - Ensures user is authenticated
- RequireStaff - Ensures user has staff role
- LoadUser - Loads user from session (optional auth)
Featuresโ
- โ Ownership checks for resource access
- โ Admin action audit logging
- โ Active user validation
- โ Graceful handling of deleted/inactive users
๐ Audit Loggingโ
Featuresโ
- Structured logging - Using Zap logger (JSON output)
- Admin actions tracked - All admin panel operations logged
- IP address logging - Real client IP (properly extracted)
- Request/response tracking - Duration, status codes, user info
Logged Informationโ
- User ID and email
- Action performed
- Resource accessed
- Client IP address
- Timestamp and duration
- Response status code
Locationโ
app/gojang/http/middleware/audit.go
๐ Security Disclosureโ
security.txtโ
- Location:
/.well-known/security.txt - Contact: security@gojangframework.org
- Response time: Within 48 hours
- Safe harbor policy - Supports responsible disclosure
๐งช Security Testing & CI/CDโ
Automated Scanningโ
gosec - Go security checker
- Scans for common security issues
- Runs on every pull request
govulncheck - Vulnerability scanner
- Checks for known vulnerabilities in dependencies
- Runs on every pull request
CI/CD Integrationโ
- Location:
.github/workflows/test.yml - Runs automatically on pull requests
- Reports findings in GitHub Actions
๐ Configuration Securityโ
Environment Variablesโ
- Required fields validated -
DATABASE_URL,SESSION_KEY - Debug mode warning - Logs warning when DEBUG=true
- No default secrets -
.env.examplerequires manual secret generation
Secret Generationโ
# Generate SESSION_KEY
openssl rand -base64 32
Featuresโ
- โ Secrets never committed to repository
- โ
.envfile gitignored - โ
.env.exampleprovides template (no actual secrets)
๐ง Error Handlingโ
Security Featuresโ
- Generic error messages - Don't expose internal details
- Structured logging - Sensitive data never logged
- Custom error pages - 404 handler configured
Featuresโ
- โ Stack traces hidden in production
- โ Database errors sanitized
- โ Authentication failures return generic messages
๐ Documentationโ
Security Documentationโ
- Authentication & Authorization Guide
- Deployment Guide - Production security checklist
- Logging Guide - Secure logging practices
๐ฏ Production Deployment Checklistโ
Essential security items for production:
- Set
DEBUG=falsein environment - Generate secure random
SESSION_KEY - Enable PostgreSQL SSL (
sslmode=require) - Configure HTTPS with valid SSL certificate
- Set
ALLOWED_HOSTSto production domains - Review and test all security headers
- Set up centralized logging
- Configure monitoring and alerting
- Test rate limiting is working
- Verify HTTPS redirect is active
- Review audit logs are being captured
- Set up regular security scanning
- Document incident response procedures
๐ Regular Maintenanceโ
Recommended Practicesโ
- Dependency updates: Review monthly
- Security patches: Apply immediately when available
- Penetration testing: Annually or after major changes
- Code reviews: Include security checklist
- Monitoring: Review audit logs regularly
- Backups: Test restoration procedures quarterly
Last Updated: 2025-10-14
Framework Version: Gojang v1.0