Skip to content

Commit 3806d87

Browse files
targostrevnorris
authored andcommitted
zlib: prevent uncaught exception in zlibBuffer
If the accumulation of data for the final Buffer is greater than kMaxLength it will throw an un-catchable RangeError. Instead now pass the generated error to the callback. PR-URL: #1811 Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 953b3e7 commit 3806d87

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

‎lib/zlib.js‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const Transform = require('_stream_transform');
55
constbinding=process.binding('zlib');
66
constutil=require('util');
77
constassert=require('assert').ok;
8+
constkMaxLength=process.binding('smalloc').kMaxLength;
9+
constkRangeErrorMessage='Cannot create final Buffer. '+
10+
'It would be larger than 0x'+kMaxLength.toString(16)+' bytes.';
811

912
// zlib doesn't provide these, so kludge them in following the same
1013
// const naming scheme zlib uses.
@@ -210,10 +213,18 @@ function zlibBuffer(engine, buffer, callback){
210213
}
211214

212215
functiononEnd(){
213-
varbuf=Buffer.concat(buffers,nread);
216+
varbuf;
217+
varerr=null;
218+
219+
if(nread>=kMaxLength){
220+
err=newRangeError(kRangeErrorMessage);
221+
}else{
222+
buf=Buffer.concat(buffers,nread);
223+
}
224+
214225
buffers=[];
215-
callback(null,buf);
216226
engine.close();
227+
callback(err,buf);
217228
}
218229
}
219230

@@ -524,6 +535,11 @@ Zlib.prototype._processChunk = function(chunk, flushFlag, cb){
524535
throwerror;
525536
}
526537

538+
if(nread>=kMaxLength){
539+
this.close();
540+
thrownewRangeError(kRangeErrorMessage);
541+
}
542+
527543
varbuf=Buffer.concat(buffers,nread);
528544
this.close();
529545

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
constassert=require('assert');
4+
5+
// Change kMaxLength for zlib to trigger the error
6+
// without having to allocate 1GB of buffers
7+
constsmalloc=process.binding('smalloc');
8+
smalloc.kMaxLength=128;
9+
constzlib=require('zlib');
10+
smalloc.kMaxLength=0x3fffffff;
11+
12+
constencoded=newBuffer('H4sIAAAAAAAAA0tMHFgAAIw2K/GAAAAA','base64');
13+
14+
// Async
15+
zlib.gunzip(encoded,function(err){
16+
assert.ok(errinstanceofRangeError);
17+
});
18+
19+
// Sync
20+
assert.throws(function(){
21+
zlib.gunzipSync(encoded);
22+
},RangeError);

0 commit comments

Comments
(0)