Node js csrf token protection not working
Asked Answered
A

1

6

I have the following in my app.js file server side after bodyParser

let dotEnv = require("dotenv");
dotEnv.load();
let express = require("express");
let app = express();
if (process.env.NODE_ENV === 'production') {
  app = require('./public/web/server').app;
}
let passport = require("passport");
let server = require("http").Server(app);
let io = require("socket.io").listen(server);
// load intial configuration
require("./startup/initial-configuration")(app, io);
require("./server/config/socket")(io);
require("./server/config/database")(app, process.env.NODE_ENV);
require("./server/authentication/passport.local")(passport);
require("./server/authentication/passport.impersonate");
require("./startup/initial-routes")(app);
if (process.env.NODE_ENV === 'production') {
  app.get('*.*', express.static('./public/web/browser', {
  maxAge: '1y'
}));
app.get('*', (req, res) => {
  res.render('index', {
   req,
   res
  }, (err, html) => {
    if (html) {
      res.send(html);
    } else {
      // console.error(err);
      res.send(err);
    }
   });
  });
 }
 require("./server/middleware/custom-middleware")(app);
 module.exports = { app: app, server: server };

As you can see, I have a file initial-configuration loaded inside app.js, the content of that file is:

const path = require("path");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const csurf = require("csurf");
const helmet = require("helmet");
const compression = require("compression");
const useragent = require("express-useragent");
const cors = require("cors");
const passport = require("passport");
const express = require("express");
const cookieMiddleware = require("../server/middleware/cookie-middleware");
const checkCSRFMiddleware = require("../server/middleware/checkCSRF-middleware");
const notificationModel = require("../server/model/notification/notification.model");
const logger = require("./logger");
const morgan = require("morgan");
module.exports = (app, io) => {
 app.set("case sensetive routing", true);
 if (process.env.NODE_ENV === "production") {
   app.enable("trust proxy");
 }
 app.use((req, res, next) => {
   res.io = io;
   res.header(
    "Access-Control-Allow-Headers",
    "X-CSRF-Token, Content-Type"
   );
  notificationModel.setIO(io);
  next();
 });

 let corsOption = {
   origin: true,
   methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
   credentials: true,
   exposedHeaders: ["x-auth-token"]
 };

 app.use(cors(corsOption));
 // app.use(logger('dev'));
 app.use(helmet());
 app.use(useragent.express());
 app.use(compression());
 app.use(bodyParser.json());
 app.use(
   bodyParser.urlencoded({
    extended: false
  })
 );
 app.use(cookieParser());
 app.use(cookieMiddleware);
 app.use(passport.initialize());
 app.use(require('csurf')({cookie: true}))
 // error handler
 app.use(function (err, req, res, next) {
  if (err.code !== 'EBADCSRFTOKEN') return next(err)
  // handle CSRF token errors here
  res.status(403)
  res.send('session has expired or form tampered with')
 })
 app.use(function (req, res, next) {
 res.cookie('XSRF-TOKEN', req.csrfToken())
 next()
})

// app.use(express.static(path.join(__dirname, "../public/web/browser")));
app.use(
 morgan("combined", {
   stream: logger.stream
 })
);
};

And in Angular, I have only imported following lines in app.module

HttpClientXsrfModule.withOptions({
  cookieName: "XSRF-TOKEN",
  headerName: "X-CSRF-TOKEN"
}),

And all of my requests header has Cookie:_csrf=TmghRq3eWC-PxQfp6pvuHw07; XSRF-TOKEN=vMPrZZtA--BgtY1YVqDRXmi5A6RSbMNb61JA

But all my post requests failed and says code: "EBADCSRFTOKEN".

Should I do something on the angular side? Should I append that with form data?

For any help thanks.

Here are my request details request details

my login request

Ahq answered 10/12, 2018 at 12:1 Comment(2)
Can you share your angular code which is posting the data? I don't think your CSRF token is included correctly - you've also mentioned that your requests header has the Cookie - are you sure it's not the response header?Gyatt
@MaviDomates I have not done anything with my angular post requests, Don't know what should I doAhq
S
6

