Skip to content

Commit ae22f55

Browse files
aularonisaacs
authored andcommitted
Check for invalid code points before passing them to String.fromCodePoint
- Now the call will not throw RangeError exceptions - Tests are included for both strict: true and strict: false modes - Fixes #116 and #160 PR-URL: #163 Credit: @aularon Close: #163 Reviewed-by: @isaacs
1 parent fce4e2f commit ae22f55

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/sax.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,12 @@
999999
}
10001000
}
10011001
entity = entity.replace(/^0+/, '')
1002-
if (isNaN(num) || numStr.toLowerCase() !== entity) {
1002+
if (
1003+
isNaN(num) ||
1004+
numStr.toLowerCase() !== entity ||
1005+
num < 0 ||
1006+
num > 0x10ffff
1007+
) {
10031008
strictFail(parser, 'Invalid character entity')
10041009
return '&' + parser.entity + ';'
10051010
}

test/invalid-entities.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var invalidEntities = ['1114112', '-1', 'NaN']
2+
3+
for (var i = invalidEntities.length - 1; i >= 0; --i) {
4+
require(__dirname).test({
5+
xml: '<r>&#' + invalidEntities[i] + ';</r>',
6+
strict: false,
7+
expect: [
8+
['opentagstart', { name: 'R', attributes: {} }],
9+
['opentag', { name: 'R', attributes: {}, isSelfClosing: false }],
10+
['text', '&#' + invalidEntities[i] + ';'],
11+
['closetag', 'R'],
12+
],
13+
})
14+
require(__dirname).test({
15+
xml: '<r>&#' + invalidEntities[i] + ';</r>',
16+
strict: true,
17+
expect: [
18+
['opentagstart', { name: 'r', attributes: {} }],
19+
['opentag', { name: 'r', attributes: {}, isSelfClosing: false }],
20+
[
21+
'error',
22+
'Invalid character entity\nLine: 0\nColumn: ' +
23+
(6 + invalidEntities[i].length) +
24+
'\nChar: ;',
25+
],
26+
['text', '&#' + invalidEntities[i] + ';'],
27+
['closetag', 'r'],
28+
],
29+
})
30+
}

0 commit comments

Comments
 (0)