CGI::A...ectCSRF(3pm)

πŸ“¦
version 1.01 Β· 2 versions
  • 1.01 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 1.01 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– CGI::A...ectCSRF β€” the anti-CSRF baddie πŸ’–

okay so bestie, gather round, because this is the CGI::Application::Plugin::ProtectCSRF page from the User Contributed Perl Documentation, and it's literally here to stop CSRF attacks from ruining your whole entire life. no cap. πŸ›‘οΈβœ¨


🌸 NAME 🌸

CGI::Application::Plugin::ProtectCSRF β€” generate and verify anti-CSRF tickets. that's the whole job, she's an icon, she does one thing and she does it well πŸ’…


⭐ VERSION ⭐

1.01 β€” yes ma'am, we're on 1.01 πŸŽ€


πŸ¦‹ SYNPSIS πŸ¦‹

(yes it's spelled like that in the original, we don't edit the source bestie, we respect her 🫢)

 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();
 }
heads up bestie: that CGI::Application::Plugin::Session line says mandatory !! and she MEANS it. no session, no tickets, no vibes. πŸ’€

🌈 DESCRIPTION 🌈

CGI::Application::Plugin::ProtectCSRF hands you the tools to protect forms in CGI::Application web apps from CSRF attacks. periodt.

run mode handlers can be declared with the "PublishCSFRID" or "ProtectCSFR" attributes (spelled exactly like that in the source, keeping it real ✨).

  • the "PublishCSFRID" one usually goes on a run mode whose HTML has a "form" tag in it. in that case a ticket gets generated and stashed in the session during a prerun callback, and then a "hidden" control field that publishes the ticket gets added to the form during a postrun callback. it's giving magic ✨
  • the "ProtectCSRF" one goes on 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 that check fails, the page gets redirected to a customizable error page. if it succeeds, the form-processing run mode should use the "clear_csrf_id" method so that later calls to forms from that session generate fresh tickets. new ticket, new era πŸŽ€

πŸ‘‘ ACTION πŸ‘‘

πŸ”₯ PublishCSRFID πŸ”₯

run modes declared with the "PublishCSRFID" attribute take these actions:

  • generate CSRF ticket and store it in the session;
  • generate the form as per the module code;
  • add a hidden element to the form publishing the CSRF ticket.
 # 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>

see that? the module slides the hidden field in for you. main character energy, she did that 🫢

πŸ›‘οΈ ProtectCSRF πŸ›‘οΈ

run modes declared with the "ProtectCSRF" attribute take these actions:

  • 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;
  • the form is processed as per the module code;
  • the form should call the "clear_csfr_id" method so that subsequent forms generate fresh tickets. the code does not do this for you, because if the form validation fails it might be best to retain the same ticket. (so valid, honestly πŸ™ˆ)
 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();
 }
the comment says required! and she's not being dramatic. if forms and their processing aren't tightly coupled by clearing the ticket between invocations, the meaning of the ticket is lost. πŸ’€

πŸ’… METHOD πŸ’…

πŸŽ€ csrf_id πŸŽ€

this method returns the CSRF ticket saved in the session. cute, simple, effective.

example:

 sub input_form : PublishCSRFID {
   my $self = shift;

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

✨ protect_csrf_config ✨

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

  • 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.
  • csrf_error_mode β€” the CGI::Application runmode name. this defaults to "_csrf_error".
  • 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 HTML::Template for inspiration on thse formats (typo's in the source, we keep her exactly as she is 🫢). the default is $CSRF_ERROR_TMPL, which is a scalarref.
  • csrf_error_tmpl_param β€” a hashref of parameters to be placed in the above template. see HTML::Template.
  • csrf_id β€” the name of the session parameter used to store the CSRF ticket.This defaults to "_csrf_id".
  • 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.

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>

"your access is csrf!" β€” the error message of all time honestly πŸ˜­πŸ’€

🌸 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..
 }

🧚 CALLBACK 🧚

_publish_csrf_id

prerun callback

_csrf_forbidden

prerun callback

_add_csrf_id

postrun callback


⚠️ CAUTION ⚠️

bestie, read this one twice. 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 their application may face, including whether this module is an adequate and useful tool. she's one tool in the kit, not the whole kit. πŸ’…

πŸ’œ SEE ALSO πŸ’œ

Attribute::Handlers, Carp, CGI::Application, CGI::Application::Plugin::Session, Digest::SHA, Exporter, HTML::TokeParser, HTML::Template


πŸŽ€ AUTHOR πŸŽ€

Akira Horimoto <kurt0027@gmail.com>


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.


okay that's the whole page bestie, you're officially CSRF-protected and cute πŸ’–

this little gem ships as perl v5.34.0, dated 2022-06-09, and the page itself is CGI::A...ectCSRF(3pm) β€” so go forth, generate tickets, verify tickets, clear tickets, and stay ungovernable πŸ¦‹βœ¨ periodt 🫢

πŸ“¦ where this page comes from