Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.4.0-M3] Bug: CSRF cookie not set when using application.context #4154

Merged
merged 1 commit into from Apr 7, 2015

Conversation

jroper
Copy link
Member

@jroper jroper commented Apr 1, 2015

To reproduce:

  • Create a plain new play-java project.
  • Add the Global CSRF filter to Global.java like shown in the docs
  • Add filters to the libraryDependencies in build.sbt
  • In your application.conf set
    application.context=/abc
    csrf.cookie.name=csrfcookie
  • Make sure you use Milestone 3 in your project/plugins.sbt
    addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.0-M3")

Result: The csrfcookie does not get set (which also causes "Missing CSRF Token" exceptions when e.g. using @CSRF.formField in a view).
When using the exact same project and switching to 2.3.8 it works.
(Make sure you always clean the cookies between browser refreshes.)

@mkurz mkurz changed the title [2.4.0-M3] Bug: CSRF cookie not set when using application.context [2.4.0-M3] Bug: CSRF cookie not set when using application.context Mar 30, 2015
@jroper jroper added this to the 2.4.0 milestone Mar 31, 2015
@jroper
Copy link
Member

jroper commented Mar 31, 2015

Does it happen if you go to a sub path of the application context? Eg, /abc/def?

@mkurz
Copy link
Member Author

mkurz commented Mar 31, 2015

@jroper Just tested it: No, it does not happen on a sub path.

@mkurz
Copy link
Member Author

mkurz commented Mar 31, 2015

@jroper Also testet it with latest SNAPSHOT (7c4a4b6). Same here, does not work on /abc but works on /abc/def

@jroper
Copy link
Member

jroper commented Mar 31, 2015

Thanks for verifying that, should hopefully be straight forward to track down and fix.

jroper added a commit to jroper/playframework that referenced this pull request Apr 1, 2015
Fixes playframework#4154

* Ensured that when the context equals the path, the filters are applied
* Also fixed a bug in the DefaultHttpErrorHandler that ensured that the
  not found handler uses the request from the upstream filters that
  didn't exist in the GlobalSettings implementation
@jroper
Copy link
Member

jroper commented Apr 1, 2015

PR attached. Also found a bug in our error handling where filters weren't applied to the not found handler.

@richdougherty
Copy link
Member

Since I was just working on optimizing this (#4164), I have a suggestion for a faster check for the context. It should be faster than the regex used in DefaultHttpErrorHandler and faster than the double string traversal used in GlobalSettings.

def inContext(context: String, path: String): Boolean = {
  // Assume context is a string without a trailing '/'.
  // Handle three cases:
  // * !path.startsWith(context)
  //   - Either path is shorter than context or starts with a different prefix.
  // * path.startsWith(context) && path.length == context.length
  //   - Path is equal to context.
  // * path.startsWith(context) && path.charAt(context.length) == '/')
  //   - Path starts with context followed by a '/' character.
  path.startsWith(context) && (path.length == context.length || path.charAt(context.length) == '/')
}

jroper added a commit to jroper/playframework that referenced this pull request Apr 7, 2015
Fixes playframework#4154

* Ensured that when the context equals the path, the filters are applied
* Also fixed a bug in the DefaultHttpErrorHandler that ensured that the
  not found handler uses the request from the upstream filters that
  didn't exist in the GlobalSettings implementation
@jroper
Copy link
Member

jroper commented Apr 7, 2015

@richdougherty I used your logic, plus an additional check - if the context is empty (which is by far the most common Play configuration, not many people use application contexts), then there's no context so everything is in context. So I used context.isEmpty to short circuit all the other checks. In GlobalSettings, I just added the short circuit, and not the other checks, so it will be fast in the default, most common case, and slow in other cases, but that doesn't matter because users can switch to the DefaultHttpRequestHandler, and we're going to delete GlobalSettings soon anyway.

@richdougherty
Copy link
Member

Build error:

[error] /scratch/jenkins/workspace/play-master-PRs/framework/src/play/src/main/scala/play/api/http/HttpRequestHandler.scala:147: type mismatch;
[error]  found   : Unit
[error]  required: Boolean
[error]         case action: EssentialAction if inContext(request.path) => filterAction(action)
[error]                                                  ^

Fixes playframework#4154

* Ensured that when the context equals the path, the filters are applied
* Also fixed a bug in the DefaultHttpErrorHandler that ensured that the
  not found handler uses the request from the upstream filters that
  didn't exist in the GlobalSettings implementation
@jroper
Copy link
Member

jroper commented Apr 7, 2015

Maybe I should try compiling before pushing? Nah... that's what CI is for.

@richdougherty
Copy link
Member

;)

richdougherty added a commit that referenced this pull request Apr 7, 2015
[2.4.0-M3] Bug: CSRF cookie not set when using application.context
@richdougherty richdougherty merged commit 5921269 into playframework:master Apr 7, 2015
@jroper jroper deleted the 4154-filter-context branch April 7, 2015 03:37
ClaraAllende pushed a commit to ClaraAllende/playframework that referenced this pull request Aug 28, 2015
Fixes playframework#4154

* Ensured that when the context equals the path, the filters are applied
* Also fixed a bug in the DefaultHttpErrorHandler that ensured that the
  not found handler uses the request from the upstream filters that
  didn't exist in the GlobalSettings implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants