# Improper Output Neutralization for Logs (CWE-117) The product does not neutralize or incorrectly neutralizes output that is written to logs. - Prevalence: Średnia Pokryto 3 języków - Impact: Średni Zalecany przegląd - Prevention: Udokumentowane 4 przykładów poprawek **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 Strategie zapobiegania dla Log Injection oparte na 4 regułach detekcji Shoulder. ### Go Strip newlines and control characters from user input before logging ### JavaScript Strip newline characters from user input before writing to log files Sanitize user input by stripping CRLF characters before writing to logs ### Python Use structured logging with separate fields for user data instead of string interpolation ## Warning Signs - [MEDIUM] unsanitized user input flowing into log statements, enabling log forging attacks - [MEDIUM] user input flowing directly into log messages without sanitization - [LOW] user input flowing to persistent log files without sanitization ## Consequences - Modyfikacja danych aplikacji - Ukrywanie aktywności - Wykonanie nieautoryzowanego kodu ## Mitigations - Waliduj i sanitizuj wszystkie wejścia przed zapisaniem ich do logów - Stosuj uporządkowane formaty logów oddzielające dane od składni logu - Koduj znaki specjalne podczas zapisywania danych kontrolowanych przez użytkownika do logów ## Detection - Total rules: 4 - Languages: go, javascript, typescript, python ## Rules by Language ### Javascript (2 rules) - **Log Injection** [LOW]: Detects user input flowing to persistent log files without sanitization. - Remediation: Sanitize user input by removing newline characters before logging. ```javascript const safe = userInput.replace(/[\r\n]/g, ''); logger.info(safe); ``` Learn more: https://shoulder.dev/learn/javascript/cwe-117/log-injection - **Log Injection** [MEDIUM]: Detects user input flowing to persistent log files without sanitization. - Remediation: Sanitize user input before logging to prevent log forgery: ```javascript const sanitize = (str) => str.replace(/[\r\n]/g, '').substring(0, 200); logger.info('Login attempt', { username: sanitize(req.body.username) }); ``` Learn more: https://shoulder.dev/learn/javascript/cwe-117/log-injection ### 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 ### Typescript (1 rules) - **Log Injection** [MEDIUM]: Detects user input flowing to persistent log files without sanitization. - Remediation: Sanitize user input before logging to prevent log forgery: ```javascript const sanitize = (str) => str.replace(/[\r\n]/g, '').substring(0, 200); logger.info('Login attempt', { username: sanitize(req.body.username) }); ``` Learn more: https://shoulder.dev/learn/javascript/cwe-117/log-injection ### Python (1 rules) - **Log Injection / Log Forging** [MEDIUM]: Detects user input flowing directly into log messages without sanitization. - Remediation: Use structured logging with separate fields for user data. ```python logging.info("Login attempt", extra={'username': username}) ``` Learn more: https://shoulder.dev/learn/python/cwe-117/log-injection