CVE-2020-27783
XSS vulnerability in python-lxml's clean module allows remote code execution
- CVSS 6.1
- CWE-79 Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
- Input Validation and Sanitization
- Remote
A XSS vulnerability was discovered in python-lxml's clean module. The module's parser didn't properly imitate browsers, which caused different behaviors between the sanitizer and the user's page. A remote attacker could exploit this flaw to run arbitrary HTML/JS code.
- CVSS base score
- 6.1
- Published
- 2020-12-03
- OWASP
- A03 Injection
- Orthogonal defect classification
- Function
- Code defect classification
- Incorrect Functionality
- Category
- Input Validation and Sanitization
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- python-lxml
- Fixed by upgrading
- Yes
Solution
Update python-lxml to the latest version.
Vulnerable code sample
from lxml import html
from lxml.html.clean import Cleaner
# Create a Cleaner instance without proper sanitization settings
cleaner = Cleaner(
scripts=False, # Disallow scripts (vulnerability)
javascript=False, # Disallow javascript (vulnerability)
comments=True,
style=True,
links=True,
meta=True,
page_structure=True,
remove_unknown_tags=False,
safe_attrs_only=False, # Allow unsafe attributes (vulnerability)
)
# Sample potentially malicious input
malicious_input = '<div><script>alert("XSS Attack!");</script></div>'
# Sanitize the input
sanitized_output = cleaner.clean_html(malicious_input)
print(sanitized_output) # This may output the malicious script without proper sanitizationPatched code sample
from lxml import html
from lxml.html.clean import Cleaner
# Create a Cleaner instance with safe settings
cleaner = Cleaner(
scripts=True, # Allow scripts (to be sanitized)
javascript=True, # Allow javascript (to be sanitized)
comments=True, # Allow comments (to be sanitized)
style=True, # Allow style (to be sanitized)
links=True, # Allow links (to be sanitized)
meta=True, # Allow meta tags (to be sanitized)
page_structure=True, # Ensure page structure is maintained
remove_unknown_tags=False, # Do not remove unknown tags
safe_attrs_only=True, # Only allow safe attributes
)
# Sample potentially malicious input
malicious_input = '<div><script>alert("XSS Attack!");</script></div>'
# Sanitize the input
sanitized_output = cleaner.clean_html(malicious_input)
print(sanitized_output)Published fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- Prevent combinations of <math/svg> and <style> to sneak JavaScript through the HTML cleaner. (lxml/lxml)
src/lxml/html/clean.py,src/lxml/html/clean.py · Cleaner._has_sneaky_javascript,src/lxml/html/clean.py · Cleaner.__call__
Cite this entry
@misc{vaitp:cve202027783,
title = {{XSS vulnerability in python-lxml's clean module allows remote code execution}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2020},
note = {VAITP Python Vulnerability Dataset, entry CVE-2020-27783},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2020-27783/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
