Skip to content

fix: escape HTML in heading ID and code info attributes to prevent XSS - #723

Open
Yanhu007 wants to merge 1 commit into
russross:v2from
Yanhu007:fix/xss-heading-id-code-info
Open

fix: escape HTML in heading ID and code info attributes to prevent XSS#723
Yanhu007 wants to merge 1 commit into
russross:v2from
Yanhu007:fix/xss-heading-id-code-info

Conversation

@Yanhu007

Copy link
Copy Markdown

Security Fix

Addresses #722Critical XSS via unescaped attribute injection

Vulnerabilities

Finding 1: Heading ID injection (Critical)

# Test {#"><img src=x onerror=alert(1)>}

Renders as:

<h1 id=""><img src=x onerror=alert(1)>">Test</h1>

Finding 2: Fenced code info string injection (Critical)

```lang"><img src=x onerror=alert(1)>
code
```

Renders as:

<code class="language-lang"><img src=x onerror=alert(1)>">code</code>

Both fire automatically on page load, bypass SkipHTML, and are enabled by default via CommonExtensions.

Fix

Apply html.EscapeString() to:

  1. Heading IDs before interpolation into id="" attribute (line 679)
  2. Code info strings before interpolation into class="" attribute (line 330)

After Fix

<h1 id="&#34;&gt;&lt;img src=x onerror=alert(1)&gt;">Test</h1>
<code class="language-lang&#34;&gt;&lt;img">code</code>

All existing tests pass.

Heading IDs from {#id} syntax and fenced code block info strings
were interpolated into HTML attributes without escaping. This allows
stored XSS that fires automatically on page load:

  # Test {#"><img src=x onerror=alert(1)>}
  →  <h1 id=""><img src=x onerror=alert(1)>">Test</h1>

  ```lang"><img src=x onerror=alert(1)>
  →  <code class="language-lang"><img src=x ...>

Both are enabled by default via CommonExtensions and bypass SkipHTML
since the injection targets are generated by the renderer, not
preserved from the input.

Fix: apply html.EscapeString() to heading IDs and code info strings
before interpolation into attribute values.

Ref russross#722
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.

1 participant