Skip to content

Commit 203254e

Browse files
committed
fs: validate fd synchronously on c++
1 parent 2e458d9 commit 203254e

File tree

8 files changed

+200
-63
lines changed

8 files changed

+200
-63
lines changed

lib/fs.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,12 @@ function defaultCloseCallback(err) {
513513
* @returns {void}
514514
*/
515515
function close(fd, callback = defaultCloseCallback) {
516-
fd = getValidatedFd(fd);
517516
if (callback !== defaultCloseCallback)
518517
callback = makeCallback(callback);
519518

520519
const req = new FSReqCallback();
521520
req.oncomplete = callback;
522-
binding.close(fd, req);
521+
binding.close(getValidatedFd(fd), req);
523522
}
524523

525524
/**
@@ -528,9 +527,7 @@ function close(fd, callback = defaultCloseCallback) {
528527
* @returns {void}
529528
*/
530529
function closeSync(fd) {
531-
fd = getValidatedFd(fd);
532-
533-
binding.close(fd);
530+
binding.close(getValidatedFd(fd));
534531
}
535532

536533
/**
@@ -1110,7 +1107,6 @@ function ftruncate(fd, len = 0, callback) {
11101107
callback = len;
11111108
len = 0;
11121109
}
1113-
fd = getValidatedFd(fd);
11141110
validateInteger(len, 'len');
11151111
len = MathMax(0, len);
11161112
callback = makeCallback(callback);
@@ -1127,7 +1123,6 @@ function ftruncate(fd, len = 0, callback) {
11271123
* @returns {void}
11281124
*/
11291125
function ftruncateSync(fd, len = 0) {
1130-
fd = getValidatedFd(fd);
11311126
validateInteger(len, 'len');
11321127
len = MathMax(0, len);
11331128
binding.ftruncate(fd, len);
@@ -1279,7 +1274,6 @@ function rmSync(path, options) {
12791274
* @returns {void}
12801275
*/
12811276
function fdatasync(fd, callback) {
1282-
fd = getValidatedFd(fd);
12831277
const req = new FSReqCallback();
12841278
req.oncomplete = makeCallback(callback);
12851279
binding.fdatasync(fd, req);
@@ -1293,7 +1287,6 @@ function fdatasync(fd, callback) {
12931287
* @returns {void}
12941288
*/
12951289
function fdatasyncSync(fd) {
1296-
fd = getValidatedFd(fd);
12971290
binding.fdatasync(fd);
12981291
}
12991292

@@ -1305,7 +1298,6 @@ function fdatasyncSync(fd) {
13051298
* @returns {void}
13061299
*/
13071300
function fsync(fd, callback) {
1308-
fd = getValidatedFd(fd);
13091301
const req = new FSReqCallback();
13101302
req.oncomplete = makeCallback(callback);
13111303
binding.fsync(fd, req);
@@ -1318,7 +1310,6 @@ function fsync(fd, callback) {
13181310
* @returns {void}
13191311
*/
13201312
function fsyncSync(fd) {
1321-
fd = getValidatedFd(fd);
13221313
binding.fsync(fd);
13231314
}
13241315

@@ -1539,7 +1530,6 @@ function fstat(fd, options = { bigint: false }, callback) {
15391530
callback = options;
15401531
options = kEmptyObject;
15411532
}
1542-
fd = getValidatedFd(fd);
15431533
callback = makeStatsCallback(callback);
15441534

15451535
const req = new FSReqCallback(options.bigint);
@@ -1622,7 +1612,6 @@ function statfs(path, options = { bigint: false }, callback) {
16221612
* @returns {Stats | undefined}
16231613
*/
16241614
function fstatSync(fd, options = { bigint: false }) {
1625-
fd = getValidatedFd(fd);
16261615
const stats = binding.fstat(fd, options.bigint, undefined, false);
16271616
if (stats === undefined) {
16281617
return;
@@ -1888,7 +1877,6 @@ function unlinkSync(path) {
18881877
* @returns {void}
18891878
*/
18901879
function fchmod(fd, mode, callback) {
1891-
fd = getValidatedFd(fd);
18921880
mode = parseFileMode(mode, 'mode');
18931881
callback = makeCallback(callback);
18941882

@@ -1905,7 +1893,7 @@ function fchmod(fd, mode, callback) {
19051893
*/
19061894
function fchmodSync(fd, mode) {
19071895
binding.fchmod(
1908-
getValidatedFd(fd),
1896+
fd,
19091897
parseFileMode(mode, 'mode'),
19101898
);
19111899
}
@@ -2033,14 +2021,13 @@ function lchownSync(path, uid, gid) {
20332021
* @returns {void}
20342022
*/
20352023
function fchown(fd, uid, gid, callback) {
2036-
fd = getValidatedFd(fd);
20372024
validateInteger(uid, 'uid', -1, kMaxUserId);
20382025
validateInteger(gid, 'gid', -1, kMaxUserId);
20392026
callback = makeCallback(callback);
20402027

20412028
const req = new FSReqCallback();
20422029
req.oncomplete = callback;
2043-
binding.fchown(fd, uid, gid, req);
2030+
binding.fchown(getValidatedFd(fd), uid, gid, req);
20442031
}
20452032

20462033
/**
@@ -2051,12 +2038,11 @@ function fchown(fd, uid, gid, callback) {
20512038
* @returns {void}
20522039
*/
20532040
function fchownSync(fd, uid, gid) {
2054-
fd = getValidatedFd(fd);
20552041
validateInteger(uid, 'uid', -1, kMaxUserId);
20562042
validateInteger(gid, 'gid', -1, kMaxUserId);
20572043

20582044
const ctx = {};
2059-
binding.fchown(fd, uid, gid, undefined, ctx);
2045+
binding.fchown(getValidatedFd(fd), uid, gid, undefined, ctx);
20602046
handleErrorFromBinding(ctx);
20612047
}
20622048

@@ -2147,7 +2133,6 @@ function utimesSync(path, atime, mtime) {
21472133
* @returns {void}
21482134
*/
21492135
function futimes(fd, atime, mtime, callback) {
2150-
fd = getValidatedFd(fd);
21512136
atime = toUnixTimestamp(atime, 'atime');
21522137
mtime = toUnixTimestamp(mtime, 'mtime');
21532138
callback = makeCallback(callback);
@@ -2168,7 +2153,7 @@ function futimes(fd, atime, mtime, callback) {
21682153
*/
21692154
function futimesSync(fd, atime, mtime) {
21702155
binding.futimes(
2171-
getValidatedFd(fd),
2156+
fd,
21722157
toUnixTimestamp(atime, 'atime'),
21732158
toUnixTimestamp(mtime, 'mtime'),
21742159
);

src/node_file.cc

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@
4040
#include "string_bytes.h"
4141

4242
#include <fcntl.h>
43-
#include <sys/types.h>
4443
#include <sys/stat.h>
44+
#include <sys/types.h>
45+
#include <cstdio>
4546
#include <cstring>
46-
#include <cerrno>
47-
#include <climits>
4847

4948
#if defined(__MINGW32__) || defined(_MSC_VER)
5049
# include <io.h>
@@ -1154,8 +1153,10 @@ static void FStat(const FunctionCallbackInfo<Value>& args) {
11541153
const int argc = args.Length();
11551154
CHECK_GE(argc, 2);
11561155

1157-
CHECK(args[0]->IsInt32());
1158-
int fd = args[0].As<Int32>()->Value();
1156+
int fd;
1157+
if (!GetValidatedFd(env, args[0]).To(&fd)) {
1158+
return;
1159+
}
11591160

11601161
bool use_bigint = args[1]->IsTrue();
11611162
if (!args[2]->IsUndefined()) { // fstat(fd, use_bigint, req)
@@ -1405,18 +1406,20 @@ static void FTruncate(const FunctionCallbackInfo<Value>& args) {
14051406
const int argc = args.Length();
14061407
CHECK_GE(argc, 2);
14071408

1408-
CHECK(args[0]->IsInt32());
1409-
const int fd = args[0].As<Int32>()->Value();
1409+
int fd;
1410+
if (!GetValidatedFd(env, args[0]).To(&fd)) {
1411+
return;
1412+
}
14101413

14111414
CHECK(IsSafeJsInt(args[1]));
14121415
const int64_t len = args[1].As<Integer>()->Value();
14131416

1414-
if (argc > 2) {
1417+
if (argc > 2) { // ftruncate(fd, len, req)
14151418
FSReqBase* req_wrap_async = GetReqWrap(args, 2);
14161419
FS_ASYNC_TRACE_BEGIN0(UV_FS_FTRUNCATE, req_wrap_async)
14171420
AsyncCall(env, req_wrap_async, args, "ftruncate", UTF8, AfterNoArgs,
14181421
uv_fs_ftruncate, fd, len);
1419-
} else {
1422+
} else { // ftruncate(fd, len)
14201423
FSReqWrapSync req_wrap_sync("ftruncate");
14211424
FS_SYNC_TRACE_BEGIN(ftruncate);
14221425
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_ftruncate, fd, len);
@@ -1430,8 +1433,10 @@ static void Fdatasync(const FunctionCallbackInfo<Value>& args) {
14301433
const int argc = args.Length();
14311434
CHECK_GE(argc, 1);
14321435

1433-
CHECK(args[0]->IsInt32());
1434-
const int fd = args[0].As<Int32>()->Value();
1436+
int fd;
1437+
if (!GetValidatedFd(env, args[0]).To(&fd)) {
1438+
return;
1439+
}
14351440

14361441
if (argc > 1) { // fdatasync(fd, req)
14371442
FSReqBase* req_wrap_async = GetReqWrap(args, 1);
@@ -1453,8 +1458,10 @@ static void Fsync(const FunctionCallbackInfo<Value>& args) {
14531458
const int argc = args.Length();
14541459
CHECK_GE(argc, 1);
14551460

1456-
CHECK(args[0]->IsInt32());
1457-
const int fd = args[0].As<Int32>()->Value();
1461+
int fd;
1462+
if (!GetValidatedFd(env, args[0]).To(&fd)) {
1463+
return;
1464+
}
14581465

14591466
if (argc > 1) {
14601467
FSReqBase* req_wrap_async = GetReqWrap(args, 1);
@@ -2525,8 +2532,10 @@ static void FChmod(const FunctionCallbackInfo<Value>& args) {
25252532
const int argc = args.Length();
25262533
CHECK_GE(argc, 2);
25272534

2528-
CHECK(args[0]->IsInt32());
2529-
const int fd = args[0].As<Int32>()->Value();
2535+
int fd;
2536+
if (!GetValidatedFd(env, args[0]).To(&fd)) {
2537+
return;
2538+
}
25302539

25312540
CHECK(args[1]->IsInt32());
25322541
const int mode = args[1].As<Int32>()->Value();
@@ -2683,8 +2692,10 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) {
26832692
const int argc = args.Length();
26842693
CHECK_GE(argc, 3);
26852694

2686-
CHECK(args[0]->IsInt32());
2687-
const int fd = args[0].As<Int32>()->Value();
2695+
int fd;
2696+
if (!GetValidatedFd(env, args[0]).To(&fd)) {
2697+
return;
2698+
}
26882699

26892700
CHECK(args[1]->IsNumber());
26902701
const double atime = args[1].As<Number>()->Value();

src/util.cc

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
#include "util.h" // NOLINT(build/include_inline)
23+
#include <cmath>
2324
#include "util-inl.h"
2425

2526
#include "debug_utils-inl.h"
@@ -31,6 +32,7 @@
3132
#include "node_v8_platform-inl.h"
3233
#include "string_bytes.h"
3334
#include "uv.h"
35+
#include "v8-value.h"
3436

3537
#ifdef _WIN32
3638
#include <io.h> // _S_IREAD _S_IWRITE
@@ -702,4 +704,87 @@ RAIIIsolate::RAIIIsolate(const SnapshotData* data)
702704

703705
RAIIIsolate::~RAIIIsolate() {}
704706

707+
// Returns a string representation of the input value, including type.
708+
// JavaScript implementation is available in lib/internal/errors.js
709+
std::string DetermineSpecificErrorType(Environment* env,
710+
v8::Local<v8::Value> input) {
711+
if (input->IsFunction()) {
712+
return "function";
713+
} else if (input->IsString()) {
714+
auto value = Utf8Value(env->isolate(), input).ToString();
715+
if (value.size() > 28) {
716+
value = value.substr(0, 25) + "...";
717+
}
718+
if (value.find('\'') == std::string::npos) {
719+
return SPrintF("type string ('%s')", value);
720+
}
721+
722+
// Stringify the input value.
723+
Local<String> stringified =
724+
v8::JSON::Stringify(env->context(), input).ToLocalChecked();
725+
Utf8Value stringified_value(env->isolate(), stringified);
726+
return SPrintF("type string (%s)", stringified_value.out());
727+
} else if (input->IsObject()) {
728+
v8::Local<v8::String> constructor_name =
729+
input.As<v8::Object>()->GetConstructorName();
730+
Utf8Value name(env->isolate(), constructor_name);
731+
return SPrintF("an instance of %s", name.out());
732+
}
733+
734+
Utf8Value utf8_value(env->isolate(),
735+
input->ToString(env->context()).ToLocalChecked());
736+
737+
if (input->IsNumber() || input->IsInt32() || input->IsUint32()) {
738+
auto value = input.As<v8::Number>()->Value();
739+
if (std::isnan(value)) {
740+
return "type number (NaN)";
741+
} else if (std::isinf(value)) {
742+
return "type number (Infinity)";
743+
}
744+
return SPrintF("type number (%s)", utf8_value.out());
745+
} else if (input->IsBigInt() || input->IsBoolean() || input->IsSymbol()) {
746+
Utf8Value type(env->isolate(), input->TypeOf(env->isolate()));
747+
return SPrintF("type %s (%s)", type.out(), utf8_value.out());
748+
}
749+
750+
// For example: null, undefined
751+
return utf8_value.ToString();
752+
}
753+
754+
v8::Maybe<int32_t> GetValidatedFd(Environment* env,
755+
v8::Local<v8::Value> input) {
756+
if (!input->IsInt32() && !input->IsNumber()) {
757+
std::string error_type = node::DetermineSpecificErrorType(env, input);
758+
THROW_ERR_INVALID_ARG_TYPE(env,
759+
"The \"fd\" argument must be of type "
760+
"number. Received %s",
761+
error_type.c_str());
762+
return v8::Nothing<int32_t>();
763+
}
764+
765+
const double fd = input.As<v8::Number>()->Value();
766+
const bool is_out_of_range = fd < 0 || fd > INT32_MAX;
767+
768+
if (is_out_of_range || !IsSafeJsInt(input)) {
769+
Utf8Value utf8_value(
770+
env->isolate(), input->ToDetailString(env->context()).ToLocalChecked());
771+
if (is_out_of_range && !std::isinf(fd)) {
772+
THROW_ERR_OUT_OF_RANGE(env,
773+
"The value of \"fd\" is out of range. "
774+
"It must be >= 0 && <= %s. Received %d",
775+
std::to_string(INT32_MAX),
776+
utf8_value.out());
777+
} else {
778+
THROW_ERR_OUT_OF_RANGE(
779+
env,
780+
"The value of \"fd\" is out of range. It must be an integer. "
781+
"Received %s",
782+
utf8_value.out());
783+
}
784+
return v8::Nothing<int32_t>();
785+
}
786+
787+
return v8::Just(static_cast<int32_t>(fd));
788+
}
789+
705790
} // namespace node

src/util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,11 @@ class RAIIIsolate {
996996
v8::Isolate::Scope isolate_scope_;
997997
};
998998

999+
std::string DetermineSpecificErrorType(Environment* env,
1000+
v8::Local<v8::Value> input);
1001+
1002+
v8::Maybe<int32_t> GetValidatedFd(Environment* env, v8::Local<v8::Value> input);
1003+
9991004
} // namespace node
10001005

10011006
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

0 commit comments

Comments
 (0)