From b6f232e841cdc6828f556b271b6e1c43abf44742 Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Thu, 21 Dec 2023 12:56:02 +0000 Subject: [PATCH] test: add URL tests to fs-read in pm PR-URL: https://github.com/nodejs/node/pull/51213 Reviewed-By: Yagiz Nizipli Reviewed-By: Marco Ippolito --- test/fixtures/permission/fs-read.js | 87 +++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 6 deletions(-) diff --git a/test/fixtures/permission/fs-read.js b/test/fixtures/permission/fs-read.js index 37ab086ebb2895..5b2035628c38d3 100644 --- a/test/fixtures/permission/fs-read.js +++ b/test/fixtures/permission/fs-read.js @@ -7,6 +7,7 @@ const fs = require('fs'); const path = require('path'); const blockedFile = process.env.BLOCKEDFILE; +const blockedFileURL = new URL('file://' + process.env.BLOCKEDFILE); const blockedFolder = process.env.BLOCKEDFOLDER; const allowedFolder = process.env.ALLOWEDFOLDER; const regularFile = __filename; @@ -21,15 +22,12 @@ const regularFile = __filename; resource: path.toNamespacedPath(blockedFile), })); assert.throws(() => { - fs.readFile(path.join(blockedFolder, 'anyfile'), () => {}); + fs.readFile(blockedFileURL, () => {}); }, common.expectsError({ code: 'ERR_ACCESS_DENIED', permission: 'FileSystemRead', - resource: path.toNamespacedPath(path.join(blockedFolder, 'anyfile')), + resource: path.toNamespacedPath(blockedFile), })); - - // doesNotThrow - fs.readFile(regularFile, () => {}); } // fs.createReadStream @@ -44,6 +42,16 @@ const regularFile = __filename; permission: 'FileSystemRead', resource: path.toNamespacedPath(blockedFile), })).then(common.mustCall()); + assert.rejects(() => { + return new Promise((_resolve, reject) => { + const stream = fs.createReadStream(blockedFileURL); + stream.on('error', reject); + }); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })).then(common.mustCall()); assert.rejects(() => { return new Promise((_resolve, reject) => { @@ -66,6 +74,13 @@ const regularFile = __filename; permission: 'FileSystemRead', resource: path.toNamespacedPath(blockedFile), })); + assert.throws(() => { + fs.stat(blockedFileURL, () => {}); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })); assert.throws(() => { fs.stat(path.join(blockedFolder, 'anyfile'), () => {}); }, common.expectsError({ @@ -89,6 +104,13 @@ const regularFile = __filename; permission: 'FileSystemRead', resource: path.toNamespacedPath(blockedFile), })); + assert.throws(() => { + fs.access(blockedFileURL, fs.constants.R_OK, () => {}); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })); assert.throws(() => { fs.access(path.join(blockedFolder, 'anyfile'), fs.constants.R_OK, () => {}); }, common.expectsError({ @@ -112,6 +134,13 @@ const regularFile = __filename; permission: 'FileSystemRead', resource: path.toNamespacedPath(blockedFile), })); + assert.throws(() => { + fs.copyFile(blockedFileURL, path.join(blockedFolder, 'any-other-file'), () => {}); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })); assert.throws(() => { fs.copyFile(blockedFile, path.join(__dirname, 'any-other-file'), () => {}); }, common.expectsError({ @@ -131,6 +160,14 @@ const regularFile = __filename; // cpSync calls statSync before reading blockedFile resource: path.toNamespacedPath(blockedFolder), })); + assert.throws(() => { + fs.cpSync(blockedFileURL, path.join(blockedFolder, 'any-other-file')); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + // cpSync calls statSync before reading blockedFile + resource: path.toNamespacedPath(blockedFolder), + })); assert.throws(() => { fs.cpSync(blockedFile, path.join(__dirname, 'any-other-file')); }, common.expectsError({ @@ -149,6 +186,13 @@ const regularFile = __filename; permission: 'FileSystemRead', resource: path.toNamespacedPath(blockedFile), })); + assert.throws(() => { + fs.open(blockedFileURL, 'r', () => {}); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })); assert.throws(() => { fs.open(path.join(blockedFolder, 'anyfile'), 'r', () => {}); }, common.expectsError({ @@ -237,6 +281,13 @@ const regularFile = __filename; permission: 'FileSystemRead', resource: path.toNamespacedPath(blockedFile), })); + assert.throws(() => { + fs.watchFile(blockedFileURL, common.mustNotCall()); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })); } // fs.rename @@ -248,6 +299,13 @@ const regularFile = __filename; permission: 'FileSystemRead', resource: path.toNamespacedPath(blockedFile), })); + assert.throws(() => { + fs.rename(blockedFileURL, 'newfile', () => {}); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })); } // fs.openAsBlob @@ -259,6 +317,13 @@ const regularFile = __filename; permission: 'FileSystemRead', resource: path.toNamespacedPath(blockedFile), })); + assert.throws(() => { + fs.openAsBlob(blockedFileURL); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })); } // fs.exists @@ -267,6 +332,9 @@ const regularFile = __filename; fs.exists(blockedFile, (exists) => { assert.equal(exists, false); }); + fs.exists(blockedFileURL, (exists) => { + assert.equal(exists, false); + }); } // fs.statfs @@ -278,4 +346,11 @@ const regularFile = __filename; permission: 'FileSystemRead', resource: path.toNamespacedPath(blockedFile), })); -} + assert.throws(() => { + fs.statfs(blockedFileURL, () => {}); + }, common.expectsError({ + code: 'ERR_ACCESS_DENIED', + permission: 'FileSystemRead', + resource: path.toNamespacedPath(blockedFile), + })); +} \ No newline at end of file