NAME

Mojolicious::Plugin::Fondation::CSRF - CSRF protection plugin for Fondation — route condition, OpenAPI integration, JS injection

VERSION

version 0.01

SYNOPSIS

# In myapp.conf
plugin 'Fondation' => {
    dependencies => [
        'Fondation::SessionStore',     # sessions required for CSRF tokens
        'Fondation::CSRF',
    ],
};

# Per-route opt-in (when auto_protect is disabled):
$r->post('/secure-action')->requires('fondation.csrf')->to('mycontroller#action');

# With exemptions:
'Fondation::CSRF' => {
    auto_protect => 1,
    exemptions   => [qr{^/webhook/}, qr{^/api/public/}],
},

DESCRIPTION

Mojolicious::Plugin::Fondation::CSRF provides Cross-Site Request Forgery protection for Fondation applications. It uses Mojolicious' built-in CSRF token mechanism (stored in session, validated via "csrf_protect" in Validation).

Three protection layers, all using the same underlying Mojo validation:

Token transmission works two ways, both handled automatically by Mojo:

CONFIGURATION

DEPENDENCIES

Mojolicious::Plugin::Fondation.

Sessions must be enabled (Fondation::SessionStore or Mojolicious' default signed cookies). The CSRF token lives in $c->session->{csrf_token}.

STATIC JS FILE

The plugin ships share/public/js/csrf.js — a standalone script that reads the CSRF token from <meta name="csrf-token"> and auto-injects it as X-CSRF-Token header on all fetch and XMLHttpRequest POST/PUT/PATCH/DELETE calls.

Add it to your assetpack.def:

< js/csrf.js

Or include it directly in your layout:

<script src="/js/csrf.js"></script>

The meta tag must be present in the page. Fondation::Layout-Bootstrap provides it by default:

<meta name="csrf-token" content="<%= csrf_token %>">

END-TO-END FLOW

When the CSRF plugin is loaded alongside the standard Fondation stack (Layout-Bootstrap, Asset, OpenAPI, Auth), protection works automatically:

HOW TOKEN VALIDATION WORKS

The route condition and around_dispatch both delegate to Mojo's csrf_protect validation:

SEE ALSO

Mojolicious::Plugin::Fondation, Mojolicious::Plugin::Fondation::OpenAPI, Mojolicious::Guides::Routing, Mojolicious::Validator::Validation

AUTHOR

Daniel Brosseau dab@cpan.org

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Daniel Brosseau.

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