sanitizeName not applied to names inside stopNodes (quote injection / tag break-out bypass)

Open
#34 1 comment 0 reactions 0 assignees View on GitHub

@hicay is already working on this.

Since Nov 13, 2026.

  • #33 by @hicay — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript
Domain
security

Research direction

The unsanitized names come from buildRawContent() and buildAttributesForStopNode() in fxb.js, and from getRawContent(), attr_to_str_raw() and attr_to_str() in orderedJs2Xml.js. Start by reading how resolveName() is used on the regular path, then route the stopNode paths through it. Done means the three reproduction payloads come out sanitized in both build modes, with regression tests. The issue says PR #33 already implements this, so check that PR before starting.

Written by the indexing model from the issue text.

Description

Description

When a sanitizeName callback is configured, it is applied to tag and attribute names on the regular build path — but not inside stopNodes. This affects both build modes:

  • fxb.js (plain-object mode): buildRawContent() and buildAttributesForStopNode() write names to the output without routing them through resolveName().
  • orderedJs2Xml.js (preserveOrder mode): getRawContent() and attr_to_str_raw() do the same, and attr_to_str() explicitly skips name sanitization when isStopNode is true.

Reproduction

const { XMLBuilder } = require("fast-xml-builder");

const builder = new XMLBuilder({
  ignoreAttributes: false,
  sanitizeName: (n) => n.replace(/[^\w.:-]/g, '_'), // per docs
  stopNodes: ["root.raw"],
});

// 1. Quote injection in an attribute name forges a new attribute:
builder.build({ root: { raw: { '#text': 'content', '@_x" onmouseover="alert(1)': 'y' } } });
// <root><raw x" onmouseover="alert(1)="y">content</raw></root>

// 2. '>' in a tag name breaks out of the tag
builder.build({ root: { raw: { 'a href="javascript:x"': 'v' } } });
// <root><raw><a href="javascript:x">v</a href="javascript:x"></raw></root>

// 3. preserveOrder mode is also affected
builder.build([{ root: [{ raw: [{ '#text': 'x' }, { 'a onclick="alert(1)': [{ '#text': 'y' }] }] }] }]);
// <root><raw>x<a onclick="alert(1)">y</a onclick="alert(1)"></raw></root>

The same payload on a non-stopNode path is sanitized correctly (a_href__javascript_x_), so this is an inconsistent bypass of the sanitizeName security control (CWE-74: Special Element Injection) rather than missing-by-design behavior — sanitizeName was introduced in 1.2.0 specifically to guard against injection via invalid XML names.

Impact

Any application that sets both sanitizeName and stopNodes gets no name sanitization for everything inside the stopNode subtree: quotes in attribute names can forge additional attributes, and > can close a tag early, changing document structure for downstream consumers. Round-tripping through the parser yields a structure that differs from what was built, so this can also cause silent data-integrity problems.

Suggested fix

Route both paths through the existing resolveName() helper (pass matcher and qNameValidator down from arrToStr/processTextOrObjNode). PR #33 implements this fix plus regression tests for both build modes.

Dominant language
JavaScript
Stars
22
Forks
9
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NaturalIntelligence/fast-xml-builder

All issues in NaturalIntelligence/fast-xml-builder

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.