Skip to content

Latest commit

 

History

History
129 lines (118 loc) · 11.7 KB

File metadata and controls

129 lines (118 loc) · 11.7 KB

Mitigation Strategies Analysis for uber-go/zap

  • Mitigation Strategy: zap Data Sanitization
  • Description:
    1. Identify Sensitive Data for zap Logging: Developers must identify data considered sensitive that might be logged using zap. This includes passwords, API keys, PII, financial data, etc.
    2. Utilize zap's Structured Logging for Sanitization: Leverage zap's structured logging capabilities (fields) to control exactly what data is logged. Instead of logging entire objects or raw strings, log specific, sanitized fields.
    3. Sanitize Data Before Passing to zap Fields: Before adding data as a zap field (e.g., using zap.String, zap.Int, zap.Any), apply sanitization functions. This might involve redaction, masking, hashing, or filtering sensitive parts of the data.
    4. Example with zap: Instead of logger.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
      )
    5. Code Reviews Focused on zap Usage: Conduct code reviews specifically to ensure developers are correctly using zap's structured logging and applying sanitization before logging sensitive data through zap.
  • Threats Mitigated:
    • Information Disclosure (High Severity): Accidental logging of sensitive data through zap can lead to unauthorized access if logs are compromised.
  • Impact:
    • Information Disclosure (High Reduction): Significantly reduces information disclosure by controlling data logged via zap and sanitizing it beforehand.
  • Currently Implemented:
    • Hypothetical Project - Partially implemented in the user module where password fields are redacted before being passed to zap.String for logging.
  • Missing Implementation:
    • Sanitization before zap logging is not consistently applied across all modules, especially when using zap.Any or constructing complex log messages. Needs to be enforced wherever zap is used to log potentially sensitive data.
  • Mitigation Strategy: Production zap Log Level Configuration
  • Description:
    1. Define Production zap Log Level Policy: Establish a policy to restrict zap log levels in production to Info, Warn, Error, and Fatal. Avoid Debug and Verbose levels in production zap loggers.
    2. Configure zap Logger Level for Production: Configure zap logger instances used in production to enforce this policy. This is typically done during zap logger initialization using configuration options or environment variables.
    3. 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()
    4. Dynamic zap Log Level Adjustment (Optional): Implement a mechanism to dynamically adjust zap log levels in production without restarts, if needed. This could involve a configuration server or secure API to modify the zap logger's atomic level.
    5. Regular Audits of zap Level Configuration: Periodically audit the zap logger level configuration in production to ensure it adheres to the defined policy and hasn't been inadvertently set to a more verbose level.
  • 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.
  • 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.
  • Currently Implemented:
    • Hypothetical Project - Implemented in production deployments by setting the zap logger level to InfoLevel via environment variables during logger initialization.
  • Missing Implementation:
    • Dynamic adjustment of zap log levels is not implemented. Consider adding dynamic control for faster issue response.
  • Mitigation Strategy: zap Log Message Encoding
  • Description:
    1. Prioritize zap Structured Logging (Fields): Primarily use zap's field-based logging (zap.String, zap.Int, etc.). This inherently treats data as data, reducing injection risks compared to raw string messages.
    2. Contextual Encoding for zap String Messages (If Necessary): If you must construct string log messages with zap, especially with external data, apply encoding appropriate for the log output format configured in zap.
      • JSON Encoding (Common for zap): If zap outputs JSON, ensure any user-provided strings in log messages are JSON-encoded to escape special characters.
      • Example with zap and JSON Encoding (manual, but zap 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))
    3. Avoid Code Execution from zap Logs: Ensure downstream log processing systems do not interpret zap log messages as executable code. Treat zap log data as data.
  • 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 with zap's structured logging, but relevant for string messages.
  • Impact:
    • Log Injection (Medium Reduction): Reduces log injection risk by favoring zap structured logging and encoding string messages when needed.
  • Currently Implemented:
    • Hypothetical Project - Primarily uses zap's structured logging. String message construction is minimized. zap's JSON encoder handles basic encoding.
  • 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 with zap.
  • Mitigation Strategy: Input Validation & Sanitization for zap Logging
  • Description:
    1. Identify Input Sources Logged by zap: Pinpoint all external input sources that are logged using zap, such as user requests, API calls, etc.
    2. Validate Input Data Before zap Logging: Validate all input data before it's passed to zap for logging. This includes data type, format, range, and allowlist validation.
    3. Sanitize Input for zap Logging (Injection Prevention): Sanitize input data specifically to prevent log injection attacks before logging with zap. This involves escaping special characters relevant to log processing systems.
    4. Log Validated and Sanitized Data with zap: Only log the validated and sanitized version of input data using zap.
    5. 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")
      }
  • 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.
  • Impact:
    • Log Injection (Medium Reduction): Reduces log injection risk by ensuring data logged by zap is validated and sanitized.
  • Currently Implemented:
    • Hypothetical Project - Input validation exists for application logic, but specific sanitization for log injection before zap logging is inconsistent.
  • 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 with zap.
  • Mitigation Strategy: Secure zap Configuration
  • Description:
    1. Externalize zap Configuration: Externalize zap configuration from application code. Use configuration files, environment variables, or configuration management systems to manage zap settings.
    2. Secure Storage for zap Configuration: Store zap configuration securely. Avoid hardcoding sensitive information (like API keys for log aggregation services used by zap's outputs) directly in configuration. Use secure storage like environment variables or secrets management systems.
    3. 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()
    4. Restrict Access to zap Configuration: Limit access to zap configuration files and systems to authorized personnel. Use access control to prevent unauthorized modification of zap settings.
    5. Audit zap Configuration Changes: Audit changes to zap configuration to track modifications and identify unauthorized changes.
  • 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.
  • 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.
  • Currently Implemented:
    • Hypothetical Project - zap configuration is partially externalized using environment variables for log level and output paths.
  • Missing Implementation:
    • Sensitive configuration values for zap (like API keys) are sometimes hardcoded. Migrate all sensitive zap configuration to a secrets management system. Implement auditing of zap configuration changes.