Skip to content

fix: apply sanitizeName to tag/attr names inside stopNodes - #33

Open
hicay wants to merge 1 commit into
NaturalIntelligence:mainfrom
hicay:fix-sanitizename-stopnode-bypass
Open

hicay wants to merge 1 commit into
NaturalIntelligence:mainfrom
hicay:fix-sanitizename-stopnode-bypass

Conversation

@hicay

@hicay hicay commented Nov 13, 2026 •

Copy link
Copy Markdown

Summary

sanitizeName is not applied to tag and attribute names inside stopNodes — in both build modes:

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

So when a sanitizeName callback is configured, names inside a stopNode subtree bypass sanitization entirely, allowing quote injection in attribute names and > break-out in tag names to forge document structure.

Before (v1.3.2)

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

// Plain-object mode
builder.build({ root: { raw: { '@_x" onmouseover="alert(1)': 'y', '#text': 'content' } } });
// <root><raw x" onmouseover="alert(1)="y">content</raw></root>
//                  ^ quote injection forges a new attribute

builder.build({ root: { raw: { 'a href="javascript:x"': 'v' } } });
// <root><raw><a href="javascript:x">v</a href="javascript:x"></raw></root>
//              ^ '>' break-out forges tag structure

// preserveOrder mode
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 correctly sanitized (a_href__javascript_x_), so the stopNode path is an inconsistent bypass of the security control.

After

All stopNode name paths now go through resolveName():

fxb.js:

  • buildRawContent(obj, depth, matcher, qNameValidator) — element names are resolved before use; matcher/qNameValidator propagate to nested calls.
  • buildAttributesForStopNode(obj, matcher, qNameValidator) — attribute names are resolved in both the attributesGroupName branch and the individual-attribute branch.

orderedJs2Xml.js:

  • getRawContent(arr, options, depth, matcher, qNameValidator) — nested element names are resolved; parameters propagate to nested calls.
  • attr_to_str_raw(attrMap, options, matcher, qNameValidator) — attribute names are resolved.
  • attr_to_str() — attribute names are now always resolved for stopNodes too (raw VALUE handling is unchanged).

Backwards compatibility: all new parameters default to null, in which case behavior is unchanged (raw name), so direct callers of these internal functions keep working.

Tests

8 new regression specs in spec/sanitize_spec.js — 4 for plain-object mode, 4 for preserveOrder mode, each covering tag-name sanitization, attribute-name sanitization (on the stopNode itself and nested), and no-op for valid names. Full suite: 178 specs, 0 failures.

Fixes #34

buildRawContent()/buildAttributesForStopNode() in fxb.js (plain-object
mode) and getRawContent()/attr_to_str_raw() in orderedJs2Xml.js
(preserveOrder mode) wrote element and attribute names to the output
without routing them through resolveName(). When sanitizeName is
configured, names inside a stopNode subtree therefore bypassed
sanitization entirely, allowing quote injection in attribute names
and '>'/'"' break-out in tag names to forge document structure.

Route all stopNode name paths through resolveName so sanitizeName
applies uniformly. Raw VALUE handling for stopNodes is unchanged.

8 regression specs added (4 plain-object, 4 preserveOrder).
178 specs, 0 failures.
@hicay
hicay force-pushed the fix-sanitizename-stopnode-bypass branch from 2e494ea to af295a4 Compare November 13, 2026 18:58
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.

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

1 participant