Skip to content

fix(examples): base64-encode HTML in site-to-markdown to prevent f-string injection#179

Open
Ricardo-M-L wants to merge 1 commit into
agent-infra:mainfrom
Ricardo-M-L:fix/site-to-markdown-html-injection
Open

fix(examples): base64-encode HTML in site-to-markdown to prevent f-string injection#179
Ricardo-M-L wants to merge 1 commit into
agent-infra:mainfrom
Ricardo-M-L:fix/site-to-markdown-html-injection

Conversation

@Ricardo-M-L
Copy link
Copy Markdown

Summary

Fixes #178.

The site-to-markdown example interpolated scraped HTML directly into an f-string containing an inner triple-quoted literal. Pages whose content includes ''' (or stray backslashes) break out of the string, producing either a SyntaxError or arbitrary Python execution in the sandbox.

Fix: base64-encode the HTML on the host and decode it inside the sandbox. Only ASCII alphanumerics and +/= cross the f-string boundary, so no page content can alter the generated Python source.

Test plan

  • Read the generated Jupyter code for a page containing ''' in the body — previously raised SyntaxError, now runs cleanly and produces correct markdown.

Related

Part of splitting closed PR #167 into focused single-bug PRs. Previously opened: #171 (Bug 1), #173 (Bug 2), #175 (Bug 3), #177 (Bug 4). This is Bug 5.

🤖 Generated with Claude Code

The site-to-markdown example interpolates scraped page HTML directly
into the generated Jupyter code inside triple-quoted strings:

    code=f\"\"\"
    html = '''{html}'''
    ...
    \"\"\"

Any page whose HTML contains \`'''\` (unlikely but possible) or a
backslash sequence would break out of the string literal and either
produce a SyntaxError or, in the worst case, execute attacker-chosen
Python in the sandbox. Since the sandbox is the same one the user is
running other code in, this is more than cosmetic.

Base64-encode the HTML on the host side and decode it inside the
sandbox so only 7-bit-safe ASCII crosses the f-string boundary.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant