@@ -51,11 +51,20 @@ internals.state = {
5151} ;
5252
5353
54- exports . Dispenser = internals . Dispenser = function ( contentType ) {
54+ internals . defaults = {
55+ maxBytes : Infinity
56+ } ;
57+
58+
59+ exports . Dispenser = internals . Dispenser = function ( options ) {
5560
5661 Stream . Writable . call ( this ) ;
5762
58- this . _boundary = contentType . boundary ;
63+ Hoek . assert ( options !== null && typeof options === 'object' ,
64+ 'options must be an object' ) ;
65+ const settings = Hoek . applyToDefaults ( internals . defaults , options ) ;
66+
67+ this . _boundary = settings . boundary ;
5968 this . _state = internals . state . preamble ;
6069 this . _held = '' ;
6170
@@ -64,8 +73,10 @@ exports.Dispenser = internals.Dispenser = function (contentType) {
6473 this . _name = '' ;
6574 this . _pendingHeader = '' ;
6675 this . _error = null ;
76+ this . _bytes = 0 ;
77+ this . _maxBytes = settings . maxBytes ;
6778
68- this . _parts = new Nigel . Stream ( new Buffer ( '--' + contentType . boundary ) ) ;
79+ this . _parts = new Nigel . Stream ( new Buffer ( '--' + settings . boundary ) ) ;
6980 this . _lines = new Nigel . Stream ( new Buffer ( '\r\n' ) ) ;
7081
7182 this . _parts . on ( 'needle' , ( ) => {
@@ -102,6 +113,7 @@ exports.Dispenser = internals.Dispenser = function (contentType) {
102113 let finish = ( err ) => {
103114
104115 if ( piper ) {
116+ piper . removeListener ( 'data' , onReqData ) ;
105117 piper . removeListener ( 'error' , finish ) ;
106118 piper . removeListener ( 'aborted' , onReqAborted ) ;
107119 }
@@ -143,9 +155,19 @@ exports.Dispenser = internals.Dispenser = function (contentType) {
143155 finish ( Boom . badRequest ( 'Client request aborted' ) ) ;
144156 } ;
145157
158+ const onReqData = ( data ) => {
159+
160+ this . _bytes += Buffer . byteLength ( data ) ;
161+
162+ if ( this . _bytes > this . _maxBytes ) {
163+ finish ( Boom . badRequest ( 'Maximum size exceeded' ) ) ;
164+ }
165+ } ;
166+
146167 this . once ( 'pipe' , ( req ) => {
147168
148169 piper = req ;
170+ req . on ( 'data' , onReqData ) ;
149171 req . once ( 'error' , finish ) ;
150172 req . once ( 'aborted' , onReqAborted ) ;
151173 } ) ;
0 commit comments