Skip to content

Commit

Permalink
Ignore (strip) illegal characters.
Browse files Browse the repository at this point in the history
Leaving the invalid sequences in the document seems incorrect.
Another option could be to throw an error, seeing as the characters
are not legal.

Not doing this would probably open up attack vectors allowing the
entry of binary data and control sequences into data structures.
  • Loading branch information
mogsie committed Jun 15, 2018
1 parent 9cc1867 commit ff29990
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ var unescapeXMLTable = {

function unescapeXMLReplace (match) {
if (match[1] === '#') {
var num
if (match[2] === 'x') {
return String.fromCodePoint(parseInt(match.slice(3), 16))
num = parseInt(match.slice(3), 16)
} else {
return String.fromCodePoint(parseInt(match.slice(2), 10))
num = parseInt(match.slice(2), 10)
}
// https://www.w3.org/TR/xml/#NT-Char defines legal XML characters:
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
if (num === 0x9 || num === 0xA || num === 0xD ||
(num >= 0x20 && num <= 0xD7FF) ||
(num >= 0xE000 && num <= 0xFFFD) ||
(num >= 0x10000 && num <= 0x10FFFF)) {
return String.fromCodePoint(num)
}
return ''
}
return unescapeXMLTable[match]
}
Expand Down
3 changes: 3 additions & 0 deletions test/escape-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ vows.describe('escape').addBatch({
'unescapes numeric entities': function () {
assert.equal(unescapeXML('&#64;'), '@')
},
'strips control characters': function () {
assert.equal(unescapeXML('&#0;'), '')
},
'unescapes hexadecimal entities': function () {
assert.equal(unescapeXML('&#x40;'), '@')
},
Expand Down

0 comments on commit ff29990

Please sign in to comment.