Closed elaine-jackson closed 3 years ago
Thanks for the report!
Broken highlighting: highlight.js detected the wrong language as less
:
Seems like the source code doesn't have line break symbols, could you please check?
\n
instead of < br >
Also you can use another way to call the plugin:
const block = document.querySelector('code.hljs');
hljs.lineNumbersBlock(block);
HI thanks for replying quickly @wcoder . For the broken hilighting its unclear why the language was detected as less. I don't init hightlight until after the paste has been decrypted. I ensure this by using the promise based fetch API. This means that it will not init until the data has both been fetched and decrypted. So to answer your question about line breaks: On one page https://paste.is/p/v/2bcb2f5c-f795-466a-a128-b44bd0b06651 I turned off the encryption and the same bug occurs (see API response at https://paste.is/api/v1/paste?dataType=text&uuid=2bcb2f5c-f795-466a-a128-b44bd0b06651) as its a text file with new lines I believe line breaks are included as \n
otherwise we wouldn't see the new lines? Beyond that is there any insight you could provide here if part of the issue lies in my code. It's admittedly challenging for my API serverto modify user input any as it's typically end to end encrypted to protect user privacy. Even when encrypted
are not added by the API or my JavaScript as far as I can tell.
Alternatively if you have insights on what a bug fix would ensue maybe I could contribute a pull request as your library is very helpful to my project.
One other note, it's possible that JavaScript changes the line breaks to
tags. Is there a way to turn off this behavior or replace every
element that was inserted into a \n
again? I use the document.querySelector().innerText =
to modify the #pasteContent
element. This is needed because the API Server cannot modify an encrypted paste as not even I have access to the encryption keys. As a result when putting a user's content into the webpage I have to rely on JavaScript treating the text as text rather than HTML. Without it would be trivial for a user to put raw HTML and JavaScript into the page and perform a cross-site scripting attack.
A read of https://w3c.github.io/DOM-Parsing/#dfn-text doesn't mention the innerText
adding <br>
tags so it's unclear if this is undefined behavior and a browser implementation choice.
An unencrypted paste in VIM shows the line breaks.
On another note I did submit an issue to highlightjs project concerning the text being detected as LESS instead of C. https://github.com/highlightjs/highlight.js/issues/3184
@irlcatgirl Please try to use innerHTML
:
document.getElementById("pasteContent").innerHTML = textContent;
...
hljs.initHighlighting();
hljs.initLineNumbersOnLoad();
Works fine:
So yes that works but it poses another problem (keep in mind the server cannot see let alone filter user input text). It's now unfiltered HTML / JS and results in a cross-site-scripting vulnerability. Any ideas on how to do about fixing it?
Sure, you can use any HTML escaping library for this (on backend or frontend side).
So the following works for me
document.getElementById("pasteContent").innerHTML = originalText.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
a copy and paste of the https://owasp.org/www-community/xss-filter-evasion-cheatsheet does not trigger any alerts indicating its good enough.
Do you want me to close this issue since we found a solution or leave it open as a potential bug?
Yes, it can be closed. Also, can be reopened when behaver will be wrong.
Describe the bug I have an application which uses the JavaScript
fetch()
API to fetch some data and render it on a page.To Reproduce
Expected behavior I expect the content to be fetched, decrypted, hightlighted with highlightjs (this works great due to the usage of JavaScript promises) and finally for highlightjs line numbers to add a number to each line.
Screenshots
Additional context Example URL https://paste.is/p/v/264ce2ae-c9c0-40f9-9863-61f1b0c3fd1b#NXZNeWVpM3lEV2xpNXZENldOOE5qaENUYlE5eHpoajJYTUJUblViQlZXWHY2dTJTYTFrWmVKeXZON0NHZ2haUQ==