Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 9, 2023 05:01
Show Gist options
  • Save code-boxx/6a10b2dccd116808601690c7d12730a7 to your computer and use it in GitHub Desktop.
Save code-boxx/6a10b2dccd116808601690c7d12730a7 to your computer and use it in GitHub Desktop.
Simple CSRF With NodeJS Express

NODEJS EXPRESS CSRF EXAMPLE

https://code-boxx.com/simple-nodejs-express-csrf/

NOTES

  1. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Create public folder, move x-dummy.css inside.
    • Create views folder, move 1-form inside.
    • Install required modules - npm i express express-session ejs cookie-parser body-parser
    • Start the server - node 2-server.js
  2. Access http://localhost in your browser and submit the dummy form.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Dummy Form</title>
<link rel="stylesheet" href="public/x-dummy.css">
</head>
<body>
<form method="post" action="go" target="_blank">
<!-- (A) HIDDEN HTML TOKEN -->
<div>* This token should be hidden in your actual project</div>
<input type="text" name="token" value="<%= token %>">
<!-- (B) FIELDS AS USUAL -->
<input type="email" required name="email" value="jon@doe.com">
<input type="submit" value="GO">
</form>
</body>
</html>
// (A) LOAD REQUIRED MODULES
const express = require("express"),
path = require("path"),
bodyParser = require("body-parser"),
cookieParser = require("cookie-parser"),
session = require("express-session");
// (B) INIT EXPRESS SERVER
const app = express();
app.set("view engine", "ejs");
app.use(cookieParser());
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(session({
secret: "YOUR-SECRET-KEY", // CHANGE TO YOUR OWN!
resave: false,
saveUninitialized: true
}));
// (C) HTML PAGES
// (C1) PUBLIC FOLDER
app.use("/public", express.static(path.join(__dirname, "public")))
// (C2) HTML FORM
app.get("/", (req, res) => {
req.session.token = (Math.random() + 1).toString(36).substring(4);
res.render("1-form.ejs" , { token: req.session.token })
});
// (C3) SUBMIT HTML FORM
app.post("/go", (req, res) => {
// (C3-1) CHECK TOKEN
if (req.session.token == null) { res.send("Invalid token"); }
if (req.session.token != req.body.token) { res.send("Invalid token"); }
// (C3-2) PROCEED ONLY IF TOKEN MATCH
console.log(req.session.token);
console.log(req.body);
// (C3-3) REMOVE TOKEN & RESPOND
req.session.token = null;
res.send("OK");
});
// (D) START SERVER
app.listen(80, () => console.log(`Server running at port 80`));
md public
md views
move x-dummy.css public
move 1-form.ejs views
call npm i express express-session ejs cookie-parser body-parser
node 2-server.js
mkdir -m 777 public
mkdir -m 777 views
mv ./x-dummy.css ./public
mv ./1-form.ejs ./views
source "npm i express express-session ejs cookie-parser body-parser"
node 2-server.js
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
form {
max-width: 500px;
margin: 0 auto;
padding: 20px;
background: #fafafa;
border: 1px solid #ebebeb;
}
input {
width: 100%;
padding: 10px;
}
input[type=text], input[type=email] {
margin-top: 10px;
border: 1px solid #d7d7d7;
}
input[type=submit] {
font-weight: 700;
border: 0;
margin-top: 10px;
color: #fff;
background: #bb1b1b;
cursor: pointer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment