
PHP 8.4+ security library (mirror)
Comprehensive PHP security library providing CSP, Security Headers, CSRF protection, Secure Cookies, Password Validation, Input Sanitization, Rate Limiting, SRI, Secrets Loading, Encryption, Session Security, TOTP two-factor authentication, Signed URLs, and Security Event Logging.
unsafe-*, HTTPS-firstwith*() API| Module | Description | Key Classes |
|---|---|---|
| CSP | Content Security Policy header building and violation reporting | CspDirectives, HeaderBuilder, NonceGenerator, CspReportParser |
| Headers | Security headers (HSTS, Permissions-Policy, etc.) | SecurityHeaders, SecurityHeadersBuilder |
| CSRF | Cross-Site Request Forgery protection | CsrfProtection, CsrfConfig |
| Cookie | Secure cookie handling | SecureCookie, CookieBuilder, CookieOptions |
| Encryption | XChaCha20-Poly1305 authenticated encryption | SymmetricEncryptor, EnvelopeEncryptor, EncryptionKey, KeyRingEncryptor |
| Password | Password validation and hashing | PasswordPolicy, PwnedPasswordChecker, PepperedPasswordHasher |
| Sanitization | Input sanitization (HTML, SQL, URI, Path) and file upload validation | HtmlSanitizer, UriSanitizer, PathValidator, UploadValidator |
| RateLimiting | Rate limiting with multiple algorithms | DefaultRateLimiter, RateLimitConfig |
| SRI | Subresource Integrity hash generation | SriHashGenerator, IntegrityAttribute |
| Secrets | Docker/file-based secret loading | SecretLoader, SecretValue, FileSecretSource |
| Session | Session hardening and fixation protection | SessionGuard, SessionConfig, SessionConfigurator |
| SignedUrl | HMAC-signed URLs with mandatory expiry | UrlSigner, SigningKey |
| TOTP | Time-based one-time passwords (RFC 6238) | TotpAuthenticator, TotpSecret, ProvisioningUri, RecoveryCodeGenerator |
| Analyzer | Security header analysis and auditing | SecurityHeaderAnalyzer, AnalysisResult |
| Scanner | CLI security header scanner | ScanCommand, StreamHeaderFetcher |
| Middleware | PSR-15 middleware for drop-in framework integration | SecurityHeadersMiddleware, CspMiddleware, CspReportHandler, CsrfMiddleware, DoubleSubmitCsrfMiddleware, RateLimitMiddleware, CorsMiddleware |
| Logging | Security event logging | SecurityAuditLogger, SecurityEvent |
ext-domext-libxmlext-sodiumcomposer require zappzarapp/security
use Zappzarapp\Security\Headers\Builder\SecurityHeadersBuilder;
$headers = SecurityHeadersBuilder::recommended()->build();
foreach ($headers as $name => $value) {
header("{$name}: {$value}");
}
use Zappzarapp\Security\Csp\HeaderBuilder;
use Zappzarapp\Security\Csp\Directive\CspDirectives;
use Zappzarapp\Security\Csp\Nonce\NonceGenerator;
$generator = new NonceGenerator();
$csp = HeaderBuilder::build(CspDirectives::strict(), $generator);
header("Content-Security-Policy: {$csp}");
$nonce = $generator->get();
echo "<script nonce=\"{$nonce}\">console.log('Safe!');</script>";
use Zappzarapp\Security\Csrf\CsrfProtection;
use Zappzarapp\Security\Csrf\Storage\SessionCsrfStorage;
$csrf = new CsrfProtection(new SessionCsrfStorage());
// Generate token for form
$token = $csrf->generateToken();
echo '<input type="hidden" name="_token" value="' . $token->value() . '">';
// Validate on submission
if (!$csrf->validateToken($_POST['_token'])) {
throw new Exception('CSRF validation failed');
}
use Zappzarapp\Security\Sanitization\Html\HtmlSanitizer;
use Zappzarapp\Security\Sanitization\Path\PathValidationConfig;
use Zappzarapp\Security\Sanitization\Path\PathValidator;
// Sanitize HTML (removes dangerous tags/attributes)
$sanitizer = new HtmlSanitizer();
$safe = $sanitizer->sanitize($userInput);