Skip to content

Commit

Permalink
Allow CSRFTokenSupport-derived traits to redefine the forgery test.
Browse files Browse the repository at this point in the history
This commit does not include any additional tests  as the semantics should not change. The forgery test was exposed to derived types.
  • Loading branch information
Jon Buffington committed Mar 24, 2011
1 parent 36b1fb5 commit caf6772
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions core/src/main/scala/org/scalatra/CSRFTokenSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,31 @@ trait CSRFTokenSupport { self: ScalatraKernel =>
protected def csrfToken = session(csrfKey).asInstanceOf[String]

before {
if (request.isWrite && session.get(csrfKey) != params.get(csrfKey))
halt(403, "Request tampering detected!")
if (isForged) {
handleForgery()
}
prepareCSRFToken
}

/**
* Test whether a POST request is a potential cross-site forgery.
*
* @return Returns true if the POST request is suspect.
*/
protected def isForged: Boolean = {
request.isWrite && session.get(csrfKey) != params.get(csrfKey)
}

/**
* Take an action when a forgery is detected. The default action
* halts further request processing and returns a 403 HTTP status code.
*/
protected def handleForgery() {
halt(403, "Request tampering detected!")
}

protected def prepareCSRFToken = {
session.getOrElseUpdate(csrfKey, GenerateId())
}

}
}

0 comments on commit caf6772

Please sign in to comment.