Extract the contents of escaped CDATA sections (#440)#572
Open
Labib-Bin-Salam wants to merge 1 commit into
Open
Extract the contents of escaped CDATA sections (#440)#572Labib-Bin-Salam wants to merge 1 commit into
Labib-Bin-Salam wants to merge 1 commit into
Conversation
When a feed XML-escapes a CDATA section -- for example, a <description> whose text content is the literal string '<![CDATA[...]]>' -- the HTML processor handled the marked section with SGMLParser.unknown_decl(), whose default implementation discards it. The character data was therefore dropped and the field was parsed as an empty string. Override unknown_decl() in BaseHTMLProcessor to emit the contents of a CDATA marked section (escaped for the surrounding markup) instead of discarding them. (kurtmckee#440)
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.
Fixes #440.
The problem
When a feed XML-escapes a CDATA section, feedparser parses the field as an empty string. For example:
>>> feedparser.parse(rss).entries[0].description '' # expected: 'some text'After the document is parsed, the text content of
<description>is the literal string<![CDATA[some text]]>. feedparser treats that content as HTML, so it runs through the SGML-based HTML processor, which recognizes<![CDATA[ ... ]]>as a marked section and hands its body tounknown_decl().BaseHTMLProcessornever overrodeunknown_decl(), so the basesgmllib.SGMLParser.unknown_decl()ran — and its default implementation discards the data. The character data was silently dropped.This has been reported several times against real-world feeds (e.g. the feeds linked in #440).
The fix
Override
unknown_decl()inBaseHTMLProcessorto emit the contents of a CDATA marked section instead of discarding them. The contents of a CDATA section are character data, so the few characters that are special in the surrounding markup (&,<,>) are escaped before being emitted. This keeps a script smuggled inside an escaped CDATA section inert — it is rendered as literal text rather than executed.Real (unescaped) CDATA sections are unaffected: the XML parser strips those before the HTML processor ever sees them.
Tests
tests/wellformed/sgml/escaped_cdata_section.xmlcovering the reported case (runs under both the strict and loose parsers).4307 passed).