Skip to content

Fix typo in marked.js sanitizer - #111258

Merged
Matt Bierner (mjbvz) merged 2 commits into
microsoft:masterfrom
Zemnmez:master
Jan 15, 2021
Merged

Matt Bierner (mjbvz) merged 2 commits into
microsoft:masterfrom
Zemnmez:master

Conversation

@Zemnmez

@Zemnmez Thomas Neil James Shadwell (Zemnmez) commented Nov 24, 2020 •

Copy link
Copy Markdown
Contributor

There was / is a typo in markdownRenderer.js that allowed any trusted document to pass arbitrary HTML through the marked.js sanitizer provided it is wrapped in <span></span> tags, or similar.

What could you have done with this? Not much that was not already possible in trusted mode, which, as far as I can tell is used just for Jypiter Notebooks that pretty much definitionally can execute Python anyway.

Insane strips everything worthwhile except <a data-href=''/> which you can use to send command: URIs on click (javascript: URIs are disabled at a higher level of abstraction), but the ability to do this is already allowlisted (L141) for trusted documents.

This PR fixes #111257

There was / is a typo in `markdownRenderer.js` that allowed any *trusted* document to pass arbitrary HTML through the marked.js sanitizer provided it is wrapped in `<span></span>` tags, or similar.

What could you have done with this? Not much that was not already possible in trusted mode, which, as far as I can tell is used just for Jypiter Notebooks that pretty much definitionally can execute Python anyway.

Insane strips everything worthwhile except `<a data-href=''/>` which you can use to send `command:` URIs on click (`javascript:` URIs are disabled at a higher level of abstraction), but are already whitelisted (L141) for trusted documents.
// marked for sanitization.
markedOptions.sanitizer = (html: string): string => {
const match = markdown.isTrusted ? html.match(/^(<span[^<]+>)|(<\/\s*span>)$/) : undefined;
const match = markdown.isTrusted ? html.match(/^(<span[^>]+>)|(<\/\s*span>)$/) : undefined;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure that [^>] is correct. We still want to prevent html tags from beginning. [^<>] would probably be the best.

@Zemnmez Thomas Neil James Shadwell (Zemnmez) Nov 25, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An HTML tag can’t begin inside the attributes of another HTML tag.

I wrote this kind of hurriedly. I think you're right in that this regex is not a perfect way to do what's being attempted here, which is to allow <span class="codicon codicon-something" style="color:#ffff"> defined later in the file:

return !!token.attrs['style'].match(/^(color\:#[0-9a-fA-F]+;)?(background-color\:#[0-9a-fA-F]+;)?$/);

It's a bigger fix than just a typo, but we might want to be safe by going so far as to allowlist this specific set of inputs, since the regexes are already there in part:

/^(?:<span (?:(?:class="codicon codicon-[a-z\-]+(?:codicon-animation-[a-z\-]+)?")?|(?:style="(?:color\:#[0-9a-fA-F]+;|background-color\:#[0-9a-fA-F]+;)"))>|^<\/\s*span>)$/g

refiddle: http://refiddle.com/refiddles/5fbe763e75622d16b3130000
rail diagram: rail diagram

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of blocking a specific character, can we only allow specific characters? Something like:

<span( [a-z\-'"#0-9]*)?>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be possible to confuse that regex into showing something it's not meant to by ending each pass with a trailing quote:

<span "> "this text may not be interpreted as HTML, but is parsed as HTML now"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be safe if you require it to match the entire string

@Zemnmez

Copy link
Copy Markdown
Contributor Author

@mjbvz
Matt Bierner (mjbvz) merged commit 4566eeb into microsoft:master Jan 15, 2021
@mjbvz

Copy link
Copy Markdown
Contributor

Thanks! Will be in the VS Code insiders build

Ladislau Szomoru (lszomoru) pushed a commit that referenced this pull request Jan 15, 2021
There was / is a typo in `markdownRenderer.js` that allowed any *trusted* document to pass arbitrary HTML through the marked.js sanitizer provided it is wrapped in `<span></span>` tags, or similar.

What could you have done with this? Not much that was not already possible in trusted mode, which, as far as I can tell is used just for Jypiter Notebooks that pretty much definitionally can execute Python anyway.

Insane strips everything worthwhile except `<a data-href=''/>` which you can use to send `command:` URIs on click (`javascript:` URIs are disabled at a higher level of abstraction), but are already whitelisted (L141) for trusted documents.
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Typo in marked.js sanitization regex

3 participants