Skip to content
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

enh(parser) Warn if unescaped HTML is present #3057

Merged
merged 4 commits into from Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/highlight.js
Expand Up @@ -45,6 +45,7 @@ const HLJS = function(hljs) {
// calling the `hljs.configure` function.
/** @type HLJSOptions */
let options = {
ignoreUnescapedHTML: false,
noHighlightRe: /^(no-?highlight)$/i,
languageDetectRe: /\blang(?:uage)?-([\w-]+)\b/i,
classPrefix: 'hljs-',
Expand Down Expand Up @@ -702,15 +703,19 @@ const HLJS = function(hljs) {

if (shouldNotHighlight(language)) return;

// support for v10 API
fire("before:highlightElement",
{ el: element, language: language });

if (!options.ignoreUnescapedHTML && element.innerHTML !== element.textContent) {
Copy link
Member

Choose a reason for hiding this comment

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

<div id="foo">
  I'm just a &lt;code&gt;sample&lt;/code&gt; block.
</div>

<div id="innerHTML"></div>
<div id="textContent"></div>
<div id="result"></div>

<script>
document.getElementById("innerHTML").innerText = document.getElementById("foo").innerHTML;
document.getElementById("textContent").innerText = document.getElementById("foo").textContent;
document.getElementById("result").innerText = document.getElementById("foo").innerHTML == document.getElementById("foo").textContent;
</script>

https://jsfiddle.net/uj5qzn2e/

Am I doing something wrong? Or does innerHTML need to be have HTML entities transformed before they can be compared with textContent

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, my test cases locally didn't include escaped HTML... that certainly breaks the comparison approach. I think instead perhaps we just need to check node.children...

console.warn("One of your code blocks includes unescaped HTML. This is a potentially serious security risk.");
console.warn("https://github.com/highlightjs/highlight.js/issues/2886");
console.warn(element);
}

node = element;
const text = node.textContent;
const result = language ? highlight(language, text, true) : highlightAuto(text);

// support for v10 API
fire("after:highlightElement", { el: element, result, text });

element.innerHTML = result.value;
Expand Down Expand Up @@ -1005,7 +1010,7 @@ const HLJS = function(hljs) {

// built-in plugins, likely to be moved out of core in the future
hljs.addPlugin(brPlugin); // slated to be removed in v11
hljs.addPlugin(mergeHTMLPlugin);
// hljs.addPlugin(mergeHTMLPlugin);
hljs.addPlugin(tabReplacePlugin);
return hljs;
};
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Expand Up @@ -120,6 +120,7 @@ interface HLJSOptions {
useBR: boolean
languages?: string[]
__emitter: EmitterConstructor
ignoreUnescapedHTML?: boolean
}

interface CallbackResponse {
Expand Down