Skip to content

Commit a771305

Browse files
Adding CSURF
1 parent b5d9365 commit a771305

File tree

6 files changed

+77
-7
lines changed

6 files changed

+77
-7
lines changed

index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const checkinRouter = require('./routes/checkins')
66
const settingsRouter = require('./routes/settings')
77
const cookieParser = require('cookie-parser')
88
const bodyParser = require('body-parser');
9-
const { asyncHandler } = require("./routes/utils")
9+
const { asyncHandler } = require("./routes/utils");
10+
const csrf = require("csurf");
1011

1112
// Create the Express app.
1213
const app = express();
@@ -17,6 +18,7 @@ app.use(express.static(path.join(__dirname, "public")));
1718
app.use(express.json());
1819
app.use(cookieParser());
1920
app.use(bodyParser.urlencoded({ extended: false }));
21+
const csrfProtection = csrf({ cookie: true });
2022
app.use("/users", userRouter);
2123
app.use("/checkins", checkinRouter);
2224
app.use("/settings", settingsRouter);
@@ -189,11 +191,11 @@ app.get('/breweries/:id(\\d+)', asyncHandler(async (req, res) => {
189191

190192
app.get("/create", (req, res) => { res.render("create") });
191193

192-
app.get("/sign-up", (req, res) => {
193-
res.render("sign-up");
194+
app.get("/sign-up",csrfProtection, (req, res) => {
195+
res.render("sign-up",{csrfToken: req.csrfToken()});
194196
});
195-
app.get("/log-in", (req, res) => {
196-
res.render("log-in")
197+
app.get("/log-in",csrfProtection, (req, res) => {
198+
res.render("log-in",{csrfToken: req.csrfToken()})
197199
})
198200

199201
app.get("/profile", (req, res) => {

package-lock.json

+63
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"dependencies": {
1515
"body-parser": "^1.19.0",
1616
"cookie-parser": "^1.4.5",
17+
"csurf": "^1.11.0",
1718
"dotenv": "^8.2.0",
1819
"express": "^4.17.1",
1920
"express-validator": "^6.5.0",

routes/users.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const express = require('express');
22
const {asyncHandler } = require('./utils');
33
const fetch = require('node-fetch');
4+
const csrf = require("csurf");
5+
const csrfProtection = csrf({ cookie: true });
46

57
const router = express.Router();
68

7-
router.post("/sign-up", asyncHandler(async (req,res)=>{
9+
router.post("/sign-up",csrfProtection, asyncHandler(async (req,res)=>{
810
const body = req.body;
9-
11+
console.log(body);
1012
const backendRes = await fetch(`${process.env.BACKEND_URL}/users/`, {
1113
method: "POST",
1214
body: JSON.stringify(body),

views/log-in.pug

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ block content
1111
h2.log-in__logo TAPPDIN
1212
p.log-in__quote Drink Responsibly
1313
form(class="log-in-form" method="post" action="/users/log-in")
14+
input(type="hidden" name="_csrf" value=csrfToken)
1415
include form-elements.pug
1516
button.btn.btn-primary(type='submit') Log In
1617
a(href='/sign-up') Don't have an account? Sign up here.

views/sign-up.pug

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ block content
99
p.sign-up__quote Drink Responsibly
1010
//- TODO: update w more info for Tappdin
1111
form(class="sign-up-form" method="post" action="/users/sign-up")
12+
input(type="hidden" name="_csrf" value=csrfToken)
1213
.form-group
1314
label(for='username') Username
1415
input#username.form-control(type='text' name='username' placeholder="Username" required)

0 commit comments

Comments
 (0)