When doing a XSS, we usually put the JavaScript code between the <script> tags.

I have noticed that HTML injections are very similar because you are doing almost the same thing, but using different tags (e.g <h1>, <p>, <span>, etc.)

The result might very different. When doing a XSS attack, you might create the usual popup with alert(), while doing a HTML injection you might put some fancy text onto the webpage.

  • if you can do one, you can do either, therefore a distinction is not terribly useful.
    – dandavis
    Commented Mar 26, 2019 at 17:11

4 Answers 4


In general most of the other answers deal with the question correctly but I didn't notice a few important details being omitted from results.

  1. XSS is so ply the ability to execute attacker-controllee JavaScript in the user's browser. This is not limited to HTML injection as you might have guessed. XSS could be considered the lethal subset of HTML injection.

  2. HTML injection would be a simply the ability to inject attack-controlled HTML. Usually refers to instances where all forms of JavaScript appear to be successfully blocked by the application. This is not very serious, worst case scenario I can see if someone injecting a CSS keylogger.

Some users above mentioned that you prevent XSS by encoding the characters (using htmlspecialchars or another similar, alternative function). Whilst this advice is good, it's not true. You should also never inject user input inside a range of element attributes. For example I could use javascript:alert(0) payload to injection inside a link href, iFrame src and a range of other potential locations.


XSS is the ability to execute javascript code (i.e. alert(1)). HTML injection is the ability to execute HTML (such as <p>, like you said). Often they go hand in hand.

When <script> tags are deleted by a filter of the web app, attackers can often still achieve to execute javascript (XSS) by leveraging HTML injection through:

<img src='x' onerror=alert(1);>

HTML injection and XSS injection are not the same. As the name suggest in one you inject HTML tags and the other you try to inject and execute javascript somehow. If you are able to inject HTML you can deface a website but if you inject javascript you could do pretty much anything a real user can do. HTML injection can be avoided by HTML encoding user data while XSS in general can be tricky, and where the user data appears should be taken into account. There is quite a bit of difference between both. But yeah both of them happen due to injection theory and improper validation of user input and putting it directly into the DOM or website.

  • when can you inject html but not JS? I can see it going the other way, like an poorly-sanitized attrib leak, but if whole tags are able to be inserted, when does that preclude <script> tags?
    – dandavis
    Commented Mar 26, 2019 at 17:07
  • One case could be "whitelisting" in which only a few tags are allowed.
    – yeah_well
    Commented Mar 26, 2019 at 17:53

It depends on how you define it but both input validation and output encoding issues.I have no problems with people defining XSS as a kind of HTML injection attack. Then XSS can be further subcategorised as persistent and non persistent(which will further branch out as traditional vs DOM based XSS).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .