Skip to content

Invalid character '#' in entity name: "#xD" #823

@FlowmasterHaggla

Description

@FlowmasterHaggla
  • Are you running the latest version?

    • Have you included sample input, output, error, and expected output?
    • Have you checked if you are using correct configuration?
    • Did you try online tool?
    • Have you checked the docs for helpful APIs and examples?

    Description

    Regression in fast-xml-parser@5.7.0: parser.addEntity("#xD", "\r") succeeds, but parsing later fails with [EntityReplacer] Invalid character '#' in entity name: "#xD".

    This breaks the AWS XML protocol usage pattern (@aws-sdk/xml-builder) which registers #xD and #10 entities before parsing XML responses.

    Input

    Run with Node.js (reproduced on v22.18.0 and v24.13.0) and fast-xml-parser@5.7.0.

    Code

    const { XMLParser } = require("fast-xml-parser");
    
    const xml =
      '<?xml version="1.0" encoding="UTF-8"?>' +
      "<Tagging><TagSet>" +
      "<Tag><Key>kind</Key><Value>anamnesis</Value></Tag>" +
      "<Tag><Key>pii</Key><Value>true</Value></Tag>" +
      "<Tag><Key>retention</Key><Value>permanent</Value></Tag>" +
      "</TagSet></Tagging>";
    
    const parser = new XMLParser({
      attributeNamePrefix: "",
      processEntities: {
        enabled: true,
        maxTotalExpansions: Number.POSITIVE_INFINITY,
      },
      htmlEntities: true,
      ignoreAttributes: false,
      ignoreDeclaration: true,
      parseTagValue: false,
      trimValues: false,
      tagValueProcessor: (_, val) =>
        val.trim() === "" && val.includes("\n") ? "" : undefined,
      maxNestedTags: Number.POSITIVE_INFINITY,
    });
    
    parser.addEntity("#xD", "\r"); // succeeds
    parser.addEntity("#10", "\n"); // succeeds
    
    // throws in 5.7.0:
    const out = parser.parse(xml, true);
    console.log(out);
    
    ### Output
    
    Error: [EntityReplacer] Invalid character '#' in entity name: "#xD"
    
    ### expected data

#xD is a legal XML Character -> https://www.w3.org/TR/2006/PER-xml-20060614/
The XML should parse successfully, returning an object like:

{
Tagging: {
TagSet: {
Tag: [
{ Key: "kind", Value: "anamnesis" },
{ Key: "pii", Value: "true" },
{ Key: "retention", Value: "permanent" }
]
}
}
}

Additional context from version matrix:

  • @aws-sdk/xml-builder@3.972.18 with nested fast-xml-parser@5.5.8 => works
  • same setup with npm override forcing fast-xml-parser@5.7.0 => fails with error above

Would you like to work on this issue?

  • Yes
  • No

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions