From 292d0174df32c93ddc6c8f6e742948b9440a5f70 Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Fri, 12 Jan 2024 11:28:32 -0300 Subject: [PATCH] test: add URL tests to fs-write PR-URL: https://github.com/nodejs/node/pull/51352 Reviewed-By: Yagiz Nizipli Reviewed-By: LiviaMedeiros --- test/fixtures/permission/fs-write.js | 76 ++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/test/fixtures/permission/fs-write.js b/test/fixtures/permission/fs-write.js index 390ac4e53b1910..8c0bf3124b0af3 100644 --- a/test/fixtures/permission/fs-write.js +++ b/test/fixtures/permission/fs-write.js @@ -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); @@ -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', () => {}); }, { @@ -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(), () => {}); }, { @@ -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 @@ -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); @@ -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); }, { @@ -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); }, { @@ -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'); }, { @@ -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); }, { @@ -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); }, { @@ -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')); }, { @@ -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), + }); }