| 1 | package PAGI::Middleware::TrustedHosts; | ||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::Middleware::TrustedHosts::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::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 | 3 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 47 | 3 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | ▶ | $self->{hosts} = $config->{hosts} // die "TrustedHosts requires 'hosts' option"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"hosts"} // die("TrustedHosts requires 'hosts' option")
MC/DC0%0/1
$config->{"hosts"} // die("TrustedHosts requires 'hosts' option")
$config->{"hosts"}
Truth table50%1/2
A
A= $config->{"hosts"}
| |||||||||||||||||||||||||||||
| 50 | 3 | ▶ | $self->{allow_empty} = $config->{allow_empty} // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"allow_empty"} // 0
MC/DC0%0/1
$config->{"allow_empty"} // 0
$config->{"allow_empty"}
Truth table50%1/2
A
A= $config->{"allow_empty"}
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Compile host patterns to regexes | ||||||||||||||||||||||||||||
| 53 | 3 | $self->{_patterns} = [map { $self->_compile_pattern($_) } @{$self->{hosts}}]; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 4 | ▶ | sub _compile_pattern { | ||||||||||||||||||||||||||
|
Subroutine
_compile_pattern: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 4 | my ($self, $pattern) = @_; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Escape regex special chars except * | ||||||||||||||||||||||||||||
| 60 | 4 | my $escaped = quotemeta($pattern); | |||||||||||||||||||||||||||
| 61 | # Convert escaped * back to regex wildcard | ||||||||||||||||||||||||||||
| 62 | 4 | $escaped =~ s/\\\*/.*/g; | |||||||||||||||||||||||||||
| 63 | 4 | return qr/^$escaped$/i; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 3 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.4
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 3 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=24.2
| |||||||||||||||||||||||||||||
| 70 | 3 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 71 | # Only handle HTTP requests | ||||||||||||||||||||||||||||
| 72 | 3 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 74 | 0 | return; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Get Host header | ||||||||||||||||||||||||||||
| 78 | 3 | my $host = $self->_get_header($scope, 'host'); | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Check if host is allowed | ||||||||||||||||||||||||||||
| 81 | 3 | ▶ | if (!defined $host || $host eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $host or $host eq ""
MC/DC0%0/2
not defined $host or $host eq ""
not defined $host
$host eq ""
Truth table33%1/3
not(A) or B
A= not defined $hostB= $host eq ""
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ($self->{allow_empty}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 0 | await $self->_send_error($send, 400, 'Missing Host header'); | |||||||||||||||||||||||||||
| 87 | 0 | return; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Strip port for matching if needed | ||||||||||||||||||||||||||||
| 91 | 3 | my $host_for_match = $host; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # Check against patterns | ||||||||||||||||||||||||||||
| 94 | 3 | my $allowed = 0; | |||||||||||||||||||||||||||
| 95 | 3 | for my $pattern (@{$self->{_patterns}}) { | |||||||||||||||||||||||||||
| 96 | 3 | ▶ | if ($host_for_match =~ $pattern) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 2 | $allowed = 1; | |||||||||||||||||||||||||||
| 98 | 2 | last; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 3 | ▶ | if ($allowed) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 2 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||
| 105 | 1 | await $self->_send_error($send, 400, 'Invalid Host header'); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 3 | }; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | ▶ | sub _get_header { | ||||||||||||||||||||||||||
|
Subroutine
_get_header: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||
| 111 | 3 | my ($self, $scope, $name) = @_; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 3 | $name = lc($name); | |||||||||||||||||||||||||||
| 114 | 3 | ▶ | for my $h (@{$scope->{headers} // []}) { | ||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"headers"} // []
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||||||||||
| 115 | 3 | ▶ | return $h->[1] if lc($h->[0]) eq $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 0 | return; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | ▶ | async sub _send_error { | ||||||||||||||||||||||||||
|
Subroutine
_send_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 121 | 1 | my ($self, $send, $status, $message) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 1 | 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 | 1 | await $send->({ | |||||||||||||||||||||||||||
| 132 | type => 'http.response.body', | ||||||||||||||||||||||||||||
| 133 | body => $message, | ||||||||||||||||||||||||||||
| 134 | more => 0, | ||||||||||||||||||||||||||||
| 135 | }); | ||||||||||||||||||||||||||||
| 136 | 1 | } | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 1 | 1; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||||||||||