| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Dancer-Plugin-RequireSSL | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2012 by Natal Ngétal. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | package Dancer::Plugin::RequireSSL; | ||||||||||||||||||||||||||||
| 10 | { | ||||||||||||||||||||||||||||
| 11 | 1 | $Dancer::Plugin::RequireSSL::VERSION = '0.121370'; | |||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use Dancer ':syntax'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | #ABSTRACT: Configure your application to redirect all incoming requests to HTTPS | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | register require_ssl => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | hook before => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 27 | 0 | my $req = request; | |||||||||||||||||||||||||||
| 28 | 0 | _redirect_to_ssl($req); | |||||||||||||||||||||||||||
| 29 | 0 | }; | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | hook after => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 31 | 0 | _set_hsts_header(); | |||||||||||||||||||||||||||
| 32 | 0 | }; | |||||||||||||||||||||||||||
| 33 | 1 | }; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub _redirect_to_ssl { | ||||||||||||||||||||||||||
|
Subroutine
_redirect_to_ssl: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 36 | 0 | my $req = shift; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ( ! request->secure | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not request()->secure and setting("environment") ne "development"
MC/DC0%0/2
not request()->secure and setting("environment") ne "development"
not request()->secure
setting("environment") ne "development"
Truth table0%0/3
not(A) and B
A= not request()->secureB= setting("environment") ne "development"
| |||||||||||||||||||||||||||||
| 39 | && setting('environment') ne 'development' ) { | ||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ( $req->base =~ /http:\/\// | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$req->base =~ m[http://] or not $req->header("X-Forwarded-Proto") =~ /https/
MC/DC0%0/2
$req->base =~ m[http://] or not $req->header("X-Forwarded-Proto") =~ /https/
$req->base =~ m[http://]
not $req->header("X-Forwarded-Proto") =~ /https/
Truth table0%0/3
A or not(B)
A= $req->base =~ m[http://]B= not $req->header("X-Forwarded-Proto") =~ /https/
| |||||||||||||||||||||||||||||
| 41 | || $req->header('X-Forwarded-Proto') !~ 'https' ) { | ||||||||||||||||||||||||||||
| 42 | 0 | my $url = 'https://' . $req->host . $req->path; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | return redirect($url); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub _set_hsts_header { | ||||||||||||||||||||||||||
|
Subroutine
_set_hsts_header: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 50 | 0 | my $settings = plugin_setting; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $hsts_age = $settings->{hsts_age} // 31536000; | ||||||||||||||||||||||||||
|
Condition0%0/2
$settings->{"hsts_age"} // 31536000
MC/DC0%0/1
$settings->{"hsts_age"} // 31536000
$settings->{"hsts_age"}
Truth table0%0/2
A
A= $settings->{"hsts_age"}
| |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my $subdomains = $settings->{hsts_include_subdomains} // 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$settings->{"hsts_include_subdomains"} // 0
MC/DC0%0/1
$settings->{"hsts_include_subdomains"} // 0
$settings->{"hsts_include_subdomains"}
Truth table0%0/2
A
A= $settings->{"hsts_include_subdomains"}
| |||||||||||||||||||||||||||||
| 53 | 0 | my $hsts_policy = "max-age=$hsts_age"; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | $hsts_policy .= '; includeSubDomains' if $subdomains; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | header 'Strict-Transport-Security' => $hsts_policy; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | return; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | register_plugin; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | 1; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | __END__ | ||||||||||||||||||||||||||||