refactor: add csrf protection to webinstall POST route

This commit is contained in:
Barış Soner Uşaklı
2024-12-10 10:17:38 -05:00
parent 04eb9e1ad7
commit 8f465afc97
2 changed files with 17 additions and 3 deletions

View File

@@ -2,6 +2,7 @@
const winston = require('winston'); const winston = require('winston');
const express = require('express'); const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser'); const bodyParser = require('body-parser');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
@@ -13,7 +14,10 @@ const nconf = require('nconf');
const Benchpress = require('benchpressjs'); const Benchpress = require('benchpressjs');
const { mkdirp } = require('mkdirp'); const { mkdirp } = require('mkdirp');
const { paths } = require('../src/constants'); const { paths } = require('../src/constants');
const sass = require('../src/utils').getSass(); const utils = require('../src/utils');
const sass = utils.getSass();
const { generateToken, csrfSynchronisedProtection } = require('../src/middleware/csrf');
const app = express(); const app = express();
let server; let server;
@@ -73,6 +77,13 @@ web.install = async function (port) {
app.use(bodyParser.urlencoded({ app.use(bodyParser.urlencoded({
extended: true, extended: true,
})); }));
app.use(session({
secret: utils.generateUUID(),
resave: false,
saveUninitialized: false,
}));
try { try {
await Promise.all([ await Promise.all([
compileTemplate(), compileTemplate(),
@@ -103,8 +114,8 @@ function launchExpress(port) {
} }
function setupRoutes() { function setupRoutes() {
app.get('/', welcome); app.get('/', csrfSynchronisedProtection, welcome);
app.post('/', install); app.post('/', csrfSynchronisedProtection, install);
app.get('/testdb', testDatabase); app.get('/testdb', testDatabase);
app.get('/ping', ping); app.get('/ping', ping);
app.get('/sping', ping); app.get('/sping', ping);
@@ -160,6 +171,7 @@ function welcome(req, res) {
minimumPasswordStrength: defaults.minimumPasswordStrength, minimumPasswordStrength: defaults.minimumPasswordStrength,
installing: installing, installing: installing,
percentInstalled: installing ? ((Date.now() - timeStart) / totalTime * 100).toFixed(2) : 0, percentInstalled: installing ? ((Date.now() - timeStart) / totalTime * 100).toFixed(2) : 0,
csrf_token: generateToken(req),
}); });
} }

View File

@@ -35,6 +35,8 @@
You are just a few steps away from launching your own NodeBB forum! You are just a few steps away from launching your own NodeBB forum!
</p> </p>
<form id="install" action="/" method="post" autocomplete="off"> <form id="install" action="/" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="{csrf_token}" />
{{{ if !skipGeneralSetup }}} {{{ if !skipGeneralSetup }}}
<div class="general"> <div class="general">
<p> <p>