Skip to content

Instantly share code, notes, and snippets.

@AntonLydike
Last active April 15, 2021 11:44
Show Gist options
  • Save AntonLydike/8516520dd0db85dca99c3700026d6324 to your computer and use it in GitHub Desktop.
Save AntonLydike/8516520dd0db85dca99c3700026d6324 to your computer and use it in GitHub Desktop.
Non-terminating HTML Document

Creating valid HTML documents that can never be parsed

While watching Andreas Klings video on building an HTML parser, I noticed, that a piece of HTML can be constructed, which will never terminate when parsed.

The document.write() keyword:

When parsing an HTML document, scripts have to be executed and their document.write() output has to be parsed as well. This allows us, js to insert html into a document at parse-time, such as ... Hello <script>document.write(localStorage.getItem('username'))</script> ... to print greetings. The spec allows for document.write to write out any html tags, even <script>, which has to be executed again as per the spec. You might see where this is going...

This can now be used, similar to a zip bomb or xml bomb, to create documents which will never finish parsing (or to grow in size indefinitely, similar to other modern web apps). See the example below:

<html>
	<head>
		<title>what</title>
	</head>
	<body>
	<script>document.write(document.getElementsByTagName("script")[0].outerHTML)</script>
	</body>
</html>

In the real world, all browsers tested are actually not conforming to the spec and stop after 22 tags (so 21 newly generated ones).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment