Skip to content

Node.js based forum software built for the modern web

Cloudron io fix/register invalid csrf (#14599)

Barış Uşaklı4e36ceeff1ad
diff --git a/src/request.js b/src/request.js
index 576626f6eb..17de6b0c04 100644
--- a/src/request.js
+++ b/src/request.js
@@ -143,6 +143,10 @@ async function call(url, method, { body, timeout, jar, ...config } = {}) {
signal: timeout > 0 ? AbortSignal.timeout(timeout) : undefined,
dispatcher: manualDispatcher,
};
+ if (body instanceof FormData) {
+ // If body is FormData, let fetch handle the content-type header
+ delete opts.headers['content-type'];
+ }
if (body && ['POST', 'PUT', 'PATCH', 'DEL', 'DELETE'].includes(method)) {
if (opts.headers['content-type'] && jsonTest.test(opts.headers['content-type'])) {
diff --git a/src/routes/admin.js b/src/routes/admin.js
index 05bca9b21f..75e213a86e 100644
--- a/src/routes/admin.js
+++ b/src/routes/admin.js
@@ -97,9 +97,9 @@ function apiRoutes(router, name, middleware, controllers) {
const upload = require('../middleware/multer');
const middlewares = [
+ upload.array('files[]', 20),
middleware.applyCSRF,
middleware.ensureLoggedIn,
- upload.array('files[]', 20),
middleware.validateFiles,
];
diff --git a/src/routes/api.js b/src/routes/api.js
index c370803dab..adc2cf0aa3 100644
--- a/src/routes/api.js
+++ b/src/routes/api.js
@@ -27,8 +27,8 @@ module.exports = function (app, middleware, controllers) {
const postMiddlewares = [
middleware.maintenanceMode,
- middleware.applyCSRF,
upload.array('files[]', 20),
+ middleware.applyCSRF,
middleware.validateFiles,
middleware.uploads.ratelimit,
];
diff --git a/src/routes/authentication.js b/src/routes/authentication.js
index 5ac48ded9d..bfb01a124e 100644
--- a/src/routes/authentication.js
+++ b/src/routes/authentication.js
@@ -159,9 +159,9 @@ Auth.reloadRoutes = async function (params) {
const upload = require('../middleware/multer');
const middlewares = [
+ upload.any(),
Auth.middleware.applyCSRF,
Auth.middleware.applyBlacklist,
- upload.any(),
];
router.post('/register', middlewares, controllers.authentication.register);
diff --git a/test/authentication.js b/test/authentication.js
index 547f013f84..e620dd57ef 100644
--- a/test/authentication.js
+++ b/test/authentication.js
@@ -437,12 +437,12 @@ describe('authentication', () => {
}
async function completeRegistration(jar) {
+ const formData = new FormData();
+ formData.append('gdpr_agree_data', 'on');
+ formData.append('gdpr_agree_email', 'on');
return await helpers.request('post', '/register/complete', {
jar,
- body: {
- gdpr_agree_data: 'on',
- gdpr_agree_email: 'on',
- },
+ body: formData,
redirect: 'manual',
});
}
diff --git a/test/helpers/index.js b/test/helpers/index.js
index 670128870e..711b1f94e8 100644
--- a/test/helpers/index.js
+++ b/test/helpers/index.js
@@ -25,7 +25,9 @@ helpers.request = async function (method, uri, options = {}) {
}
options.headers = options.headers || {};
- if (csrf_token) {
+ if (csrf_token && options.body instanceof FormData) {
+ options.body.append('csrf_token', csrf_token);
+ } else if (csrf_token) {
options.headers['x-csrf-token'] = csrf_token;
}
return await request[lowercaseMethod](`${nconf.get('url')}${uri}`, options);