Skip to content
This repository was archived by the owner on Jan 7, 2021. It is now read-only.

Commit e05fa24

Browse files
authored
Merge pull request #150 from ehoogerbeets/master
Support asymmetric sanitization to compensate for node-expat's problems
2 parents d3a3af9 + b9b044c commit e05fa24

File tree

6 files changed

+33
-15
lines changed

6 files changed

+33
-15
lines changed

lib/json2xml.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ ToXml.prototype.openTag = function(key) {
7676
}
7777
ToXml.prototype.addAttr = function(key, val) {
7878
if (this.options.sanitize) {
79-
val = sanitizer.sanitize(val);
79+
val = sanitizer.sanitize(val, false, true);
8080
}
8181
this.xml += ' ' + key + '="' + val + '"';
8282
}

lib/sanitize.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,30 @@
1212
* " "
1313
* ' '
1414
*/
15-
var chars = {
15+
// used for body text
16+
var charsEscape = {
17+
'&': '&',
18+
'<': '&lt;',
19+
'>': '&gt;'
20+
};
21+
22+
var charsUnescape = {
23+
'&amp;': '&',
24+
'&#35;': '#',
25+
'&lt;': '<',
26+
'&gt;': '>',
27+
'&#40;': '(',
28+
'&#41;': ')',
29+
'&quot;': '"',
30+
'&apos;': "'",
31+
"&#31;": "\u001F"
32+
};
33+
34+
// used in attribute values
35+
var charsAttrEscape = {
1636
'&': '&amp;',
17-
'#': '&#35;',
1837
'<': '&lt;',
1938
'>': '&gt;',
20-
'(': '&#40;',
21-
')': '&#41;',
2239
'"': '&quot;',
2340
"'": '&apos;'
2441
};
@@ -27,17 +44,17 @@ function escapeRegExp(string) {
2744
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
2845
}
2946

30-
exports.sanitize = function sanitize(value, reverse) {
47+
// sanitize body text
48+
exports.sanitize = function sanitize(value, reverse, attribute) {
3149
if (typeof value !== 'string') {
3250
return value;
3351
}
3452

35-
Object.keys(chars).forEach(function(key) {
36-
if (reverse) {
37-
value = value.replace(new RegExp(escapeRegExp(chars[key]), 'g'), key);
38-
} else {
39-
value = value.replace(new RegExp(escapeRegExp(key), 'g'), chars[key]);
40-
}
53+
var chars = reverse ? charsUnescape : (attribute ? charsAttrEscape : charsEscape);
54+
var keys = Object.keys(chars);
55+
56+
keys.forEach(function(key) {
57+
value = value.replace(new RegExp(escapeRegExp(key), 'g'), chars[key]);
4158
});
4259

4360
return value;

lib/xml2json.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ function endElement(name) {
6060
currentObject[textNodeName()] = currentObject[textNodeName()].trim()
6161
}
6262

63+
// node-expat already reverse sanitizes it whether we like it or not
6364
//if (options.sanitize) {
6465
// currentObject[textNodeName()] = sanitizer.sanitize(currentObject[textNodeName()], true);
6566
//}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xml2json",
3-
"version": "0.11.1",
3+
"version": "0.11.2",
44
"description": "Converts xml to json and vice-versa, using node-expat.",
55
"repository": "git://github.com/buglabs/node-xml2json.git",
66
"license": "MIT",

test/fixtures/xmlsanitize.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"e":{"a":{"b":"Smith & Son","$t":"Movers & <b>Shakers</b> Extraordinaire"}}}
1+
{"e":{"a":{"b":"<\"Smith\" & 'Son'>","$t":"Movers & <b>Shakers</b> Extraordinaire #()\"'"}}}

test/fixtures/xmlsanitize.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<e><a b="Smith &amp; Son">Movers &amp; &lt;b&gt;Shakers&lt;/b&gt; Extraordinaire</a></e>
1+
<e><a b="&lt;&quot;Smith&quot; &amp; &apos;Son&apos;&gt;">Movers &amp; &lt;b&gt;Shakers&lt;/b&gt; Extraordinaire #()"'</a></e>

0 commit comments

Comments
 (0)