question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

session access for CSRF tokens

See original GitHub issue

I’ve recently started using supertest to write tests for an Express app. I have added CSRF protection middleware, and I’m having trouble testing my login route. Tests work fine if I remove CSRF, but that’s obviously not ideal. This also prevents me from testing parts of the app that are behind authentication.

Based on examples in https://github.com/visionmedia/superagent/blob/master/lib/node/agent.js, I started fiddling with a solution, but I haven’t figured out how to get the csrf token, then send that token back with the next POST.

a snippet of my code:

agent1 = superagent.agent()
agent2 = superagent.agent()
token  = null


loginUser = (agent) ->
  (done) ->
    onResponse = (err, res) ->
      res.should.have.status 200
      done()


    agent1.get("http://localhost:3000/", (req, res) ->
      token = req.session._csrf
    )

    agent2.post("http://localhost:3000/login").send(
      # login creds..
      _csrf           : token
    ).end onResponse

req seems to be null in the first agent each time, and I’m not really sure why? Is this something I should use PhantomJS or Zombie JS for?

My apologies for my ignorance; I am stuck on this and would appreciate any input.

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
zedd45commented, Nov 7, 2012

I realize, in retrospect, it’s much easier simply to disable CSRF middleware in the test environment.

app.configure 'development', 'staging', 'production', ->
    app.use express.csrf()

Then I use @NODE_ENV=test like in Express JS’s Makefile figured this might help someone else down the line

0reactions
zedd45commented, Oct 29, 2012

closing

Read more comments on GitHub >

github_iconTop Results From Across the Web

CSRF tokens | Web Security Academy - PortSwigger
A CSRF token is a unique, secret, unpredictable value that is generated by the server-side application and transmitted to the client in such...
Read more >
Cross-Site Request Forgery Prevention Cheat Sheet
CSRF tokens should be generated on the server-side. They can be generated once per user session or for each request. Per-request tokens are...
Read more >
Writeup: CSRF where token is not tied to user session ...
I notice that I get a new CSRF-token on each request of the /my-account page, even within a session. A new session contains...
Read more >
session management - Do I still need a CSRF token?
In order to do that, they'd need the user's session ID, and I can't see how they could obtain it, unless the user...
Read more >
CSRF Tokens vs Session Cookies - Stack Overflow
My question is, "What separates the CSRF token from a session cookie?" How can the session cookie be accessed but not the CSRF...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found