File: ProtectCSRF.pm

package info (click to toggle)
libcgi-application-plugin-protectcsrf-perl 1.01-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 212 kB
  • sloc: perl: 194; makefile: 2
file content (513 lines) | stat: -r--r--r-- 14,096 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package CGI::Application::Plugin::ProtectCSRF;

=pod

=head1 NAME

CGI::Application::Plugin::ProtectCSRF - generate and verify anti-CSRF tickets

=head1 VERSION

1.01

=head1 SYNPSIS

  use Your::App;
  use base qw(CGI::Application);
  use CGI::Application::Plugin::Session; # mandatory !!
  use CGI::Application::Plugin::ProtectCSRF;

  sub input_form : PublishCSRFID {
    my $self = shift;
    do_something();
  }

  sub finish : ProtectCSRF {
    my $self = shift;
    $self->clear_csrf_id;
    do_something();
  }

=head1 DESCRIPTION

CGI::Application::Plugin::ProtectCSRF provides tools to protect forms in
L<CGI::Application> web applications from CSRF attacks. Run mode handlers
may be declared with the C<PublishCSFRID> or C<ProtectCSFR> attributes.
The former should usually be applied to a run mode, whose HTML includes
a C<form> tag. In this case a ticket is generated and stored in the session
during a prerun callback and a C<hidden> control field, publishing the
ticket, is added to the form during a postrun callback. Conversely the
C<ProtectCSRF> attribute should normally be applied to the corresponding
run modes that process data from a submitted form. A prerun callback checks
for the hidden field and checks that it matches the ticket saved
in the session. If the check fails the page is redirected to a
customizable error page. On success the form processing run mode should
use the C<clear_csrf_id> method, so that subsequent calls to forms from that
session will generate fresh tickets.

=cut

use strict;
use base qw(Exporter);
use Carp;
use HTML::TokeParser;
use Digest::SHA1 qw(sha1_hex);
use Attribute::Handlers;

our(
    @EXPORT,
    $CSRF_ERROR_MODE,
    $CSRF_ERROR_STATUS,
    $CSRF_ERROR_TMPL,
    $CSRF_ID,
    $CSRF_ID_LENGTH,
    $CSRF_POST_ONLY,
    $VERSION
);

@EXPORT                 = qw(
                            clear_csrf_id
                            csrf_id
                            protect_csrf_config
                            );

$CSRF_ERROR_MODE        = "_csrf_error";
$CSRF_ERROR_STATUS      = 200;
$CSRF_ERROR_TMPL        = \qq{<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>CSRF ERROR</title></head>
<body>
<h1>CSRF ERROR</h1>
<p>Access denied. Please contact the website administrator.</p>
</body>
</html>
};
$CSRF_ID                = "_csrf_id";
$CSRF_POST_ONLY         = 0;
$VERSION                = 1.01;

my(%publish_csrf_id_runmodes, %protect_csrf_runmodes);

sub import {

    my $pkg = caller;

# C::A::P::Session method check
    croak("CGI::Aplication::Plugin::Session module is not loaded in your app") if !$pkg->can("session");

    $pkg->add_callback("prerun",  \&_publish_csrf_id);
    $pkg->add_callback("prerun",  \&_csrf_forbidden);
    $pkg->add_callback("postrun", \&_add_csrf_id);

    goto &Exporter::import;
}

=pod

=head1 ACTION

=head2 PublishCSRFID

Run modes declared with the C<PublishCSRFID> attribute, take the following
actions:

=over

=item - generate CSRF ticket and store it in the session;

=item - generate the form as per the module code;

=item - add a hidden element to the form publishing the CSRF ticket.

=back

  # publish CSRF ticket
  sub input_form : PublishCSRFID {
    my $self = shift;
    return <<HTML;
  <form action="foo" method="post">
  <input type="text" name="name">
  <input type="submit" value="submit!">
  <input type="hidden" name="rm" value="finish">
  </form>
  HTML
  }
  
  # display html source
  <form action="foo" method="post">
  <input type="hidden" name="_csrf_id" value="random string" /> <- insert hidden field
  <input type="text" name="name">
  <input type="submit" value="submit!">
  <input type="hidden" name="rm" value="finish">
  </form>

=head2 ProtectCSRF

Run modes declared with the C<ProtectCSRF> attribute, take the following
actions:

=over

=item - verify that the submitted CSRF ticket matches the ticket saved in the
session. If there is any sort of issue with the ticket the page is
redirected to a customizable error page;

=item - the form is processed as per the module code;

=item - the form should call the C<clear_csfr_id> method so that subsequent forms
generate fresh tickets. The code does not do this because if the form validation
fails it might be best to retain the same ticket.

=back

  sub finish : ProtectCSRF {
    my $self = shift;

    # required! Unless forms and their processing are tightly
    # coupled by clearing the ticket between invocations,
    # the meaning of the ticket is lost.
    $self->clear_csrf_id;

    # The processing that you want to perform (DB processing etc)
    do_something();
  }

