1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| const express = require('express'); const bodyParser = require('body-parser'); const session = require('express-session'); const randomize = require('randomatic'); const ejs = require('ejs'); const path = require('path'); const app = express();
function merge(target, source) { for (let key in source) { if (key === '__proto__') { throw new Error("Detected Prototype Pollution") } if (key in source && key in target) { merge(target[key], source[key]) } else { target[key] = source[key] } } }
app.use(bodyParser.urlencoded({extended: true})).use(bodyParser.json()); app.set('views', path.join(__dirname, "./views")); app.set('view engine', 'ejs'); app.use(session({ name: 'session', secret: randomize('aA0', 16), resave: false, saveUninitialized: false }))
app.all("/login", (req, res) => { if (req.method == 'POST') { console.log(req.body) let data = {}; try { merge(data, req.body) } catch (e) { return res.render("login", {message: "Don't pollution my shared diary!"}) } req.session.data = data let user = {}; user.password = req.body.password; if (user.password=== "testpassword") { user.role = 'admin' } if (user.role === 'admin') { req.session.role = 'admin' return res.redirect('/') }else { return res.render("login", {message: "Login as admin or don't touch my shared diary!"}) } } res.render('login', {message: ""}); });
app.all('/', (req, res) => { if (!req.session.data || !req.session.data.username || req.session.role !== 'admin') { return res.redirect("/login") } if (req.method == 'POST') { let diary = ejs.render(`<div>${req.body.diary}</div>`) req.session.diary = diary return res.render('diary', {diary: req.session.diary, username: req.session.data.username}); } return res.render('diary', {diary: req.session.diary, username: req.session.data.username}); })
app.listen(8888, '0.0.0.0');
|