Skip to content

Node.js based forum software built for the modern web

refactor: remove middleware.addUploadHeaders from statics

Barış Soner Uşaklı709b91941483
diff --git a/src/controllers/errors.js b/src/controllers/errors.js
index 5c7de56b9b..16b37c2272 100644
--- a/src/controllers/errors.js
+++ b/src/controllers/errors.js
@@ -84,7 +84,7 @@ exports.handleErrors = async function handleErrors(err, req, res, next) { // esl
return helpers.formatApiResponse(status, res, err);
}
- winston.error(`${req.method} ${req.originalUrl}\n${err.stack}`);
+ winston.error(`${req.method} ${req.originalUrl}${err.stack ? `\n${err.stack}` : ''}`);
res.status(status || 500);
const data = {
path: path,
diff --git a/src/middleware/index.js b/src/middleware/index.js
index 8c66a51d4d..6a10ec4cfa 100644
--- a/src/middleware/index.js
+++ b/src/middleware/index.js
@@ -34,10 +34,6 @@ const middleware = module.exports;
const relative_path = nconf.get('relative_path');
-middleware.regexes = {
- timestampedUpload: /^\d+-.+$/,
-};
-
const csrfMiddleware = csrfSynchronisedProtection;
middleware.applyCSRF = function (req, res, next) {
@@ -205,74 +201,6 @@ async function expose(exposedField, method, canViewMethod, field, req, res, next
next();
}
-middleware.privateUploads = function privateUploads(req, res, next) {
- if (req.loggedIn || !meta.config.privateUploads) {
- return next();
- }
-
- const uploadPrefix = `${nconf.get('relative_path')}/assets/uploads/files`.toLowerCase();
- let requestPath = req.path;
- try {
- requestPath = normalizeUploadRequestPath(requestPath);
- } catch (err) {
- return res.status(403).json('not-allowed');
- }
-
- if (requestPath === uploadPrefix || requestPath.startsWith(`${uploadPrefix}/`)) {
- const privateExtensions = (meta.config.privateUploadsExtensions || '')
- .split(',')
- .map(ext => ext.trim().toLowerCase())
- .filter(Boolean);
- let ext = path.extname(requestPath);
- ext = ext ? ext.replace(/^\./, '').trim().toLowerCase() : ext;
- if (!ext || !privateExtensions.length || privateExtensions.includes(ext)) {
- return res.status(403).json('not-allowed');
- }
- }
- next();
-};
-
-const unsafeExtensions = new Set([
- '', '.',
- '.html', '.htm', '.xhtml', '.mht', '.mhtml', '.stm', '.shtm', '.shtml',
- '.svg', '.svgz',
- '.xml', '.xsl', '.xslt',
- '.rss', '.atom', '.rpf', '.rng', '.sch', '.dtd', '.epub',
- '.xaml', '.plist', '.vcf', '.opf', '.rdf', '.wsdl', '.resx',
- '.xsd', '.mathml', '.xht',
-]);
-
-middleware.addUploadHeaders = helpers.try(function addUploadHeaders(req, res, next) {
- // Trim uploaded files' timestamps when downloading + force download if unsafe
- const p = normalizeUploadRequestPath(req.path);
-
- if (p.startsWith('/uploads/')) {
- let basename = path.basename(p);
- const extname = path.extname(p).trim().toLowerCase();
- const isInlineSafe = !unsafeExtensions.has(extname);
- const dispositionType = isInlineSafe ? 'inline' : 'attachment';
- if (middleware.regexes.timestampedUpload.test(basename)) {
- basename = basename.slice(14);
- }
- res.setHeader('X-Content-Type-Options', 'nosniff');
- res.header('Content-Disposition', `${dispositionType}; filename="${basename}"`);
- }
-
- next();
-});
-
-function normalizeUploadRequestPath(requestPath) {
- const decodedPath = decodeURIComponent(requestPath);
- if (decodedPath.includes(':') || /[. ](\/|$)/.test(decodedPath)) {
- const err = new Error('[[error:invalid-path]]');
- err.status = 403;
- throw err;
- }
- return path.posix.normalize(
- decodedPath.replace(/\\/g, '/')
- ).toLowerCase();
-}
-
middleware.busyCheck = function busyCheck(req, res, next) {
if (process.env.NODE_ENV === 'production' && meta.config.eventLoopCheckEnabled && toobusy()) {
analytics.increment('errors:503');
diff --git a/src/middleware/uploads.js b/src/middleware/uploads.js
index 0c7c5bbde0..a68846e41b 100644
--- a/src/middleware/uploads.js
+++ b/src/middleware/uploads.js
@@ -1,5 +1,6 @@
'use strict';
+const path = require('path');
const cacheCreate = require('../cache/ttl');
const meta = require('../meta');
const helpers = require('./helpers');
@@ -7,6 +8,8 @@ const user = require('../user');
let cache;
+const timestampedUploadRegex = /^\d+-.+$/;
+
exports.clearCache = function () {
if (cache) {
cache.clear();
@@ -33,3 +36,73 @@ exports.ratelimit = helpers.try(async (req, res, next) => {
next();
});
+exports.privateUploads = helpers.try(function privateUploads(req, res, next) {
+ if (req.loggedIn || !meta.config.privateUploads) {
+ return next();
+ }
+
+ const uploadPrefix = `/uploads/files`;
+ const requestPath = normalizeUploadRequestPath(req.path);
+ if (requestPath === uploadPrefix || requestPath.startsWith(`${uploadPrefix}/`)) {
+ const privateExtensions = (meta.config.privateUploadsExtensions || '')
+ .split(',')
+ .map(ext => ext.trim().toLowerCase())
+ .filter(Boolean);
+ let ext = path.extname(requestPath);
+ ext = ext ? ext.replace(/^\./, '').trim().toLowerCase() : ext;
+ if (!ext || !privateExtensions.length || privateExtensions.includes(ext)) {
+ return res.status(403).json('not-allowed');
+ }
+ }
+ next();
+});
+
+const unsafeExtensions = new Set([
+ '', '.',
+ '.html', '.htm', '.xhtml', '.mht', '.mhtml', '.stm', '.shtm', '.shtml',
+ '.svg', '.svgz',
+ '.xml', '.xsl', '.xslt',
+ '.rss', '.atom', '.rpf', '.rng', '.sch', '.dtd', '.epub',
+ '.xaml', '.plist', '.vcf', '.opf', '.rdf', '.wsdl', '.resx',
+ '.xsd', '.mathml', '.xht',
+]);
+
+exports.addUploadHeaders = helpers.try(function addUploadHeaders(req, res, next) {
+ const requestedPath = normalizeUploadRequestPath(req.path);
+ if (requestedPath.startsWith('/uploads/')) {
+ let basename = path.basename(requestedPath);
+ const extname = path.extname(requestedPath).trim().toLowerCase();
+ // Trim uploaded files' timestamps when downloading + force download if unsafe
+ if (timestampedUploadRegex.test(basename)) {
+ basename = basename.slice(14);
+ }
+ res.setHeader('X-Content-Type-Options', 'nosniff');
+ if (unsafeExtensions.has(extname)) {
+ res.attachment(basename);
+ }
+ }
+
+ next();
+});
+
+function normalizeUploadRequestPath(requestPath) {
+ let decodedPath;
+ try {
+ decodedPath = decodeURIComponent(requestPath);
+ } catch (_err) {
+ invalidPathError();
+ }
+ if (decodedPath.includes(':') || /[. ](\/|$)/.test(decodedPath)) {
+ invalidPathError();
+ }
+ return path.posix.normalize(
+ decodedPath.replace(/\\/g, '/')
+ ).toLowerCase();
+}
+
+function invalidPathError() {
+ const err = new Error('[[error:invalid-path]]');
+ err.status = 403;
+ err.stack = err.stack.split('\n').slice(0, 1).join('\n');
+ throw err;
+}
diff --git a/src/routes/index.js b/src/routes/index.js
index a293075506..e22c496785 100644
--- a/src/routes/index.js
+++ b/src/routes/index.js
@@ -174,7 +174,8 @@ function addCoreRoutes(app, router, middleware, mounts) {
require('./debug')(app, middleware, controllers);
}
- app.use(middleware.privateUploads);
+ app.use(`${relativePath}/assets`, middleware.uploads.privateUploads);
+ app.use(`${relativePath}/assets`, middleware.uploads.addUploadHeaders);
const statics = [
{ route: '/assets', path: path.join(__dirname, '../../build/public') },
@@ -185,11 +186,11 @@ function addCoreRoutes(app, router, middleware, mounts) {
};
if (path.resolve(__dirname, '../../public/uploads') !== nconf.get('upload_path')) {
- statics.unshift({ route: '/assets', path: path.join(nconf.get('upload_path'), '..') });
+ statics.unshift({ route: '/assets/uploads', path: nconf.get('upload_path') });
}
statics.forEach((obj) => {
- app.use(relativePath + obj.route, middleware.addUploadHeaders, express.static(obj.path, staticOptions));
+ app.use(relativePath + obj.route, express.static(obj.path, staticOptions));
});
app.use(`${relativePath}/uploads`, (req, res) => {
res.redirect(`${relativePath}/assets/uploads${req.path}?${meta.config['cache-buster']}`);
diff --git a/test/uploads.js b/test/uploads.js
index 88b7d62f1c..c41de6c772 100644
--- a/test/uploads.js
+++ b/test/uploads.js
@@ -334,6 +334,7 @@ describe('Upload Controllers', () => {
`/assets/uploads/%2e%2e/uploads/files/${filename}`,
`/assets/uploads/%2e/files/${filename}`,
`/assets/uploads%5cfiles%5c${filename}`,
+ `/assets/uploads/files/test-%G1.pdf`, // invalid percent encoding, should be blocked
];
for (const p of requestPathsToCheck) {
// eslint-disable-next-line no-await-in-loop