Stringify sanitized HTML as HTML (not XML)#3575
Merged
tillprochaska merged 1 commit intodevelopfrom Jan 25, 2024
Merged
Conversation
The `etree.ElementTree.tostring` method stringifies as valid XML by default. While XML allows self-closing tags by default, only some tags can be self-closing in HTML. This causes problems when rendering the sanitized HTML. Consider this example: ```html <noscript><script src="proxy.php?url=..."></script></noscript> <h1>Page title</h1> <p>Some text</p> ``` When converting the sanitized element tree to XML, this would result in something like this: ```html <noscript /> <h1>Page title</h1> <p>Some text</p> ``` `noscript` is not a valid void element which results in browsers treating all following elements as children of the `noscript` tag, parsing the HTML into an effective DOM structure like this: ```html <noscript> <h1>Page title</h1> <p>Some text</p> </noscript> ``` As a result the actual page contents are hidden when rendered in a browser (if JavaScript is enabled).
stchris
approved these changes
Jan 25, 2024
Contributor
stchris
left a comment
There was a problem hiding this comment.
Brilliant investigation, thanks for documenting this in detail! And for fixing it, of course!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes a bug where Aleph doesn’t correctly preview HTML pages (see #3334).
The
etree.ElementTree.tostringmethod stringifies as valid XML by default. While XML allows self-closing tags by default, only some tags can be self-closing in HTML.This causes problems when rendering the sanitized HTML. Consider this example:
When converting the sanitized element tree to XML, this would result in something like this:
noscriptis not a valid void element which results in browsers treating all following elements as children of thenoscripttag, parsing the HTML into an effective DOM structure like this:As a result the actual page contents are hidden when rendered in a browser (if JavaScript is enabled).