Skip to content

fix: reject raw ']]>' in element character data (#901) - #915

Open
chiragbitoni wants to merge 1 commit into
NaturalIntelligence:masterfrom
chiragbitoni:fix/reject-raw-cdata-close-in-char-data
Open

chiragbitoni wants to merge 1 commit into
NaturalIntelligence:masterfrom
chiragbitoni:fix/reject-raw-cdata-close-in-char-data

Conversation

@chiragbitoni

Copy link
Copy Markdown

Purpose / Goal

Under the W3C XML 1.0 Recommendation (§2.4 Character Data and Markup):

[14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)

"The right angle bracket (>) MUST be escaped using '>' or a character reference when it appears in the string ']]>' in content, when that string is not marking the end of a CDATA section."

Previously, XMLValidator.validate() and XMLParser with validation enabled allowed raw ]]> in element text content (such as <a>]]></a> or <a><![CDATA[]]>]]></a>) without reporting an error.

This PR adds a validation check in src/validator.js while reading tag text content to ensure that any literal ]]> sequence outside of a CDATA section is rejected with an InvalidChar error, while valid escaped controls (<a>]]&gt;</a>), CDATA blocks (<a><![CDATA[]]></a>), and attributes (<a attr=']]>'></a>) continue to be accepted.

Fixes #901.

Type

  • Bug Fix
  • Refactoring / Technology upgrade
  • New Feature

)

Signed-off-by: Chirag Bitoni <chiragbitoni4994@gmail.com>

@yxshee yxshee left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the branch locally (validator spec: 66 specs, 0 failures) and compared these inputs with libxml2 (xmllint --noout), which agrees on all of them: <r>]]>x</r>, <r><![CDATA[x]]>]]></r> and <r>x]]> are rejected, <r>]]&gt;</r>, <r>] ]></r>, and ]]> inside a comment, a PI or an attribute value stay valid. The check sits in the character-data branch, so CDATA sections, comments and PIs are unaffected.

One small ordering note: for text after the root element, e.g. <r></r>]]>, the new branch runs before the reachedRoot check, so the error is InvalidChar ("char ']]>' is not expected.") rather than InvalidXml ("Extra text at the end"), which is what <r></r>x or <r></r>]] report (libxml2 reports all of them as extra content at the end). Moving the ]]> check below the reachedRoot branch would keep that message consistent; either way the document is rejected, so this is only a nit.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XMLValidator accepts a literal ]]> in element text

2 participants