fs,win: fix bug in paths with trailing slashes

Fixes: https://github.com/nodejs/node/issues/17801
Refs: https://github.com/nodejs/node/pull/33831
PR-URL: https://github.com/nodejs/node/pull/54160
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
Hüseyin Açacak 2024-10-10 14:14:56 +03:00 committed by GitHub
parent 4a3fffaf58
commit 00b2f07f9d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 237 additions and 20 deletions

View file

@ -385,7 +385,11 @@ function readFile(path, options, callback) {
const req = new FSReqCallback();
req.context = context;
req.oncomplete = readFileAfterOpen;
binding.open(getValidatedPath(path), flagsNumber, 0o666, req);
binding.open(
getValidatedPath(path, 'path', { expectFile: true, syscall: 'read' }),
flagsNumber,
0o666,
req);
}
function tryStatSync(fd, isUserFd) {
@ -437,7 +441,9 @@ function readFileSync(path, options) {
if (options.encoding === 'utf8' || options.encoding === 'utf-8') {
if (!isInt32(path)) {
path = getValidatedPath(path);
path = getValidatedPath(path,
'path',
{ expectFile: true, syscall: 'read' });
}
return binding.readFileUtf8(path, stringToFlags(options.flag));
}
@ -531,7 +537,7 @@ function closeSync(fd) {
* @returns {void}
*/
function open(path, flags, mode, callback) {
path = getValidatedPath(path);
path = getValidatedPath(path, 'path', { expectFile: true, syscall: 'open' });
if (arguments.length < 3) {
callback = flags;
flags = 'r';
@ -560,7 +566,7 @@ function open(path, flags, mode, callback) {
*/
function openSync(path, flags, mode) {
return binding.open(
getValidatedPath(path),
getValidatedPath(path, 'path', { expectFile: true, syscall: 'open' }),
stringToFlags(flags),
parseFileMode(mode, 'mode', 0o666),
);
@ -2339,7 +2345,9 @@ function writeFileSync(path, data, options) {
// C++ fast path for string data and UTF8 encoding
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) {
if (!isInt32(path)) {
path = getValidatedPath(path);
path = getValidatedPath(path,
'path',
{ expectFile: true, syscall: 'write' });
}
return binding.writeFileUtf8(
@ -2984,8 +2992,8 @@ function copyFile(src, dest, mode, callback) {
mode = 0;
}
src = getValidatedPath(src, 'src');
dest = getValidatedPath(dest, 'dest');
src = getValidatedPath(src, 'src', { expectFile: true, syscall: 'cp' });
dest = getValidatedPath(dest, 'dest', { expectFile: true, syscall: 'cp' });
callback = makeCallback(callback);
const req = new FSReqCallback();
@ -3003,8 +3011,8 @@ function copyFile(src, dest, mode, callback) {
*/
function copyFileSync(src, dest, mode) {
binding.copyFile(
getValidatedPath(src, 'src'),
getValidatedPath(dest, 'dest'),
getValidatedPath(src, 'src', { expectFile: true, syscall: 'cp' }),
getValidatedPath(dest, 'dest', { expectFile: true, syscall: 'cp' }),
mode,
);
}