Repository navigation
fix: prefix raw HTML ids without doubling footnote clobber prefixes #695
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
Merged
farnabaz
merged 3 commits into
vercel:main
from
umeraamir69:fix/sanitize-clobber-prefix-raw-html-ids
Oct 23, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "streamdown": patch | ||
| --- | ||
|
|
||
| fix: restore `user-content-` clobberPrefix on sanitize so raw HTML ids cannot clobber `window`, and strip the doubled prefix on footnote ids that remark-rehype already prefixed. |
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,67 @@ | ||
| import { render } from "@testing-library/react"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { Streamdown } from "../index"; | ||
|
|
||
| describe("Sanitized fragment links", () => { | ||
| it("rewrites raw HTML fragments but leaves prefixed and external links alone", () => { | ||
| const { container } = render( | ||
| <Streamdown linkSafety={{ enabled: false }} mode="static"> | ||
| { | ||
| '<div id="details">Details</div>\n\n<a href="#details">Raw</a>\n\n[Prefixed](#user-content-details)\n\n[External](https://example.com/#details)' | ||
| } | ||
| </Streamdown> | ||
| ); | ||
|
|
||
| expect(container.querySelector("[id]")?.id).toBe("user-content-details"); | ||
| expect( | ||
| Array.from(container.querySelectorAll("a[href]"), (link) => | ||
| link.getAttribute("href") | ||
| ) | ||
| ).toEqual([ | ||
| "#user-content-details", | ||
| "#user-content-details", | ||
| "https://example.com/#details", | ||
| ]); | ||
| }); | ||
|
|
||
| it("keeps footnote references and backrefs aligned", () => { | ||
| const { container } = render( | ||
| <Streamdown linkSafety={{ enabled: false }} mode="static"> | ||
| {"Note[^1].\n\n[^1]: Details."} | ||
| </Streamdown> | ||
| ); | ||
|
|
||
| const links = container.querySelectorAll('a[href^="#"]'); | ||
| expect(links.length).toBe(2); | ||
| for (const link of links) { | ||
| expect( | ||
| container.querySelector(link.getAttribute("href") ?? "") | ||
| ).toBeTruthy(); | ||
| expect(link.getAttribute("href")).not.toContain( | ||
| "user-content-user-content-" | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| for (const mode of ["static", "streaming"] as const) { | ||
| for (const allowedTags of [undefined, { snippet: ["id"] }]) { | ||
| it(`keeps raw HTML targets reachable in ${mode} mode (${allowedTags ? "extended" : "default"} schema)`, () => { | ||
| const { container } = render( | ||
| <Streamdown | ||
| allowedTags={allowedTags} | ||
| linkSafety={{ enabled: false }} | ||
| mode={mode} | ||
| > | ||
| {'<div id="details">Details</div>\n\n[Go](#details)'} | ||
| </Streamdown> | ||
| ); | ||
|
|
||
| const target = container.querySelector("[id]"); | ||
| const link = container.querySelector("a"); | ||
|
|
||
| expect(target?.id).toBe("user-content-details"); | ||
| expect(link?.getAttribute("href")).toBe(`#${target?.id}`); | ||
| }); | ||
| } | ||
| } | ||
| }); |
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 |
|---|---|---|
|
|
@@ -391,14 +391,52 @@ export type StreamdownProps = Options & { | |
| | undefined; | ||
| } & AriaAttributes; | ||
|
|
||
| const CLOBBER_PREFIX = "user-content-"; | ||
|
|
||
| interface HastNode { | ||
| children?: HastNode[]; | ||
| properties?: { id?: unknown; href?: unknown }; | ||
| type?: string; | ||
| } | ||
|
|
||
| /** Normalize sanitized ids and keep fragment links aligned with their targets. */ | ||
| function rehypeUndoubleClobberPrefix() { | ||
| const doubled = `${CLOBBER_PREFIX}${CLOBBER_PREFIX}`; | ||
| const visit = (node: HastNode) => { | ||
| if ( | ||
| node.type === "element" && | ||
| typeof node.properties?.id === "string" && | ||
| node.properties.id.startsWith(doubled) | ||
| ) { | ||
| node.properties.id = node.properties.id.slice(CLOBBER_PREFIX.length); | ||
| } | ||
| // Fragment targets are sanitized too. Rewrite even when the target is in | ||
| // another streaming block, but leave already-prefixed footnote links alone. | ||
| if ( | ||
| node.type === "element" && | ||
| typeof node.properties?.href === "string" && | ||
| node.properties.href.startsWith("#") && | ||
| node.properties.href.length > 1 && | ||
| !node.properties.href.startsWith(`#${CLOBBER_PREFIX}`) | ||
| ) { | ||
| node.properties.href = `#${CLOBBER_PREFIX}${node.properties.href.slice(1)}`; | ||
| } | ||
| if (Array.isArray(node.children)) { | ||
| for (const child of node.children) { | ||
| visit(child); | ||
| } | ||
| } | ||
| }; | ||
| return (tree: HastNode) => { | ||
| visit(tree); | ||
| }; | ||
| } | ||
|
|
||
| const defaultSanitizeSchema = { | ||
| ...defaultSchema, | ||
| // remark-rehype already prefixes footnote ids and backref hrefs with | ||
| // `user-content-` (its default `clobberPrefix`). hast-util-sanitize's default | ||
| // `clobberPrefix` is also `user-content-`, which would double-prefix ids like | ||
| // `user-content-user-content-fn-1` while leaving the (already-prefixed) href | ||
| // pointing at the un-doubled anchor. Disable it here to avoid the mismatch. | ||
| clobberPrefix: "", | ||
| // Keep the default `user-content-` clobberPrefix so raw HTML ids (from | ||
| // rehype-raw) cannot clobber `window`. remark-rehype already prefixes | ||
| // footnote ids; `rehypeUndoubleClobberPrefix` strips the doubled prefix. | ||
| protocols: { | ||
| ...defaultSchema.protocols, | ||
| // `streamdown:` is the remend sentinel scheme for incomplete links/images | ||
|
|
@@ -415,6 +453,7 @@ const defaultSanitizeSchema = { | |
| export const defaultRehypePlugins: Record<string, Pluggable> = { | ||
| raw: rehypeRaw, | ||
| sanitize: [rehypeSanitize, defaultSanitizeSchema], | ||
| undoubleClobberPrefix: rehypeUndoubleClobberPrefix, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| harden: [ | ||
| harden, | ||
| { | ||
|
|
@@ -1028,6 +1067,7 @@ export const Streamdown = memo( | |
| result = [ | ||
| defaultRehypePlugins.raw, | ||
| [rehypeSanitize, extendedSchema], | ||
| defaultRehypePlugins.undoubleClobberPrefix, | ||
| defaultRehypePlugins.harden, | ||
| ]; | ||
| } | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.