GeezerVison is showing github.com — lib: unmask mode_t values with 0o777 · nodejs/node@b4b7d36
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/fs.md +6Lines changed: 6 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1089,6 +1089,12 @@ For example, the octal value `0o765` means: | 1089 | 1089 | * The group may read and write the file. | 1090 | 1090 | * Others may read and execute the file. | 1091 | 1091 |
| | 1092 | +Note: When using raw numbers where file modes are expected, | | 1093 | +any value larger than `0o777` may result in platform-specific | | 1094 | +behaviors that are not supported to work consistently. | | 1095 | +Therefore constants like `S_ISVTX`, `S_ISGID` or `S_ISUID` are | | 1096 | +not exposed in `fs.constants`. | | 1097 | + | 1092 | 1098 | Caveats: on Windows only the write permission can be changed, and the | 1093 | 1099 | distinction among the permissions of group, owner or others is not | 1094 | 1100 | implemented. | |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +9-9Lines changed: 9 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,7 +77,7 @@ const { | 77 | 77 | } = require('internal/constants'); | 78 | 78 | const { | 79 | 79 | isUint32, | 80 | | - validateAndMaskMode, | | 80 | + validateMode, | 81 | 81 | validateInteger, | 82 | 82 | validateInt32, | 83 | 83 | validateUint32 | @@ -416,7 +416,7 @@ function open(path, flags, mode, callback) { | 416 | 416 | callback = makeCallback(mode); | 417 | 417 | mode = 0o666; | 418 | 418 | } else { | 419 | | - mode = validateAndMaskMode(mode, 'mode', 0o666); | | 419 | + mode = validateMode(mode, 'mode', 0o666); | 420 | 420 | callback = makeCallback(callback); | 421 | 421 | } | 422 | 422 |
| @@ -434,7 +434,7 @@ function openSync(path, flags, mode) { | 434 | 434 | path = getPathFromURL(path); | 435 | 435 | validatePath(path); | 436 | 436 | const flagsNumber = stringToFlags(flags); | 437 | | - mode = validateAndMaskMode(mode, 'mode', 0o666); | | 437 | + mode = validateMode(mode, 'mode', 0o666); | 438 | 438 |
| 439 | 439 | const ctx = { path }; | 440 | 440 | const result = binding.open(pathModule.toNamespacedPath(path), | @@ -721,7 +721,7 @@ function mkdir(path, mode, callback) { | 721 | 721 | mode = 0o777; | 722 | 722 | } else { | 723 | 723 | callback = makeCallback(callback); | 724 | | - mode = validateAndMaskMode(mode, 'mode', 0o777); | | 724 | + mode = validateMode(mode, 'mode', 0o777); | 725 | 725 | } | 726 | 726 |
| 727 | 727 | const req = new FSReqWrap(); | @@ -732,7 +732,7 @@ function mkdir(path, mode, callback) { | 732 | 732 | function mkdirSync(path, mode) { | 733 | 733 | path = getPathFromURL(path); | 734 | 734 | validatePath(path); | 735 | | - mode = validateAndMaskMode(mode, 'mode', 0o777); | | 735 | + mode = validateMode(mode, 'mode', 0o777); | 736 | 736 | const ctx = { path }; | 737 | 737 | binding.mkdir(pathModule.toNamespacedPath(path), mode, undefined, ctx); | 738 | 738 | handleErrorFromBinding(ctx); | @@ -915,7 +915,7 @@ function unlinkSync(path) { | 915 | 915 |
| 916 | 916 | function fchmod(fd, mode, callback) { | 917 | 917 | validateInt32(fd, 'fd', 0); | 918 | | - mode = validateAndMaskMode(mode, 'mode'); | | 918 | + mode = validateMode(mode, 'mode'); | 919 | 919 | callback = makeCallback(callback); | 920 | 920 |
| 921 | 921 | const req = new FSReqWrap(); | @@ -925,7 +925,7 @@ function fchmod(fd, mode, callback) { | 925 | 925 |
| 926 | 926 | function fchmodSync(fd, mode) { | 927 | 927 | validateInt32(fd, 'fd', 0); | 928 | | - mode = validateAndMaskMode(mode, 'mode'); | | 928 | + mode = validateMode(mode, 'mode'); | 929 | 929 | const ctx = {}; | 930 | 930 | binding.fchmod(fd, mode, undefined, ctx); | 931 | 931 | handleErrorFromBinding(ctx); | @@ -966,7 +966,7 @@ function lchmodSync(path, mode) { | 966 | 966 | function chmod(path, mode, callback) { | 967 | 967 | path = getPathFromURL(path); | 968 | 968 | validatePath(path); | 969 | | - mode = validateAndMaskMode(mode, 'mode'); | | 969 | + mode = validateMode(mode, 'mode'); | 970 | 970 | callback = makeCallback(callback); | 971 | 971 |
| 972 | 972 | const req = new FSReqWrap(); | @@ -977,7 +977,7 @@ function chmod(path, mode, callback) { | 977 | 977 | function chmodSync(path, mode) { | 978 | 978 | path = getPathFromURL(path); | 979 | 979 | validatePath(path); | 980 | | - mode = validateAndMaskMode(mode, 'mode'); | | 980 | + mode = validateMode(mode, 'mode'); | 981 | 981 |
| 982 | 982 | const ctx = { path }; | 983 | 983 | binding.chmod(pathModule.toNamespacedPath(path), mode, undefined, ctx); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,8 +31,8 @@ const { | 31 | 31 | validatePath | 32 | 32 | } = require('internal/fs/utils'); | 33 | 33 | const { | 34 | | - validateAndMaskMode, | 35 | 34 | validateInteger, | | 35 | + validateMode, | 36 | 36 | validateUint32 | 37 | 37 | } = require('internal/validators'); | 38 | 38 | const pathModule = require('path'); | @@ -191,7 +191,7 @@ async function copyFile(src, dest, flags) { | 191 | 191 | async function open(path, flags, mode) { | 192 | 192 | path = getPathFromURL(path); | 193 | 193 | validatePath(path); | 194 | | - mode = validateAndMaskMode(mode, 'mode', 0o666); | | 194 | + mode = validateMode(mode, 'mode', 0o666); | 195 | 195 | return new FileHandle( | 196 | 196 | await binding.openFileHandle(pathModule.toNamespacedPath(path), | 197 | 197 | stringToFlags(flags), | @@ -286,7 +286,7 @@ async function fsync(handle) { | 286 | 286 | async function mkdir(path, mode) { | 287 | 287 | path = getPathFromURL(path); | 288 | 288 | validatePath(path); | 289 | | - mode = validateAndMaskMode(mode, 'mode', 0o777); | | 289 | + mode = validateMode(mode, 'mode', 0o777); | 290 | 290 | return binding.mkdir(pathModule.toNamespacedPath(path), mode, kUsePromises); | 291 | 291 | } | 292 | 292 |
| @@ -358,14 +358,14 @@ async function unlink(path) { | 358 | 358 |
| 359 | 359 | async function fchmod(handle, mode) { | 360 | 360 | validateFileHandle(handle); | 361 | | - mode = validateAndMaskMode(mode, 'mode'); | | 361 | + mode = validateMode(mode, 'mode'); | 362 | 362 | return binding.fchmod(handle.fd, mode, kUsePromises); | 363 | 363 | } | 364 | 364 |
| 365 | 365 | async function chmod(path, mode) { | 366 | 366 | path = getPathFromURL(path); | 367 | 367 | validatePath(path); | 368 | | - mode = validateAndMaskMode(mode, 'mode'); | | 368 | + mode = validateMode(mode, 'mode'); | 369 | 369 | return binding.chmod(pathModule.toNamespacedPath(path), mode, kUsePromises); | 370 | 370 | } | 371 | 371 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/validators.js +17-6Lines changed: 17 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,11 +16,22 @@ function isUint32(value) { | 16 | 16 |
| 17 | 17 | const octalReg = /^[0-7]+$/; | 18 | 18 | const modeDesc = 'must be a 32-bit unsigned integer or an octal string'; | 19 | | -// Validator for mode_t (the S_* constants). Valid numbers or octal strings | 20 | | -// will be masked with 0o777 to be consistent with the behavior in POSIX APIs. | 21 | | -function validateAndMaskMode(value, name, def) { | | 19 | + | | 20 | +/** | | 21 | + * Validate values that will be converted into mode_t (the S_* constants). | | 22 | + * Only valid numbers and octal strings are allowed. They could be converted | | 23 | + * to 32-bit unsigned integers or non-negative signed integers in the C++ | | 24 | + * land, but any value higher than 0o777 will result in platform-specific | | 25 | + * behaviors. | | 26 | + * | | 27 | + * @param {*} value Values to be validated | | 28 | + * @param {string} name Name of the argument | | 29 | + * @param {number} def If specified, will be returned for invalid values | | 30 | + * @returns {number} | | 31 | + */ | | 32 | +function validateMode(value, name, def) { | 22 | 33 | if (isUint32(value)) { | 23 | | - return value & 0o777; | | 34 | + return value; | 24 | 35 | } | 25 | 36 |
| 26 | 37 | if (typeof value === 'number') { | @@ -37,7 +48,7 @@ function validateAndMaskMode(value, name, def) { | 37 | 48 | throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc); | 38 | 49 | } | 39 | 50 | const parsed = parseInt(value, 8); | 40 | | - return parsed & 0o777; | | 51 | + return parsed; | 41 | 52 | } | 42 | 53 |
| 43 | 54 | // TODO(BridgeAR): Only return `def` in case `value == null` | @@ -106,7 +117,7 @@ function validateUint32(value, name, positive) { | 106 | 117 | module.exports = { | 107 | 118 | isInt32, | 108 | 119 | isUint32, | 109 | | - validateAndMaskMode, | | 120 | + validateMode, | 110 | 121 | validateInteger, | 111 | 122 | validateInt32, | 112 | 123 | validateUint32 | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-chmod-mask.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | | -// This tests that mode > 0o777 will be masked off with 0o777 in fs APIs. | | 3 | +// This tests that the lower bits of mode > 0o777 still works in fs APIs. | 4 | 4 |
| 5 | 5 | const common = require('../common'); | 6 | 6 | const assert = require('assert'); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-mkdir-mode-mask.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | | -// This tests that mode > 0o777 will be masked off with 0o777 in fs.mkdir(). | | 3 | +// This tests that the lower bits of mode > 0o777 still works in fs.mkdir(). | 4 | 4 |
| 5 | 5 | const common = require('../common'); | 6 | 6 | const assert = require('assert'); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-open-mode-mask.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | | -// This tests that mode > 0o777 will be masked off with 0o777 in fs.open(). | | 3 | +// This tests that the lower bits of mode > 0o777 still works in fs.open(). | 4 | 4 |
| 5 | 5 | const common = require('../common'); | 6 | 6 | const assert = require('assert'); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-fs-promises.js +2-3Lines changed: 2 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -107,9 +107,8 @@ function verifyStatObject(stat) { | 107 | 107 | await chmod(dest, 0o666); | 108 | 108 | await handle.chmod(0o666); | 109 | 109 |
| 110 | | - // Mode larger than 0o777 should be masked off. | 111 | | - await chmod(dest, (0o777 + 1)); | 112 | | - await handle.chmod(0o777 + 1); | | 110 | + await chmod(dest, (0o10777)); | | 111 | + await handle.chmod(0o10777); | 113 | 112 |
| 114 | 113 | await utimes(dest, new Date(), new Date()); | 115 | 114 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-process-umask-mask.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | 'use strict'; | 2 | 2 |
| 3 | | -// This tests that mask > 0o777 will be masked off with 0o777 in | | 3 | +// This tests that the lower bits of mode > 0o777 still works in | 4 | 4 | // process.umask() | 5 | 5 |
| 6 | 6 | const common = require('../common'); | |
|