blib/lib/PAGI/Middleware/SecurityHeaders.pm untested

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 98 CC 15 SCAR 38.7
CC
15
Cov
0%
_add_security_headers
90.0
_init
30.0
wrap
12.0
CRAP 47.8
1 package PAGI::Middleware::SecurityHeaders;
2 $PAGI::Middleware::SecurityHeaders::VERSION = '0.002001';
3 use strict;
4 use warnings;
5 use parent 'PAGI::Middleware';
6 use Future::AsyncAwait;
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 sub _init {
68 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 ? $config->{x_frame_options} : 'SAMEORIGIN';
73 $self->{x_content_type_options} = exists $config->{x_content_type_options}
74 ? $config->{x_content_type_options} : 'nosniff';
75 $self->{x_xss_protection} = exists $config->{x_xss_protection}
76 ? $config->{x_xss_protection} : '1; mode=block';
77 $self->{referrer_policy} = exists $config->{referrer_policy}
78 ? $config->{referrer_policy} : 'strict-origin-when-cross-origin';
79 $self->{strict_transport_security} = $config->{strict_transport_security};
80 $self->{content_security_policy} = $config->{content_security_policy};
81 $self->{permissions_policy} = $config->{permissions_policy};
82 }
83  
84 sub wrap {
85 my ($self, $app) = @_;
86  
87 return async sub {
88 my ($scope, $receive, $send) = @_;
89 # Only handle HTTP requests
90 if ($scope->{type} ne 'http') {
91 await $app->($scope, $receive, $send);
92 return;
93 }
94  
95 # Intercept send to add security headers
96 my $wrapped_send = async sub {
97 my ($event) = @_;
98 if ($event->{type} eq 'http.response.start') {
99 $self->_add_security_headers($event->{headers}, $scope);
100 }
101 await $send->($event);
102 };
103  
104 await $app->($scope, $receive, $wrapped_send);
105 };
106 }
107  
108 sub _add_security_headers {
109 my ($self, $headers, $scope) = @_;
110  
111 # X-Frame-Options
112 if (defined $self->{x_frame_options}) {
113 push @$headers, ['X-Frame-Options', $self->{x_frame_options}];
114 }
115  
116 # X-Content-Type-Options
117 if (defined $self->{x_content_type_options}) {
118 push @$headers, ['X-Content-Type-Options', $self->{x_content_type_options}];
119 }
120  
121 # X-XSS-Protection
122 if (defined $self->{x_xss_protection}) {
123 push @$headers, ['X-XSS-Protection', $self->{x_xss_protection}];
124 }
125  
126 # Referrer-Policy
127 if (defined $self->{referrer_policy}) {
128 push @$headers, ['Referrer-Policy', $self->{referrer_policy}];
129 }
130  
131 # Strict-Transport-Security (only for HTTPS)
132 if (defined $self->{strict_transport_security}) {
133 my $scheme = $scope->{scheme} // 'http';
134 if ($scheme eq 'https') {
135 push @$headers, ['Strict-Transport-Security', $self->{strict_transport_security}];
136 }
137 }
138  
139 # Content-Security-Policy
140 if (defined $self->{content_security_policy}) {
141 push @$headers, ['Content-Security-Policy', $self->{content_security_policy}];
142 }
143  
144 # Permissions-Policy
145 if (defined $self->{permissions_policy}) {
146 push @$headers, ['Permissions-Policy', $self->{permissions_policy}];
147 }
148 }
149  
150 1;
151  
152 __END__