Anchor provides comprehensive security features to protect your application against common web vulnerabilities. This guide covers all security features and best practices.
| Feature | Protection Against | Documentation |
|---|---|---|
| CSRF Protection | Cross-Site Request Forgery | csrf |
| SQL Injection Protection | SQL Injection | Query Builder |
| XSS Protection | Cross-Site Scripting | views |
| Authentication | Unauthorized Access | authentication |
| Encryption | Data Breaches | encryption |
| Firewall | Brute Force, Rate Limiting | firewall |
| Security Headers | Clickjacking, MIME Sniffing | middleware |
| File Upload Validation | Malicious Uploads | #file-uploads |
| Input Validation | Invalid Data | validation |
APP_KEYgenerated and secured- HTTPS enabled (
APP_SECURE=true) - Debug mode disabled (
APP_DEBUG=false) - CSRF protection enabled
- Security headers middleware enabled
- Strong password policies enforced
- File upload validation implemented
- Database credentials secured
- Error reporting configured properly
- Session timeout configured
- Firewall thresholds set appropriately
- All user input validated
- Output escaped in views (
$this->escape()) - Parameterized queries used (Query Builder)
- Passwords hashed with Argon2ID
- Sensitive data encrypted at rest
- Authorization checks implemented
- File uploads validated
- CORS configured for APIs (if needed)
Enabled by default - Protects against Cross-Site Request Forgery attacks.
// In forms
<form method="POST">
<?php echo $this->csrf(); ?>
<!-- form fields -->
</form>
// In AJAX
fetch('/api/endpoint', {
headers: {
'X-CSRF-Token': '<?= csrf_token() ?>'
}
});Configuration: App/Config/default.php → csrf
See csrf for details.
Built-in - Query Builder uses parameterized queries.
// Safe - uses bindings
User::query()->where('email', '=', $email)->first();
// Safe - uses bindings
DB::table('user')->where('id', '=', $id)->get();
// Use whereRaw() carefully
DB::table('user')->whereRaw('created_at > NOW()')->get();Automatic escaping in views + Security Headers middleware.
// Safe - automatically escaped
<?= $this->escape($user->name) ?>
// Unsafe - only use for trusted HTML
<?= $trustedHtml ?>Security Headers:
X-XSS-Protection: 1; mode=blockX-Content-Type-Options: nosniffContent-Security-Policy(optional)
Argon2ID password hashing + Session-based authentication.
// Hash password
$hashedPassword = enc()->hashPassword('user-password');
// Verify password
if (enc()->verifyPassword('input', $hashedPassword)) {
// Valid
}
// Check authentication
if ($this->auth->isAuthenticated()) {
// User is logged in
}
// Check authorization
if ($this->auth->isAuthorized($route)) {
// User can access this route
}See authentication.md for details.
AES-256-GCM for strings, Argon2ID for passwords, PBKDF2 for files.
// Encrypt sensitive data
$encrypted = encrypt('sensitive data');
// Decrypt
$decrypted = decrypt($encrypted);
// Hash password
$hash = enc()->hashPassword('password');
// Verify password
if (enc()->verifyPassword('password', $hash)) {
// Valid
}See encryption.md for details.
Middleware that adds security headers to all responses.
// In App/Config/middleware.php
return [
'web' => [
\App\Middleware\Web\SecurityHeadersMiddleware::class,
// ... other middleware
],
];Headers Added:
X-Frame-Options: SAMEORIGIN- Prevents clickjackingX-Content-Type-Options: nosniff- Prevents MIME sniffingX-XSS-Protection: 1; mode=block- Legacy XSS protectionReferrer-Policy: strict-origin-when-cross-originPermissions-Policy- Controls browser featuresStrict-Transport-Security- HSTS (HTTPS only)
Configuration: App/Config/default.php → security_headers
'security_headers' => [
'enabled' => true,
'x_frame_options' => 'SAMEORIGIN',
'x_content_type_options' => 'nosniff',
'hsts_enabled' => true,
];Protects against brute force and excessive requests.
use Security\Firewall\Drivers\AccountFirewall;
$firewall = resolve(AccountFirewall::class);
$firewall->user(['id' => $userId])
->handle();
if ($firewall->isBlocked()) {
// User is blocked
}See firewall for details.
Securely handle file uploads to prevent malicious code execution.
$file = $this->request->file('avatar');
// Validate type, size, and use a safe filename
$path = $file->moveSecurely('/uploads/avatars', [
'type' => 'image', // Only allow images
'maxSize' => 2097152, // Max 2MB
'extensions' => ['jpg', 'png'] // Explicit extensions
]);
if (!$path) {
// Validation failed
$error = $file->getValidationError();
}Key Security Features:
- MIME Type Validation: Checks actual file content, not just extension
- Extension Allow-list: Only allows safe file extensions
- Filename Sanitization: Generates random safe filenames or sanitizes input
- Size Limits: Prevents DoS attacks via large files
- Permissions: Sets non-executable permissions on uploaded files
See requests.md#file-uploads for implementation details.
For APIs - Configure allowed origins, methods, and headers.
// Enable in App/Config/cors.php
'enabled' => true,
'allowed_origins' => [
'https://yourdomain.com',
'https://*.yourdomain.com', // Wildcard subdomain
],
'allowed_methods' => ['GET', 'POST', 'PUT', 'DELETE'],Add to API middleware:
// In App/Config/middleware.php
return [
'api' => [
\App\Middleware\Api\CorsMiddleware::class,
// ... other middleware
],
];Class-based validation with type checking and sanitization.
public function rules(): array
{
return [
'email' => ['type' => 'email'],
'password' => [
'type' => 'password',
'config' => [
'uppercase' => 1,
'numeric' => 1,
'special' => 1,
'length_min' => 12, // Increased for security
'not_common' => true,
]
],
];
}See validation for details.
Database-backed sessions with secure cookies.
// Configuration in App/Config/default.php
'session' => [
'timeout' => 14400, // 4 hours
'cookie' => [
'secure' => true, // HTTPS only
'http_only' => true, // No JavaScript access
'samesite' => 'Lax', // CSRF protection
],
],# .env
APP_SECURE=true// Always validate before processing
$validator->validate($this->request->post());
if ($validator->has_error()) {
// Handle errors
}// In views
<?= $this->escape($user->name) ?>// Safe
User::query()->where('email', '=', $email)->first();
// Never do this
DB::raw("SELECT * FROM user WHERE email = '$email'");Don't Encrypt
// Correct
$user->password = enc()->hashPassword($password);
// Wrong
$user->password = encrypt($password);// Encrypt sensitive data at rest
$user->ssn = encrypt($ssn);
// Use HTTPS for data in transit// Check if user can access resource
if (!$this->auth->isAuthorized($route)) {
return $this->response->status(403)->json(['error' => 'Forbidden']);
}composer updateEnable SecurityHeadersMiddleware in production.
- Review code for vulnerabilities
- Update dependencies
- Test authentication/authorization
- Verify HTTPS configuration
- Check file upload handling
| Vulnerability | Protection | Status |
|---|---|---|
| SQL Injection | Parameterized queries | ✅ Built-in |
| XSS | Output escaping + CSP | ✅ Built-in |
| CSRF | Token validation | ✅ Built-in |
| Clickjacking | X-Frame-Options | ✅ Middleware |
| MIME Sniffing | X-Content-Type-Options | ✅ Middleware |
| Session Fixation | Session regeneration | ✅ Built-in |
| Brute Force | Firewall rate limiting | ✅ Built-in |
| Weak Passwords | Argon2ID hashing | ✅ Built-in |
| Insecure Deserialization | Class whitelists | ✅ Built-in |
| Malicious Uploads | File validation | ✅ Utility |
The framework's security features support compliance with:
- PCI DSS - Payment card data protection
- HIPAA - Healthcare data encryption
- GDPR - Personal data protection
- SOC 2 - Security controls
If you discover a security vulnerability, please email security@example.com. Do not create public issues for security vulnerabilities.