-
Notifications
You must be signed in to change notification settings - Fork 38
✅ TEST(#143): add categorical XSS guard across plugin renderers #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KyleKing
wants to merge
5
commits into
executablebooks:claude/practical-goldberg-xaa9j6
Choose a base branch
from
KyleKing:kyle/xss-and-attrs-fixes-143
base: claude/practical-goldberg-xaa9j6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+113
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
046fc16
🐛 FIX: escape default renderers and harden attrs against XSS (#143)
KyleKing 811e7e0
test(143): add categorical XSS guard across plugin renderers
KyleKing ec1a5a4
Merge branch 'fix/xss-and-attrs-143' into kyle/xss-and-attrs-fixes-143
KyleKing e4eb4cd
refactor: minimize diff
KyleKing c977b60
docs: also rollback cosmetic docstring changes to minimize diff
KyleKing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| """Categorical XSS guard: render injection probes through each plugin's defaults. | ||
|
|
||
| Several plugins register default HTML render rules that interpolate | ||
| attacker-controlled token content into markup. Rather than trust review to catch | ||
| the next unescaped interpolation, this drives each plugin end-to-end with a probe | ||
| and asserts the raw marker never reaches the output (``markdown-it`` runs with raw | ||
| HTML disabled, so a correctly-escaped plugin emits ``<xss>`` instead). | ||
|
|
||
| Two probe shapes cover the two sinks: | ||
|
|
||
| - ``"><xss>`` for HTML/attribute contexts: a plugin that escapes turns the ``<`` | ||
| into ``<`` so the ``<xss`` marker disappears; one that doesn't leaks a live | ||
| element (or breaks out of an attribute). | ||
| - ``on*`` / ``style`` attribute keys for ``attrs``, where the injection is the | ||
| attribute itself: the default config diverts these to ``token.meta`` so the | ||
| marker is absent from output. | ||
|
|
||
| The only per-plugin cost is one activation payload placing the probe where the | ||
| plugin captures token content. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
|
|
||
| from markdown_it import MarkdownIt | ||
| import pytest | ||
|
|
||
| from mdit_py_plugins.attrs import attrs_block_plugin, attrs_plugin | ||
| from mdit_py_plugins.dollarmath import dollarmath_plugin | ||
| from mdit_py_plugins.myst_blocks import myst_block_plugin | ||
| from mdit_py_plugins.texmath import texmath_plugin | ||
|
|
||
| # A live-element / attribute-breakout probe and the marker that only survives in | ||
| # the output if the plugin emitted it without HTML-escaping. | ||
| _TAG = '"><xss>' | ||
| _TAG_MARKER = "<xss" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Case: | ||
| id: str | ||
| configure: Callable[[MarkdownIt], None] | ||
| payload: str | ||
| marker: str | ||
|
|
||
|
|
||
| _CASES = [ | ||
| Case( | ||
| "texmath-inline-content", | ||
| lambda md: md.use(texmath_plugin), | ||
| f"a ${_TAG}$ b", | ||
| _TAG_MARKER, | ||
| ), | ||
| Case( | ||
| "texmath-block-eqno-info", | ||
| lambda md: md.use(texmath_plugin, delimiters="gitlab"), | ||
| "```math x ``` (<xss onx=1>)", | ||
| "<xss", | ||
| ), | ||
| Case( | ||
| "dollarmath-label", | ||
| lambda md: md.use(dollarmath_plugin), | ||
| f"$$a=1$$ ({_TAG})\n", | ||
| _TAG_MARKER, | ||
| ), | ||
| Case( | ||
| "myst-target", | ||
| lambda md: md.use(myst_block_plugin), | ||
| f"({_TAG})=\n", | ||
| _TAG_MARKER, | ||
| ), | ||
| Case( | ||
| "attrs-span-event", | ||
| lambda md: md.use(attrs_plugin, spans=True), | ||
| '[click]{onxss="alert(1)"}\n', | ||
| "onxss=", | ||
| ), | ||
| Case( | ||
| "attrs-image-event", | ||
| lambda md: md.use(attrs_plugin), | ||
| '{onxss="alert(1)"}\n', | ||
| "onxss=", | ||
| ), | ||
| Case( | ||
| "attrs-span-style", | ||
| lambda md: md.use(attrs_plugin, spans=True), | ||
| '[click]{style="color:red"}\n', | ||
| "style=", | ||
| ), | ||
| Case( | ||
| "attrs-block-event", | ||
| lambda md: md.use(attrs_block_plugin), | ||
| '{onxss="alert(1)"}\nparagraph\n', | ||
| "onxss=", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("case", _CASES, ids=lambda case: case.id) | ||
| def test_plugin_escapes_injection(case: Case) -> None: | ||
| md = MarkdownIt("commonmark") | ||
| case.configure(md) | ||
| out = md.render(case.payload) | ||
| assert case.marker not in out, ( | ||
| f"{case.id} emitted {case.marker!r} unescaped: {out!r}" | ||
| ) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the rendering logic below is implemented, we want the escaping to be owned here because doing so around render would break the valid HTML and reasonable to be owned by render