lib: mask mode_t type of arguments with 0o777 · nodejs/node@2fe88d2 · GitHub
Skip to content

Commit 2fe88d2

Browse files
joyeecheungtargos
authored andcommitted
lib: mask mode_t type of arguments with 0o777
- Introduce the `validateAndMaskMode` validator that validates `mode_t` arguments and mask them with 0o777 if they are 32-bit unsigned integer or octal string to be more consistent with POSIX APIs. - Use the validator in fs APIs and process.umask for consistency. - Add tests for 32-bit unsigned modes larger than 0o777. PR-URL: #20636 Fixes: #20498 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Backport-PR-URL: #21172
1 parent fc2956d commit 2fe88d2

12 files changed

Lines changed: 313 additions & 116 deletions

lib/fs.js

Lines changed: 31 additions & 32 deletions

lib/internal/fs/promises.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ const { Buffer, kMaxLength } = require('buffer');
1212
const {
1313
ERR_FS_FILE_TOO_LARGE,
1414
ERR_INVALID_ARG_TYPE,
15-
ERR_METHOD_NOT_IMPLEMENTED,
16-
ERR_OUT_OF_RANGE
15+
ERR_METHOD_NOT_IMPLEMENTED
1716
} = require('internal/errors').codes;
1817
const { getPathFromURL } = require('internal/url');
1918
const { isUint8Array } = require('internal/util/types');
2019
const {
2120
copyObject,
2221
getOptions,
2322
getStatsFromBinding,
24-
modeNum,
2523
nullCheck,
2624
preprocessSymlinkDestination,
2725
stringToFlags,
@@ -33,6 +31,7 @@ const {
3331
validatePath
3432
} = require('internal/fs/utils');
3533
const {
34+
validateAndMaskMode,
3635
validateInteger,
3736
validateUint32
3837
} = require('internal/validators');
@@ -190,10 +189,9 @@ async function copyFile(src, dest, flags) {
190189
// Note that unlike fs.open() which uses numeric file descriptors,
191190
// fsPromises.open() uses the fs.FileHandle class.
192191
async function open(path, flags, mode) {
193-
mode = modeNum(mode, 0o666);
194192
path = getPathFromURL(path);
195193
validatePath(path);
196-
validateUint32(mode, 'mode');
194+
mode = validateAndMaskMode(mode, 'mode', 0o666);
197195
return new FileHandle(
198196
await binding.openFileHandle(pathModule.toNamespacedPath(path),
199197
stringToFlags(flags),
@@ -286,10 +284,9 @@ async function fsync(handle) {
286284
}
287285

288286
async function mkdir(path, mode) {
289-
mode = modeNum(mode, 0o777);
290287
path = getPathFromURL(path);
291288
validatePath(path);
292-
validateUint32(mode, 'mode');
289+
mode = validateAndMaskMode(mode, 'mode', 0o777);
293290
return binding.mkdir(pathModule.toNamespacedPath(path), mode, kUsePromises);
294291
}
295292

@@ -360,19 +357,15 @@ async function unlink(path) {
360357
}
361358

362359
async function fchmod(handle, mode) {
363-
mode = modeNum(mode);
364360
validateFileHandle(handle);
365-
validateUint32(mode, 'mode');
366-
if (mode > 0o777)
367-
throw new ERR_OUT_OF_RANGE('mode', undefined, mode);
361+
mode = validateAndMaskMode(mode, 'mode');
368362
return binding.fchmod(handle.fd, mode, kUsePromises);
369363
}
370364

371365
async function chmod(path, mode) {
372366
path = getPathFromURL(path);
373367
validatePath(path);
374-
mode = modeNum(mode);
375-
validateUint32(mode, 'mode');
368+
mode = validateAndMaskMode(mode, 'mode');
376369
return binding.chmod(pathModule.toNamespacedPath(path), mode, kUsePromises);
377370
}
378371

lib/internal/fs/utils.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,6 @@ function getOptions(options, defaultOptions) {
7070
return options;
7171
}
7272

73-
function modeNum(m, def) {
74-
if (typeof m === 'number')
75-
return m;
76-
if (typeof m === 'string') {
77-
const parsed = parseInt(m, 8);
78-
if (Number.isNaN(parsed))
79-
return m;
80-
return parsed;
81-
}
82-
// TODO(BridgeAR): Only return `def` in case `m == null`
83-
if (def !== undefined)
84-
return def;
85-
return m;
86-
}
87-
8873
// Check if the path contains null types if it is a string nor Uint8Array,
8974
// otherwise return silently.
9075
function nullCheck(path, propName, throwError = true) {
@@ -391,7 +376,6 @@ module.exports = {
391376
assertEncoding,
392377
copyObject,
393378
getOptions,
394-
modeNum,
395379
nullCheck,
396380
preprocessSymlinkDestination,
397381
realpathCacheKey: Symbol('realpathCacheKey'),

lib/internal/validators.js

Lines changed: 36 additions & 0 deletions

0 commit comments

Comments
 (0)