Skip to content

Commit

Permalink
test: add URL tests to fs-write
Browse files Browse the repository at this point in the history
PR-URL: #51352
Reviewed-By: Yagiz Nizipli <yagiz.nizipli@sentry.io>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
  • Loading branch information
RafaelGSS authored and richardlau committed Mar 25, 2024
1 parent 4a8a012 commit 292d017
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 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 blockedFileURL = require('url').pathToFileURL(process.env.BLOCKEDFILE);
const relativeProtectedFile = process.env.RELATIVEBLOCKEDFILE;
const relativeProtectedFolder = process.env.RELATIVEBLOCKEDFOLDER;
const absoluteProtectedFile = path.resolve(relativeProtectedFile);
Expand All @@ -30,6 +31,13 @@ const absoluteProtectedFolder = path.resolve(relativeProtectedFolder);
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
assert.throws(() => {
fs.writeFile(blockedFileURL, 'example', () => {});
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.writeFile(relativeProtectedFile, 'example', () => {});
}, {
Expand Down Expand Up @@ -91,6 +99,13 @@ const absoluteProtectedFolder = path.resolve(relativeProtectedFolder);
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
assert.throws(() => {
fs.utimes(blockedFileURL, new Date(), new Date(), () => {});
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.utimes(relativeProtectedFile, new Date(), new Date(), () => {});
}, {
Expand All @@ -117,6 +132,13 @@ const absoluteProtectedFolder = path.resolve(relativeProtectedFolder);
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
assert.throws(() => {
fs.lutimes(blockedFileURL, new Date(), new Date(), () => {});
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
}));
}

// fs.mkdir
Expand Down Expand Up @@ -169,6 +191,15 @@ const absoluteProtectedFolder = path.resolve(relativeProtectedFolder);
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
assert.throws(() => {
fs.rename(blockedFileURL, path.join(blockedFile, 'renamed'), (err) => {
assert.ifError(err);
});
}, common.expectsError({
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
}));
assert.throws(() => {
fs.rename(relativeProtectedFile, path.join(relativeProtectedFile, 'renamed'), (err) => {
assert.ifError(err);
Expand Down Expand Up @@ -263,6 +294,12 @@ const absoluteProtectedFolder = path.resolve(relativeProtectedFolder);
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.throws(() => {
fs.open(blockedFileURL, fs.constants.O_RDWR | 0x10000000, common.mustNotCall());
}, {
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 @@ -290,6 +327,12 @@ const absoluteProtectedFolder = path.resolve(relativeProtectedFolder);
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.throws(() => {
fs.chmod(blockedFileURL, 0o755, common.mustNotCall());
}, {
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.rejects(async () => {
await fs.promises.chmod(blockedFile, 0o755);
}, {
Expand Down Expand Up @@ -324,6 +367,12 @@ const absoluteProtectedFolder = path.resolve(relativeProtectedFolder);
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.throws(() => {
fs.appendFile(blockedFileURL, 'new data', common.mustNotCall());
}, {
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.rejects(async () => {
await fs.promises.appendFile(blockedFile, 'new data');
}, {
Expand All @@ -340,6 +389,12 @@ const absoluteProtectedFolder = path.resolve(relativeProtectedFolder);
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.throws(() => {
fs.chown(blockedFileURL, 1541, 999, common.mustNotCall());
}, {
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.rejects(async () => {
await fs.promises.chown(blockedFile, 1541, 999);
}, {
Expand All @@ -356,6 +411,12 @@ const absoluteProtectedFolder = path.resolve(relativeProtectedFolder);
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.throws(() => {
fs.lchown(blockedFileURL, 1541, 999, common.mustNotCall());
}, {
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.rejects(async () => {
await fs.promises.lchown(blockedFile, 1541, 999);
}, {
Expand All @@ -372,6 +433,12 @@ const absoluteProtectedFolder = path.resolve(relativeProtectedFolder);
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.throws(() => {
fs.link(blockedFileURL, path.join(blockedFolder, '/linked'), common.mustNotCall());
}, {
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
});
assert.rejects(async () => {
await fs.promises.link(blockedFile, path.join(blockedFolder, '/linked'));
}, {
Expand All @@ -391,4 +458,13 @@ const absoluteProtectedFolder = path.resolve(relativeProtectedFolder);
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
assert.throws(() => {
fs.unlink(blockedFileURL, (err) => {
assert.ifError(err);
});
}, {
code: 'ERR_ACCESS_DENIED',
permission: 'FileSystemWrite',
resource: path.toNamespacedPath(blockedFile),
});
}

0 comments on commit 292d017

Please sign in to comment.