In your code you are using many modules. To isolate the problem I suggest to reduce your code to a minimal version removing everything not mandatory to enable csrf.

This is only a suggestion, however In my Angular 7 application I added this (it's not required to change cookiename and token, because they have default values in Angular's sourcecode):

HttpClientModule,
HttpClientXsrfModule.withOptions()

then in my server (main file app.js) with Express 4 I added this code (in this exact order):

const csrf = require('csurf');
app.use(bodyParser.urlencoded({
  extended: false
}));
// then add cookie parser and csrf config
app.use(cookieParser());
app.use(csrf({
  cookie: {
    // here you can configure your cookie. Default values are ok, but I decided to be more explicit
    // http://expressjs.com/en/4x/api.html#req.cookies
    key: '_csrf',
    path: '/',
    httpOnly: false, // if you want you can use true here
    secure: false, // if you are using HTTPS I suggest true here
    signed: false, // I don't know if csurf supports signed cookies, so I used false
    // not mandatory, but if you want you can use sameSite: 'strict'
    // sameSite: 'strict', // https://www.owaspsafar.org/index.php/SameSite
    maxAge: 24 * 60 * 60 * 1000 // 24 hours
   }
}));
app.use((req, res, next) => {
  const csrfTokenToSendToFrontEnd = req.csrfToken();
  console.log('csrfTokenToSendToFrontEnd: ', csrfTokenToSendToFrontEnd);
  // this cookie must be XSRF-TOKEN, because already defined as default in Angular.
  res.cookie('XSRF-TOKEN', csrfTokenToSendToFrontEnd);
  next();
});

// here requires the api file with all your rest apis (not static paths)
const routesApi = require('./src/routes/index')(express, passport);
app.use('/api', routesApi);

And finally, before the end of the file (before the 500 middleware) I added this to handle errors:

// error handler
app.use((err, req, res, next) => {
  if (err.code !== 'EBADCSRFTOKEN') {
    return next(err);
  }
  res.status(403).json({
    message: 'error'
  });
});

I copied only relevant code. If you have issues feel free to ask, I'll check again in my code if I forgot something.

Ski answered 15/12, 2018 at 16:38 Comment(12)
Still, I have the same issue. I have updated my question and added more detailsAhq
How it protect if you put all routes (api) before csrf?Ahq
You are right. I copied the wrong api from my app (not protected). I updated the codeSki
I'll check again this evening to see if I forgot something. However it should be OK. Its like the official example of csurf on githubSki
@Ahq I updated my answer to use default values of Angular and csurf. I tried this code with Chrome, Safari and Firefox. It's working. Also, I tested this code in an app with only passport to prevent interferences with other middlewares. Be sure to have cookies before to do a POST, if not try to close the browser's tab and retry. I saw a weird behaviour on chorme after cleaning site data with the button under "application" tab in dev tools.Ski
@jonas if you want you can also add a secret to cookieParser and use both httpOnly: true and sameSite: 'strict' in csurf config to improve security. About signed cookies, I don't know if they are supported by csurf. I never tried.Ski
It seems working now, but how I can be sure, means how I can test it? With postman when I set X-XSRF-TOKEN to the header of requests it fails, but in my app, now its working.Ahq
One another problem occurred, my first post request login is working, and X-XSRF-TOKEN is attached with header, after login my app redirects user to two step authentication page, when I submit that form, my app not attached X-XSRF-TOKEN to request header, but my server console shows the generated csrf.Ahq
Now I'm sure its httpClient issue, because if I change httpClient to http, X-XSRF-TOKEN is appended, but I should use httpClientAhq
My current problem is with httpClient, I changed absolute path to relative, but still with httpClient no X-XSRF-TOKEN is attached to the requestAhq
Thanks its working, I have only changed absolute path to relative path.Ahq
@Ahq perfect. Good to know about httpclient issuesSki

© 2022 - 2024 — McMap. All rights reserved.