lib/PAGI/Middleware/SecurityHeaders.pm

filter: Statementstmt 86.7% 46 / 53 Branchbran 53.5% 15 / 28 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 75.5% 71 / 94 CC 16 SCAR 22.8
CC
16
Cov
73%
_add_security_headers
19.6
_init
5.2
__ANON__
2.0
CRAP 9.8
« blib/lib/PAGI/Middleware/FormBody.pm ↑ Summary blib/lib/PAGI/SSE.pm »
1 package PAGI::Middleware::SecurityHeaders;
2 1 $PAGI::Middleware::SecurityHeaders::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use parent 'PAGI::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 PAGI::Middleware::SecurityHeaders - Security headers middleware
11  
12 =head1 SYNOPSIS
13  
14 use PAGI::Middleware::Builder;
15  
16 my $app = builder {
17 enable 'SecurityHeaders',
18 x_frame_options => 'DENY',
19 x_content_type_options => 'nosniff',
20 x_xss_protection => '1; mode=block',
21 strict_transport_security => 'max-age=31536000; includeSubDomains';
22 $my_app;
23 };
24  
25 =head1 DESCRIPTION
26  
27 PAGI::Middleware::SecurityHeaders adds common security-related HTTP headers
28 to responses. These headers help protect against various web vulnerabilities.
29  
30 =head1 CONFIGURATION
31  
32 =over 4
33  
34 =item * x_frame_options (default: 'SAMEORIGIN')
35  
36 Controls whether the page can be displayed in a frame.
37 Values: 'DENY', 'SAMEORIGIN', or 'ALLOW-FROM uri'.
38  
39 =item * x_content_type_options (default: 'nosniff')
40  
41 Prevents MIME type sniffing.
42  
43 =item * x_xss_protection (default: '1; mode=block')
44  
45 Enables XSS filter in browsers.
46  
47 =item * referrer_policy (default: 'strict-origin-when-cross-origin')
48  
49 Controls the Referer header.
50  
51 =item * strict_transport_security (default: undef)
52  
53 HSTS header. Set to enable HTTPS enforcement.
54  
55 =item * content_security_policy (default: undef)
56  
57 CSP header. Set to define content security policy.
58  
59 =item * permissions_policy (default: undef)
60  
61 Permissions-Policy header for feature control.
62  
63 =back
64  
65 =cut
66  
67 3 ▶sub _init {
Subroutine _init: called CC=5 SCAR=16.4
68 3 my ($self, $config) = @_;
69  
70 # Use exists() to allow explicitly passing undef to disable a header
71 $self->{x_frame_options} = exists $config->{x_frame_options}
72 3 ▶ ? $config->{x_frame_options} : 'SAMEORIGIN';
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'x_frame_options'} ? : 1 2 3
73 $self->{x_content_type_options} = exists $config->{x_content_type_options}
74 3 ▶ ? $config->{x_content_type_options} : 'nosniff';
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'x_content_type_options'} ? : 0 3 3
75 $self->{x_xss_protection} = exists $config->{x_xss_protection}
76 3 ▶ ? $config->{x_xss_protection} : '1; mode=block';
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'x_xss_protection'} ? : 0 3 3
77 $self->{referrer_policy} = exists $config->{referrer_policy}
78 3 ▶ ? $config->{referrer_policy} : 'strict-origin-when-cross-origin';
Branch: 1 branch
BranchTrueFalseTotal
exists $config->{'referrer_policy'} ? : 0 3 3
79 3 $self->{strict_transport_security} = $config->{strict_transport_security};
80 3 $self->{content_security_policy} = $config->{content_security_policy};
81 3 $self->{permissions_policy} = $config->{permissions_policy};
82 }
83  
84 3 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
85 3 my ($self, $app) = @_;
86  
87 3 ▶ return async sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
88 3 my ($scope, $receive, $send) = @_;
89 # Only handle HTTP requests
90 3 ▶ if ($scope->{type} ne 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "http") 0 3 3
91 0 await $app->($scope, $receive, $send);
92 0 return;
93 }
94  
95 # Intercept send to add security headers
96 6 ▶ my $wrapped_send = async sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
97 6 my ($event) = @_;
98 6 ▶ if ($event->{type} eq 'http.response.start') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "http.response.start") 3 3 6
99 3 $self->_add_security_headers($event->{headers}, $scope);
100 }
101 6 await $send->($event);
102 3 };
103  
104 3 await $app->($scope, $receive, $wrapped_send);
105 3 };
106 }
107  
108 3 ▶sub _add_security_headers {
Subroutine _add_security_headers: called CC=10 SCAR=29.7
109 3 my ($self, $headers, $scope) = @_;
110  
111 # X-Frame-Options
112 3 ▶ if (defined $self->{x_frame_options}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"x_frame_options"}) 3 0 3
113 3 push @$headers, ['X-Frame-Options', $self->{x_frame_options}];
114 }
115  
116 # X-Content-Type-Options
117 3 ▶ if (defined $self->{x_content_type_options}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"x_content_type_options"}) 3 0 3
118 3 push @$headers, ['X-Content-Type-Options', $self->{x_content_type_options}];
119 }
120  
121 # X-XSS-Protection
122 3 ▶ if (defined $self->{x_xss_protection}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"x_xss_protection"}) 3 0 3
123 3 push @$headers, ['X-XSS-Protection', $self->{x_xss_protection}];
124 }
125  
126 # Referrer-Policy
127 3 ▶ if (defined $self->{referrer_policy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"referrer_policy"}) 3 0 3
128 3 push @$headers, ['Referrer-Policy', $self->{referrer_policy}];
129 }
130  
131 # Strict-Transport-Security (only for HTTPS)
132 3 ▶ if (defined $self->{strict_transport_security}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"strict_transport_security"}) 0 3 3
133 0 ▶ my $scheme = $scope->{scheme} // 'http';
Condition0%0/2
$scope->{"scheme"} // "http"
l!l
00
MC/DC0%0/1
$scope->{"scheme"} // "http"
$scope->{"scheme"}
Truth table0%0/2
A
A= $scope->{"scheme"}
Aresult
11
00
134 0 ▶ if ($scheme eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme eq "https") 0 0 0
135 0 push @$headers, ['Strict-Transport-Security', $self->{strict_transport_security}];
136 }
137 }
138  
139 # Content-Security-Policy
140 3 ▶ if (defined $self->{content_security_policy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"content_security_policy"}) 0 3 3
141 0 push @$headers, ['Content-Security-Policy', $self->{content_security_policy}];
142 }
143  
144 # Permissions-Policy
145 3 ▶ if (defined $self->{permissions_policy}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"permissions_policy"}) 0 3 3
146 0 push @$headers, ['Permissions-Policy', $self->{permissions_policy}];
147 }
148 }
149  
150 1 1;
151  
152 __END__
« blib/lib/PAGI/Middleware/FormBody.pm ↑ Summary blib/lib/PAGI/SSE.pm »