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: https://github.com/nodejs/node/pull/26809 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
751c92d972
commit
7bddfcc61a
@ -69,7 +69,6 @@ const {
|
|||||||
stringToFlags,
|
stringToFlags,
|
||||||
stringToSymlinkType,
|
stringToSymlinkType,
|
||||||
toUnixTimestamp,
|
toUnixTimestamp,
|
||||||
validateBuffer,
|
|
||||||
validateOffsetLengthRead,
|
validateOffsetLengthRead,
|
||||||
validateOffsetLengthWrite,
|
validateOffsetLengthWrite,
|
||||||
validatePath
|
validatePath
|
||||||
@ -81,6 +80,7 @@ const {
|
|||||||
const {
|
const {
|
||||||
isUint32,
|
isUint32,
|
||||||
parseMode,
|
parseMode,
|
||||||
|
validateBuffer,
|
||||||
validateInteger,
|
validateInteger,
|
||||||
validateInt32,
|
validateInt32,
|
||||||
validateUint32
|
validateUint32
|
||||||
|
@ -5,6 +5,9 @@ const {
|
|||||||
certExportPublicKey,
|
certExportPublicKey,
|
||||||
certVerifySpkac
|
certVerifySpkac
|
||||||
} = internalBinding('crypto');
|
} = internalBinding('crypto');
|
||||||
|
const {
|
||||||
|
validateBuffer
|
||||||
|
} = require('internal/validators');
|
||||||
|
|
||||||
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
|
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
|
||||||
const { isArrayBufferView } = require('internal/util/types');
|
const { isArrayBufferView } = require('internal/util/types');
|
||||||
@ -14,13 +17,7 @@ const {
|
|||||||
} = require('internal/crypto/util');
|
} = require('internal/crypto/util');
|
||||||
|
|
||||||
function verifySpkac(spkac) {
|
function verifySpkac(spkac) {
|
||||||
if (!isArrayBufferView(spkac)) {
|
validateBuffer(spkac, 'spkac');
|
||||||
throw new ERR_INVALID_ARG_TYPE(
|
|
||||||
'spkac',
|
|
||||||
['Buffer', 'TypedArray', 'DataView'],
|
|
||||||
spkac
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return certVerifySpkac(spkac);
|
return certVerifySpkac(spkac);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,13 +27,13 @@ const {
|
|||||||
stringToFlags,
|
stringToFlags,
|
||||||
stringToSymlinkType,
|
stringToSymlinkType,
|
||||||
toUnixTimestamp,
|
toUnixTimestamp,
|
||||||
validateBuffer,
|
|
||||||
validateOffsetLengthRead,
|
validateOffsetLengthRead,
|
||||||
validateOffsetLengthWrite,
|
validateOffsetLengthWrite,
|
||||||
validatePath
|
validatePath
|
||||||
} = require('internal/fs/utils');
|
} = require('internal/fs/utils');
|
||||||
const {
|
const {
|
||||||
parseMode,
|
parseMode,
|
||||||
|
validateBuffer,
|
||||||
validateInteger,
|
validateInteger,
|
||||||
validateUint32
|
validateUint32
|
||||||
} = require('internal/validators');
|
} = require('internal/validators');
|
||||||
|
@ -14,7 +14,6 @@ const {
|
|||||||
} = require('internal/errors');
|
} = require('internal/errors');
|
||||||
const {
|
const {
|
||||||
isUint8Array,
|
isUint8Array,
|
||||||
isArrayBufferView,
|
|
||||||
isDate
|
isDate
|
||||||
} = require('internal/util/types');
|
} = require('internal/util/types');
|
||||||
const { once } = require('internal/util');
|
const { once } = require('internal/util');
|
||||||
@ -393,14 +392,6 @@ function toUnixTimestamp(time, name = 'time') {
|
|||||||
throw new ERR_INVALID_ARG_TYPE(name, ['Date', 'Time in seconds'], time);
|
throw new ERR_INVALID_ARG_TYPE(name, ['Date', 'Time in seconds'], time);
|
||||||
}
|
}
|
||||||
|
|
||||||
const validateBuffer = hideStackFrames((buffer) => {
|
|
||||||
if (!isArrayBufferView(buffer)) {
|
|
||||||
throw new ERR_INVALID_ARG_TYPE('buffer',
|
|
||||||
['Buffer', 'TypedArray', 'DataView'],
|
|
||||||
buffer);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const validateOffsetLengthRead = hideStackFrames(
|
const validateOffsetLengthRead = hideStackFrames(
|
||||||
(offset, length, bufferLength) => {
|
(offset, length, bufferLength) => {
|
||||||
if (offset < 0 || offset >= bufferLength) {
|
if (offset < 0 || offset >= bufferLength) {
|
||||||
@ -453,7 +444,6 @@ module.exports = {
|
|||||||
stringToSymlinkType,
|
stringToSymlinkType,
|
||||||
Stats,
|
Stats,
|
||||||
toUnixTimestamp,
|
toUnixTimestamp,
|
||||||
validateBuffer,
|
|
||||||
validateOffsetLengthRead,
|
validateOffsetLengthRead,
|
||||||
validateOffsetLengthWrite,
|
validateOffsetLengthWrite,
|
||||||
validatePath
|
validatePath
|
||||||
|
@ -8,6 +8,9 @@ const {
|
|||||||
ERR_OUT_OF_RANGE
|
ERR_OUT_OF_RANGE
|
||||||
}
|
}
|
||||||
} = require('internal/errors');
|
} = require('internal/errors');
|
||||||
|
const {
|
||||||
|
isArrayBufferView
|
||||||
|
} = require('internal/util/types');
|
||||||
|
|
||||||
function isInt32(value) {
|
function isInt32(value) {
|
||||||
return value === (value | 0);
|
return value === (value | 0);
|
||||||
@ -107,10 +110,22 @@ function validateNumber(value, name) {
|
|||||||
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
|
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO(BridgeAR): We have multiple validation functions that call
|
||||||
|
// `require('internal/utils').toBuf()` before validating for array buffer views.
|
||||||
|
// Those should likely also be consolidated in here.
|
||||||
|
const validateBuffer = hideStackFrames((buffer, name = 'buffer') => {
|
||||||
|
if (!isArrayBufferView(buffer)) {
|
||||||
|
throw new ERR_INVALID_ARG_TYPE(name,
|
||||||
|
['Buffer', 'TypedArray', 'DataView'],
|
||||||
|
buffer);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
isInt32,
|
isInt32,
|
||||||
isUint32,
|
isUint32,
|
||||||
parseMode,
|
parseMode,
|
||||||
|
validateBuffer,
|
||||||
validateInteger,
|
validateInteger,
|
||||||
validateInt32,
|
validateInt32,
|
||||||
validateUint32,
|
validateUint32,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user