Skip to content

Commit 3b97ae5

Browse files
authoredMay 25, 2023
Merge pull request #681 from Leonidas-from-XIV/cve-compat-fix
CVE compat fix
2 parents b856cb8 + 5f6620f commit 3b97ae5

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed
 

‎lib/parser.js

+22-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/parser.coffee

+15-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ defaults = require('./defaults').defaults
1111
isEmpty = (thing) ->
1212
return typeof thing is "object" && thing? && Object.keys(thing).length is 0
1313

14+
isValidKey = (key) ->
15+
return key != '__proto__' && key != 'constructor' && key != 'prototype'
16+
1417
processItem = (processors, item, key) ->
1518
item = process(item, key) for process in processors
1619
return item
@@ -52,6 +55,7 @@ class exports.Parser extends events
5255
@emit err
5356

5457
assignOrPush: (obj, key, newValue) =>
58+
return if not isValidKey(key)
5559
if key not of obj
5660
if not @options.explicitArray
5761
obj[key] = newValue
@@ -102,18 +106,19 @@ class exports.Parser extends events
102106
charkey = @options.charkey
103107

104108
@saxParser.onopentag = (node) =>
105-
obj = Object.create(null)
109+
obj = {}
106110
obj[charkey] = ""
107111
unless @options.ignoreAttrs
108112
for own key of node.attributes
109113
if attrkey not of obj and not @options.mergeAttrs
110-
obj[attrkey] = Object.create(null)
114+
obj[attrkey] = {}
111115
newValue = if @options.attrValueProcessors then processItem(@options.attrValueProcessors, node.attributes[key], key) else node.attributes[key]
112116
processedKey = if @options.attrNameProcessors then processItem(@options.attrNameProcessors, key) else key
113-
if @options.mergeAttrs
114-
@assignOrPush obj, processedKey, newValue
115-
else
116-
obj[attrkey][processedKey] = newValue
117+
if isValidKey(processedKey)
118+
if @options.mergeAttrs
119+
@assignOrPush obj, processedKey, newValue
120+
else
121+
obj[attrkey][processedKey] = newValue
117122

118123
# need a place to store the node name
119124
obj["#name"] = if @options.tagNameProcessors then processItem(@options.tagNameProcessors, node.name) else node.name
@@ -163,7 +168,7 @@ class exports.Parser extends events
163168
# put children into <childkey> property and unfold chars if necessary
164169
if @options.explicitChildren and not @options.mergeAttrs and typeof obj is 'object'
165170
if not @options.preserveChildrenOrder
166-
node = Object.create(null)
171+
node = {}
167172
# separate attributes
168173
if @options.attrkey of obj
169174
node[@options.attrkey] = obj[@options.attrkey]
@@ -181,9 +186,9 @@ class exports.Parser extends events
181186
# append current node onto parent's <childKey> array
182187
s[@options.childkey] = s[@options.childkey] or []
183188
# push a clone so that the node in the children array can receive the #name property while the original obj can do without it
184-
objClone = Object.create(null)
189+
objClone = {}
185190
for own key of obj
186-
objClone[key] = obj[key]
191+
objClone[key] = obj[key] if isValidKey(key)
187192
s[@options.childkey].push objClone
188193
delete obj["#name"]
189194
# re-check whether we can collapse the node now to just the charkey value
@@ -198,7 +203,7 @@ class exports.Parser extends events
198203
if @options.explicitRoot
199204
# avoid circular references
200205
old = obj
201-
obj = Object.create(null)
206+
obj = {}
202207
obj[nodeName] = old
203208

204209
@resultObject = obj

‎test/parser.test.coffee

+10-10
Original file line numberDiff line numberDiff line change
@@ -547,13 +547,13 @@ module.exports =
547547

548548
'test single attrNameProcessors': skeleton(attrNameProcessors: [nameToUpperCase], (r)->
549549
console.log 'Result object: ' + util.inspect r, false, 10
550-
equ {}.hasOwnProperty.call(r.sample.attrNameProcessTest[0].$, 'CAMELCASEATTR'), true
551-
equ {}.hasOwnProperty.call(r.sample.attrNameProcessTest[0].$, 'LOWERCASEATTR'), true)
550+
equ r.sample.attrNameProcessTest[0].$.hasOwnProperty('CAMELCASEATTR'), true
551+
equ r.sample.attrNameProcessTest[0].$.hasOwnProperty('LOWERCASEATTR'), true)
552552

553553
'test multiple attrNameProcessors': skeleton(attrNameProcessors: [nameToUpperCase, nameCutoff], (r)->
554554
console.log 'Result object: ' + util.inspect r, false, 10
555-
equ {}.hasOwnProperty.call(r.sample.attrNameProcessTest[0].$, 'CAME'), true
556-
equ {}.hasOwnProperty.call(r.sample.attrNameProcessTest[0].$, 'LOWE'), true)
555+
equ r.sample.attrNameProcessTest[0].$.hasOwnProperty('CAME'), true
556+
equ r.sample.attrNameProcessTest[0].$.hasOwnProperty('LOWE'), true)
557557

558558
'test single attrValueProcessors': skeleton(attrValueProcessors: [nameToUpperCase], (r)->
559559
console.log 'Result object: ' + util.inspect r, false, 10
@@ -575,21 +575,21 @@ module.exports =
575575

576576
'test single tagNameProcessors': skeleton(tagNameProcessors: [nameToUpperCase], (r)->
577577
console.log 'Result object: ' + util.inspect r, false, 10
578-
equ {}.hasOwnProperty.call(r, 'SAMPLE'), true
579-
equ {}.hasOwnProperty.call(r.SAMPLE, 'TAGNAMEPROCESSTEST'), true)
578+
equ r.hasOwnProperty('SAMPLE'), true
579+
equ r.SAMPLE.hasOwnProperty('TAGNAMEPROCESSTEST'), true)
580580

581581
'test single tagNameProcessors in simple callback': (test) ->
582582
fs.readFile fileName, (err, data) ->
583583
xml2js.parseString data, tagNameProcessors: [nameToUpperCase], (err, r)->
584584
console.log 'Result object: ' + util.inspect r, false, 10
585-
equ {}.hasOwnProperty.call(r, 'SAMPLE'), true
586-
equ {}.hasOwnProperty.call(r.SAMPLE, 'TAGNAMEPROCESSTEST'), true
585+
equ r.hasOwnProperty('SAMPLE'), true
586+
equ r.SAMPLE.hasOwnProperty('TAGNAMEPROCESSTEST'), true
587587
test.finish()
588588

589589
'test multiple tagNameProcessors': skeleton(tagNameProcessors: [nameToUpperCase, nameCutoff], (r)->
590590
console.log 'Result object: ' + util.inspect r, false, 10
591-
equ {}.hasOwnProperty.call(r, 'SAMP'), true
592-
equ {}.hasOwnProperty.call(r.SAMP, 'TAGN'), true)
591+
equ r.hasOwnProperty('SAMP'), true
592+
equ r.SAMP.hasOwnProperty('TAGN'), true)
593593

594594
'test attrValueProcessors key param': skeleton(attrValueProcessors: [replaceValueByName], (r)->
595595
console.log 'Result object: ' + util.inspect r, false, 10

0 commit comments

Comments
 (0)
Please sign in to comment.