=cut

sub CGI::Application::PublishCSRFID : ATTR(BEGIN) {
    my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
    $publish_csrf_id_runmodes{$referent} = 1;
    #$publish_csrf_id_runmodes{*{$symbol}{NAME}} = 1;
}

sub CGI::Application::ProtectCSRF : ATTR(BEGIN) {
    my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
    $protect_csrf_runmodes{$referent} = 1;
}

=pod

=head1 METHOD

=head2 csrf_id

This method returns the CSRF ticket saved in the session.

Example: 

  sub input_form : PublishCSRFID {
    my $self = shift;

    my $csrf_id = $self->csrf_id;
    do_something();
  }

=cut

sub csrf_id {

    my $self = shift;
    return $self->session->param($self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_id});
}

=head2 protect_csrf_config

This method initializes the ProtectCSRF state using any configuration options
that were passed to it. The available options are:

=over

=item B<csrf_error_status> - The HTTP status code that would be set on the
CSRF error page if a CSRF attack is identified. It defaults to 200.

=item B<csrf_error_mode> - The L<CGI::Application> runmode name. This defaults to C<_csrf_error>.

=for comment

The Debian maintainer is unclear why this option is useful. Surely an
anonymous run mode would be cleaner here.

=end comment

=item B<csrf_error_tmpl> - The HTML displayed in the event of a CSRF attack being
detected in the form of a scalarref or filepath or filehandle. One may
consider L<HTML::Template> for inspiration on thse formats. The default is
C<$CSRF_ERROR_TMPL> which is a scalarref.

=item B<csrf_error_tmpl_param> - A hashref of parameters to be placed in the
above template. See L<HTML::Template>.

=for comment

The Debian maintainer thinks other templating systems should work but is
unlikely to experiment with this in the near future.

=end comment

=item B<csrf_id> - The name of the session parameter used to store the CSRF ticket.This defaults to C<_csrf_id>.

=item B<csrf_post_only> - If set non-POST requests to a run mode which is protected
by this module would be rejected. By default this is 0.

=back

Example:

  sub cgiapp_init {
    my $self = shift;
    $self->tmpl_path("/path/to/template");
    $self->protect_csrf_config(
                           csrf_error_status     => 403, # change forbidden
                           csrf_error_tmpl       => "csrf_error.tmpl",
                           csrf_error_tmpl_param => { TITLE => "CSRF ERROR", MESSAGE => "your access is csrf!"},
                           csrf_id               => "ticket_id",
                           csrf_post_only        => 1
                         );
  }

  # csrf_error.tmpl
  <html><head><title><TMPL_VAR NAME=TITLE ESCAPE=HTML></title></head>
  <body>
  <h1>CSRF Error</h1>
  <span style="color: red"><TMPL_VAR NAME=MESSAGE ESCAPE=HTML></span>
  </body>
  </html>

=cut

sub protect_csrf_config {

    my($self, %args) = @_;
    if(ref($self->{__CAP_PROTECT_CSRF_CONFIG}) ne "HASH"){
        $self->{__CAP_PROTECT_CSRF_CONFIG} = {};
    }

    $self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_error_status}     = exists $args{csrf_error_status} ? $args{csrf_error_status} : $CSRF_ERROR_STATUS;
    $self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_error_mode}       = exists $args{csrf_error_mode} ? $args{csrf_error_mode} : $CSRF_ERROR_MODE;
    $self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_error_tmpl}       = exists $args{csrf_error_tmpl} ? $args{csrf_error_tmpl} : $CSRF_ERROR_TMPL;
    $self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_error_tmpl_param} = {};
    $self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_id}               = exists $args{csrf_id} ? $args{csrf_id} : $CSRF_ID;
    $self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_post_only}        = exists $args{csrf_post_only} ? $args{csrf_post_only} : $CSRF_POST_ONLY;

    if(ref($args{csrf_error_tmpl_param}) eq "HASH" && keys %{$args{csrf_error_tmpl_param}}){
        $self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_error_tmpl_param} = $args{csrf_error_tmpl_param};
    }
}

=pod

=head2 clear_csrf_id

This method clears the CSFR ticket. This should be done during the processing
of a form request.

Example : 

  sub cgiapp_init {
    my $self = shift;
    $self->protect_csrf_config;
  }

  sub input {
    my $self = shift;
    do_something(). # input form display..
  }
  
  sub confirm : PublishCSRFID {
    my $self = shift;
    do_something(). # publish csrf_id and input check and confirm display..
  }

  sub complete : ProtectCSRF {
    my $self = shift;
    $self->clear_csrf_id(1); # clear csrf_id for CSRF protect
    do_something();          # DB insert etc..
  }

=cut

