M8: Security-Headers, Rate-Limits und Audit-Log
Erste drei Bausteine der Haertung: - Security-Headers (X-Content-Type-Options, X-Frame-Options, Referrer- Policy, Permissions-Policy, HSTS bei HTTPS) laufen automatisch ueber app_send_security_headers() am Ende von app/bootstrap.php fuer jede dynamische Seite; landing.php war als einzige Seite ganz ohne PHP und bekam einen minimalen Bootstrap-Aufruf. Bewusst kein CSP, da die bestehenden Templates durchgaengig auf Inline-style-Attribute setzen. - DB-gestuetzte Rate-Limits (neue Tabelle rate_limit_attempts) fuer Login (10/15min je E-Mail, 20/15min je IP), Registrierung (5/h je IP) und Passwort-Reset-Anfrage (5/h je E-Mail, 10/h je IP); bei ausgereiztem Reset-Limit erscheint dieselbe generische Meldung wie im Erfolgsfall, um kein Konto-Enumeration-Signal zu geben. - Zentrales Audit-Log (neue Tabelle audit_log) fuer Mitgliederverwaltung, Zugangsvergabe/-entzug, Storno, Mandant-Einstellungen, Hinweise, CSV-Import, Jahresbonus-Verteilung und Live-Mailversand; sichtbar fuer Owner/Admin auf mandant-einstellungen.php. Live getestet: Rate-Limit greift nach 10 Fehlversuchen, Audit-Log-Eintrag mit korrekten Metadaten und Nutzernamen ueber einen isolierten Test- Mandanten geprueft. Alle Regressionstests weiterhin gruen (26/26 Smoke, 104 Golden-Master-Assertions). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/bootstrap.php';
|
||||
|
||||
/**
|
||||
* Records one admin/security-relevant action. Keep $action as a short,
|
||||
* stable slug (e.g. "participant.access_granted") so entries stay
|
||||
* filterable; put anything variable in $metadata.
|
||||
*/
|
||||
function app_audit_log(
|
||||
PDO $pdo,
|
||||
?int $tenantId,
|
||||
?int $actorUserId,
|
||||
string $action,
|
||||
string $subjectType,
|
||||
?int $subjectId,
|
||||
array $metadata = []
|
||||
): void {
|
||||
$stmt = $pdo->prepare(
|
||||
'INSERT INTO audit_log (tenant_id, actor_user_id, action, subject_type, subject_id, metadata_json, ip)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)'
|
||||
);
|
||||
$stmt->execute([
|
||||
$tenantId,
|
||||
$actorUserId,
|
||||
$action,
|
||||
$subjectType,
|
||||
$subjectId,
|
||||
$metadata !== [] ? json_encode($metadata, JSON_UNESCAPED_UNICODE) : null,
|
||||
$_SERVER['REMOTE_ADDR'] ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{id: int, actor_user_id: ?int, actor_name: ?string, action: string, subject_type: string, subject_id: ?int, metadata_json: ?string, ip: ?string, created_at: string}>
|
||||
*/
|
||||
function app_fetch_audit_log(PDO $pdo, int $tenantId, int $limit = 100): array
|
||||
{
|
||||
$limit = max(1, min($limit, 500));
|
||||
$stmt = $pdo->prepare(
|
||||
"SELECT a.id, a.actor_user_id, u.display_name AS actor_name, a.action, a.subject_type, a.subject_id, a.metadata_json, a.ip, a.created_at
|
||||
FROM audit_log a
|
||||
LEFT JOIN users u ON u.id = a.actor_user_id
|
||||
WHERE a.tenant_id = ?
|
||||
ORDER BY a.created_at DESC, a.id DESC
|
||||
LIMIT {$limit}"
|
||||
);
|
||||
$stmt->execute([$tenantId]);
|
||||
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
@@ -27,6 +27,29 @@ function app_is_https(): bool
|
||||
|| (($_SERVER['SERVER_PORT'] ?? null) === '443');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends baseline security headers on every dynamic request. Deliberately
|
||||
* does not set a Content-Security-Policy: the existing templates rely on
|
||||
* inline style="" attributes throughout (banners, table cells, etc.), and
|
||||
* locking that down would need a broader template pass. Runs once per
|
||||
* request via the auto-invocation at the bottom of this file.
|
||||
*/
|
||||
function app_send_security_headers(): void
|
||||
{
|
||||
if (PHP_SAPI === 'cli' || headers_sent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
header('X-Frame-Options: DENY');
|
||||
header('Referrer-Policy: strict-origin-when-cross-origin');
|
||||
header('Permissions-Policy: geolocation=(), microphone=(), camera=()');
|
||||
|
||||
if (app_is_https()) {
|
||||
header('Strict-Transport-Security: max-age=31536000; includeSubDomains');
|
||||
}
|
||||
}
|
||||
|
||||
function app_start_session(): void
|
||||
{
|
||||
if (PHP_SAPI === 'cli' || session_status() === PHP_SESSION_ACTIVE) {
|
||||
@@ -102,3 +125,5 @@ function app_require_csrf(): void
|
||||
die('CSRF validation failed.');
|
||||
}
|
||||
}
|
||||
|
||||
app_send_security_headers();
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/bootstrap.php';
|
||||
|
||||
function app_client_ip(): string
|
||||
{
|
||||
return (string)($_SERVER['REMOTE_ADDR'] ?? 'unknown');
|
||||
}
|
||||
|
||||
/**
|
||||
* Records an attempt in the given bucket and reports whether the caller is
|
||||
* still within the allowed rate. Call before doing the sensitive work (auth
|
||||
* check, mail dispatch, account creation); if this returns false, show a
|
||||
* generic "too many attempts" message without processing the request, so a
|
||||
* bucket also can't be used to enumerate valid accounts by timing.
|
||||
*/
|
||||
function app_rate_limit_check(PDO $pdo, string $bucket, int $maxAttempts, int $windowSeconds): bool
|
||||
{
|
||||
$pdo->prepare('INSERT INTO rate_limit_attempts (bucket) VALUES (?)')->execute([$bucket]);
|
||||
|
||||
// Opportunistic cleanup so the table doesn't grow unbounded; scoped to
|
||||
// this bucket to keep each call cheap.
|
||||
$pdo->prepare('DELETE FROM rate_limit_attempts WHERE bucket = ? AND created_at < DATE_SUB(NOW(), INTERVAL ? SECOND)')
|
||||
->execute([$bucket, $windowSeconds]);
|
||||
|
||||
$stmt = $pdo->prepare(
|
||||
'SELECT COUNT(*) FROM rate_limit_attempts WHERE bucket = ? AND created_at >= DATE_SUB(NOW(), INTERVAL ? SECOND)'
|
||||
);
|
||||
$stmt->execute([$bucket, $windowSeconds]);
|
||||
|
||||
return (int)$stmt->fetchColumn() <= $maxAttempts;
|
||||
}
|
||||
Reference in New Issue
Block a user