Mitigation Strategy: Sensitive Data Masking/Sanitization (via Monolog Processors)
-
Description:
- Identify Sensitive Data: Create a comprehensive list of all sensitive data types.
- Choose a Masking Approach: Select redaction, partial masking, hashing, or tokenization.
- Implement Monolog Processors: This is the core Monolog-specific step.
- Custom Processor (Recommended): Create a PHP class implementing
Monolog\Processor\ProcessorInterface
. The__invoke
method receives the log record ($record
). Within this method:- Iterate through
$record['context']
. - Check keys against your sensitive data list.
- Apply masking to matching values.
- Modify
$record['message']
using regex or string replacements (be very careful with regex). - Return the modified
$record
.
- Iterate through
ReplaceProcessor
(Less Flexible): UseMonolog\Processor\ReplaceProcessor
with regex/replacement pairs for predictable patterns. Less robust than a custom processor.PsrLogMessageProcessor
(Supplementary): Use alongside a custom orReplaceProcessor
to handle placeholders in the message string.
- Custom Processor (Recommended): Create a PHP class implementing
- Register the Processor: Add the processor to your Monolog handler configuration in your application's logging setup.
- Testing: Create unit tests specifically for the masking processor, covering various sensitive data patterns and edge cases.
-
Threats Mitigated:
- Data Breach (Severity: Critical): Reduces exposure of sensitive data in compromised logs.
- Compliance Violations (Severity: High): Helps meet data protection regulations.
- Reputational Damage (Severity: High): Minimizes negative impact from data breaches.
- Insider Threats (Severity: Medium): Reduces risk from malicious/negligent employees accessing logs.
-
Impact:
- Data Breach: Risk significantly reduced (effectiveness depends on implementation thoroughness).
- Compliance Violations: Significantly reduces non-compliance risk.
- Reputational Damage: Reduces likelihood and severity.
- Insider Threats: Reduces risk, but doesn't eliminate it.
-
Currently Implemented:
- Example: Partially implemented. A basic
ReplaceProcessor
is used insrc/Logging/LogManager.php
.
- Example: Partially implemented. A basic
-
Missing Implementation:
- No comprehensive custom processor.
- No unit tests specifically for data masking.
- No documented list of sensitive data types.
Mitigation Strategy: Log Injection Prevention (via Monolog Usage and Configuration)
-
Description:
- Structured Logging: Always use the
$context
array in Monolog calls. Never embed user-supplied data directly into the message string.// BAD: $logger->info("User " . $userInput . " logged in."); // GOOD: $logger->info("User logged in.", ['username' => $userInput]);
- Message Size Limits (Handler Configuration): Configure the Monolog handler to limit the maximum size of log messages. This is often a handler-specific setting (e.g.,
RotatingFileHandler
's options). This is a direct Monolog configuration. - Escape special characters (within a custom Formatter, if necessary): If you absolutely must include potentially unsafe data, and you're using a custom formatter, escape special characters within the formatter's
format
method. This is a last resort and should be avoided if possible.
- Structured Logging: Always use the
-
Threats Mitigated:
- Log Forging (Severity: Medium): Prevents injection of fake log entries.
- Cross-Site Scripting (XSS) (Severity: High): Prevents XSS if logs are viewed in a web interface (requires proper output encoding in the viewer, but using context helps).
- Command Injection (Severity: Critical): (Rare) Prevents command injection if logs are misused.
- Denial of Service (DoS) (Severity: Medium): Prevents overwhelming the system with huge log entries.
-
Impact:
- Log Forging: Risk significantly reduced.
- XSS: Risk reduced (relies on secure log viewing).
- Command Injection: Risk significantly reduced.
- DoS: Risk reduced (part of a broader DoS strategy).
-
Currently Implemented:
- The application mostly uses the
$context
array.
- The application mostly uses the
-
Missing Implementation:
- No specific message size limits are configured in Monolog handlers.
- Some older code might embed user input directly (needs audit).
Mitigation Strategy: Handler-Specific Security Configuration (Direct Monolog Configuration)
-
Description:
- Handler Inventory: List all Monolog handlers used.
- Security Review: For each handler:
- Review Monolog's documentation for security options.
- Identify potential risks.
- Implement recommended security configurations directly within the handler's setup.
- Example:
SwiftMailerHandler
:- Ensure TLS/SSL is enabled in the handler's configuration.
- Use strong authentication configured within the handler.
- Example: Network Handlers (
SyslogUdpHandler
,SocketHandler
):- Use TLS/SSL if possible (configured in the handler).
- Avoid Risky Handlers: Avoid handlers sending logs to insecure destinations.
-
Threats Mitigated:
- Handler-Specific Vulnerabilities (Severity: Variable): Addresses vulnerabilities in specific handlers.
- Data Interception (Severity: High): (For network handlers) Prevents interception in transit.
- Unauthorized Access (Severity: High): (For network handlers) Prevents unauthorized access.
-
Impact:
- Handler-Specific Vulnerabilities: Risk reduced (depends on handler and configuration).
- Data Interception: Risk significantly reduced with TLS/SSL.
- Unauthorized Access: Risk significantly reduced.
-
Currently Implemented:
- The application uses
StreamHandler
,RotatingFileHandler
, andSwiftMailerHandler
.
- The application uses
-
Missing Implementation:
- Comprehensive review of all handler configurations hasn't been done.
- TLS/SSL for
SwiftMailerHandler
needs verification and explicit configuration within the Monolog setup.
Mitigation Strategy: Least Privilege Principle with Formatter (Custom Monolog Formatter)
-
Description:
- Identify Necessary Information: Determine the minimum information needed in logs.
- Choose/Create Formatter:
- Customize
LineFormatter
: Use$format
and$dateFormat
to include only necessary fields. Remove unnecessary ones (e.g., full stack traces). - Custom Formatter (Recommended): Create a class implementing
Monolog\Formatter\FormatterInterface
. Theformat
method receives the$record
and returns a formatted string. This provides full control.
- Customize
- Configure Handler: Assign the formatter to the Monolog handler in your application's logging configuration.
-
Threats Mitigated:
- Information Disclosure (Severity: Medium): Reduces information exposed in logs.
- Data Breach (Severity: Critical): Reduces the impact of a breach.
-
Impact:
- Information Disclosure: Risk reduced.
- Data Breach: Reduces scope and severity.
-
Currently Implemented:
- The application uses the default
LineFormatter
.
- The application uses the default
-
Missing Implementation:
- No custom formatters.
- Default
LineFormatter
likely includes too much information. Needs review and customization.