fix Reflected server-side cross-site scripting in roles() #9388
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
consoleme/consoleme/handlers/v2/roles.py
Lines 130 to 135 in 2795a2b
fix the issue need to ensure that any user-provided input included in the response is properly sanitized or escaped. Since the response is in JSON format, we should escape the
log_data["message"]
value to prevent any malicious content from being executed. Thetornado.escape.xhtml_escape
function can be used for this purpose, as it is designed to escape strings for safe inclusion in HTML or JSON.The fix involves:
log_data["message"]
value before including it in the response.POC
Directly writing user input (an HTTP request parameter) to a webpage without properly sanitizing the input first, allows for a cross-site scripting vulnerability. The following is a minimal flask app which shows a safe and unsafe way to render the given name back to the page. The first view is unsafe as
first_name
is not escaped, leaving the page vulnerable to cross-site scripting attacks. The second view is safe asfirst_name
is escaped, so it is not vulnerable to cross-site scripting attacks.Recommendation
To guard against cross-site scripting, consider escaping the input before writing user input to the page. The standard library provides escaping functions:
html.escape()
for Python 3.2 upwards orcgi.escape()
older versions of Python. Most frameworks also provide their own escaping functions, andflask.escape()
.References
XSS (Cross Site Scripting) Prevention Cheat Sheet
Cross-site scripting
html.escape()
CWE-79
CWE-116