Skip to content

Commit 02bbed2

Browse files
authored
Merge pull request #996 from tommcd/pySCG/CWE-117
pySCG: adding 117 as part of #531
2 parents 667e33a + 7674e07 commit 02bbed2

File tree

4 files changed

+221
-15
lines changed

4 files changed

+221
-15
lines changed
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# CWE-117: Improper Output Neutralization for Logs
2+
3+
Ensure all untrusted data is properly neutralized or sanitized before writing to application logs. Log injection occurs when untrusted data is written to application logs without proper neutralization, allowing attackers to forge log entries or inject malicious content. Attackers can inject fake log records or hide real ones by inserting newline sequences (`\r` or `\n`), misleading auditors and incident-response teams. This vulnerability can also enable injection of XSS attacks when logs are viewed in vulnerable web applications.
4+
5+
Attackers can exploit this weakness (see [*Attacks on Logs*](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html#attacks-on-logs) [[OWASP 2025]](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html#attacks-on-logs)) by submitting strings containing CRLF sequences that create fake log entries.
6+
7+
Attackers can exploit this weakness by submitting strings containing Carriage Return Line Feed (CRLF) sequences that create fake log entries. For instance, an attacker authenticating with a crafted username can make failed login attempts appear successful in audit logs, potentially framing innocent users or hiding malicious activity.
8+
9+
This vulnerability is classified as **CWE-117: Improper Output Neutralization for Logs** [[CWE-117](https://cwe.mitre.org/data/definitions/117.html)]. It occurs when CRLF sequences are not properly neutralized in log output, which is a specific instance of the broader **CWE-93: Improper Neutralization of CRLF Sequences** [[CWE-93](https://cwe.mitre.org/data/definitions/93.html)] weakness. Attackers exploit this using the **CAPEC-93: Log Injection-Tampering-Forging** [[CAPEC-93](https://capec.mitre.org/data/definitions/93.html)] attack pattern.
10+
11+
The OWASP Top 10 [[OWASP A09:2021](https://owasp.org/Top10/A09_2021-Security_Logging_and_Monitoring_Failures/)] lists “Security Logging and Monitoring Failures” as a critical security risk.
12+
13+
## Noncompliant Code Example
14+
15+
This example demonstrates how raw user input written to logs enables injection attacks:
16+
17+
*[noncompliant01.py](noncompliant01.py):*
18+
19+
```python
20+
""" Non-compliant Code Example """
21+
import logging
22+
23+
def log_authentication_failed(user):
24+
"""Simplified audit logging missing RFC 5424 details"""
25+
logging.warning("User login failed for: '%s'", user)
26+
27+
#####################
28+
# attempting to exploit above code example
29+
#####################
30+
log_authentication_failed("guest'\nWARNING:root:User login failed for: 'administrator")
31+
```
32+
33+
**Output of `noncompliant01.py`:**
34+
35+
```bash
36+
WARNING:root:User login failed for: 'guest'
37+
WARNING:root:User login failed for: 'administrator'
38+
```
39+
40+
The attacker's input creates what appears to be a legitimate log entry showing a login failure for user `administrator`.
41+
42+
## Compliant Solution
43+
44+
As per the OWASP Logging Cheat Sheet [[OWASP 2025]](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html) section on ["Event collection"](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html#event-collection), applications should sanitize event data to prevent log injection and encode data correctly for the logged format.
45+
46+
The `compliant01.py` solution uses a strict allow-list for usernames and returns early on any mismatch, so `CR/LF` or other disallowed characters never reach the logger; for rejected attempts it logs a safe one-line summary with `%r` (escaped newlines), preventing forged secondary log lines. In short: validate upfront and neutralize what you do record.
47+
48+
*[compliant01.py](compliant01.py):*
49+
50+
```python
51+
""" Compliant Code Example """
52+
53+
import logging
54+
import re
55+
56+
# Allow only ASCII letters, digits, underscore, dot, and hyphen; max 64 chars
57+
_ALLOWED_USER = re.compile(r"^[A-Za-z0-9._-]{1,64}$")
58+
59+
60+
def is_allowed_username(user: str) -> bool:
61+
"""Return True if username matches the strict allow-list."""
62+
return bool(_ALLOWED_USER.fullmatch(user))
63+
64+
65+
def log_authentication_failed(user):
66+
"""Simplified audit logging (example)"""
67+
if not is_allowed_username(user):
68+
# Safe summary: %r escapes CR/LF so the log remains one line
69+
logging.warning("Rejected login attempt: invalid username=%r", user)
70+
return
71+
logging.warning("User login failed for: '%s'", user)
72+
73+
74+
#####################
75+
# attempting to exploit above code example
76+
#####################
77+
log_authentication_failed("guest'\nWARNING:root:User login failed for: 'administrator")
78+
79+
80+
81+
# TODO: Production — keep sink-side neutralization (escape CR/LF) even with validation,
82+
# prefer structured JSON logs.
83+
```
84+
85+
The following output shows that a warning is logged, and the input is sanitized before logging.
86+
87+
**Example compliant01.py output:**
88+
89+
```bash
90+
WARNING:root:Rejected login attempt: invalid username="guest'\nWARNING:root:User login failed for: 'administrator"
91+
```
92+
93+
## Automated Detection
94+
95+
<table>
96+
<thead>
97+
<tr>
98+
<th>Tool</th>
99+
<th>Version</th>
100+
<th>Check</th>
101+
<th>Description</th>
102+
</tr>
103+
</thead>
104+
<tbody>
105+
<tr>
106+
<td>Bandit</td>
107+
<td>1.7.5</td>
108+
<td>Not Available</td>
109+
<td></td>
110+
</tr>
111+
<tr>
112+
<td>CodeQL</td>
113+
<td>Latest</td>
114+
<td><a href="https://codeql.github.com/codeql-query-help/python/py-log-injection/">py-log-injection</a></td>
115+
<td></td>
116+
</tr>
117+
<tr>
118+
<td>Veracode</td>
119+
<td>Latest</td>
120+
<td>CWE 117</td>
121+
<td><a href="https://community.veracode.com/s/article/How-to-Fix-CWE-117-Improper-Output-Neutralization-for-Logs">How to Fix CWE-117</a></td>
122+
</tr>
123+
</tbody>
124+
</table>
125+
126+
## Related Guidelines
127+
128+
<table>
129+
<tr>
130+
<td><a href="http://cwe.mitre.org/">MITRE CWE</a></td>
131+
<td>Pillar: <a href="https://cwe.mitre.org/data/definitions/707.html"> CWE-707: Improper Neutralization</a></td>
132+
</tr>
133+
<tr>
134+
<td><a href="http://cwe.mitre.org/">MITRE CWE</a></td>
135+
<td>Base: <a href="https://cwe.mitre.org/data/definitions/117.html">CWE-117: Improper Output Neutralization for Log </a></td>
136+
</tr>
137+
<tr>
138+
<td><a href="http://cwe.mitre.org/">MITRE CWE</a></td>
139+
<td>Base: <a href="https://cwe.mitre.org/data/definitions/93.html">CWE-93: Improper Neutralization of CRLF Sequences ('CRLF Injection') </a></td>
140+
</tr>
141+
<tr>
142+
<td><a href="http://cwe.mitre.org/">MITRE CWE</a></td>
143+
<td>Variant: <a href="https://cwe.mitre.org/data/definitions/113.html">CWE-113: Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Request/Response Splitting') </a></td>
144+
</tr>
145+
<tr>
146+
<td><a href="http://capec.mitre.org/">MITRE CAPEC</a></td>
147+
<td>Detailed: <a href="https://capec.mitre.org/data/definitions/93.html">CAPEC-93: Log Injection-Tampering-Forging </a></td>
148+
</tr>
149+
<tr>
150+
<td><a href="https://owasp.org/Top10/">OWASP Top 10</a></td>
151+
<td><a href="https://owasp.org/Top10/A09_2021-Security_Logging_and_Monitoring_Failures/">A09:2021 – Security Logging and Monitoring Failures </a></td>
152+
</tr>
153+
<tr>
154+
<td><a href="https://owasp.org/">OWASP ASVS 4.0</a></td>
155+
<td><a href="https://owasp.org/www-project-application-security-verification-standard/">OWASP Application Security Verification Standard (ASVS) </a>. See "V16 Security Logging and Error Handling".
156+
</td>
157+
<tr>
158+
<td><a href="https://cheatsheetseries.owasp.org/index.html">OWASP Cheat Sheet Series</a></td>
159+
<td><a href="https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html">OWASP Logging Cheat Sheet</a></td>
160+
</tr>
161+
</tr>
162+
<tr>
163+
<td>ISO/IEC TR 24772:2013</td>
164+
<td><a href="https://www.iso.org/standard/61457.html">ISO/IEC TR 24772:2013 Information technology — Programming languages — Guidance to avoiding vulnerabilities in programming languages through language selection and use </a></td>
165+
</tr>
166+
<tr>
167+
<td>NIST SP 800-92</td>
168+
<td><a href="https://csrc.nist.gov/pubs/sp/800/92/final">NIST SP 800-92 Guide to Computer Security Log Management </a></td>
169+
</tr>
170+
</table>
171+
172+
## Bibliography
173+
174+
<table>
175+
<tr>
176+
<td>[CWE-117]</a></td>
177+
<td>CWE-117: Improper Output Neutralization for Log [online]. Available from <a href="https://cwe.mitre.org/data/definitions/117.html">https://cwe.mitre.org/data/definitions/117.html</a>, [Accessed 24 September 2025]</td>
178+
</tr>
179+
<tr>
180+
<td>[CWE-93]</a></td>
181+
<td>CWE-93: Improper Neutralization of CRLF Sequences ('CRLF Injection') [online]. Available from: <a href="https://cwe.mitre.org/data/definitions/93.html">https://cwe.mitre.org/data/definitions/93.html</a>, [Accessed 24 September 2025]</td>
182+
</tr>
183+
<tr>
184+
<td>[CWE-113]</a></td>
185+
<td>CWE-113: Improper Neutralization of CRLF Sequences in HTTP Headers ('HTTP Request/Response Splitting')<a href="https://cwe.mitre.org/data/definitions/113.html">https://cwe.mitre.org/data/definitions/113.html</a>, [Accessed 24 September 2025]</td>
186+
</tr>
187+
<tr>
188+
<td>[CAPEC-93]</td>
189+
<td>CAPEC-93: Log Injection-Tampering-Forging [online]. Available from: <a href="https://capec.mitre.org/data/definitions/93.html">https://capec.mitre.org/data/definitions/93.html</a>, [Accessed 24 September 2025]</td>
190+
</tr>
191+
<tr>
192+
<td>[OWASP A09:2021]</td>
193+
<td>A09:2021 – Security Logging and Monitoring Failures [online]. Available from:<a href="https://owasp.org/Top10/A09_2021-Security_Logging_and_Monitoring_Failures/">https://owasp.org/Top10/A09_2021-Security_Logging_and_Monitoring_Failures/</a>, [Accessed 24 September 2025]</td>
194+
</tr>
195+
<tr>
196+
<td>[OWASP 2025]</td>
197+
<td>OWASP Cheat Sheet Series: Logging Cheat Sheet [online]. Available from: <a href="https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html">https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html</a> (see “Event collection” and “Attacks on Logs”). [Accessed 24 September 2025]</td>
198+
</tr>
199+
</table>
Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
11
# SPDX-FileCopyrightText: OpenSSF project contributors
22
# SPDX-License-Identifier: MIT
3-
""" Compliant Code Example """
3+
"""Compliant Code Example"""
4+
45
import logging
56
import re
67

8+
# Allow only ASCII letters, digits, underscore, dot, and hyphen; max 64 chars
9+
_ALLOWED_USER = re.compile(r"^[A-Za-z0-9._-]{1,64}$")
10+
711

8-
def allowed_chars(user):
9-
"""Verify only allowed characters are used"""
10-
if bool(re.compile(r"\w+").fullmatch(user)):
11-
return True
12-
return False
12+
def is_allowed_username(user: str) -> bool:
13+
"""Return True if username matches the strict allow-list."""
14+
return bool(_ALLOWED_USER.fullmatch(user))
1315

1416

1517
def log_authentication_failed(user):
16-
"""Simplified audit logging missing RFC 5424 details"""
17-
if not allowed_chars(user):
18-
logging.critical("Login attempt with non-allowed characters in user name: '%s'", user)
18+
"""Simplified audit logging (example)"""
19+
if not is_allowed_username(user):
20+
# Safe summary: %r escapes CR/LF so the log remains one line
21+
logging.warning("Rejected login attempt: invalid username=%r", user)
22+
return
1923
logging.warning("User login failed for: '%s'", user)
2024

2125

2226
#####################
2327
# attempting to exploit above code example
2428
#####################
25-
log_authentication_failed("""foo
26-
WARNING:root:User login failed for: administrator""")
29+
log_authentication_failed("guest'\nWARNING:root:User login failed for: 'administrator")
30+
31+
32+
# TODO: Production — keep sink-side neutralization (escape CR/LF) even with validation,
33+
# prefer structured JSON logs.

docs/Secure-Coding-Guide-for-Python/CWE-707/CWE-117/noncompliant01.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-FileCopyrightText: OpenSSF project contributors
22
# SPDX-License-Identifier: MIT
3-
""" Non-compliant Code Example """
3+
"""Non-compliant Code Example"""
4+
45
import logging
56

67

@@ -12,5 +13,4 @@ def log_authentication_failed(user):
1213
#####################
1314
# attempting to exploit above code example
1415
#####################
15-
log_authentication_failed("""foo
16-
WARNING:root:User login failed for: administrator""")
16+
log_authentication_failed("guest'\nWARNING:root:User login failed for: 'administrator")

docs/Secure-Coding-Guide-for-Python/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ It is __not production code__ and requires code-style or python best practices t
111111
|:----------------------------------------------------------------|:----|
112112
|[CWE-78: Improper Neutralization of Special Elements Used in an OS Command ("OS Command Injection")](CWE-707/CWE-78/README.md)|[CVE-2024-43804](https://www.cvedetails.com/cve/CVE-2024-43804/),<br/>CVSSv3.1: __8.8__,<br/>EPSS: __00.06__ (08.11.2024)|
113113
|[CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')](CWE-707/CWE-89/README.md)|[CVE-2019-8600](https://www.cvedetails.com/cve/CVE-2019-8600/),<br/>CVSSv3.1: __9.8__,<br/>EPSS: __01.43__ (18.02.2024)|
114-
|[CWE-117: Improper Output Neutralization for Logs](CWE-707/CWE-117/.)||
114+
|[CWE-117: Improper Output Neutralization for Logs](CWE-707/CWE-117/README.md)||
115115
|[CWE-175: Improper Handling of Mixed Encoding](CWE-707/CWE-175/README.md)||
116116
|[CWE-180: Incorrect behavior order: Validate before Canonicalize](CWE-707/CWE-180/README.md)|[CVE-2022-26136](https://www.cvedetails.com/cve/CVE-2022-26136/),<br/>CVSSv3.1: __9.8__,<br/>EPSS: __00.18__ (24.04.2025)|
117117
|[CWE-838: Inappropriate Encoding for Output Context](CWE-707/CWE-838/README.md)||

0 commit comments

Comments
 (0)