Mitigation Strategy: Implement Strict Data Sanitization Before Logging with zap
- Mitigation Strategy:
zap
Data Sanitization - Description:
- Identify Sensitive Data for
zap
Logging: Developers must identify data considered sensitive that might be logged usingzap
. This includes passwords, API keys, PII, financial data, etc. - Utilize
zap
's Structured Logging for Sanitization: Leveragezap
's structured logging capabilities (fields) to control exactly what data is logged. Instead of logging entire objects or raw strings, log specific, sanitized fields. - Sanitize Data Before Passing to
zap
Fields: Before adding data as azap
field (e.g., usingzap.String
,zap.Int
,zap.Any
), apply sanitization functions. This might involve redaction, masking, hashing, or filtering sensitive parts of the data. - Example with
zap
: Instead oflogger.Info("User details", zap.Any("user", userObject))
, use:logger.Info("User details", zap.Int("user_id", userObject.ID), zap.String("username", sanitizeUsername(userObject.Username)), // Sanitize username // Omit or redact sensitive fields like password or email )
- Code Reviews Focused on
zap
Usage: Conduct code reviews specifically to ensure developers are correctly usingzap
's structured logging and applying sanitization before logging sensitive data throughzap
.
- Identify Sensitive Data for
- Threats Mitigated:
- Information Disclosure (High Severity): Accidental logging of sensitive data through
zap
can lead to unauthorized access if logs are compromised.
- Information Disclosure (High Severity): Accidental logging of sensitive data through
- Impact:
- Information Disclosure (High Reduction): Significantly reduces information disclosure by controlling data logged via
zap
and sanitizing it beforehand.
- Information Disclosure (High Reduction): Significantly reduces information disclosure by controlling data logged via
- Currently Implemented:
- Hypothetical Project - Partially implemented in the
user
module where password fields are redacted before being passed tozap.String
for logging.
- Hypothetical Project - Partially implemented in the
- Missing Implementation:
- Sanitization before
zap
logging is not consistently applied across all modules, especially when usingzap.Any
or constructing complex log messages. Needs to be enforced whereverzap
is used to log potentially sensitive data.
- Sanitization before
Mitigation Strategy: Control zap
Log Levels in Production Environments
- Mitigation Strategy: Production
zap
Log Level Configuration - Description:
- Define Production
zap
Log Level Policy: Establish a policy to restrictzap
log levels in production toInfo
,Warn
,Error
, andFatal
. AvoidDebug
andVerbose
levels in productionzap
loggers. - Configure
zap
Logger Level for Production: Configurezap
logger instances used in production to enforce this policy. This is typically done duringzap
logger initialization using configuration options or environment variables. - Example
zap
Configuration:cfg := zap.NewProductionConfig() // Ensure Level is set to InfoLevel or higher for production cfg.Level = zap.NewAtomicLevelAt(zap.InfoLevel) logger, _ := cfg.Build()
- Dynamic
zap
Log Level Adjustment (Optional): Implement a mechanism to dynamically adjustzap
log levels in production without restarts, if needed. This could involve a configuration server or secure API to modify thezap
logger's atomic level. - Regular Audits of
zap
Level Configuration: Periodically audit thezap
logger level configuration in production to ensure it adheres to the defined policy and hasn't been inadvertently set to a more verbose level.
- Define Production
- Threats Mitigated:
- Information Disclosure (Medium Severity): Verbose
zap
logging levels in production can unintentionally log more details, increasing sensitive data exposure. - Performance Degradation (Low Severity): Excessive
zap
logging at verbose levels can consume resources, impacting performance.
- Information Disclosure (Medium Severity): Verbose
- Impact:
- Information Disclosure (Medium Reduction): Reduces accidental information disclosure by limiting
zap
's verbosity in production. - Performance Degradation (Low Reduction): Minimizes performance impact from excessive
zap
logging.
- Information Disclosure (Medium Reduction): Reduces accidental information disclosure by limiting
- Currently Implemented:
- Hypothetical Project - Implemented in production deployments by setting the
zap
logger level toInfoLevel
via environment variables during logger initialization.
- Hypothetical Project - Implemented in production deployments by setting the
- Missing Implementation:
- Dynamic adjustment of
zap
log levels is not implemented. Consider adding dynamic control for faster issue response.
- Dynamic adjustment of
Mitigation Strategy: Encode zap
Log Messages to Prevent Interpretation as Code
- Mitigation Strategy:
zap
Log Message Encoding - Description:
- Prioritize
zap
Structured Logging (Fields): Primarily usezap
's field-based logging (zap.String
,zap.Int
, etc.). This inherently treats data as data, reducing injection risks compared to raw string messages. - Contextual Encoding for
zap
String Messages (If Necessary): If you must construct string log messages withzap
, especially with external data, apply encoding appropriate for the log output format configured inzap
.- JSON Encoding (Common for
zap
): Ifzap
outputs JSON, ensure any user-provided strings in log messages are JSON-encoded to escape special characters. - Example with
zap
and JSON Encoding (manual, butzap
encoders handle this):userInput := `malicious"data` // Example potentially malicious input encodedInput := jsonEncodeString(userInput) // Hypothetical JSON encoding function logger.Info("User input received", zap.String("raw_input", encodedInput))
- JSON Encoding (Common for
- Avoid Code Execution from
zap
Logs: Ensure downstream log processing systems do not interpretzap
log messages as executable code. Treatzap
log data as data.
- Prioritize
- Threats Mitigated:
- Log Injection (Medium Severity): Attackers could inject malicious code into
zap
log messages if not properly encoded and processed as code later. Less direct threat withzap
's structured logging, but relevant for string messages.
- Log Injection (Medium Severity): Attackers could inject malicious code into
- Impact:
- Log Injection (Medium Reduction): Reduces log injection risk by favoring
zap
structured logging and encoding string messages when needed.
- Log Injection (Medium Reduction): Reduces log injection risk by favoring
- Currently Implemented:
- Hypothetical Project - Primarily uses
zap
's structured logging. String message construction is minimized.zap
's JSON encoder handles basic encoding.
- Hypothetical Project - Primarily uses
- Missing Implementation:
- Explicit, consistent JSON encoding for string messages containing external data is not enforced in all modules where string messages are used with
zap
. Standardize encoding for string messages used withzap
.
- Explicit, consistent JSON encoding for string messages containing external data is not enforced in all modules where string messages are used with
Mitigation Strategy: Validate and Sanitize Input Data Before zap
Logging (Injection Focus)
- Mitigation Strategy: Input Validation & Sanitization for
zap
Logging - Description:
- Identify Input Sources Logged by
zap
: Pinpoint all external input sources that are logged usingzap
, such as user requests, API calls, etc. - Validate Input Data Before
zap
Logging: Validate all input data before it's passed tozap
for logging. This includes data type, format, range, and allowlist validation. - Sanitize Input for
zap
Logging (Injection Prevention): Sanitize input data specifically to prevent log injection attacks before logging withzap
. This involves escaping special characters relevant to log processing systems. - Log Validated and Sanitized Data with
zap
: Only log the validated and sanitized version of input data usingzap
. - Example with
zap
and Sanitization:userInput := getUntrustedInput() if isValidInput(userInput) { sanitizedInput := sanitizeForLogs(userInput) // Sanitize for logging logger.Info("User input", zap.String("input", sanitizedInput)) } else { logger.Warn("Invalid user input received") }
- Identify Input Sources Logged by
- Threats Mitigated:
- Log Injection (Medium Severity): Prevents attackers from injecting malicious payloads into logs via user input logged by
zap
, which could be exploited in downstream processing.
- Log Injection (Medium Severity): Prevents attackers from injecting malicious payloads into logs via user input logged by
- Impact:
- Log Injection (Medium Reduction): Reduces log injection risk by ensuring data logged by
zap
is validated and sanitized.
- Log Injection (Medium Reduction): Reduces log injection risk by ensuring data logged by
- Currently Implemented:
- Hypothetical Project - Input validation exists for application logic, but specific sanitization for log injection before
zap
logging is inconsistent.
- Hypothetical Project - Input validation exists for application logic, but specific sanitization for log injection before
- Missing Implementation:
- Dedicated input sanitization for log injection prevention needs to be consistently applied before all
zap
logging points that handle external input. Integrate this into the input validation process used withzap
.
- Dedicated input sanitization for log injection prevention needs to be consistently applied before all
Mitigation Strategy: Secure zap
Configuration Management
- Mitigation Strategy: Secure
zap
Configuration - Description:
- Externalize
zap
Configuration: Externalizezap
configuration from application code. Use configuration files, environment variables, or configuration management systems to managezap
settings. - Secure Storage for
zap
Configuration: Storezap
configuration securely. Avoid hardcoding sensitive information (like API keys for log aggregation services used byzap
's outputs) directly in configuration. Use secure storage like environment variables or secrets management systems. - Example
zap
Configuration with Environment Variables:cfg := zap.NewProductionConfig() apiKey := os.Getenv("LOG_AGGREGATION_API_KEY") // Get API key from env cfg.OutputPaths = []string{"stdout", fmt.Sprintf("https://log-aggregator.example.com?apiKey=%s", apiKey)} // Use API key logger, _ := cfg.Build()
- Restrict Access to
zap
Configuration: Limit access tozap
configuration files and systems to authorized personnel. Use access control to prevent unauthorized modification ofzap
settings. - Audit
zap
Configuration Changes: Audit changes tozap
configuration to track modifications and identify unauthorized changes.
- Externalize
- Threats Mitigated:
- Information Disclosure (Medium Severity): If
zap
configuration contains sensitive data (e.g., API keys) and is insecure, it could be exposed. - Configuration Tampering (Medium Severity): Unauthorized modification of
zap
configuration could disable logging, redirect logs insecurely, or make other security-relevant changes.
- Information Disclosure (Medium Severity): If
- Impact:
- Information Disclosure (Medium Reduction): Reduces information disclosure by securely managing sensitive data in
zap
configuration. - Configuration Tampering (Medium Reduction): Protects
zap
configuration integrity by controlling access and auditing changes.
- Information Disclosure (Medium Reduction): Reduces information disclosure by securely managing sensitive data in
- Currently Implemented:
- Hypothetical Project -
zap
configuration is partially externalized using environment variables for log level and output paths.
- Hypothetical Project -
- Missing Implementation:
- Sensitive configuration values for
zap
(like API keys) are sometimes hardcoded. Migrate all sensitivezap
configuration to a secrets management system. Implement auditing ofzap
configuration changes.
- Sensitive configuration values for