Skip to content

Commit

Permalink
fs: make F_OK/R_OK/W_OK/X_OK not writable
Browse files Browse the repository at this point in the history
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: nodejs/node-v0.x-archive#9060
[[email protected] test properties are read only]
Signed-off-by: Trevor Norris <[email protected]>
  • Loading branch information
JacksonTian authored and trevnorris committed Feb 11, 2015
1 parent d2b5461 commit 4823afc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ var O_RDWR = constants.O_RDWR || 0;
var O_SYNC = constants.O_SYNC || 0;
var O_TRUNC = constants.O_TRUNC || 0;
var O_WRONLY = constants.O_WRONLY || 0;
var F_OK = constants.F_OK || 0;
var R_OK = constants.R_OK || 0;
var W_OK = constants.W_OK || 0;
var X_OK = constants.X_OK || 0;

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

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

fs.F_OK = F_OK;
fs.R_OK = R_OK;
fs.W_OK = W_OK;
fs.X_OK = X_OK;
// don't allow fs.mode to accidentally be overwritten.
['F_OK', 'R_OK', 'W_OK', 'X_OK'].forEach(function(key) {
Object.defineProperty(fs, key, {
enumerable: true, value: constants[key] || 0, writable: false
});
});

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

if (typeof mode === 'function') {
callback = mode;
mode = F_OK;
mode = fs.F_OK;
} else if (typeof callback !== 'function') {
throw new TypeError('callback must be a function');
}
Expand All @@ -212,7 +210,7 @@ fs.accessSync = function(path, mode) {
nullCheck(path);

if (mode === undefined)
mode = F_OK;
mode = fs.F_OK;
else
mode = mode | 0;

Expand Down
7 changes: 7 additions & 0 deletions test/simple/test-fs-access.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ if (process.platform !== 'win32' && process.getuid() === 0) {
}
}

// Check that {F,R,W,X}_OK are read only

fs.F_OK = null;
fs.R_OK = null;
fs.W_OK = null;
fs.X_OK = null;

assert(typeof fs.F_OK === 'number');
assert(typeof fs.R_OK === 'number');
assert(typeof fs.W_OK === 'number');
Expand Down

0 comments on commit 4823afc

Please sign in to comment.