Skip to content

Commit 0e066ba

Browse files
authored
Update: use safe Buffer allocators instead of unsafe constructor (#184)
1 parent c5c4f3f commit 0e066ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+326
-335
lines changed

.eslintrc

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
],
1313
"rules": {
1414
"mocha-no-only/mocha-no-only": ["warn"],
15+
"no-buffer-constructor": ["error"],
1516
"no-shadow": ["off"],
1617
"no-underscore-dangle": ["off"],
1718
"strict": ["error", "global"],

lib/algorithms/aes-cbc-hmac-sha2.js

+25-25
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function commonCbcEncryptFN(size) {
7373
return helpers.subtleCrypto.encrypt(alg, key, pdata);
7474
});
7575
promise = promise.then(function(cdata) {
76-
cdata = new Buffer(cdata);
76+
cdata = Buffer.from(cdata);
7777
return cdata;
7878
});
7979

@@ -163,7 +163,7 @@ function commonCbcDecryptFN(size) {
163163
return helpers.subtleCrypto.decrypt(alg, key, cdata);
164164
});
165165
promise = promise.then(function(pdata) {
166-
pdata = new Buffer(pdata);
166+
pdata = Buffer.from(pdata);
167167
return pdata;
168168
});
169169

@@ -215,8 +215,8 @@ function cbcHmacEncryptFN(size) {
215215

216216
var eKey = key.slice(size / 8),
217217
iKey = key.slice(0, size / 8),
218-
iv = props.iv || new Buffer(0),
219-
adata = props.aad || props.adata || new Buffer(0);
218+
iv = props.iv || Buffer.alloc(0),
219+
adata = props.aad || props.adata || Buffer.alloc(0);
220220

221221
// STEP 1 -- Encrypt
222222
var promise = commonEncrypt(eKey, pdata, iv);
@@ -262,9 +262,9 @@ function cbcHmacDecryptFN(size) {
262262

263263
var eKey = key.slice(size / 8),
264264
iKey = key.slice(0, size / 8),
265-
iv = props.iv || new Buffer(0),
266-
adata = props.aad || props.adata || new Buffer(0),
267-
tag = props.tag || props.mac || new Buffer(0);
265+
iv = props.iv || Buffer.alloc(0),
266+
adata = props.aad || props.adata || Buffer.alloc(0),
267+
tag = props.tag || props.mac || Buffer.alloc(0);
268268

269269
var promise = Promise.resolve();
270270

@@ -299,9 +299,9 @@ function cbcHmacDecryptFN(size) {
299299
};
300300
}
301301

302-
var EncryptionLabel = new Buffer("Encryption", "utf8");
303-
var IntegrityLabel = new Buffer("Integrity", "utf8");
304-
var DotLabel = new Buffer(".", "utf8");
302+
var EncryptionLabel = Buffer.from("Encryption", "utf8");
303+
var IntegrityLabel = Buffer.from("Integrity", "utf8");
304+
var DotLabel = Buffer.from(".", "utf8");
305305

306306
function generateCek(masterKey, alg, epu, epv) {
307307
var masterSize = masterKey.length * 8;
@@ -313,7 +313,7 @@ function generateCek(masterKey, alg, epu, epv) {
313313
helpers.int32ToBuffer(1),
314314
masterKey,
315315
helpers.int32ToBuffer(cekSize),
316-
new Buffer(alg, "utf8"),
316+
Buffer.from(alg, "utf8"),
317317
epu,
318318
epv,
319319
EncryptionLabel
@@ -342,7 +342,7 @@ function generateCik(masterKey, alg, epu, epv) {
342342
helpers.int32ToBuffer(1),
343343
masterKey,
344344
helpers.int32ToBuffer(cikSize),
345-
new Buffer(alg, "utf8"),
345+
Buffer.from(alg, "utf8"),
346346
epu,
347347
epv,
348348
IntegrityLabel
@@ -367,9 +367,9 @@ function concatKdfCbcHmacEncryptFN(size, alg) {
367367
return function(key, pdata, props) {
368368
var epu = props.epu || helpers.int32ToBuffer(0),
369369
epv = props.epv || helpers.int32ToBuffer(0),
370-
iv = props.iv || new Buffer(0),
371-
adata = props.aad || props.adata || new Buffer(0),
372-
kdata = props.kdata || new Buffer(0);
370+
iv = props.iv || Buffer.alloc(0),
371+
adata = props.aad || props.adata || Buffer.alloc(0),
372+
kdata = props.kdata || Buffer.alloc(0);
373373

374374
// Pre Step 1 -- Generate Keys
375375
var promises = [
@@ -394,11 +394,11 @@ function concatKdfCbcHmacEncryptFN(size, alg) {
394394
var mdata = Buffer.concat([
395395
adata,
396396
DotLabel,
397-
new Buffer(kdata),
397+
Buffer.from(kdata),
398398
DotLabel,
399-
new Buffer(util.base64url.encode(iv), "utf8"),
399+
Buffer.from(util.base64url.encode(iv), "utf8"),
400400
DotLabel,
401-
new Buffer(util.base64url.encode(cdata), "utf8")
401+
Buffer.from(util.base64url.encode(cdata), "utf8")
402402
]);
403403
return Promise.all([
404404
Promise.resolve(cdata),
@@ -422,10 +422,10 @@ function concatKdfCbcHmacDecryptFN(size, alg) {
422422
return function(key, cdata, props) {
423423
var epu = props.epu || helpers.int32ToBuffer(0),
424424
epv = props.epv || helpers.int32ToBuffer(0),
425-
iv = props.iv || new Buffer(0),
426-
adata = props.aad || props.adata || new Buffer(0),
427-
kdata = props.kdata || new Buffer(0),
428-
tag = props.tag || props.mac || new Buffer(0);
425+
iv = props.iv || Buffer.alloc(0),
426+
adata = props.aad || props.adata || Buffer.alloc(0),
427+
kdata = props.kdata || Buffer.alloc(0),
428+
tag = props.tag || props.mac || Buffer.alloc(0);
429429

430430
// Pre Step 1 -- Generate Keys
431431
var promises = [
@@ -447,11 +447,11 @@ function concatKdfCbcHmacDecryptFN(size, alg) {
447447
var mdata = Buffer.concat([
448448
adata,
449449
DotLabel,
450-
new Buffer(kdata),
450+
Buffer.from(kdata),
451451
DotLabel,
452-
new Buffer(util.base64url.encode(iv), "utf8"),
452+
Buffer.from(util.base64url.encode(iv), "utf8"),
453453
DotLabel,
454-
new Buffer(util.base64url.encode(cdata), "utf8")
454+
Buffer.from(util.base64url.encode(cdata), "utf8")
455455
]);
456456

457457
try {

lib/algorithms/aes-gcm.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ function gcmEncryptFN(size) {
2121

2222
// ### 'fallback' implementation -- uses forge
2323
var fallback = function(key, pdata, props) {
24-
var iv = props.iv || new Buffer(0),
25-
adata = props.aad || props.adata || new Buffer(0),
24+
var iv = props.iv || Buffer.alloc(0),
25+
adata = props.aad || props.adata || Buffer.alloc(0),
2626
cipher,
2727
cdata;
2828

@@ -40,7 +40,7 @@ function gcmEncryptFN(size) {
4040
additionalData: adata
4141
});
4242
// ciphertext is the same length as plaintext
43-
cdata = new Buffer(pdata.length);
43+
cdata = Buffer.alloc(pdata.length);
4444

4545
var promise = new Promise(function(resolve, reject) {
4646
var amt = CONSTANTS.CHUNK_SIZE,
@@ -82,8 +82,8 @@ function gcmEncryptFN(size) {
8282
// ### WebCryptoAPI implementation
8383
// TODO: cache CryptoKey sooner
8484
var webcrypto = function(key, pdata, props) {
85-
var iv = props.iv || new Buffer(0),
86-
adata = props.aad || props.adata || new Buffer(0);
85+
var iv = props.iv || Buffer.alloc(0),
86+
adata = props.aad || props.adata || Buffer.alloc(0);
8787

8888
try {
8989
commonChecks(key, iv, adata);
@@ -109,10 +109,10 @@ function gcmEncryptFN(size) {
109109
var tagStart = result.byteLength - 16;
110110

111111
var tag = result.slice(tagStart);
112-
tag = new Buffer(tag);
112+
tag = Buffer.from(tag);
113113

114114
var cdata = result.slice(0, tagStart);
115-
cdata = new Buffer(cdata);
115+
cdata = Buffer.from(cdata);
116116

117117
return {
118118
data: cdata,
@@ -125,8 +125,8 @@ function gcmEncryptFN(size) {
125125

126126
// ### NodeJS implementation
127127
var nodejs = function(key, pdata, props) {
128-
var iv = props.iv || new Buffer(0),
129-
adata = props.aad || props.adata || new Buffer(0);
128+
var iv = props.iv || Buffer.alloc(0),
129+
adata = props.aad || props.adata || Buffer.alloc(0);
130130

131131
try {
132132
commonChecks(key, iv, adata);
@@ -177,9 +177,9 @@ function gcmDecryptFN(size) {
177177

178178
// ### fallback implementation -- uses forge
179179
var fallback = function(key, cdata, props) {
180-
var adata = props.aad || props.adata || new Buffer(0),
181-
iv = props.iv || new Buffer(0),
182-
tag = props.tag || props.mac || new Buffer(0),
180+
var adata = props.aad || props.adata || Buffer.alloc(0),
181+
iv = props.iv || Buffer.alloc(0),
182+
tag = props.tag || props.mac || Buffer.alloc(0),
183183
cipher,
184184
pdata;
185185

@@ -198,7 +198,7 @@ function gcmDecryptFN(size) {
198198
tag: tag
199199
});
200200
// plaintext is the same length as ciphertext
201-
pdata = new Buffer(cdata.length);
201+
pdata = Buffer.alloc(cdata.length);
202202

203203
var promise = new Promise(function(resolve, reject) {
204204
var amt = CONSTANTS.CHUNK_SIZE,
@@ -241,9 +241,9 @@ function gcmDecryptFN(size) {
241241
// ### WebCryptoAPI implementation
242242
// TODO: cache CryptoKey sooner
243243
var webcrypto = function(key, cdata, props) {
244-
var adata = props.aad || props.adata || new Buffer(0),
245-
iv = props.iv || new Buffer(0),
246-
tag = props.tag || props.mac || new Buffer(0);
244+
var adata = props.aad || props.adata || Buffer.alloc(0),
245+
iv = props.iv || Buffer.alloc(0),
246+
tag = props.tag || props.mac || Buffer.alloc(0);
247247

248248
// validate inputs
249249
try {
@@ -270,17 +270,17 @@ function gcmDecryptFN(size) {
270270
return helpers.subtleCrypto.decrypt(alg, key, cdata);
271271
});
272272
promise = promise.then(function(pdata) {
273-
pdata = new Buffer(pdata);
273+
pdata = Buffer.from(pdata);
274274
return pdata;
275275
});
276276

277277
return promise;
278278
};
279279

280280
var nodejs = function(key, cdata, props) {
281-
var adata = props.aad || props.adata || new Buffer(0),
282-
iv = props.iv || new Buffer(0),
283-
tag = props.tag || props.mac || new Buffer(0);
281+
var adata = props.aad || props.adata || Buffer.alloc(0),
282+
iv = props.iv || Buffer.alloc(0),
283+
tag = props.tag || props.mac || Buffer.alloc(0);
284284

285285
// validate inputs
286286
try {

lib/algorithms/aes-kw.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ var helpers = require("./helpers.js"),
99
forge = require("../deps/forge.js"),
1010
DataBuffer = require("../util/databuffer.js");
1111

12-
var A0 = new Buffer("a6a6a6a6a6a6a6a6", "hex");
12+
var A0 = Buffer.from("a6a6a6a6a6a6a6a6", "hex");
1313

1414
// ### helpers
1515
function xor(a, b) {
1616
var len = Math.max(a.length, b.length);
17-
var result = new Buffer(len);
17+
var result = Buffer.alloc(len);
1818
for (var idx = 0; len > idx; idx++) {
1919
result[idx] = (a[idx] || 0) ^ (b[idx] || 0);
2020
}
@@ -32,7 +32,7 @@ function split(input, size) {
3232
function longToBigEndian(input) {
3333
var hi = Math.floor(input / 4294967296),
3434
lo = input % 4294967296;
35-
var output = new Buffer(8);
35+
var output = Buffer.alloc(8);
3636
output[0] = 0xff & (hi >>> 24);
3737
output[1] = 0xff & (hi >>> 16);
3838
output[2] = 0xff & (hi >>> 8);
@@ -114,7 +114,7 @@ function kwEncryptFN(size) {
114114
alg);
115115
});
116116
promise = promise.then(function(result) {
117-
result = new Buffer(result);
117+
result = Buffer.from(result);
118118

119119
return {
120120
data: result
@@ -194,7 +194,7 @@ function kwDecryptFN(size) {
194194
return helpers.subtleCrypto.exportKey("raw", result);
195195
});
196196
promise = promise.then(function(result) {
197-
result = new Buffer(result);
197+
result = Buffer.from(result);
198198
return result;
199199
});
200200
return promise;

lib/algorithms/concat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function concatDeriveFn(name) {
3838
return Buffer.concat(okm).slice(0, keyLen);
3939
}
4040

41-
var T = new Buffer(4 + key.length + otherInfo.length);
41+
var T = Buffer.alloc(4 + key.length + otherInfo.length);
4242
T.writeUInt32BE(idx, 0);
4343
key.copy(T, 4);
4444
otherInfo.copy(T, 4 + key.length);

lib/algorithms/ec-util.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function convertToObj(key, isPublic) {
6565
return result;
6666
}
6767

68-
var UNCOMPRESSED = new Buffer([0x04]);
68+
var UNCOMPRESSED = Buffer.from([0x04]);
6969
function convertToBuffer(key, isPublic) {
7070
key = convertToObj(key, isPublic);
7171
var result = isPublic ?

lib/algorithms/ecdh.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function ecdhDeriveFn() {
128128
return helpers.subtleCrypto.deriveBits(algParams, privKey, keyLen * 8);
129129
});
130130
p = p.then(function(result) {
131-
result = new Buffer(result);
131+
result = Buffer.from(result);
132132
return result;
133133
});
134134
return p;
@@ -262,7 +262,7 @@ function doEcdhesCommonDerive(privKey, pubKey, props) {
262262
apu = util.asBuffer(props.apu || "", "base64url"),
263263
apv = util.asBuffer(props.apv || "", "base64url");
264264
var otherInfo = Buffer.concat([
265-
prependLen(new Buffer(algId, "utf8")),
265+
prependLen(Buffer.from(algId, "utf8")),
266266
prependLen(apu),
267267
prependLen(apv),
268268
helpers.int32ToBuffer(keyLen)

lib/algorithms/ecdsa.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function ecdsaSignFN(hash) {
7171
return helpers.subtleCrypto.sign(alg, key, pdata);
7272
});
7373
promise = promise.then(function(result) {
74-
result = new Buffer(result);
74+
result = Buffer.from(result);
7575
return {
7676
data: pdata,
7777
mac: result

lib/algorithms/helpers.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ if (typeof Promise === "undefined") {
1212

1313
// ###
1414
exports.int32ToBuffer = function(v, b) {
15-
b = b || new Buffer(4);
15+
b = b || Buffer.alloc(4);
1616
b[0] = (v >>> 24) & 0xff;
1717
b[1] = (v >>> 16) & 0xff;
1818
b[2] = (v >>> 8) & 0xff;
@@ -22,7 +22,7 @@ exports.int32ToBuffer = function(v, b) {
2222

2323
var MAX_INT32 = Math.pow(2, 32);
2424
exports.int64ToBuffer = function(v, b) {
25-
b = b || new Buffer(8);
25+
b = b || Buffer.alloc(8);
2626
var hi = Math.floor(v / MAX_INT32),
2727
lo = v % MAX_INT32;
2828
hi = exports.int32ToBuffer(hi);

lib/algorithms/hkdf.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ function hkdfDeriveFn(name) {
2626
props = props || {};
2727
var salt = props.salt;
2828
if (!salt || 0 === salt.length) {
29-
salt = new Buffer(hashLen);
30-
salt.fill(0);
29+
salt = Buffer.alloc(hashLen);
3130
}
32-
var info = props.info || new Buffer(0);
31+
var info = props.info || Buffer.alloc(0);
3332
var keyLen = props.length || hashLen;
3433

3534
var promise;
@@ -44,9 +43,9 @@ function hkdfDeriveFn(name) {
4443
}
4544

4645
if (!T) {
47-
T = new Buffer(0);
46+
T = Buffer.alloc(0);
4847
}
49-
T = Buffer.concat([T, info, new Buffer([idx])]);
48+
T = Buffer.concat([T, info, Buffer.from([idx])]);
5049
T = op(key, T);
5150
T = T.then(function(result) {
5251
T = result.mac;

lib/algorithms/hmac.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function hmacSignFN(name) {
6161
return helpers.subtleCrypto.sign(alg, key, pdata);
6262
});
6363
promise = promise.then(function(result) {
64-
var sig = new Buffer(result);
64+
var sig = Buffer.from(result);
6565
return {
6666
data: pdata,
6767
mac: sig
@@ -142,7 +142,7 @@ function hmacVerifyFN(name) {
142142
return helpers.subtleCrypto.sign(alg, key, pdata);
143143
});
144144
promise = promise.then(function(result) {
145-
var sig = new Buffer(result);
145+
var sig = Buffer.from(result);
146146
return compare(props.length, mac, sig);
147147
});
148148
} else {

0 commit comments

Comments
 (0)