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

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 95 CC 7 SCAR 29.8
CC
7
Cov
0%
wrap
42.0
_get_header
6.0
_init
2.0
CRAP 19.7
1 package PAGI::Middleware::TrustedHosts;
2 $PAGI::Middleware::TrustedHosts::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::TrustedHosts - Host header validation middleware
11  
12 =head1 SYNOPSIS
13  
14 use PAGI::Middleware::Builder;
15  
16 my $app = builder {
17 enable 'TrustedHosts',
18 hosts => ['example.com', 'www.example.com', '*.example.com'];
19 $my_app;
20 };
21  
22 =head1 DESCRIPTION
23  
24 PAGI::Middleware::TrustedHosts validates the Host header against a list
25 of allowed hosts. This helps prevent host header injection attacks.
26  
27 =head1 CONFIGURATION
28  
29 =over 4
30  
31 =item * hosts (required)
32  
33 Array of allowed host patterns. Patterns can include:
34 - Exact hostnames: 'example.com'
35 - Wildcard subdomains: '*.example.com'
36 - Port specifications: 'example.com:8080'
37  
38 =item * allow_empty (default: 0)
39  
40 If true, allow requests without a Host header.
41  
42 =back
43  
44 =cut
45  
46 sub _init {
47 my ($self, $config) = @_;
48  
49 $self->{hosts} = $config->{hosts} // die "TrustedHosts requires 'hosts' option";
50 $self->{allow_empty} = $config->{allow_empty} // 0;
51  
52 # Compile host patterns to regexes
53 $self->{_patterns} = [map { $self->_compile_pattern($_) } @{$self->{hosts}}];
54 }
55  
56 sub _compile_pattern {
57 my ($self, $pattern) = @_;
58  
59 # Escape regex special chars except *
60 my $escaped = quotemeta($pattern);
61 # Convert escaped * back to regex wildcard
62 $escaped =~ s/\\\*/.*/g;
63 return qr/^$escaped$/i;
64 }
65  
66 sub wrap {
67 my ($self, $app) = @_;
68  
69 return async sub {
70 my ($scope, $receive, $send) = @_;
71 # Only handle HTTP requests
72 if ($scope->{type} ne 'http') {
73 await $app->($scope, $receive, $send);
74 return;
75 }
76  
77 # Get Host header
78 my $host = $self->_get_header($scope, 'host');
79  
80 # Check if host is allowed
81 if (!defined $host || $host eq '') {
82 if ($self->{allow_empty}) {
83 await $app->($scope, $receive, $send);
84 return;
85 }
86 await $self->_send_error($send, 400, 'Missing Host header');
87 return;
88 }
89  
90 # Strip port for matching if needed
91 my $host_for_match = $host;
92  
93 # Check against patterns
94 my $allowed = 0;
95 for my $pattern (@{$self->{_patterns}}) {
96 if ($host_for_match =~ $pattern) {
97 $allowed = 1;
98 last;
99 }
100 }
101  
102 if ($allowed) {
103 await $app->($scope, $receive, $send);
104 } else {
105 await $self->_send_error($send, 400, 'Invalid Host header');
106 }
107 };
108 }
109  
110 sub _get_header {
111 my ($self, $scope, $name) = @_;
112  
113 $name = lc($name);
114 for my $h (@{$scope->{headers} // []}) {
115 return $h->[1] if lc($h->[0]) eq $name;
116 }
117 return;
118 }
119  
120 async sub _send_error {
121 my ($self, $send, $status, $message) = @_;
122  
123 await $send->({
124 type => 'http.response.start',
125 status => $status,
126 headers => [
127 ['content-type', 'text/plain'],
128 ['content-length', length($message)],
129 ],
130 });
131 await $send->({
132 type => 'http.response.body',
133 body => $message,
134 more => 0,
135 });
136 }
137  
138 1;
139  
140 __END__