Overview
Both `docker_scanner.py` and `report_generator.py` use a hand-rolled HTML escaping approach instead of Python's built-in `html.escape()`. The custom implementation only handles 5 characters and may miss edge cases, creating a potential XSS vector when vulnerability data contains unexpected characters.
Current Code (docker_scanner.py)
def _escape_html(self, text: str) -> str:
"""Escape HTML special characters to prevent XSS."""
if not isinstance(text, str):
text = str(text)
escape_table = {
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'",
}
return "".join(escape_table.get(c, c) for c in text)
Problem
- Incomplete: Python's `html.escape()` handles the same characters but is maintained by the Python core team and tested against the full HTML spec
- Redundant maintenance burden: Keeping a custom table in sync with evolving XSS vectors is unnecessary work
- Missing `quote=True`: The stdlib function also handles attribute-context escaping properly when `quote=True`
Fix
import html
def _escape_html(self, text: str) -> str:
"""Escape HTML special characters to prevent XSS."""
if not isinstance(text, str):
text = str(text)
return html.escape(text, quote=True)
This is a one-line change per file with identical output for the current character set, plus correct handling of any edge cases the stdlib covers.
Files to Update
| File |
Line(s) |
| `docker_scanner.py` |
`_escape_html()` method |
| `report_generator.py` |
Any equivalent escaping logic |
Acceptance Criteria
Skill Level
Beginner. This is a single-method change that removes code rather than adding it.
Overview
Both `docker_scanner.py` and `report_generator.py` use a hand-rolled HTML escaping approach instead of Python's built-in `html.escape()`. The custom implementation only handles 5 characters and may miss edge cases, creating a potential XSS vector when vulnerability data contains unexpected characters.
Current Code (docker_scanner.py)
Problem
Fix
This is a one-line change per file with identical output for the current character set, plus correct handling of any edge cases the stdlib covers.
Files to Update
Acceptance Criteria
Skill Level
Beginner. This is a single-method change that removes code rather than adding it.