Skip to content

Commit

Permalink
Fix #602: fix mode masking issues, correct X_OK case in fs.access
Browse files Browse the repository at this point in the history
  • Loading branch information
humphd committed Dec 22, 2018
1 parent 4f427ed commit d4bfcd7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
25 changes: 21 additions & 4 deletions src/filesystem/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,17 +451,34 @@ function make_directory(context, path, callback) {
}

function access_file(context, path, mode, callback) {
const { F_OK, R_OK, W_OK, X_OK, S_IXUSR, S_IXGRP, S_IXOTH } = Constants.fsConstants;

path = normalize(path);
find_node(context, path, function (err, node) {
if (err) {
return callback(err);
}
if(mode === Constants.F_OK){

// If we have a node, F_OK is true.
if(mode === F_OK) {
return callback(null);
}

var st_mode = validateAndMaskMode(node.mode, callback);
if(!st_mode) return;

// For any other combo of F_OK, R_OK, W_OK, always allow. Filer user is a root user,
// so existing files are always OK, readable, and writable
if(mode & (R_OK | W_OK)) {
return callback(null);
}
if (!(mode & Constants.X_OK) || (node.mode & (Constants.fsConstants.S_IXUSR | Constants.fsConstants.S_IXGRP | Constants.fsConstants.S_IXOTH))){

// For the case of X_OK, actually check if this file is executable
if ((mode & X_OK) && (st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) {
return callback(null);
}

// In any other case, the file isn't accessible
callback(new Errors.EACCES('permission denied',path)) ;
});
}
Expand Down Expand Up @@ -2221,14 +2238,14 @@ function futimes(fs, context, fd, atime, mtime, callback) {

function chmod(fs, context, path, mode, callback) {
if(!pathCheck(path, callback)) return;
mode = validateAndMaskMode(mode, 'mode', callback);
mode = validateAndMaskMode(mode, callback);
if(!mode) return;

chmod_file(context, path, mode, callback);
}

function fchmod(fs, context, fd, mode, callback) {
mode = validateAndMaskMode(mode, 'mode', callback);
mode = validateAndMaskMode(mode, callback);
if(!mode) return;

var ofd = fs.openFiles[fd];
Expand Down
7 changes: 3 additions & 4 deletions tests/spec/fs.access.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@ describe('fs.access', function () {
});
});

// See bug https://github.com/filerjs/filer/issues/602
it.skip('should return an error if file is not executable and mode = X_OK', function (done) {
it('should return an error if file is not executable and mode = X_OK', function (done) {
var fs = util.fs();
var contents = 'This is a file.';

fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;

fs.chmod('/myfile', 0o644, function(error){
fs.chmod('/myfile', '644', function(error){
if (error) throw error;

fs.access('/myfile', fs.constants.X_OK, function (error) {
Expand All @@ -89,7 +88,7 @@ describe('fs.access', function () {
fs.writeFile('/myfile', contents, function (error) {
if (error) throw error;

fs.chmod('/myfile', 0o777, function(error){
fs.chmod('/myfile', 0o777, function(error) {
if (error) throw error;

fs.access('/myfile', fs.constants.X_OK, function (error) {
Expand Down

0 comments on commit d4bfcd7

Please sign in to comment.