Plack:...FBlock(3pm)

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

πŸ’– Plack::Middleware::CSRFBlock πŸ’–

okay bestie, so this is Plack::Middleware::CSRFBlock, a Perl module (manual page Plack::...FBlock(3pm), from the User Contributed Perl Documentation) and it's literally here to block CSRF attacks with like, minimal changes to your app. no cap.

✨ VERSION ✨

version 0.10 β€” she's serving.


🌸 SYNOPSIS 🌸

use Plack::Builder;

my $app = sub { ... }

builder {
  enable 'Session';
  enable 'CSRFBlock';
  $app;
}

πŸ¦‹ DESCRIPTION πŸ¦‹

so this middleware blocks CSRF, and the iconic part is that you can use it without any modifications to your application, in most cases. here's the strategy, bestie:

πŸ’… 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 "form"s 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. slay.

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>

πŸ‘‘

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. 😭

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.

🌈 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. obsessed. 🫢


✨ 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") β€” name of the input tag for the token. πŸŽ€
  • meta_tag (default: undef) β€” name of the "meta" tag added to the "head" tag of output pages. the content of this "meta" 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.
  • 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 "application/x-www-form-urlencoded" or "multipart/form-data".
  • token_length (default: 16); β€” length of the token string. max value is 40.
  • session_key (default: "csrfblock.token") β€” this middleware uses Plack::Middleware::Session for 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! πŸ’€
  • 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. 😭


πŸ’œ SEE ALSO πŸ’œ

Plack::Middleware::Session


πŸ‘‘ AUTHORS πŸ‘‘

  • Rintaro Ishizaki <rintaro@cpan.org>
  • William Wolf <throughnothing@gmail.com>
  • Matthew Phillips <mattp@cpan.org>

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.


that's the whole vibe bestie, go forth and block those CSRF attacks ✨ this page was served up by perl v5.34.0 on 2022-06-16, stay iconic πŸ’…πŸ¦‹πŸ’–

πŸ“¦ where this page comes from