blib/lib/Mojolicious/Plugin/Fondation/CSRF.pm

filter: Statementstmt 100% 39 / 39 Branchbran 85.0% 17 / 20 Conditioncond 40.0% 4 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 6 / 6 Podpod 50.0% 1 / 2 totaltotal 80.7% 67 / 83 CC 17 SCAR 16.5
CC
17
Cov
87%
__ANON__
7.1
__ANON__
6.2
__ANON__
4.0
CRAP 5.2
↑ Summary
1 package Mojolicious::Plugin::Fondation::CSRF;
2 4 $Mojolicious::Plugin::Fondation::CSRF::VERSION = '0.02';
3 # ABSTRACT: CSRF protection plugin for Fondation — route condition, OpenAPI integration, JS injection
4  
5 4 ▶use Mojo::Base 'Mojolicious::Plugin', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶sub fondation_meta {
Subroutine fondation_meta: called CC=1 SCAR=0.0
Pod undocumented
8 return {
9 6 dependencies => ['Fondation::Problem'],
10 defaults => {
11 auto_protect => 1,
12 exemptions => [],
13 },
14 };
15 }
16  
17 6 ▶sub register ($self, $app, $config) {
Subroutine register: called CC=3 SCAR=11.1
Pod documented
18  
19 # ═══════════════════════════════════════════════════════════════════════
20 # 1. Route condition: fondation.csrf
21 # ═══════════════════════════════════════════════════════════════════════
22  
23 10 ▶ $app->routes->add_condition('fondation.csrf' => sub {
Subroutine __ANON__: called CC=6 SCAR=18.3
24 10 my ($route, $c, $captures) = @_;
25  
26 # GET, HEAD, OPTIONS are safe — no CSRF needed
27 10 ▶ return 1 if $c->req->method =~ /^(GET|HEAD|OPTIONS)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->method =~ /^(GET|HEAD|OPTIONS)$/u 3 7 10
28  
29 # Bearer token auth bypasses CSRF — no session cookie, no CSRF risk
30 7 my $auth = $c->req->headers->authorization;
31 7 ▶ return 1 if $auth && $auth =~ /^Bearer\s+/i;
Branch: 1 branch
BranchTrueFalseTotal
if $auth and $auth =~ /^Bearer\s+/iu 0 7 7
Condition33%1/3
$auth and $auth =~ /^Bearer\s+/iu
!ll&&!rl&&r
700
MC/DC0%0/2
$auth and $auth =~ /^Bearer\s+/iu
$auth $auth =~ /^Bearer\s+/iu
Truth table33%1/3
A and B
A= $authB= $auth =~ /^Bearer\s+/iu
ABresult
0X0
100
111
32  
33 # Delegate to Mojo's built-in csrf_protect (validates csrf_token
34 # from form field or X-CSRF-Token header against session token)
35 7 $c->validation->csrf_protect;
36  
37 7 ▶ unless ($c->validation->has_error('csrf_token')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->validation->has_error("csrf_token")) 3 4 7
38 3 return 1;
39 }
40  
41 4 $c->res->code(403);
42 4 ▶ $c->stash('fondation.denied' => { status => 403, title => 'CSRF token missing or invalid' })
Branch: 1 branch
BranchTrueFalseTotal
unless $c->stash("fondation.denied") 4 0 4
43 unless $c->stash('fondation.denied');
44 4 return undef;
45 6 });
46  
47 # ═══════════════════════════════════════════════════════════════════════
48 # 2. OpenAPI integration — subscribe to openapi_routes_added hook
49 # Runs when OpenAPI generates routes during its fondation_finalyze.
50 # register() always runs before any fondation_finalyze, so this
51 # subscription is guaranteed active when the hook fires.
52 # ═══════════════════════════════════════════════════════════════════════
53  
54 1 ▶ $app->plugins->on(openapi_routes_added => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
55 1 my ($openapi, $routes) = @_;
56 1 for my $r (@$routes) {
57 6 my $defaults = $r->pattern->defaults;
58 6 ▶ my $method = $defaults->{'openapi.method'} // '';
Condition50%1/2
$defaults->{"openapi.method"} // ""
l!l
60
MC/DC0%0/1
$defaults->{"openapi.method"} // ""
$defaults->{"openapi.method"}
Truth table50%1/2
A
A= $defaults->{"openapi.method"}
Aresult
11
00
59 6 ▶ next unless $method =~ /^(post|put|patch|delete)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /^(post|put|patch|delete)$/iu 2 4 6
60 4 $r->requires('fondation.csrf');
61 }
62 6 });
63  
64 # ═══════════════════════════════════════════════════════════════════════
65 # 3. around_dispatch — blanket protection for HTML routes
66 # ═══════════════════════════════════════════════════════════════════════
67  
68 6 ▶ if ($config->{auto_protect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"auto_protect"}) 2 4 6
69 2 ▶ my $exemptions = $config->{exemptions} // [];
Condition50%1/2
$config->{"exemptions"} // []
l!l
20
MC/DC0%0/1
$config->{"exemptions"} // []
$config->{"exemptions"}
Truth table50%1/2
A
A= $config->{"exemptions"}
Aresult
11
00
70  
71 9 ▶ $app->hook(around_dispatch => sub ($next, $c) {
Subroutine __ANON__: called CC=7 SCAR=19.6
72  
73 # Safe methods
74 9 ▶ return $next->() if $c->req->method =~ /^(GET|HEAD|OPTIONS)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->method =~ /^(GET|HEAD|OPTIONS)$/u 3 6 9
75  
76 # Bearer token auth bypasses CSRF — no session cookie, no CSRF risk
77 6 my $auth = $c->req->headers->authorization;
78 6 ▶ return $next->() if $auth && $auth =~ /^Bearer\s+/i;
Branch: 1 branch
BranchTrueFalseTotal
if $auth and $auth =~ /^Bearer\s+/iu 0 6 6
Condition33%1/3
$auth and $auth =~ /^Bearer\s+/iu
!ll&&!rl&&r
600
MC/DC0%0/2
$auth and $auth =~ /^Bearer\s+/iu
$auth $auth =~ /^Bearer\s+/iu
Truth table33%1/3
A and B
A= $authB= $auth =~ /^Bearer\s+/iu
ABresult
0X0
100
111
79  
80 # Configurable exemptions (regex patterns on path)
81 6 my $path = $c->req->url->path->to_string;
82 6 for my $pattern (@$exemptions) {
83 5 ▶ return $next->() if $path =~ $pattern;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$pattern/u 2 3 5
84 }
85  
86 4 $c->validation->csrf_protect;
87  
88 4 ▶ unless ($c->validation->has_error('csrf_token')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->validation->has_error("csrf_token")) 2 2 4
89 2 return $next->();
90 }
91  
92 2 $c->problem(status => 403, title => 'CSRF token missing or invalid');
93 2 return;
94 2 });
95 }
96  
97 6 return $self;
98 }
99  
100 4 1;
101  
102 __END__
↑ Summary