| 1 | package PAGI::Middleware::HTTPSRedirect; | ||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::Middleware::HTTPSRedirect::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::HTTPSRedirect - Force HTTPS redirect middleware | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | my $app = builder { | ||||||||||||||||||||||||||||
| 17 | enable 'HTTPSRedirect'; | ||||||||||||||||||||||||||||
| 18 | $my_app; | ||||||||||||||||||||||||||||
| 19 | }; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | PAGI::Middleware::HTTPSRedirect redirects HTTP requests to HTTPS. | ||||||||||||||||||||||||||||
| 24 | Useful for enforcing secure connections in production. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =over 4 | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =item * redirect_code (default: 301) | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | HTTP status code for redirects. Use 302 for temporary redirects. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =item * exclude (optional) | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Arrayref of paths to exclude from redirect (e.g., health checks). | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =item * hsts (default: 0) | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | If true, add Strict-Transport-Security header. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =item * hsts_max_age (default: 31536000) | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | HSTS max-age in seconds (1 year default). | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =back | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 3 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||
| 51 | 3 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 3 | ▶ | $self->{redirect_code} = $config->{redirect_code} // 301; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"redirect_code"} // 301
MC/DC0%0/1
$config->{"redirect_code"} // 301
$config->{"redirect_code"}
Truth table50%1/2
A
A= $config->{"redirect_code"}
| |||||||||||||||||||||||||||||
| 54 | 3 | ▶ | $self->{exclude} = $config->{exclude} // []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"exclude"} // []
MC/DC100%1/1
$config->{"exclude"} // []
$config->{"exclude"}
Truth table100%2/2
A
A= $config->{"exclude"}
| |||||||||||||||||||||||||||||
| 55 | 3 | ▶ | $self->{hsts} = $config->{hsts} // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"hsts"} // 0
MC/DC0%0/1
$config->{"hsts"} // 0
$config->{"hsts"}
Truth table50%1/2
A
A= $config->{"hsts"}
| |||||||||||||||||||||||||||||
| 56 | 3 | ▶ | $self->{hsts_max_age} = $config->{hsts_max_age} // 31536000; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"hsts_max_age"} // 31536000
MC/DC0%0/1
$config->{"hsts_max_age"} // 31536000
$config->{"hsts_max_age"}
Truth table50%1/2
A
A= $config->{"hsts_max_age"}
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.9
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 3 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=30.2
| |||||||||||||||||||||||||||||
| 63 | 3 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 64 | 3 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 66 | 0 | return; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 3 | ▶ | my $scheme = $scope->{scheme} // 'http'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"scheme"} // "http"
MC/DC0%0/1
$scope->{"scheme"} // "http"
$scope->{"scheme"}
Truth table50%1/2
A
A= $scope->{"scheme"}
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Already HTTPS | ||||||||||||||||||||||||||||
| 72 | 3 | ▶ | if ($scheme eq 'https') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | # Add HSTS header if enabled | ||||||||||||||||||||||||||||
| 74 | 1 | ▶ | if ($self->{hsts}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $wrapped_send = async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 76 | 0 | my ($event) = @_; | |||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ($event->{type} eq 'http.response.start') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my @headers = @{$event->{headers} // []}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$event->{"headers"} // []
MC/DC0%0/1
$event->{"headers"} // []
$event->{"headers"}
Truth table0%0/2
A
A= $event->{"headers"}
| |||||||||||||||||||||||||||||
| 79 | 0 | push @headers, [ | |||||||||||||||||||||||||||
| 80 | 'Strict-Transport-Security', | ||||||||||||||||||||||||||||
| 81 | "max-age=$self->{hsts_max_age}; includeSubDomains" | ||||||||||||||||||||||||||||
| 82 | ]; | ||||||||||||||||||||||||||||
| 83 | 0 | await $send->({ | |||||||||||||||||||||||||||
| 84 | %$event, | ||||||||||||||||||||||||||||
| 85 | headers => \@headers, | ||||||||||||||||||||||||||||
| 86 | }); | ||||||||||||||||||||||||||||
| 87 | 0 | return; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 0 | await $send->($event); | |||||||||||||||||||||||||||
| 90 | 0 | }; | |||||||||||||||||||||||||||
| 91 | 0 | await $app->($scope, $receive, $wrapped_send); | |||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||
| 93 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 1 | return; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Check exclusions | ||||||||||||||||||||||||||||
| 99 | 2 | ▶ | if ($self->_is_excluded($scope->{path})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 101 | 1 | return; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | # Build HTTPS URL | ||||||||||||||||||||||||||||
| 105 | 1 | ▶ | my $host = $self->_get_header($scope, 'host') // 'localhost'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_get_header($scope, "host") // "localhost"
MC/DC0%0/1
$self->_get_header($scope, "host") // "localhost"
$self->_get_header($scope, "host")
Truth table50%1/2
A
A= $self->_get_header($scope, "host")
| |||||||||||||||||||||||||||||
| 106 | 1 | ▶ | my $path = $scope->{path} // '/'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"path"} // "/"
MC/DC0%0/1
$scope->{"path"} // "/"
$scope->{"path"}
Truth table50%1/2
A
A= $scope->{"path"}
| |||||||||||||||||||||||||||||
| 107 | 1 | my $query = $scope->{query_string}; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | my $url = "https://$host$path"; | |||||||||||||||||||||||||||
| 110 | 1 | ▶ | $url .= "?$query" if defined $query && $query ne ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $query and $query ne ""
MC/DC0%0/2
defined $query and $query ne ""
defined $query
$query ne ""
Truth table33%1/3
A and B
A= defined $queryB= $query ne ""
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | await $self->_send_redirect($send, $url); | |||||||||||||||||||||||||||
| 113 | 3 | }; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | sub _is_excluded { | ||||||||||||||||||||||||||
|
Subroutine
_is_excluded: called CC=5 SCAR=18.6
| |||||||||||||||||||||||||||||
| 117 | 2 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 2 | for my $pattern (@{$self->{exclude}}) { | |||||||||||||||||||||||||||
| 120 | 1 | ▶ | if (ref $pattern eq 'Regexp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | return 1 if $path =~ $pattern; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||
| 123 | 1 | ▶ | return 1 if $path eq $pattern; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 1 | return 0; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | ▶ | async sub _send_redirect { | ||||||||||||||||||||||||||
|
Subroutine
_send_redirect: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 130 | 1 | my ($self, $send, $location) = @_; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 1 | my $status = $self->{redirect_code}; | |||||||||||||||||||||||||||
| 133 | 1 | my $body = "Redirecting to $location"; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | await $send->({ | |||||||||||||||||||||||||||
| 136 | type => 'http.response.start', | ||||||||||||||||||||||||||||
| 137 | status => $status, | ||||||||||||||||||||||||||||
| 138 | headers => [ | ||||||||||||||||||||||||||||
| 139 | ['Content-Type', 'text/plain'], | ||||||||||||||||||||||||||||
| 140 | ['Content-Length', length($body)], | ||||||||||||||||||||||||||||
| 141 | ['Location', $location], | ||||||||||||||||||||||||||||
| 142 | ], | ||||||||||||||||||||||||||||
| 143 | }); | ||||||||||||||||||||||||||||
| 144 | 1 | await $send->({ | |||||||||||||||||||||||||||
| 145 | type => 'http.response.body', | ||||||||||||||||||||||||||||
| 146 | body => $body, | ||||||||||||||||||||||||||||
| 147 | more => 0, | ||||||||||||||||||||||||||||
| 148 | }); | ||||||||||||||||||||||||||||
| 149 | 1 | } | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 1 | ▶ | sub _get_header { | ||||||||||||||||||||||||||
|
Subroutine
_get_header: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||
| 152 | 1 | my ($self, $scope, $name) = @_; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 1 | $name = lc($name); | |||||||||||||||||||||||||||
| 155 | 1 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 156 | 1 | ▶ | return $h->[1] if lc($h->[0]) eq $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 0 | return; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 1 | 1; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | __END__ | ||||||||||||||||||||||||||||