Skip to content

Commit 09e1bba

Browse files
🔒 Sanitize objects that may or may not be strings for logging
Potential fix for pull request finding 'CodeQL / Log Injection' Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 8e00658 commit 09e1bba

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

  • python_jobs/src/hbnmigration/utility_functions

python_jobs/src/hbnmigration/utility_functions/logging.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ def initialize_logging(
140140
return logging.getLogger(name)
141141

142142

143-
def safe_record_for_log(record: str) -> str:
143+
def safe_record_for_log(record: object) -> str:
144144
r"""
145-
Sanitize a string for safe inclusion in log output.
145+
Sanitize a value for safe inclusion in log output.
146146
147147
Mitigates CRLF log injection attacks by removing characters that could
148148
be used to forge log entries, evade log analysis, or exploit log viewers.
@@ -161,15 +161,17 @@ def safe_record_for_log(record: str) -> str:
161161
Parameters
162162
----------
163163
record
164-
The untrusted string to sanitize before logging.
164+
The untrusted value to sanitize before logging.
165165
166166
Returns
167167
-------
168168
str
169-
A sanitized copy of the string with dangerous characters removed.
169+
A sanitized copy of the value with dangerous characters removed.
170170
171171
"""
172-
result = _ANSI_RE.sub("", record)
172+
text = str(record)
173+
text = text.replace("\r", "").replace("\n", "")
174+
result = _ANSI_RE.sub("", text)
173175
return "".join(
174176
ch
175177
for ch in result

0 commit comments

Comments
 (0)