Skip to content

examples/site-to-markdown: raw HTML injected into f-string inside triple-quote literal #178

Description

@Ricardo-M-L

Description

examples/site-to-markdown/main.py builds Jupyter code dynamically by interpolating scraped page HTML into an outer f-string that contains inner triple-quoted string literals:

code=f"""
from markdownify import markdownify
html = '''{html}'''
screenshot_b64 = "{screenshot_b64}"
...
"""

If the scraped page contains ''' anywhere in its markup (or an attacker-controlled page does), the inner triple-quote is terminated early and the rest of {html} is parsed as Python source. The sandbox then executes that Python. Similarly, raw backslashes in the HTML can produce SyntaxErrors or break escape sequences.

Impact

Since the same sandbox is used for other user code, an attacker who controls any page the user scrapes can execute code inside their sandbox environment. Even without malice, perfectly legitimate pages with inline code samples using triple quotes will break the example.

Fix

Base64-encode the HTML on the host, then decode inside the sandbox. Only 7-bit-safe ASCII crosses the f-string boundary, so no content of the scraped page can alter the generated Python source.

PR forthcoming.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions