Coder Social home page Coder Social logo

Comments (6)

stephenmathieson avatar stephenmathieson commented on May 8, 2024

Yes. You can avoid CSRF checks by not mounting the middleware.

from csrf.

arunrreddy avatar arunrreddy commented on May 8, 2024

How do you conditionally mount or not mount the middleware?

from csrf.

stephenmathieson avatar stephenmathieson commented on May 8, 2024

You can do something like:

var app = new Koa()
var middleware = new CSRF({ ...  })
app.post('/with-csrf', middleware, (ctx, next) => { ... })
app.post('/without-csrf', (ctx, next) => { ... })
app.post('/another/with-csrf', middleware, (ctx, next) => { ... })

from csrf.

arunrreddy avatar arunrreddy commented on May 8, 2024

async function conditionalCsrf(ctx, next) {
debug('Entered conditional csrf middleware');
const request = ctx.request;
debug(request.path, !.includes(request.path, '/webhook/'));
if(!
.includes(request.path, '/webhook/')) {
return new CSRF(); // eslint-disable-line
}
await next(); // eslint-disable-line
};

This is present between a bunch of middlewares.
app.use(conditionalCsrf);
I am trying to use it like this since I have a number of middlewares to be executed. I am unable to get it to work this way.

from csrf.

stephenmathieson avatar stephenmathieson commented on May 8, 2024

There's a similar example in the README:

app.use((ctx, next) => {
  if (![ 'GET', 'POST' ].includes(ctx.method))
    return next();
  if (ctx.method === 'GET') {
    ctx.body = ctx.csrf;
    return;
  }
  ctx.body = 'OK';
});

So rather than checking the request method, you could check the request path.

from csrf.

arunrreddy avatar arunrreddy commented on May 8, 2024

The above example is just setting the generated csrf in the koa context. In the docs, above your written middleware there is app.use(new CSRF()) defined. I trying to conditionally mount that middleware.

from csrf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.