-
Notifications
You must be signed in to change notification settings - Fork 0
If unsanitized user input is written to a log entry, a malicious user… #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… may be able to forge new log entries.
@@ -40,7 +40,8 @@ | |||
"""Create an item with a username and users favourite colour and return it.""" | |||
user_colour.append(item) | |||
print(user_colour) | |||
logger.info(item) | |||
# Sanitize log message to prevent log injection | |||
logger.info("New user-color entry added: username=%s, color=%s", item.username, item.color) |
Check failure
Code scanning / CodeQL
Log Injection High
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 2 months ago
To fix the log injection issue, we need to sanitize the user input before logging it. Specifically, we should remove any newline characters from the item.username
and item.color
fields to prevent log injection attacks. This can be done using the replace
method to replace \r\n
and \n
with empty strings.
-
Copy modified lines R44-R46
@@ -43,3 +43,5 @@ | ||
# Sanitize log message to prevent log injection | ||
logger.info("New user-color entry added: username=%s, color=%s", item.username, item.color) | ||
sanitized_username = item.username.replace('\r\n', '').replace('\n', '') | ||
sanitized_color = item.color.replace('\r\n', '').replace('\n', '') | ||
logger.info("New user-color entry added: username=%s, color=%s", sanitized_username, sanitized_color) | ||
return item |
@@ -40,7 +40,8 @@ | |||
"""Create an item with a username and users favourite colour and return it.""" | |||
user_colour.append(item) | |||
print(user_colour) | |||
logger.info(item) | |||
# Sanitize log message to prevent log injection | |||
logger.info("New user-color entry added: username=%s, color=%s", item.username, item.color) |
Check failure
Code scanning / CodeQL
Log Injection High
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 2 months ago
To fix the log injection issue, we need to sanitize the user-provided values before logging them. Specifically, we should remove any newline characters from the item.color
and item.username
values to prevent log injection. This can be achieved using the replace
method to replace newline characters with empty strings.
-
Copy modified lines R44-R46
@@ -43,3 +43,5 @@ | ||
# Sanitize log message to prevent log injection | ||
logger.info("New user-color entry added: username=%s, color=%s", item.username, item.color) | ||
sanitized_username = item.username.replace('\r\n', '').replace('\n', '') | ||
sanitized_color = item.color.replace('\r\n', '').replace('\n', '') | ||
logger.info("New user-color entry added: username=%s, color=%s", sanitized_username, sanitized_color) | ||
return item |
… may be able to forge new log entries.
Issue #1
To fix the log injection issue, we need to sanitize the user input before logging it. Specifically, we should remove any newline characters from the user input to prevent log forgery. This can be done by replacing \r\n and \n with empty strings.
We will modify the code to sanitize the item before logging it. This involves converting the item to a string and then replacing any newline characters.