=pod =encoding UTF-8 =head1 NAME Plack::Middleware::CSRFBlock - Block CSRF Attacks with minimal changes to your app =head1 VERSION version 0.10 =head1 SYNOPSIS use Plack::Builder; my $app = sub { ... } builder { enable 'Session'; enable 'CSRFBlock'; $app; } =head1 DESCRIPTION This middleware blocks CSRF. You can use this middleware without any modifications to your application, in most cases. Here is the strategy: =over 4 =item output filter When the application response content-type is "text/html" or "application/xhtml+xml", this inserts a hidden input tag that contains a token string into C
s in the response body. For example, when the application response body is: input form
This becomes: input form
This affects C
tags with C, case insensitive. It is possible to add an optional meta tag by setting C to a defined value. The 'name' attribute of the HTML tag will be set to the value of C. For the previous example, when C is set to 'csrf_token', the output will be: input form =item input check For every POST requests, this module checks the C header first, then C input parameters. If the correct token is not found in either, then a 403 Forbidden is returned by default. Supports C and C for input parameters, but any C will be validated with the C header. Thus, every C will have to have either the header, or the appropriate form parameters in the body. =item javascript This module can be used easily with javascript by having your javascript provide the C with any ajax C requests it makes. You can get the C in javascript by getting the value of the C C tag in the page . Here is sample code that will work for C: $(document).ajaxSend(function(e, xhr, options) { var token = $("meta[name='csrftoken']").attr("content"); xhr.setRequestHeader("X-CSRF-Token", token); }); This will include the X-CSRF-Token header with any C requests made from your javascript. =back =head1 OPTIONS use Plack::Builder; my $app = sub { ... } builder { enable 'Session'; enable 'CSRFBlock', parameter_name => 'csrf_secret', token_length => 20, session_key => 'csrf_token', blocked => sub { [302, [Location => 'http://www.google.com'], ['']]; }, onetime => 0, ; $app; } =over 4 =item parameter_name (default:"SEC") Name of the input tag for the token. =item meta_tag (default:undef) Name of the C tag added to the C tag of output pages. The content of this C tag will be the token value. The purpose of this tag is to give javascript access to the token if needed for AJAX requests. If this attribute is not explicitly set the meta tag will not be included. =item header_name (default:"X-CSRF-Token") Name of the HTTP Header that the token can be sent in. This is useful for sending the header for Javascript AJAX requests, and this header is required for any post request that is not of type C or C. =item token_length (default:16); Length of the token string. Max value is 40. =item session_key (default:"csrfblock.token") This middleware uses L for token storage. this is the session key for that. =item blocked (default:403 response) The application called when CSRF is detected. Note: This application can read posted data, but DO NOT use them! =item onetime (default:FALSE) If this is true, this middleware uses B token, that is, whenever client sent collect token and this middleware detect that, token string is regenerated. This makes your applications more secure, but in many cases, is too strict. =back =head1 SEE ALSO L =head1 AUTHORS =over 4 =item * Rintaro Ishizaki =item * William Wolf =item * Matthew Phillips =back =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2014 by the Authors of Plack-Middleware-CSRFBlock. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut