Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: Apache2::AuthCookieDBI, Mason, and protecting against Cross-Site Request Forgery (CSRF)

by tinita (Parson)
on Sep 25, 2012 at 23:27 UTC ( [id://995651]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Apache2::AuthCookieDBI, Mason, and protecting against Cross-Site Request Forgery (CSRF)
in thread Apache2::AuthCookieDBI, Mason, and protecting against Cross-Site Request Forgery (CSRF)

Like I said, it depends on the method you choose, and it's difficult to implement something generic because it depends on that.
In my framework all requests go through a process of user authentification. Simplified, the user token in the database is checked and updated if it is too old. (I actually have two tokens which overlap.) If you are using Apache::AuthTicket you could add the code to a class that inherits from it.
In the template where I have form to be protected I just add the token into a hidden field with
[%= .user.token.hidden escape=0 %] which gives me the html for the hidden field.
Wherever a request is made that requires a valid token I simply write:
$framework->require_token;
which automatically throws an exception if the token parameter is invalid, which is caught by the framework and a short error message is displayed. Since I have two overlapping tokens this should never happen except somebody has an open form and presses submit after a long time (12 hours for example).

So for each request/form that needs to be protected there is only one line to be added per template and perl code.
You can even put the require_token call into the framework code before the actual method is called; do this for every post request. (and in the actual code also check for post; this should be done anyway).
but I'm not sure if this is a good idea. Then you have to add the token to every post form, even if a valid token is not needed. Or you do it the other way round and explicitly define which requests don't need a check. This way it cannot be forgotten when adding new code.
  • Comment on Re^3: Apache2::AuthCookieDBI, Mason, and protecting against Cross-Site Request Forgery (CSRF)
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://995651]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found