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
Open
Conversation
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>
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #178.
The
site-to-markdownexample 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
'''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