README(3pm)
π NAME π
Plack::Middleware::CSRFBlock β blocks CSRF attacks with minimal changes to your app, fr fr β¨
It's a Perl module from the README(3pm) manual page (User Contributed Perl Documentation, bestie π).
πΈ VERSION πΈ
version 0.10
β¨ SYNOPSIS β¨
use Plack::Builder;
my $app = sub { ... }
builder {
enable 'Session';
enable 'CSRFBlock';
$app;
}
π¦ DESCRIPTION π¦
Okay so here's the tea π: this middleware blocks CSRF. And the best part? You can use this middleware without any modifications to your application, in most cases. No cap. Here's the strategy:
π 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 forms in the response body. For example, when the application response body is:
<html>
<head>
<title>input form</title>
</head>
<body>
<form action="/api" method="post">
<input type="text" name="email" /><input type="submit" />
</form>
</body>
</html>
This becomes:
<html>
<head>
<title>input form</title>
</head>
<body>
<form action="/api" method="post"><input type="hidden" name="SEC" value="0f15ba869f1c0d77" />
<input type="text" name="email" /><input type="submit" />
</form>
</body>
</html>
This affects form tags with method="post", case insensitive. π
It is possible to add an optional meta tag by setting meta_tag to a defined value. The 'name' attribute of the HTML tag will be set to the value of meta_tag. For the previous example, when meta_tag is set to 'csrf_token', the output will be:
<html>
<head><meta name="csrf_token" content="0f15ba869f1c0d77"/>
<title>input form</title>
</head>
<body>
<form action="/api" method="post"><input type="hidden" name="SEC" value="0f15ba869f1c0d77" />
<input type="text" name="email" /><input type="submit" />
</form>
</body>
</html>
π₯ input check
For every POST requests, this module checks the X-CSRF-Token header first, then POST input parameters. If the correct token is not found in either, then a 403 Forbidden is returned by default. Yeah, it said what it said π€
Supports application/x-www-form-urlencoded and multipart/form-data for input parameters, but any POST will be validated with the X-CSRF-Token header. Thus, every POST will have to have either the header, or the appropriate form parameters in the body. Periodt. π
π javascript
This module can be used easily with javascript by having your javascript provide the X-CSRF-Token with any ajax POST requests it makes. You can get the token in javascript by getting the value of the csrftoken meta tag in the page <head>. Here is sample code that will work for jQuery:
$(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 AJAX requests made from your javascript. Iconic behavior honestly π«Ά
π 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;
}
parameter_name(default:"SEC") β the name of the input tag for the token. That's the lil guy carrying your token around πmeta_tag(default:undef) β name of themetatag added to theheadtag of output pages. The content of thismetatag will be the token value. The whole point 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. Lowkey important, bestie π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 typeapplication/x-www-form-urlencodedormultipart/form-data. No exceptions, iykyk β¨token_length(default:16); β length of the token string. Max value is 40. Don't get greedy πsession_key(default:"csrfblock.token") β this middleware usesPlack::Middleware::Sessionfor token storage. this is the session key for that. ποΈblocked(default: 403 response) β the application called when CSRF is detected.
Note: This application can read posted data, but DO NOT use them! π¨ Like genuinely, don't. That's the whole vibe check failing.
onetime(default:FALSE) β if this is true, this middleware uses onetime 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. Highkey a tradeoff, so pick your fighter π
π§ SEE ALSO π§
Plack::Middleware::Session
π AUTHORS π
- Rintaro Ishizaki <rintaro@cpan.org>
- William Wolf <throughnothing@gmail.com>
- Matthew Phillips <mattp@cpan.org>
π 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. Slay π
And that's the whole page, bestie!! π«Ά This was README(3pm) from the User Contributed Perl Documentation, running under perl v5.24.1, dated 2016-10-29. Go forth and block those CSRF attacks, you absolute main character πβ¨
π¦ where this page comes from
- ubuntu jammy: libplack-middleware-csrfblock-perl 0.10-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms