Skip to content

xml: parse decodes XML entities twice (and inside CDATA), and stringify leaves < unescaped in attributes #130

Open

Description

@trackoor

@libs/xml 8.x decodes XML entities a second time during parse (including inside CDATA) and does not escape < in attribute values during stringify, breaking stringify -> parse round-tripping under default options.

1. Double entity decoding (and CDATA decoding) in parse

repro-parse.ts:

import { parse, stringify } from "jsr:@libs/xml@8.0.1"

console.log(parse(`<r a="&amp;lt;">&amp;amp; &amp;#65;</r>`))
console.log(parse(`<r><![CDATA[&lt; &#65;]]></r>`))
console.log(parse(stringify({ r: "&amp;" })))

Run with deno run repro-parse.ts:

Actual:

{ r: { "#text": "& A", "@a": "<" } }
{ r: "< A" }
{ r: "&" }

Expected:

{ r: { "#text": "&amp; &#65;", "@a": "&lt;" } }
{ r: "&lt; &#65;" }
{ r: "&amp;" }

(@libs/xml@8.0.1/wasm/parse and @std/xml return &amp; &#65; and @a: "&lt;" for the first input.) @std/xml already decodes character and predefined entity references while leaving CDATA literal, so running revive with options.revive.entities ??= true in @libs/xml/_parser.ts decodes the already-decoded strings again.

2. Unescaped < in attribute values in stringify

repro-stringify.ts:

import { parse, stringify } from "jsr:@libs/xml@8.0.1"

const xml = stringify({ r: { "@a": "x<y", "#text": "1" } })
console.log(xml)
parse(xml)

Run with deno run repro-stringify.ts:

Actual:

<r a="x<y">1</r>
error: Uncaught (in promise) XmlSyntaxError: Cannot use '<' in attribute value at line 1, column 8

Expected: <r a="x&lt;y">1</r>, which parses back to { r: { "#text": "1", "@a": "x<y" } } (< is disallowed in attribute values per https://www.w3.org/TR/xml/#NT-AttValue). xml_attributes in @libs/xml/stringify.ts escapes ["&", '"', "'"] by default, omitting "<".

Versions tested: jsr:@libs/xml@8.0.1 (Deno 2.9.8), npm @lowlighter/xml@8.0.1 (Node v26.13.0), and lowlighter/libs main (1e152ad).

Credit: https://ucsbplse.github.io/

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions