Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v10.x backport] fs: improve truncate length validation #21171

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const {
} = require('internal/constants');
const {
isUint32,
validateInt32,
validateInteger,
validateUint32
} = require('internal/validators');

Expand Down Expand Up @@ -746,6 +746,7 @@ fs.truncate = function(path, len, callback) {
len = 0;
}

validateInteger(len, 'len');
callback = maybeCallback(callback);
fs.open(path, 'r+', function(er, fd) {
if (er) return callback(er);
Expand Down Expand Up @@ -786,11 +787,7 @@ fs.ftruncate = function(fd, len = 0, callback) {
len = 0;
}
validateUint32(fd, 'fd');
// TODO(BridgeAR): This does not seem right.
// There does not seem to be any validation before and if there is any, it
// should work similar to validateUint32 or not have a upper cap at all.
// This applies to all usage of `validateInt32(len, 'len')`.
validateInt32(len, 'len');
validateInteger(len, 'len');
len = Math.max(0, len);
const req = new FSReqWrap();
req.oncomplete = makeCallback(callback);
Expand All @@ -799,7 +796,7 @@ fs.ftruncate = function(fd, len = 0, callback) {

fs.ftruncateSync = function(fd, len = 0) {
validateUint32(fd, 'fd');
validateInt32(len, 'len');
validateInteger(len, 'len');
len = Math.max(0, len);
const ctx = {};
binding.ftruncate(fd, len, undefined, ctx);
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const {
} = require('internal/fs/utils');
const {
isUint32,
validateInt32,
validateInteger,
validateUint32
} = require('internal/validators');
const pathModule = require('path');
Expand Down Expand Up @@ -265,7 +265,7 @@ async function truncate(path, len = 0) {

async function ftruncate(handle, len = 0) {
validateFileHandle(handle);
validateInt32(len, 'len');
validateInteger(len, 'len');
len = Math.max(0, len);
return binding.ftruncate(handle.fd, len, kUsePromises);
}
Expand Down
17 changes: 16 additions & 1 deletion lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ function isUint32(value) {
return value === (value >>> 0);
}

function validateInt32(value, name) {
function validateInteger(value, name) {
let err;

if (typeof value !== 'number')
err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
else if (!Number.isSafeInteger(value))
err = new ERR_OUT_OF_RANGE(name, 'an integer', value);

if (err) {
Error.captureStackTrace(err, validateInteger);
throw err;
}
}

function validateInt32(value, name, min = -2147483648, max = 2147483647) {
if (!isInt32(value)) {
let err;
if (typeof value !== 'number') {
Expand Down Expand Up @@ -53,6 +67,7 @@ function validateUint32(value, name, positive) {
module.exports = {
isInt32,
isUint32,
validateInteger,
validateInt32,
validateUint32
};
25 changes: 21 additions & 4 deletions test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ function testFtruncate(cb) {
process.on('exit', () => fs.closeSync(fd));

['', false, null, {}, []].forEach((input) => {
assert.throws(
() => fs.truncate(file5, input, common.mustNotCall()),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: 'The "len" argument must be of type number. ' +
`Received type ${typeof input}`
}
);

assert.throws(
() => fs.ftruncate(fd, input),
{
Expand All @@ -191,6 +201,16 @@ function testFtruncate(cb) {
});

[-1.5, 1.5].forEach((input) => {
assert.throws(
() => fs.truncate(file5, input),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "len" is out of range. It must be ' +
`an integer. Received ${input}`
}
);

assert.throws(
() => fs.ftruncate(fd, input),
{
Expand All @@ -200,17 +220,14 @@ function testFtruncate(cb) {
`an integer. Received ${input}`
}
);
});

// 2 ** 31 = 2147483648
[2147483648, -2147483649].forEach((input) => {
assert.throws(
() => fs.ftruncate(fd, input),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError [ERR_OUT_OF_RANGE]',
message: 'The value of "len" is out of range. It must be ' +
`> -2147483649 && < 2147483648. Received ${input}`
`an integer. Received ${input}`
}
);
});
Expand Down