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

Commit b9d3594

Browse files
committed
security: remove regexp vulnerable to DOS in cast option, npm report 69742
1 parent 76d96e1 commit b9d3594

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
* max_comment_size: new option
88
* promise: new API module
99

10+
## Trunk
11+
12+
* security: remove regexp vulnerable to DOS in cast option, npm report 69742
13+
1014
## Version 4.4.5
1115

1216
* ts: add buffer as allowed type for input, fix #248

lib/es5/index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -985,21 +985,20 @@ function (_Transform) {
985985
}
986986
}
987987

988-
if (this.__isInt(field) === true) {
989-
return [undefined, parseInt(field)];
990-
} else if (this.__isFloat(field)) {
988+
if (this.__isFloat(field)) {
991989
return [undefined, parseFloat(field)];
992990
} else if (this.options.cast_date !== false) {
993991
return [undefined, this.options.cast_date.call(null, field, context)];
994992
}
995993

996994
return [undefined, field];
997-
}
998-
}, {
999-
key: "__isInt",
1000-
value: function __isInt(value) {
1001-
return /^(\-|\+)?([1-9]+[0-9]*)$/.test(value);
1002-
}
995+
} // Keep it in case we implement the `cast_int` option
996+
// __isInt(value){
997+
// // return Number.isInteger(parseInt(value))
998+
// // return !isNaN( parseInt( obj ) );
999+
// return /^(\-|\+)?[1-9][0-9]*$/.test(value)
1000+
// }
1001+
10031002
}, {
10041003
key: "__isFloat",
10051004
value: function __isFloat(value) {

lib/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -747,18 +747,19 @@ class Parser extends Transform {
747747
return [err]
748748
}
749749
}
750-
if(this.__isInt(field) === true){
751-
return [undefined, parseInt(field)]
752-
}else if(this.__isFloat(field)){
750+
if(this.__isFloat(field)){
753751
return [undefined, parseFloat(field)]
754752
}else if(this.options.cast_date !== false){
755753
return [undefined, this.options.cast_date.call(null, field, context)]
756754
}
757755
return [undefined, field]
758756
}
759-
__isInt(value){
760-
return /^(\-|\+)?([1-9]+[0-9]*)$/.test(value)
761-
}
757+
// Keep it in case we implement the `cast_int` option
758+
// __isInt(value){
759+
// // return Number.isInteger(parseInt(value))
760+
// // return !isNaN( parseInt( obj ) );
761+
// return /^(\-|\+)?[1-9][0-9]*$/.test(value)
762+
// }
762763
__isFloat(value){
763764
return (value - parseFloat( value ) + 1) >= 0 // Borrowed from jquery
764765
}

test/option.cast.coffee

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ describe 'Option `cast`', ->
3131
parser.end()
3232

3333
it 'ints', (next) ->
34-
parse '123a,123,0123,', cast: true, (err, data) ->
35-
data.should.eql [ ['123a', 123, 123, ''] ]
34+
parse '123a,123,+123,-123,0123,+0123,-0123,', cast: true, (err, data) ->
35+
data.should.eql [ ['123a', 123, 123, -123, 123, 123, -123, ''] ]
36+
next()
37+
38+
it 'ints isnt exposed to DOS vulnerabilities, npm security issue 69742', (next) ->
39+
data = Array.from( length: 3000000 ).map( (x) -> '1' ).join('') + '!'
40+
parse data, cast: true, (err, data) ->
41+
data[0][0].length.should.eql 3000001
3642
next()
3743

3844
it 'float', (next) ->
@@ -54,11 +60,11 @@ describe 'Option `cast`', ->
5460
, (err, records) ->
5561
records.should.eql [
5662
[ '2000-01-01T05:00:00.000Z', {
57-
column: 1, empty_lines: 0, header: false, index: 1,
63+
column: 1, empty_lines: 0, header: false, index: 1,
5864
invalid_field_length: 0, lines: 1, quoting: false, records: 0
5965
} ]
6066
[ '2050-11-27T05:00:00.000Z', {
61-
column: 1, empty_lines: 0, header: false, index: 1,
67+
column: 1, empty_lines: 0, header: false, index: 1,
6268
invalid_field_length: 0, lines: 2, quoting: false, records: 1
6369
} ]
6470
] unless err

0 commit comments

Comments
 (0)