Skip to content

Node.js based forum software built for the modern web

fix: check upload folder containment in admin uploadFile (#14693)

palmoni512230a39d10e
diff --git a/src/controllers/admin/uploads.js b/src/controllers/admin/uploads.js
index 44659c1942..f3f04169db 100644
--- a/src/controllers/admin/uploads.js
+++ b/src/controllers/admin/uploads.js
@@ -233,7 +233,9 @@ uploadsController.uploadFile = async function (req, res, next) {
return next(new Error('[[error:invalid-json]]'));
}
- if (!await file.exists(path.join(nconf.get('upload_path'), params.folder))) {
+ const uploadPath = nconf.get('upload_path');
+ const targetFolder = path.join(uploadPath, params.folder || '');
+ if (!file.isPathInside(uploadPath, targetFolder) || !await file.exists(targetFolder)) {
return next(new Error('[[error:invalid-path]]'));
}
diff --git a/test/uploads.js b/test/uploads.js
index c41de6c772..8b057b4409 100644
--- a/test/uploads.js
+++ b/test/uploads.js
@@ -692,6 +692,17 @@ describe('Upload Controllers', () => {
assert.strictEqual(body.error, '[[error:invalid-path]]');
});
+ it('should fail to upload regular file to an existing directory outside of upload_path', async () => {
+ const { response, body } = await helpers.uploadFile(`${nconf.get('url')}/api/admin/upload/file`, path.join(__dirname, '../test/files/test.png'), {
+ params: JSON.stringify({
+ folder: '../',
+ }),
+ }, jar, csrf_token);
+
+ assert.equal(response.statusCode, 500);
+ assert.strictEqual(body.error, '[[error:invalid-path]]');
+ });
+
it('should fail to upload regular file if directory does not exist', async () => {
const { response, body } = await helpers.uploadFile(`${nconf.get('url')}/api/admin/upload/file`, path.join(__dirname, '../test/files/test.png'), {
params: JSON.stringify({