Coder Social home page Coder Social logo

play-scala-csrf's Introduction

Demonstration of CSRF issue

This project is only here to demonstrate a potential issue with the CSRF check in Play Framework 2.5.0.

Manual testing in browser

  • Start application: sbt run
  • Navigate to http://localhost:9000/form/notoken. This will not add a CSRF token to the session. Make sure there are no cookies present in the browser added by previous apps that ran on localhost.
  • Submit the form via a POST request by clicking the Submit button.
  • Due to the missing CSRF token you should get an error response, but you will see a success message.
  • Now navigate to http://localhost:9000/form/addtoken. This will add a CSRF token to the session and the form. (The CSRF token in the form has been put into an input type="text" field for easier manipulation. Usually this would be an input type="hidden" field.)
  • Submit the form via a POST request by clicking the Submit button.
  • You will correctly see a success message, because the CSRF tokens in the session cookie matched the token in the form.
  • Go back to http://localhost:9000/form/addtoken (reload the page).
  • Remove or manipulate the CSRF token in the input field and submit the form again.
  • You will see the request is correctly rejected, because tokens in session and form did not match.
  • Go back to http://localhost:9000/form/addtoken (reload the page).
  • Delete all cookies for the localhost domain in your browser, e.g. using Google Chrome's inspector, select tab Resources, select Cookies, select localhost, select the cookie PLAY_SESSION (and any other cookies that might be there).
  • Put anything you want into the input field of the form.
  • Submit the form via a POST request by clicking the Submit button.
  • Due to the missing CSRF token you should get an error response, but you will see a success message.

Manual testing with Curl

Run the application

sbt run

Then post a request to the protected route, which should give you an error response, but doesn't:

curl -X POST http://localhost:9000/form/check

Or post a request to the protected route with any cookie, which will produce an error response:

curl -X POST --cookie "SOME_COOKIE=abc" http://localhost:9000/form/check

Automated tests

I've added tests for all this in test/CsrfSpec.scala.

Start sbt and run test-only CsrfSpec. Three of the tests will fail; whenever there is no cookie in the header of the POST request to the protected route, the action is executed.

play-scala-csrf's People

Contributors

manc avatar

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.