Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3111,8 +3111,17 @@ def process_image(self, url: str, title_attr: str, link_text: str) -> Tuple[str,
This section will be skipped by the link processor
'''
img_class_str = self.md._html_class_str_from_tag("img")
safe_src = self.md._protect_url(url)
if self.md.safe_mode:
# Defense-in-depth: strip schemes that are never valid for an image
# `src` and only serve as XSS vectors. `data:` images, http(s) and
# relative URLs are preserved (and quote-escaped by `_protect_url`),
# matching markdown2's existing image handling.
normalized = re.sub(r"[\s\x00-\x1f]+", "", url).lower()
if normalized.startswith(("javascript:", "vbscript:")):
safe_src = ""
result = (
f'<img src="{self.md._protect_url(url)}"'
f'<img src="{safe_src}"'
f' alt="{self.md._hash_span(_xml_escape_attr(link_text))}"'
f'{title_attr}{img_class_str}{self.md.empty_element_suffix}'
)
Expand Down