A secure PHP endpoint for receiving and processing Content Security Policy (CSP) violation reports. This handler validates incoming CSP reports, implements rate limiting, and forwards reports via email using SendGrid.
- Secure CSP report processing with origin validation
- Rate limiting to prevent abuse
- Email notifications via SendGrid
- Input sanitization and validation
- Comprehensive error handling
- JSON payload validation
- Size limit enforcement
- PHP 7.4 or higher
- SendGrid PHP library
- Write access to temporary directory (for rate limiting)
- Composer for dependency management
- Clone the repository:
git clone https://github.com/yourusername/csp-report-handler.git
cd csp-report-handler- Install dependencies using Composer:
composer require sendgrid/sendgrid- Copy the configuration template:
cp config.example.php config.php- Update your configuration in
config.php:
$emailTo = "security@yourdomain.com";
$emailFrom = "csp-reports@yourdomain.com";
$sendgridApiKey = 'YOUR_SENDGRID_API_KEY';
$allowedOrigin = 'https://yourdomain.com';$emailTo: Email address where reports will be sent$emailFrom: Email address used as the sender$sendgridApiKey: Your SendGrid API key$allowedOrigin: The domain allowed to submit reports
$rateLimitFile: Path to the rate limit tracking file$maxRequests: Maximum number of requests allowed per time window$timeWindow: Time window in seconds for rate limiting
Add this endpoint as your CSP report-uri:
Content-Security-Policy-Report-Only: default-src 'self'; report-uri /path/to/csp-handler.phpOr for enforced CSP:
Content-Security-Policy: default-src 'self'; report-uri /path/to/csp-handler.phpReports are sent in plain text format with the following sections:
CSP Violation Report
===================
Document-uri: https://example.com/page
Violated-directive: script-src
Blocked-uri: https://malicious-site.com/script.js
Server Details:
Time: 2024-12-12 13:45:23
IP: 192.168.1.1
Origin: https://example.com
-
Origin Validation
- Ensures reports only come from allowed domains
-
Rate Limiting
- Prevents DoS attacks
- Configurable request limits and time windows
-
Input Validation
- Size limits on payloads
- JSON structure validation
- Content-Type verification
-
Output Sanitization
- HTML encoding of report data
- Sanitized email content
The endpoint returns appropriate HTTP status codes:
- 204: Success
- 400: Invalid JSON
- 403: Invalid origin
- 413: Payload too large
- 415: Invalid content type
- 429: Rate limit exceeded
- 500: Email sending failure
All errors are logged using PHP's error_log() function, including:
- Invalid origins
- Rate limit violations
- JSON parsing errors
- SendGrid API errors
- Fork the repository
- Create your feature branch (
git checkout -b feature/improvement) - Commit your changes (
git commit -am 'Add rate limiting feature') - Push to the branch (
git push origin feature/improvement) - Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Keep your SendGrid API key secure
- Regularly monitor logs for abuse patterns
- Consider implementing additional validation for your specific needs
- Ensure proper file permissions on the rate limit file
- Rate Limit File Permissions
chmod 644 /tmp/csp_rate_limit.json- SendGrid Connection Issues
error_log("SendGrid Error: " . $e->getMessage());Check your PHP error logs for detailed error messages.
For issues, questions, or contributions, please open an issue on the GitHub repository.