You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Control characters not escaped when creating an xml document #116
Describe the bug
We wound up with a control character in a document we intended to serialize to XML using xmlbuilder2, and we found that it doesn't end up escaping, throwing them out, or error.
If I use Jest's inline snapshot functionality, I get something that looks like this in VSCode (shows the control character, un-escaped with VSCode's representation of it):
Expected behavior
Ideally it would escape them (though not sure XML 1.0 allows this). If escaping isn't possible, ideally there would be an option to throw them out silently or error.
Version:
node.js: 16 LTS
xmlbuilder2: 3.0.2 (though tried 2.x as well)
Additional context
Happy to provide more (though feel like repro case should capture it)! Thanks for your help!
Just dug into this one. Looks like the library already has exactly the option you were asking for, it's just not surfaced anywhere obvious.
Passing { wellFormed: true } to .end() (or as a writer option elsewhere) makes the serializer check every text/attribute value against the XML Char production before emitting it, and it throws instead of silently writing out an invalid control character:
create({encoding: 'utf-8',version: '1.0'},{test: {value: '\b'}}).end({wellFormed: true})// throws: Text data contains invalid characters (well-formed required).
Without that flag the default behavior is to leave the character as-is, per the DOM XMLSerializer spec's own wording (only validate well-formedness when explicitly asked to). That's documented at https://oozcitak.github.io/xmlbuilder2/pages/serialization.html#serialization-settings, just easy to miss since it's not mentioned in the top-level README.
Since escaping isn't actually legal here (XML 1.0 doesn't allow a character reference to resolve to an illegal Char either), throwing via wellFormed seems like the right tool for what you were after rather than a new option. Might be worth closing this one, or maybe just linking wellFormed from the README so it's easier to find next time someone hits this.
Activity
afonsojanu commentedon Sep 7, 2026
Just dug into this one. Looks like the library already has exactly the option you were asking for, it's just not surfaced anywhere obvious.
Passing
{ wellFormed: true }to.end()(or as a writer option elsewhere) makes the serializer check every text/attribute value against the XML Char production before emitting it, and it throws instead of silently writing out an invalid control character:Without that flag the default behavior is to leave the character as-is, per the DOM XMLSerializer spec's own wording (only validate well-formedness when explicitly asked to). That's documented at https://oozcitak.github.io/xmlbuilder2/pages/serialization.html#serialization-settings, just easy to miss since it's not mentioned in the top-level README.
Since escaping isn't actually legal here (XML 1.0 doesn't allow a character reference to resolve to an illegal Char either), throwing via
wellFormedseems like the right tool for what you were after rather than a new option. Might be worth closing this one, or maybe just linkingwellFormedfrom the README so it's easier to find next time someone hits this.