| 1 |
NAME |
| 2 |
CGI::Application::Plugin::ProtectCSRF - Plug-in protected from CSRF |
| 3 |
|
| 4 |
VERSION |
| 5 |
1.01 |
| 6 |
|
| 7 |
SYNPSIS |
| 8 |
use Your::App; |
| 9 |
use base qw(CGI::Application); |
| 10 |
use CGI::Application::Plugin::Session; # require!! |
| 11 |
use CGI::Application::Plugin::ProtectCSRF; |
| 12 |
|
| 13 |
sub input_form : PublishCSRFID { |
| 14 |
my $self = shift; |
| 15 |
do_something(); |
| 16 |
} |
| 17 |
|
| 18 |
sub finish : ProtectCSRF { |
| 19 |
my $self = shift; |
| 20 |
$self->clear_csrf_id; |
| 21 |
do_something(); |
| 22 |
} |
| 23 |
|
| 24 |
DESCRIPTION |
| 25 |
CGI::Application::Plugin::ProtectCSRF is C::A::P protected from CSRF. |
| 26 |
|
| 27 |
When CSRF is detected, Forbidden is returned and processing is |
| 28 |
interrupted. |
| 29 |
|
| 30 |
ACTION |
| 31 |
PublishCSRFID |
| 32 |
PublishCSRFID is action publishes CSRF ticket. CSRF ticket is published |
| 33 |
when I define it as an attribute of runmode method publishing CSRF |
| 34 |
ticket, and it is saved in session. If there is form tag in HTML to |
| 35 |
display after the processing end, as for runmode method to publish, CSRF |
| 36 |
ticket is set automatically by hidden field |
| 37 |
|
| 38 |
# publish CSRF ticket |
| 39 |
sub input_form : PublishCSRFID { |
| 40 |
my $self = shift; |
| 41 |
return <<HTML; |
| 42 |
<form action="foo" method="post"> |
| 43 |
<input type="text" name="name"> |
| 44 |
<input type="submit" value="submit!"> |
| 45 |
<input type="hidden" name="rm" value="finish"> |
| 46 |
</form> |
| 47 |
HTML |
| 48 |
} |
| 49 |
|
| 50 |
# display html source |
| 51 |
<form action="foo" method="post"> |
| 52 |
<input type="hidden" name="_csrf_id" value="random string" /> <- insert hidden field |
| 53 |
<input type="text" name="name"> |
| 54 |
<input type="submit" value="submit!"> |
| 55 |
<input type="hidden" name="rm" value="finish"> |
| 56 |
</form> |
| 57 |
|
| 58 |
ProtectCSRF |
| 59 |
ProtectCSRF is action to protect from CSRF Attack. If session CSRF |
| 60 |
ticket does not accord with query CSRF ticket, application consideres it |
| 61 |
to be CSRF attack and refuse to access it. Carry out the processing that |
| 62 |
you want to perform after having carried out clear_csrf_id method when |
| 63 |
access it, and it was admitted. |
| 64 |
|
| 65 |
sub finish : ProtectCSRF { |
| 66 |
my $self = shift; |
| 67 |
$self->clear_csrf_id; # require! There is not a meaning unless I do it |
| 68 |
do_something(); # The processing that you want to perform (DB processing etc) |
| 69 |
} |
| 70 |
|
| 71 |
METHOD |
| 72 |
csrf_id |
| 73 |
Get ticket for protect CSRF |
| 74 |
|
| 75 |
Example: |
| 76 |
|
| 77 |
sub input_form : PublishCSRFID { |
| 78 |
my $self = shift; |
| 79 |
|
| 80 |
my $csrf_id = $self->csrf_id; |
| 81 |
do_something(); |
| 82 |
} |
| 83 |
|
| 84 |
protect_csrf_config |
| 85 |
Initialize ProtectCSRF |
| 86 |
|
| 87 |
Option: |
| 88 |
|
| 89 |
csrf_error_status : CSRF error status code (default: 200) |
| 90 |
csrf_error_mode : CSRF error runmode name (default: _csrf_error) |
| 91 |
csrf_error_tmpl : CSRF error display html. scalarref or filepath or filehandle (default: $CSRF_ERROR_TMPL - scalarref) |
| 92 |
csrf_error_tmpl_param : CSRF error display html parameter (for HTML::Template) |
| 93 |
csrf_id : CSRF ticket name (default: _csrf_id) |
| 94 |
csrf_post_only : CSRF protect runmode request method check(default:0 1:POST Only) |
| 95 |
|
| 96 |
Example: |
| 97 |
|
| 98 |
sub cgiapp_init { |
| 99 |
my $self = shift; |
| 100 |
$self->tmpl_path("/path/to/template"); |
| 101 |
$self->protect_csrf_config( |
| 102 |
csrf_error_status => 403, # change forbidden |
| 103 |
csrf_error_tmpl => "csrf_error.tmpl", |
| 104 |
csrf_error_tmpl_param => { TITLE => "CSRF ERROR", MESSAGE => "your access is csrf!"}, |
| 105 |
csrf_id => "ticket_id", |
| 106 |
csrf_post_only => 1 |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
# csrf_error.tmpl |
| 111 |
<html><head><title><TMPL_VAR NAME=TITLE ESCAPE=HTML></title></head> |
| 112 |
<body> |
| 113 |
<h1>CSRF Error</h1> |
| 114 |
<span style="color: red"><TMPL_VAR NAME=MESSAGE ESCAPE=HTML></span> |
| 115 |
</body> |
| 116 |
</html> |
| 117 |
|
| 118 |
clear_csrf_id |
| 119 |
Clear csrfid. It is preferable to make it execute after processing ends. |
| 120 |
|
| 121 |
Example : |
| 122 |
|
| 123 |
sub cgiapp_init { |
| 124 |
my $self = shift; |
| 125 |
$self->protect_csrf_config; |
| 126 |
} |
| 127 |
|
| 128 |
sub input { |
| 129 |
my $self = shift; |
| 130 |
do_something(). # input form display.. |
| 131 |
} |
| 132 |
|
| 133 |
sub confirm : PublishCSRFID { |
| 134 |
my $self = shift; |
| 135 |
do_something(). # publish csrf_id and input check and confirm display.. |
| 136 |
} |
| 137 |
|
| 138 |
sub complete : ProtectCSRF { |
| 139 |
my $self = shift; |
| 140 |
$self->clear_csrf_id(1); # clear csrf_id for CSRF protect |
| 141 |
do_something(); # DB insert etc.. |
| 142 |
} |
| 143 |
|
| 144 |
CALLBACK |
| 145 |
_publish_csrf_id |
| 146 |
prerun callback |
| 147 |
|
| 148 |
_csrf_forbidden |
| 149 |
prerun callback |
| 150 |
|
| 151 |
_add_csrf_id |
| 152 |
postrun callback |
| 153 |
|
| 154 |
CAUTION |
| 155 |
It has only the protection function of basic CSRF,and mount other |
| 156 |
security checks in the application, please. |
| 157 |
|
| 158 |
SEE ALSO |
| 159 |
Attribute::Handlers Carp CGI::Application |
| •160 |
CGI::Application::Plugin::Session Digest::SHA Exporter HTML::TokeParser |
| 161 |
|
| 162 |
AUTHOR |
| 163 |
Akira Horimoto <[email protected]> |
| 164 |
|
| 165 |
COPYRIGHT |
| 166 |
Copyright (C) 2006 - 2008 Akira Horimoto |
| 167 |
|
| 168 |
This module is free software; you can redistribute it and/or modify it |
| 169 |
under the same terms as Perl itself. |
| 170 |
|