Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix EACCES error on access by root user #369

Merged
merged 1 commit into from Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 8 additions & 13 deletions lib/binding.js
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
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
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());
});
});
}
});