Skip to content

Commit

Permalink
test: add buffer to fs_permission tests
Browse files Browse the repository at this point in the history
PR-URL: nodejs#55734
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
RafaelGSS authored and tpoisseau committed Nov 21, 2024
1 parent c4b7820 commit a46099e
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/fixtures/permission/fs-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const regularFile = __filename;
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
fs.readFile(bufferBlockedFile, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.readFileSync(blockedFile);
}, common.expectsError({
Expand Down Expand Up @@ -79,6 +84,11 @@ const regularFile = __filename;
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
fs.stat(bufferBlockedFile, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.statSync(blockedFile);
}, common.expectsError({
Expand Down Expand Up @@ -112,6 +122,11 @@ const regularFile = __filename;
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
fs.access(bufferBlockedFile, fs.constants.R_OK, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.accessSync(blockedFileURL, fs.constants.R_OK);
}, common.expectsError({
Expand Down Expand Up @@ -140,6 +155,11 @@ const regularFile = __filename;
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
fs.copyFile(bufferBlockedFile, path.join(blockedFolder, 'any-other-file'), common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.copyFileSync(blockedFileURL, path.join(blockedFolder, 'any-other-file'));
}, common.expectsError({
Expand All @@ -165,6 +185,13 @@ const regularFile = __filename;
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.cpSync(bufferBlockedFile, path.join(blockedFolder, 'any-other-file'));
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.cpSync(blockedFileURL, path.join(blockedFolder, 'any-other-file'));
}, common.expectsError({
Expand All @@ -188,6 +215,11 @@ const regularFile = __filename;
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
fs.open(bufferBlockedFile, 'r', common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.openSync(blockedFileURL, 'r');
}, common.expectsError({
Expand Down Expand Up @@ -312,6 +344,11 @@ const regularFile = __filename;
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
fs.rename(bufferBlockedFile, 'newfile', common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.renameSync(blockedFile, 'newfile');
}, common.expectsError({
Expand All @@ -337,6 +374,13 @@ const regularFile = __filename;
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.openAsBlob(bufferBlockedFile);
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.openAsBlob(blockedFileURL);
}, common.expectsError({
Expand Down Expand Up @@ -371,6 +415,11 @@ const regularFile = __filename;
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
fs.statfs(bufferBlockedFile, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemRead',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.statfsSync(blockedFile);
}, common.expectsError({
Expand Down Expand Up @@ -405,6 +454,11 @@ const regularFile = __filename;
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
}));
assert.throws(() => {
fs.lstatSync(bufferBlockedFile);
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
}));
assert.throws(() => {
fs.lstatSync(path.join(blockedFolder, 'anyfile'));
}, common.expectsError({
Expand Down
63 changes: 63 additions & 0 deletions test/fixtures/permission/fs-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const regularFolder = process.env.ALLOWEDFOLDER;
const regularFile = process.env.ALLOWEDFILE;
const blockedFolder = process.env.BLOCKEDFOLDER;
const blockedFile = process.env.BLOCKEDFILE;
const bufferBlockedFile = Buffer.from(process.env.BLOCKEDFILE);
const blockedFileURL = require('url').pathToFileURL(process.env.BLOCKEDFILE);
const relativeProtectedFile = process.env.RELATIVEBLOCKEDFILE;
const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
Expand All @@ -34,6 +35,11 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
}));
fs.writeFile(bufferBlockedFile, 'example', common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.writeFileSync(blockedFileURL, 'example');
}, {
Expand Down Expand Up @@ -102,6 +108,13 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
assert.throws(() => {
fs.utimes(bufferBlockedFile, new Date(), new Date(), () => {});
}, {
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
assert.throws(() => {
fs.utimes(blockedFileURL, new Date(), new Date(), () => {});
}, {
Expand Down Expand Up @@ -135,6 +148,13 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
assert.throws(() => {
fs.lutimes(bufferBlockedFile, new Date(), new Date(), () => {});
},{
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
assert.throws(() => {
fs.lutimes(blockedFileURL, new Date(), new Date(), () => {});
}, {
Expand Down Expand Up @@ -193,6 +213,11 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
}));
fs.rename(bufferBlockedFile, path.join(blockedFile, 'renamed'), common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.renameSync(blockedFileURL, path.join(blockedFile, 'renamed'));
}, {
Expand Down Expand Up @@ -245,6 +270,11 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(path.join(relativeProtectedFolder, 'any-file')),
}));
fs.copyFile(bufferBlockedFile, path.join(relativeProtectedFolder, 'any-file'), common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(path.join(relativeProtectedFolder, 'any-file')),
}));
}

// fs.cp
Expand Down Expand Up @@ -295,6 +325,10 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}));
fs.open(bufferBlockedFile, fs.constants.O_RDWR | 0x10000000, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}));
assert.rejects(async () => {
await fs.promises.open(blockedFile, fs.constants.O_RDWR | fs.constants.O_NOFOLLOW);
}, {
Expand Down Expand Up @@ -322,6 +356,12 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.throws(() => {
fs.chmod(bufferBlockedFile, 0o755, common.mustNotCall());
}, {
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.throws(() => {
fs.chmod(blockedFileURL, 0o755, common.mustNotCall());
}, {
Expand Down Expand Up @@ -358,6 +398,10 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}));
fs.appendFile(bufferBlockedFile, 'new data', common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}));
assert.throws(() => {
fs.appendFileSync(blockedFileURL, 'new data');
}, {
Expand All @@ -378,6 +422,10 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}));
fs.chown(bufferBlockedFile, 1541, 999, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}));
assert.throws(() => {
fs.chownSync(blockedFileURL, 1541, 999);
}, {
Expand All @@ -399,6 +447,10 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}));
fs.lchown(bufferBlockedFile, 1541, 999, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}));
assert.throws(() => {
fs.lchownSync(blockedFileURL, 1541, 999);
}, {
Expand Down Expand Up @@ -426,6 +478,10 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}));
fs.link(bufferBlockedFile, path.join(blockedFolder, '/linked'), common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
}));
assert.throws(() => {
fs.linkSync(blockedFileURL, path.join(blockedFolder, '/linked'));
}, {
Expand All @@ -450,6 +506,13 @@ const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
assert.throws(() => {
fs.unlinkSync(bufferBlockedFile);
}, {
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
fs.unlink(blockedFile, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
Expand Down

0 comments on commit a46099e

Please sign in to comment.