Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 4823afc

Browse files
JacksonTiantrevnorris
authored andcommitted
fs: make F_OK/R_OK/W_OK/X_OK not writable
Change the fs.F_OK/R_OK/W_OK/X_OK out of fs.js will lead unexpect exception. Make these properties readonly. PR-URL: #9060 [[email protected] test properties are read only] Signed-off-by: Trevor Norris <[email protected]>
1 parent d2b5461 commit 4823afc

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

lib/fs.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ var O_RDWR = constants.O_RDWR || 0;
5050
var O_SYNC = constants.O_SYNC || 0;
5151
var O_TRUNC = constants.O_TRUNC || 0;
5252
var O_WRONLY = constants.O_WRONLY || 0;
53-
var F_OK = constants.F_OK || 0;
54-
var R_OK = constants.R_OK || 0;
55-
var W_OK = constants.W_OK || 0;
56-
var X_OK = constants.X_OK || 0;
5753

5854
var isWindows = process.platform === 'win32';
5955

@@ -186,18 +182,20 @@ fs.Stats.prototype.isSocket = function() {
186182
return this._checkModeProperty(constants.S_IFSOCK);
187183
};
188184

189-
fs.F_OK = F_OK;
190-
fs.R_OK = R_OK;
191-
fs.W_OK = W_OK;
192-
fs.X_OK = X_OK;
185+
// don't allow fs.mode to accidentally be overwritten.
186+
['F_OK', 'R_OK', 'W_OK', 'X_OK'].forEach(function(key) {
187+
Object.defineProperty(fs, key, {
188+
enumerable: true, value: constants[key] || 0, writable: false
189+
});
190+
});
193191

194192
fs.access = function(path, mode, callback) {
195193
if (!nullCheck(path, callback))
196194
return;
197195

198196
if (typeof mode === 'function') {
199197
callback = mode;
200-
mode = F_OK;
198+
mode = fs.F_OK;
201199
} else if (typeof callback !== 'function') {
202200
throw new TypeError('callback must be a function');
203201
}
@@ -212,7 +210,7 @@ fs.accessSync = function(path, mode) {
212210
nullCheck(path);
213211

214212
if (mode === undefined)
215-
mode = F_OK;
213+
mode = fs.F_OK;
216214
else
217215
mode = mode | 0;
218216

test/simple/test-fs-access.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ if (process.platform !== 'win32' && process.getuid() === 0) {
7777
}
7878
}
7979

80+
// Check that {F,R,W,X}_OK are read only
81+
82+
fs.F_OK = null;
83+
fs.R_OK = null;
84+
fs.W_OK = null;
85+
fs.X_OK = null;
86+
8087
assert(typeof fs.F_OK === 'number');
8188
assert(typeof fs.R_OK === 'number');
8289
assert(typeof fs.W_OK === 'number');

0 commit comments

Comments
 (0)