测试版 Shoulder 目前处于测试阶段 — 结果有时可能不正确。您的反馈塑造我们接下来要修复的内容。 分享反馈
📝

Improper Output Neutralization for Logs

🛡️ 4 条规则检测到此问题

Improper Output Neutralization for Logs

The product does not neutralize or incorrectly neutralizes output that is written to logs.

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.

普遍性
覆盖 3 种语言
影响
建议审查
预防
已记录
4 个修复示例
2 预防
2 预防

如何修复此漏洞

基于 4 条 Shoulder 检测规则的 Log Injection 预防策略。

Log Injection / Log Forging MEDIUM

Strip newlines and control characters from user input before logging

+13 -6 go
  package main
  
  import (
      "log"
      "net/http"
- )
- 
- func handler(w http.ResponseWriter, r *http.Request) {
-     username := r.URL.Query().Get("user")
-     // Vulnerable: user input logged directly
-     log.Printf("Login attempt for user: %s", username)
+     "strings"
+ )
+ 
+ func sanitizeLogInput(s string) string {
+     s = strings.ReplaceAll(s, "\n", "")
+     s = strings.ReplaceAll(s, "\r", "")
+     return s
+ }
+ 
+ func handler(w http.ResponseWriter, r *http.Request) {
+     username := r.URL.Query().Get("user")
+     // Safe: newlines stripped before logging
+     log.Printf("Login attempt for user: %s", sanitizeLogInput(username))
  }
  
Log Injection LOW

Strip newline characters from user input before writing to log files

+1 -1 javascript
  const express = require('express');
  const winston = require('winston');
  const app = express();
  
  app.post('/login', (req, res) => {
-   const username = req.body.username;
+   const username = req.body.username.replace(/[\r\n]/g, '');
    winston.info(`Login attempt: ${username}`);
    res.json({ status: 'ok' });
  });
  
Log Injection MEDIUM

Sanitize user input by stripping CRLF characters before writing to logs

+4 -2 javascript
- app.post('/login', (req, res) => {
-   logger.info(`Login attempt from: ${req.body.username}`);
+ const sanitize = (str) => str.replace(/[\r\n]/g, '').substring(0, 200);
+ 
+ app.post('/login', (req, res) => {
+   logger.info('Login attempt', { username: sanitize(req.body.username) });
  });
  
Log Injection / Log Forging MEDIUM

Use structured logging with separate fields for user data instead of string interpolation

+6 -4 python
  import logging
  from flask import request
  
- @app.route('/login', methods=['POST'])
- def login():
-     username = request.form.get('username')
-     logging.info(f"Login attempt for user: {username}")
+ logger = logging.getLogger(__name__)
+ 
+ @app.route('/login', methods=['POST'])
+ def login():
+     username = request.form.get('username', '')
+     logger.info("Login attempt", extra={'username': username})
      return "OK"
  
3 检测
3 检测
4 警告信号
4 警告信号

代码审查中需要关注的内容

这些模式表明潜在的Improper Output Neutralization for Logs漏洞。在代码审查和安全审计中注意查找。

🟡
unsanitized user input flowing into log statements, enabling log forging attacks go-log-injection
🟡
user input flowing directly into log messages without sanitization python-log-injection
🔵
user input flowing to persistent log files without sanitization javascript-log-injection
🔍

扫描你的代码库: Improper Output Neutralization for Logs

Shoulder CLI 在整个代码库中找到易受攻击的模式。