Skip to content

Commit

Permalink
Merge pull request #369 from danielkatz/access-root
Browse files Browse the repository at this point in the history
fix: fix EACCES error on access by root user
  • Loading branch information
tschaub committed Oct 28, 2022
2 parents faaa676 + c50954d commit 642863a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
21 changes: 8 additions & 13 deletions lib/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -1325,19 +1325,14 @@ Binding.prototype.access = function (filepath, mode, callback, ctx) {
throw new FSError('ENOENT', filepath);
}
if (mode && process.getuid && process.getgid) {
const itemMode = item.getMode();
if (item.getUid() === process.getuid()) {
if ((itemMode & (mode * 64)) !== mode * 64) {
throw new FSError('EACCES', filepath);
}
} else if (item.getGid() === process.getgid()) {
if ((itemMode & (mode * 8)) !== mode * 8) {
throw new FSError('EACCES', filepath);
}
} else {
if ((itemMode & mode) !== mode) {
throw new FSError('EACCES', filepath);
}
if (mode & constants.R_OK && !item.canRead()) {
throw new FSError('EACCES', filepath);
}
if (mode & constants.W_OK && !item.canWrite()) {
throw new FSError('EACCES', filepath);
}
if (mode & constants.X_OK && !item.canExecute()) {
throw new FSError('EACCES', filepath);
}
}
});
Expand Down
20 changes: 19 additions & 1 deletion test/lib/binding.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,14 @@ describe('Binding', function () {
});

describe('#access()', function () {
const originalGetuid = process.getuid;
const originalGetgid = process.getgid;

beforeEach(function () {
process.getuid = originalGetuid;
process.getgid = originalGetgid;
});

it('works if file exists', function () {
const binding = new Binding(system);
const pathname = path.join('mock-dir', 'one-link.txt');
Expand All @@ -1639,7 +1647,7 @@ describe('Binding', function () {
}, /ENOENT/);
});

if (process.getuid && process.getgid) {
if (originalGetuid && originalGetgid) {
it('fails in case of insufficient user permissions', function () {
const binding = new Binding(system);
const item = system.getItem(path.join('mock-dir', 'one.txt'));
Expand Down Expand Up @@ -1669,6 +1677,16 @@ describe('Binding', function () {
binding.access(path.join('mock-dir', 'one.txt'), 5);
}, /EACCES/);
});

it('sould not throw if process runs as root', function () {
const binding = new Binding(system);
const item = system.getItem(path.join('mock-dir', 'one.txt'));
item.setUid(42);
item.setGid(42);
item.setMode(parseInt('0000', 8));
process.getuid = () => 0;
binding.access(path.join('mock-dir', 'one.txt'), 5);
});
}

it('fails for bogus paths', function () {
Expand Down
32 changes: 30 additions & 2 deletions test/lib/item.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,15 @@ describe('Item', function () {
});

if (process.getgid && process.getuid) {
const uid = process.getuid();
const gid = process.getgid();
const originalGetuid = process.getuid;
const originalGetgid = process.getgid;
const uid = originalGetuid();
const gid = originalGetgid();

let item;
beforeEach(function () {
process.getuid = originalGetuid;
process.getgid = originalGetgid;
item = new Item();
});

Expand Down Expand Up @@ -220,6 +224,14 @@ describe('Item', function () {
item.setMode(parseInt('0777', 8));
assert.isTrue(item.canRead());
});

it('always returns true if process runs as root', function () {
process.getuid = () => 0;
item.setUid(42);
item.setGid(42);
item.setMode(parseInt('0000', 8));
assert.isTrue(item.canRead());
});
});

describe('#canWrite()', function () {
Expand Down Expand Up @@ -295,6 +307,14 @@ describe('Item', function () {
item.setMode(parseInt('0777', 8));
assert.isTrue(item.canWrite());
});

it('always returns true if process runs as root', function () {
process.getuid = () => 0;
item.setUid(42);
item.setGid(42);
item.setMode(parseInt('0000', 8));
assert.isTrue(item.canWrite());
});
});

describe('#canExecute()', function () {
Expand Down Expand Up @@ -370,6 +390,14 @@ describe('Item', function () {
item.setMode(parseInt('0777', 8));
assert.isTrue(item.canExecute());
});

it('always returns true if process runs as root', function () {
process.getuid = () => 0;
item.setUid(42);
item.setGid(42);
item.setMode(parseInt('0000', 8));
assert.isTrue(item.canExecute());
});
});
}
});

0 comments on commit 642863a

Please # to comment.