CSP、セキュリティヘッダー、CSRF保護、セキュアクッキー、パスワード検証、入力サニタイズ、レート制限、SRI、シークレット読み込み、暗号化、セッションセキュリティ、TOTP二要素認証、署名付きURL、セキュリティイベントログを提供する包括的なPHPセキュリティライブラリです。
unsafe-*なし、HTTPS優先with*() API| モジュール | 説明 | 主要クラス |
|---|---|---|
| CSP | コンテンツセキュリティポリシーヘッダーの構築と違反レポート | CspDirectives, HeaderBuilder, NonceGenerator, CspReportParser |
| Headers | セキュリティヘッダー(HSTS、Permissions-Policyなど) | SecurityHeaders, SecurityHeadersBuilder |
| CSRF | クロスサイトリクエストフォージェリ保護 | CsrfProtection, CsrfConfig |
| Cookie | セキュアクッキー処理 | SecureCookie, CookieBuilder, CookieOptions |
| Encryption | XChaCha20-Poly1305認証付き暗号化 | SymmetricEncryptor, EnvelopeEncryptor, EncryptionKey, KeyRingEncryptor |
| Password | パスワード検証とハッシュ化 | PasswordPolicy, PwnedPasswordChecker, PepperedPasswordHasher |
| Sanitization | 入力サニタイズ(HTML、SQL、URI、パス)とファイルアップロード検証 | HtmlSanitizer, UriSanitizer, PathValidator, UploadValidator |
| RateLimiting | 複数のアルゴリズムによるレート制限 | DefaultRateLimiter, RateLimitConfig |
| SRI | サブリソース完全性ハッシュ生成 | SriHashGenerator, IntegrityAttribute |
| Secrets | Docker/ファイルベースのシークレット読み込み | SecretLoader, SecretValue, FileSecretSource |
| Session | セッション強化と固定化保護 | SessionGuard, SessionConfig, SessionConfigurator |
| SignedUrl | 有効期限必須のHMAC署名付きURL | UrlSigner, SigningKey |
| TOTP | 時間ベースのワンタイムパスワード(RFC 6238) | TotpAuthenticator, TotpSecret, ProvisioningUri, RecoveryCodeGenerator |
| Analyzer | セキュリティヘッダーの分析と監査 | SecurityHeaderAnalyzer, AnalysisResult |
| Scanner | CLIセキュリティヘッダースキャナー | ScanCommand, StreamHeaderFetcher |
| Middleware | ドロップイン統合のためのPSR-15ミドルウェア | SecurityHeadersMiddleware, CspMiddleware, CspReportHandler, CsrfMiddleware, DoubleSubmitCsrfMiddleware, RateLimitMiddleware, CorsMiddleware |
| 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());
// フォーム用トークンの生成
$token = $csrf->generateToken();
echo '<input type="hidden" name="_token" value="' . $token->value() . '">';
// 送信時の検証
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;
// HTMLのサニタイズ(危険なタグ/属性を削除)
$sanitizer = new HtmlSanitizer();
$safe = $sanitizer->sanitize($userInput);
// ファイルパスの検証(ディレクトリトラバーサル防止)
$validator = new PathValidator(new PathValidationConfig(basePath: '/var/www/uploads'));
if (!$validator->isSafe($userPath)) {
throw new Exception('Invalid path');
}
全モジュールの詳細な例についてはドキュメントを参照してください。
各モジュールには、クラスリファレンス、設定オプション、コード例を含む詳細なAPIドキュメントがあります: