mirror of
https://github.com/ivuorinen/monolog-gdpr-filter.git
synced 2026-02-09 02:48:20 +00:00
feat: performance, integrations, advanced features (#2)
* feat: performance, integrations, advanced features * chore: fix linting problems * chore: suppressions and linting * chore(lint): pre-commit linting, fixes * feat: comprehensive input validation, security hardening, and regression testing - Add extensive input validation throughout codebase with proper error handling - Implement comprehensive security hardening with ReDoS protection and bounds checking - Add 3 new regression test suites covering critical bugs, security, and validation scenarios - Enhance rate limiting with memory management and configurable cleanup intervals - Update configuration security settings and improve Laravel integration - Fix TODO.md timestamps to reflect actual development timeline - Strengthen static analysis configuration and improve code quality standards * feat: configure static analysis tools and enhance development workflow - Complete configuration of Psalm, PHPStan, and Rector for harmonious static analysis. - Fix invalid configurations and tool conflicts that prevented proper code quality analysis. - Add comprehensive safe analysis script with interactive workflow, backup/restore capabilities, and dry-run modes. Update documentation with linting policy requiring issue resolution over suppression. - Clean completed items from TODO to focus on actionable improvements. - All static analysis tools now work together seamlessly to provide code quality insights without breaking existing functionality. * fix(test): update Invalid regex pattern expectation * chore: phpstan, psalm fixes * chore: phpstan, psalm fixes, more tests * chore: tooling tweaks, cleanup * chore: tweaks to get the tests pass * fix(lint): rector config tweaks and successful run * feat: refactoring, more tests, fixes, cleanup * chore: deduplication, use constants * chore: psalm fixes * chore: ignore phpstan deliberate errors in tests * chore: improve codebase, deduplicate code * fix: lint * chore: deduplication, codebase simplification, sonarqube fixes * fix: resolve SonarQube reliability rating issues Fix useless object instantiation warnings in test files by assigning instantiated objects to variables. This resolves the SonarQube reliability rating issue (was C, now targeting A). Changes: - tests/Strategies/MaskingStrategiesTest.php: Fix 3 instances - tests/Strategies/FieldPathMaskingStrategyTest.php: Fix 1 instance The tests use expectException() to verify that constructors throw exceptions for invalid input. SonarQube flagged standalone `new` statements as useless. Fixed by assigning to variables with explicit unset() and fail() calls. All tests pass (623/623) and static analysis tools pass. * fix: resolve more SonarQube detected issues * fix: resolve psalm detected issues * fix: resolve more SonarQube detected issues * fix: resolve psalm detected issues * fix: duplications * fix: resolve SonarQube reliability rating issues * fix: resolve psalm and phpstan detected issues
This commit is contained in:
88
src/SecuritySanitizer.php
Normal file
88
src/SecuritySanitizer.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ivuorinen\MonologGdprFilter;
|
||||
|
||||
use Ivuorinen\MonologGdprFilter\MaskConstants as Mask;
|
||||
|
||||
/**
|
||||
* Sanitizes error messages to prevent information disclosure.
|
||||
*
|
||||
* This class removes sensitive information from error messages
|
||||
* before they are logged to prevent security vulnerabilities.
|
||||
*/
|
||||
final class SecuritySanitizer
|
||||
{
|
||||
/**
|
||||
* Sanitize error messages to prevent information disclosure.
|
||||
*
|
||||
* @param string $message The original error message
|
||||
* @return string The sanitized error message
|
||||
*/
|
||||
public static function sanitizeErrorMessage(string $message): string
|
||||
{
|
||||
// List of sensitive patterns to remove or mask
|
||||
$sensitivePatterns = [
|
||||
// Database credentials
|
||||
'/password=\S+/i' => 'password=***',
|
||||
'/pwd=\S+/i' => 'pwd=***',
|
||||
'/pass=\S+/i' => 'pass=***',
|
||||
|
||||
// Database hosts and connection strings
|
||||
'/host=[\w\.-]+/i' => 'host=***',
|
||||
'/server=[\w\.-]+/i' => 'server=***',
|
||||
'/hostname=[\w\.-]+/i' => 'hostname=***',
|
||||
|
||||
// User credentials
|
||||
'/user=\S+/i' => 'user=***',
|
||||
'/username=\S+/i' => 'username=***',
|
||||
'/uid=\S+/i' => 'uid=***',
|
||||
|
||||
// API keys and tokens
|
||||
'/api[_-]?key[=:]\s*\S+/i' => 'api_key=***',
|
||||
'/token[=:]\s*\S+/i' => 'token=***',
|
||||
'/bearer\s+\S+/i' => 'bearer ***',
|
||||
'/sk_\w+/i' => 'sk_***',
|
||||
'/pk_\w+/i' => 'pk_***',
|
||||
|
||||
// File paths (potential information disclosure)
|
||||
'/\/[\w\/\.-]*\/(config|secret|private|key)[\w\/\.-]*/i' => '/***/$1/***',
|
||||
'/[a-zA-Z]:\\\\[\w\\\\.-]*\\\\(config|secret|private|key)[\w\\\\.-]*/i' => 'C:\\***\\$1\\***',
|
||||
|
||||
// Connection strings
|
||||
'/redis:\/\/[^@]*@[\w\.-]+:\d+/i' => 'redis://***:***@***:***',
|
||||
'/mysql:\/\/[^@]*@[\w\.-]+:\d+/i' => 'mysql://***:***@***:***',
|
||||
'/postgresql:\/\/[^@]*@[\w\.-]+:\d+/i' => 'postgresql://***:***@***:***',
|
||||
|
||||
// JWT secrets and other secrets (enhanced to catch more patterns)
|
||||
'/secret[_-]?key[=:\s]+\S+/i' => 'secret_key=***',
|
||||
'/jwt[_-]?secret[=:\s]+\S+/i' => 'jwt_secret=***',
|
||||
'/\bsuper_secret_\w+/i' => Mask::MASK_SECRET,
|
||||
|
||||
// Generic secret-like patterns (alphanumeric keys that look sensitive)
|
||||
'/\b[a-z_]*secret[a-z_]*[=:\s]+[\w\d_-]{10,}/i' => 'secret=***',
|
||||
'/\b[a-z_]*key[a-z_]*[=:\s]+[\w\d_-]{10,}/i' => 'key=***',
|
||||
|
||||
// IP addresses in internal ranges
|
||||
'/\b(?:10\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.(?:1[6-9]|2\d|3[01])\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3})\b/' => '***.***.***',
|
||||
];
|
||||
|
||||
$sanitized = $message;
|
||||
|
||||
foreach ($sensitivePatterns as $pattern => $replacement) {
|
||||
$sanitized = preg_replace($pattern, $replacement, $sanitized) ?? $sanitized;
|
||||
}
|
||||
|
||||
// Truncate very long messages to prevent log flooding
|
||||
if (strlen($sanitized) > 500) {
|
||||
return substr($sanitized, 0, 500) . '... (truncated for security)';
|
||||
}
|
||||
|
||||
return $sanitized;
|
||||
}
|
||||
|
||||
/** @psalm-suppress UnusedConstructor */
|
||||
private function __construct()
|
||||
{}
|
||||
}
|
||||
Reference in New Issue
Block a user