question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Detect HTML/JS injection attacks and warn users pro-actively

See original GitHub issue

Is your request related to a specific problem you’re having?

https://github.com/highlightjs/highlight.js/issues/2884 https://github.com/ccampbell/rainbow/issues/249

This comes up again and again (though thankfully not TOO often). Beginners are VERY confused about this whole HTML escaping thing.

The solution you’d prefer / feature you’d like to see added…

While we can’t do anything smart about this yet (because we unfortunately allow HTML inside code blocks for “clever” users) I’d like this to change with v11. With v11 we should drop this HTML pass-thru behavior and move it to a plugin (making it very much opt-in). The default behavior should be that HTML is silently dropped and I’d even consider adding some sort of error:

[code block]
WARNING.
Are you missing a bunch of HTML code you expected to see here? 
Your HTML wasn't properly escaped and that can lead to serious
security issues. _Learn More_

Properly escape your code and the highlighting you expect will kick in.
[/code block]

This would of course be a breaking change so we’d need to wait until v11. For 95% of users I can’t see the downside to this and it seems we could potentially educate and prevent a lot of harm. Someone wanting the HTML to pass thru would install a plug-in and thus change the behavior, get the old behavior back, etc.

Any alternative solutions you considered…

Silent dropping but no error… but that just leads to support issues… I suppose we could log the error to the console vs actually showing it on the webpage.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:53 (26 by maintainers)

github_iconTop GitHub Comments

5reactions
voraciouscommented, Jul 27, 2021

@FractalHQ make sure you know what you’re doing (regarding sanitization), and then use this config option.

hljs.configure({ ignoreUnescapedHTML: true })
3reactions
joshgoebelcommented, May 30, 2021

Your very first snippet has embedded unescaped HTML - two hyperlinks. We have no way to know if that might be malicious code or not - code blocks are expected to contain only text, not HTML. If you know for sure you can disable the warning (see documentation), but those hyperlinks will still be removed - we do not process HTML by default any longer.

If you need the HTML to be preserved you should see the issue on this subject and may need to use a plugin now: https://github.com/highlightjs/highlight.js/issues/2889

Screen Shot 2021-05-30 at 6 17 43 AM
Read more comments on GitHub >

github_iconTop Results From Across the Web

XSS aka HTML Injection Attack explained | by Jamis Charles
The general rule is this: Treat any user input as unsafe. This means that we need to sanitize any user-provided values. There are...
Read more >
The Essential Guide to Preventing JavaScript Injection
Five Best Practices to Prevent Javascript Injections · 1. Leverage Automation for Monitoring and Inspecting Your Website · 2. HTML Encoded Data ...
Read more >
HTML Injection Tutorial: Types & Prevention with Examples
The essence of this type of injection attack is injecting HTML code through the vulnerable parts of the website. The Malicious user sends ......
Read more >
Cross Site Scripting Prevention - OWASP Cheat Sheet Series
For XSS attacks to be successful, an attacker needs to insert and execute malicious content in a webpage. Each variable in a web...
Read more >
WebMTD: Defeating Web Code Injection Attacks using Web ...
For example, an attacker might inject a JavaScript code by exploiting an XSS vulnerability in the Web application. When a user visits the....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found