mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 19:15:58 +01:00 
			
		
		
		
	refactor: add csrf protection to webinstall POST route
This commit is contained in:
		| @@ -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), | ||||||
| 	}); | 	}); | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -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> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user