# Improper Output Neutralization for Logs (CWE-117) The product does not neutralize or incorrectly neutralizes output that is written to logs. **Stack:** Go - Prevalence: 中 覆盖 3 种语言 - Impact: 中 建议审查 - Prevention: 已记录 4 个修复示例 **OWASP:** Injection (A03:2021-Injection) - #3 ## Description Log injection attacks occur when user input is written to log files without proper sanitization. This can allow attackers to forge log entries, inject malicious content, or exploit log analysis tools. ## Prevention 基于 1 条 Shoulder 检测规则的 Log Injection 预防策略。 ### Go Strip newlines and control characters from user input before logging ## Warning Signs - [MEDIUM] unsanitized user input flowing into log statements, enabling log forging attacks ## Consequences - 修改应用程序数据 - 隐藏活动 - 执行未授权代码 ## Mitigations - 在写入日志前,对所有输入进行验证和净化 - 使用将数据与日志语法分离的结构化日志格式 - 将用户可控数据写入日志时,对特殊字符进行编码 ## Detection - Total rules: 4 - Languages: go, javascript, typescript, python ## Rules by Language ### Go (1 rules) - **Log Injection / Log Forging** [MEDIUM]: Detects unsanitized user input flowing into log statements, enabling log forging attacks. - Remediation: Remove newlines and control characters from user input before logging. ```go sanitized := strings.ReplaceAll(userInput, "\n", "") sanitized = strings.ReplaceAll(sanitized, "\r", "") log.Printf("User action: %s", sanitized) ``` Learn more: https://shoulder.dev/learn/go/cwe-117/log-injection