sub clear_csrf_id {

    my($self, $fast) = @_;
    $self->session->clear($self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_id});
    $self->session->flush if $fast;
}

=pod

=head1 CALLBACK

=head2 _publish_csrf_id

prerun callback

=cut

sub _publish_csrf_id {

    my($self, $rm) = @_;
    return if !exists $publish_csrf_id_runmodes{$self->can($rm)};

    if(ref($self->{__CAP_PROTECT_CSRF_CONFIG}) ne "HASH"){
        $self->protect_csrf_config;
    }

    my @words = ('A'..'Z', 'a'..'z', 0..9, '/', '.');
    my $salt = join "", @words[ map { sprintf( "%d", rand(scalar @words) ) } 1..2 ];
    my $csrf_id = sha1_hex($salt . time . $$ . rand(10000));
    $self->session->param($self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_id}, $csrf_id);
}

=pod

=head2 _csrf_forbidden

prerun callback

=cut

sub _csrf_forbidden {

    my($self, $rm) = @_;
    my $err_flg = 0;

    return if !exists $protect_csrf_runmodes{$self->can($rm)};

    if(ref($self->{__CAP_PROTECT_CSRF_CONFIG}) ne "HASH"){
        $self->protect_csrf_config;
    }

    if($self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_post_only} && $ENV{REQUEST_METHOD} ne "POST"){
        $err_flg = 1;
    } else {

        if(
            !$self->query->param($self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_id}) || 
            !$self->csrf_id                                                     ||
            $self->query->param($self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_id}) ne $self->csrf_id
        ){
            $err_flg = 1;
        }
    }

    if($err_flg){

        $self->run_modes( $self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_error_mode} => sub {       
     
            my $self = shift;
            $self->header_props( -type => "text/html", -status => $self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_error_status} );

            my $tmpl_obj = $self->load_tmpl($self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_error_tmpl}, die_on_bad_params => 0);
            if(keys %{$self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_error_tmpl_param}}){
                $tmpl_obj->param(%{$self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_error_tmpl_param}});
            }
            return $tmpl_obj->output;
        });
        $self->prerun_mode($self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_error_mode});
    }

    return 0;
}


=pod

=head2 _add_csrf_id

postrun callback

=cut

sub _add_csrf_id {

    my($self, $scalarref) = @_;
    my $rm = $self->get_current_runmode;
    my $coderef = $self->can($rm);
    return if !$coderef || !exists $publish_csrf_id_runmodes{$coderef};

    if(ref($self->{__CAP_PROTECT_CSRF_CONFIG}) ne "HASH"){
        $self->protect_csrf_config;
    }

    # my %header = $self->header_props;
    # return if %header && $header{-type} ne "text/html";

    my $body = "";
    my $hidden = sprintf qq{<input type="hidden" name="%s" value="%s" />}, $self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_id}, $self->csrf_id;

    my $parser = HTML::TokeParser->new($scalarref);
    while(my $token = $parser->get_token){

# start tag(<form> sniping)
        if($token->[0] eq "S"){
            
            if(lc($token->[1]) eq "form"){
                $body .= $token->[4] . "\n" . $hidden;
            # In the future...
            #}elsif(lc($token->[1]) eq "a"){
            #    
            #   if(exists $token->[2]->{href} && defined $token->[2]->{href}){
            #       my $uri = URI->new($token->[2]->{href});
            #       my %query_form = $uri->query_form;
            #       $query_form{$self->{__CAP_PROTECT_CSRF_CONFIG}->{csrf_id}} = $self->csrf_id;
            #       $uri->query_form(%query_form);
            #       $token->[2]->{href} = $uri->path_query;
            #       my $prop = join " ", (map { $_ . "=\"" . $token->[2]->{$_} . "\"" } keys %{$token->[2]});
            #       $body .= "<" . lc($token->[1]) . " ". $prop . ">";
            #   }else{
            #       $body .= $token->[4];
            #   }

            }else{
                $body .= $token->[4];
            }

# end tag, process instructions
        }elsif($token->[0] =~ /^(E|PI)$/){
            $body .= $token->[2];
            
# text, comment, declaration
        }elsif($token->[0] =~ /^(T|C|D)$/){
            $body .= $token->[1];
        }
    }

    ${$scalarref} = $body;
}

1;

__END__

=head1 CAUTION

This module should not be seen as a panacea for all web security issues.
The user should fully understand and act on all security threats his
application may face, including whether this module is an adequate and
useful tool.

=head1 SEE ALSO

L<Attribute::Handlers>,
L<Carp>,
L<CGI::Application>,
L<CGI::Application::Plugin::Session>,
L<Digest::SHA1>,
L<Exporter>,
L<HTML::TokeParser>,
L<HTML::Template>

=head1 AUTHOR

Akira Horimoto <kurt0027@gmail.com>

=head1 COPYRIGHT

Copyright (C) 2006 - 2008 Akira Horimoto

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut