Skip to content

Commit 7bddfcc

Browse files
committed
lib: consolidate arrayBufferView validation
There are lots of places that validate for arrayBufferView and we have multiple functions that do the same thing. Instead, move the validation into `internal/validators` so all files can use that instead. There are more functions throughout the code that do the same but it takes some more work to fully consolidate all of those. PR-URL: #26809 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 751c92d commit 7bddfcc

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

lib/fs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ const {
6969
stringToFlags,
7070
stringToSymlinkType,
7171
toUnixTimestamp,
72-
validateBuffer,
7372
validateOffsetLengthRead,
7473
validateOffsetLengthWrite,
7574
validatePath
@@ -81,6 +80,7 @@ const {
8180
const {
8281
isUint32,
8382
parseMode,
83+
validateBuffer,
8484
validateInteger,
8585
validateInt32,
8686
validateUint32

lib/internal/crypto/certificate.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const {
55
certExportPublicKey,
66
certVerifySpkac
77
} = internalBinding('crypto');
8+
const {
9+
validateBuffer
10+
} = require('internal/validators');
811

912
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
1013
const { isArrayBufferView } = require('internal/util/types');
@@ -14,13 +17,7 @@ const {
1417
} = require('internal/crypto/util');
1518

1619
function verifySpkac(spkac) {
17-
if (!isArrayBufferView(spkac)) {
18-
throw new ERR_INVALID_ARG_TYPE(
19-
'spkac',
20-
['Buffer', 'TypedArray', 'DataView'],
21-
spkac
22-
);
23-
}
20+
validateBuffer(spkac, 'spkac');
2421
return certVerifySpkac(spkac);
2522
}
2623

lib/internal/fs/promises.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ const {
2727
stringToFlags,
2828
stringToSymlinkType,
2929
toUnixTimestamp,
30-
validateBuffer,
3130
validateOffsetLengthRead,
3231
validateOffsetLengthWrite,
3332
validatePath
3433
} = require('internal/fs/utils');
3534
const {
3635
parseMode,
36+
validateBuffer,
3737
validateInteger,
3838
validateUint32
3939
} = require('internal/validators');

lib/internal/fs/utils.js

-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const {
1414
} = require('internal/errors');
1515
const {
1616
isUint8Array,
17-
isArrayBufferView,
1817
isDate
1918
} = require('internal/util/types');
2019
const { once } = require('internal/util');
@@ -393,14 +392,6 @@ function toUnixTimestamp(time, name = 'time') {
393392
throw new ERR_INVALID_ARG_TYPE(name, ['Date', 'Time in seconds'], time);
394393
}
395394

396-
const validateBuffer = hideStackFrames((buffer) => {
397-
if (!isArrayBufferView(buffer)) {
398-
throw new ERR_INVALID_ARG_TYPE('buffer',
399-
['Buffer', 'TypedArray', 'DataView'],
400-
buffer);
401-
}
402-
});
403-
404395
const validateOffsetLengthRead = hideStackFrames(
405396
(offset, length, bufferLength) => {
406397
if (offset < 0 || offset >= bufferLength) {
@@ -453,7 +444,6 @@ module.exports = {
453444
stringToSymlinkType,
454445
Stats,
455446
toUnixTimestamp,
456-
validateBuffer,
457447
validateOffsetLengthRead,
458448
validateOffsetLengthWrite,
459449
validatePath

lib/internal/validators.js

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const {
88
ERR_OUT_OF_RANGE
99
}
1010
} = require('internal/errors');
11+
const {
12+
isArrayBufferView
13+
} = require('internal/util/types');
1114

1215
function isInt32(value) {
1316
return value === (value | 0);
@@ -107,10 +110,22 @@ function validateNumber(value, name) {
107110
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
108111
}
109112

113+
// TODO(BridgeAR): We have multiple validation functions that call
114+
// `require('internal/utils').toBuf()` before validating for array buffer views.
115+
// Those should likely also be consolidated in here.
116+
const validateBuffer = hideStackFrames((buffer, name = 'buffer') => {
117+
if (!isArrayBufferView(buffer)) {
118+
throw new ERR_INVALID_ARG_TYPE(name,
119+
['Buffer', 'TypedArray', 'DataView'],
120+
buffer);
121+
}
122+
});
123+
110124
module.exports = {
111125
isInt32,
112126
isUint32,
113127
parseMode,
128+
validateBuffer,
114129
validateInteger,
115130
validateInt32,
116131
validateUint32,

0 commit comments

Comments
 (0)