| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1abff07 commit 3ec20f2
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -108,7 +108,6 @@ const { | |||
| 108 | 108 | getOptions, | |
| 109 | 109 | getValidatedFd, | |
| 110 | 110 | getValidatedPath, | |
| 111 | - getValidMode, | ||
| 112 | 111 | handleErrorFromBinding, | |
| 113 | 112 | preprocessSymlinkDestination, | |
| 114 | 113 | Stats, | |
@@ -233,7 +232,6 @@ function access(path, mode, callback) { | |||
| 233 | 232 | } | |
| 234 | 233 | ||
| 235 | 234 | path = getValidatedPath(path); | |
| 236 | - mode = getValidMode(mode, 'access'); | ||
| 237 | 235 | callback = makeCallback(callback); | |
| 238 | 236 | ||
| 239 | 237 | const req = new FSReqCallback(); | |
@@ -250,8 +248,6 @@ function access(path, mode, callback) { | |||
| 250 | 248 | */ | |
| 251 | 249 | function accessSync(path, mode) { | |
| 252 | 250 | path = getValidatedPath(path); | |
| 253 | - mode = getValidMode(mode, 'access'); | ||
| 254 | - | ||
| 255 | 251 | binding.access(pathModule.toNamespacedPath(path), mode); | |
| 256 | 252 | } | |
| 257 | 253 | ||
@@ -2984,7 +2980,6 @@ function copyFile(src, dest, mode, callback) { | |||
| 2984 | 2980 | ||
| 2985 | 2981 | src = pathModule.toNamespacedPath(src); | |
| 2986 | 2982 | dest = pathModule.toNamespacedPath(dest); | |
| 2987 | - mode = getValidMode(mode, 'copyFile'); | ||
| 2988 | 2983 | callback = makeCallback(callback); | |
| 2989 | 2984 | ||
| 2990 | 2985 | const req = new FSReqCallback(); | |
@@ -3007,7 +3002,7 @@ function copyFileSync(src, dest, mode) { | |||
| 3007 | 3002 | binding.copyFile( | |
| 3008 | 3003 | pathModule.toNamespacedPath(src), | |
| 3009 | 3004 | pathModule.toNamespacedPath(dest), | |
| 3010 | - getValidMode(mode, 'copyFile'), | ||
| 3005 | + mode, | ||
| 3011 | 3006 | ); | |
| 3012 | 3007 | } | |
| 3013 | 3008 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,7 +59,6 @@ const { | |||
| 59 | 59 | getStatFsFromBinding, | |
| 60 | 60 | getStatsFromBinding, | |
| 61 | 61 | getValidatedPath, | |
| 62 | - getValidMode, | ||
| 63 | 62 | preprocessSymlinkDestination, | |
| 64 | 63 | stringToFlags, | |
| 65 | 64 | stringToSymlinkType, | |
@@ -600,7 +599,6 @@ async function readFileHandle(filehandle, options) { | |||
| 600 | 599 | async function access(path, mode = F_OK) { | |
| 601 | 600 | path = getValidatedPath(path); | |
| 602 | 601 | ||
| 603 | - mode = getValidMode(mode, 'access'); | ||
| 604 | 602 | return await PromisePrototypeThen( | |
| 605 | 603 | binding.access(pathModule.toNamespacedPath(path), mode, kUsePromises), | |
| 606 | 604 | undefined, | |
@@ -618,7 +616,6 @@ async function cp(src, dest, options) { | |||
| 618 | 616 | async function copyFile(src, dest, mode) { | |
| 619 | 617 | src = getValidatedPath(src, 'src'); | |
| 620 | 618 | dest = getValidatedPath(dest, 'dest'); | |
| 621 | - mode = getValidMode(mode, 'copyFile'); | ||
| 622 | 619 | return await PromisePrototypeThen( | |
| 623 | 620 | binding.copyFile(pathModule.toNamespacedPath(src), | |
| 624 | 621 | pathModule.toNamespacedPath(dest), | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1000,7 +1000,6 @@ module.exports = { | |||
| 1000 | 1000 | getOptions, | |
| 1001 | 1001 | getValidatedFd, | |
| 1002 | 1002 | getValidatedPath, | |
| 1003 | - getValidMode, | ||
| 1004 | 1003 | handleErrorFromBinding, | |
| 1005 | 1004 | possiblyTransformPath, | |
| 1006 | 1005 | preprocessSymlinkDestination, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -38,6 +38,7 @@ | |||
| 38 | 38 | #include "req_wrap-inl.h" | |
| 39 | 39 | #include "stream_base-inl.h" | |
| 40 | 40 | #include "string_bytes.h" | |
| 41 | + #include "uv.h" | ||
| 41 | 42 | ||
| 42 | 43 | #if defined(__MINGW32__) || defined(_MSC_VER) | |
| 43 | 44 | # include <io.h> | |
@@ -950,10 +951,12 @@ void Access(const FunctionCallbackInfo<Value>& args) { | |||
| 950 | 951 | HandleScope scope(isolate); | |
| 951 | 952 | ||
| 952 | 953 | const int argc = args.Length(); | |
| 953 | - CHECK_GE(argc, 2); | ||
| 954 | + CHECK_GE(argc, 2); // path, mode | ||
| 954 | 955 | ||
| 955 | - CHECK(args[1]->IsInt32()); | ||
| 956 | - int mode = args[1].As<Int32>()->Value(); | ||
| 956 | + int mode; | ||
| 957 | + if (!GetValidFileMode(env, args[1], UV_FS_ACCESS).To(&mode)) { | ||
| 958 | + return; | ||
| 959 | + } | ||
| 957 | 960 | ||
| 958 | 961 | BufferValue path(isolate, args[0]); | |
| 959 | 962 | CHECK_NOT_NULL(*path); | |
@@ -1982,7 +1985,12 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) { | |||
| 1982 | 1985 | Isolate* isolate = env->isolate(); | |
| 1983 | 1986 | ||
| 1984 | 1987 | const int argc = args.Length(); | |
| 1985 | - CHECK_GE(argc, 3); | ||
| 1988 | + CHECK_GE(argc, 3); // src, dest, flags | ||
| 1989 | + | ||
| 1990 | + int flags; | ||
| 1991 | + if (!GetValidFileMode(env, args[2], UV_FS_COPYFILE).To(&flags)) { | ||
| 1992 | + return; | ||
| 1993 | + } | ||
| 1986 | 1994 | ||
| 1987 | 1995 | BufferValue src(isolate, args[0]); | |
| 1988 | 1996 | CHECK_NOT_NULL(*src); | |
@@ -1994,9 +2002,6 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) { | |||
| 1994 | 2002 | THROW_IF_INSUFFICIENT_PERMISSIONS( | |
| 1995 | 2003 | env, permission::PermissionScope::kFileSystemWrite, dest.ToStringView()); | |
| 1996 | 2004 | ||
| 1997 | - CHECK(args[2]->IsInt32()); | ||
| 1998 | - const int flags = args[2].As<Int32>()->Value(); | ||
| 1999 | - | ||
| 2000 | 2005 | if (argc > 3) { // copyFile(src, dest, flags, req) | |
| 2001 | 2006 | FSReqBase* req_wrap_async = GetReqWrap(args, 3); | |
| 2002 | 2007 | FS_ASYNC_TRACE_BEGIN2(UV_FS_COPYFILE, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,6 +21,7 @@ | |||
| 21 | 21 | ||
| 22 | 22 | #include "util.h" // NOLINT(build/include_inline) | |
| 23 | 23 | #include <cmath> | |
| 24 | + #include <cstdint> | ||
| 24 | 25 | #include "util-inl.h" | |
| 25 | 26 | ||
| 26 | 27 | #include "debug_utils-inl.h" | |
@@ -31,7 +32,6 @@ | |||
| 31 | 32 | #include "node_snapshot_builder.h" | |
| 32 | 33 | #include "node_v8_platform-inl.h" | |
| 33 | 34 | #include "string_bytes.h" | |
| 34 | - #include "uv.h" | ||
| 35 | 35 | #include "v8-value.h" | |
| 36 | 36 | ||
| 37 | 37 | #ifdef _WIN32 | |
@@ -56,6 +56,31 @@ | |||
| 56 | 56 | ||
| 57 | 57 | static std::atomic_int seq = {0}; // Sequence number for diagnostic filenames. | |
| 58 | 58 | ||
| 59 | + // F_OK etc. constants | ||
| 60 | + #ifdef _WIN32 | ||
| 61 | + #include "uv.h" | ||
| 62 | + #else | ||
| 63 | + #include <unistd.h> | ||
| 64 | + #endif | ||
| 65 | + | ||
| 66 | + // The access modes can be any of F_OK, R_OK, W_OK or X_OK. Some might not be | ||
| 67 | + // available on specific systems. They can be used in combination as well | ||
| 68 | + // (F_OK | R_OK | W_OK | X_OK). | ||
| 69 | + constexpr int kMaximumAccessMode = F_OK | W_OK | R_OK | X_OK; | ||
| 70 | + constexpr int kMinimumAccessMode = std::min({F_OK, W_OK, R_OK, X_OK}); | ||
| 71 | + | ||
| 72 | + constexpr int kDefaultCopyMode = 0; | ||
| 73 | + // The copy modes can be any of UV_FS_COPYFILE_EXCL, UV_FS_COPYFILE_FICLONE or | ||
| 74 | + // UV_FS_COPYFILE_FICLONE_FORCE. They can be used in combination as well | ||
| 75 | + // (US_FS_COPYFILE_EXCL | US_FS_COPYFILE_FICLONE | | ||
| 76 | + // US_FS_COPYFILE_FICLONE_FORCE). | ||
| 77 | + constexpr int kMinimumCopyMode = std::min({kDefaultCopyMode, | ||
| 78 | + UV_FS_COPYFILE_EXCL, | ||
| 79 | + UV_FS_COPYFILE_FICLONE, | ||
| 80 | + UV_FS_COPYFILE_FICLONE_FORCE}); | ||
| 81 | + constexpr int kMaximumCopyMode = | ||
| 82 | + UV_FS_COPYFILE_EXCL | UV_FS_COPYFILE_FICLONE | UV_FS_COPYFILE_FICLONE_FORCE; | ||
| 83 | + | ||
| 59 | 84 | namespace node { | |
| 60 | 85 | ||
| 61 | 86 | using v8::ArrayBuffer; | |
@@ -787,4 +812,49 @@ v8::Maybe<int32_t> GetValidatedFd(Environment* env, | |||
| 787 | 812 | return v8::Just(static_cast<int32_t>(fd)); | |
| 788 | 813 | } | |
| 789 | 814 | ||
| 815 | + v8::Maybe<int> GetValidFileMode(Environment* env, | ||
| 816 | + v8::Local<v8::Value> input, | ||
| 817 | + uv_fs_type type) { | ||
| 818 | + // Allow only int32 or null/undefined values. | ||
| 819 | + if (input->IsNumber()) { | ||
| 820 | + // We cast the input to v8::Number to avoid overflows. | ||
| 821 | + auto num = input.As<v8::Number>()->Value(); | ||
| 822 | + | ||
| 823 | + // Handle infinity and NaN values | ||
| 824 | + if (std::isinf(num) || std::isnan(num)) { | ||
| 825 | + THROW_ERR_OUT_OF_RANGE(env, "mode is out of range"); | ||
| 826 | + return v8::Nothing<int>(); | ||
| 827 | + } | ||
| 828 | + } else if (!input->IsNullOrUndefined()) { | ||
| 829 | + THROW_ERR_INVALID_ARG_TYPE(env, "mode must be int32 or null/undefined"); | ||
| 830 | + return v8::Nothing<int>(); | ||
| 831 | + } | ||
| 832 | + | ||
| 833 | + int min = kMinimumAccessMode; | ||
| 834 | + int max = kMaximumAccessMode; | ||
| 835 | + int def = F_OK; | ||
| 836 | + | ||
| 837 | + CHECK(type == UV_FS_ACCESS || type == UV_FS_COPYFILE); | ||
| 838 | + | ||
| 839 | + if (type == UV_FS_COPYFILE) { | ||
| 840 | + min = kMinimumCopyMode; | ||
| 841 | + max = kMaximumCopyMode; | ||
| 842 | + def = input->IsNullOrUndefined() ? kDefaultCopyMode | ||
| 843 | + : input.As<v8::Int32>()->Value(); | ||
| 844 | + } | ||
| 845 | + | ||
| 846 | + if (input->IsNullOrUndefined()) { | ||
| 847 | + return v8::Just(def); | ||
| 848 | + } | ||
| 849 | + | ||
| 850 | + const int mode = input.As<v8::Int32>()->Value(); | ||
| 851 | + if (mode < min || mode > max) { | ||
| 852 | + THROW_ERR_OUT_OF_RANGE( | ||
| 853 | + env, "mode is out of range: >= %d && <= %d", min, max); | ||
| 854 | + return v8::Nothing<int>(); | ||
| 855 | + } | ||
| 856 | + | ||
| 857 | + return v8::Just(mode); | ||
| 858 | + } | ||
| 859 | + | ||
| 790 | 860 | } // namespace node | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,7 @@ | |||
| 24 | 24 | ||
| 25 | 25 | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
| 26 | 26 | ||
| 27 | + #include "uv.h" | ||
| 27 | 28 | #include "v8.h" | |
| 28 | 29 | ||
| 29 | 30 | #include "node.h" | |
@@ -1027,6 +1028,9 @@ std::string DetermineSpecificErrorType(Environment* env, | |||
| 1027 | 1028 | v8::Local<v8::Value> input); | |
| 1028 | 1029 | ||
| 1029 | 1030 | v8::Maybe<int32_t> GetValidatedFd(Environment* env, v8::Local<v8::Value> input); | |
| 1031 | + v8::Maybe<int> GetValidFileMode(Environment* env, | ||
| 1032 | + v8::Local<v8::Value> input, | ||
| 1033 | + uv_fs_type type); | ||
| 1030 | 1034 | ||
| 1031 | 1035 | } // namespace node | |
| 1032 | 1036 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments