blib/lib/Lemonldap/NG/Portal/CDC.pm

filter: Statementstmt 76.1% 99 / 130 Branchbran 43.5% 27 / 62 Conditioncond 36.8% 7 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 61.1% 143 / 234 CC 46 SCAR 36.1
CC
46
Cov
63%
handler
86.4
_buildTrustedDomainsRe
24.1
init
12.8
CRAP 37.1
« blib/lib/Lemonldap/NG/Portal/Auth/REST.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/WebAuthn.pm »
1 ## @file
2 # Module for SAML Common Domain Cookie Support
3  
4 ## @class Lemonldap::NG::Portal::CDC
5 # Class for SAML Common Domain Cookie Support
6 package Lemonldap::NG::Portal::CDC;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Regexp::Assemble;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Lemonldap::NG::Common::FormEncode;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '2.23.1';
16  
17 1 extends 'Lemonldap::NG::Common::PSGI';
18  
19 # PROPERTIES
20  
21 1 has cdc_name => ( is => 'rw' );
22 1 has cdc_domain => ( is => 'rw' );
23 1 has httpOnly => ( is => 'rw' );
24 1 has cookieExpiration => ( is => 'rw' );
25 1 has oldStyleUrl => ( is => 'rw' );
26 1 has cdc_values => ( is => 'rw' );
27 1 has trustedDomainsRe => ( is => 'rw' );
28  
29 # INITIALIZATION
30  
31 1 ▶sub init {
Subroutine init: called CC=10 SCAR=25.5
Pod undocumented
32 1 my ( $self, $args ) = @_;
33 1 my $tmp = Lemonldap::NG::Common::Conf->new( $args->{configStorage} );
34 1 ▶ unless ($tmp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp) 0 1 1
35 0 $self->error(
36 "Unable to access configuration: $Lemonldap::NG::Common::Conf::msg"
37 );
38 0 return 0;
39 }
40 1 ▶ my $lconf = $tmp->getLocalConf('portal') // {};
Condition50%1/2
$tmp->getLocalConf("portal") // {}
l!l
10
MC/DC0%0/1
$tmp->getLocalConf("portal") // {}
$tmp->getLocalConf("portal")
Truth table50%1/2
A
A= $tmp->getLocalConf("portal")
Aresult
11
00
41 1 my $conf = $tmp->getConf();
42 1 ▶ unless ( ref($conf) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $conf) 0 1 1
43 0 $self->error(
44 "Unable to load configuration: $Lemonldap::NG::Common::Conf::msg");
45 0 return 0;
46 }
47 1 $lconf->{$_} = $args->{$_} foreach ( keys %$args );
48 1 $conf->{$_} = $lconf->{$_} foreach ( keys %$lconf );
49 1 ▶ $self->SUPER::init($lconf) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init($lconf) or return 0 1 0 1
50 1 ▶ $self->cdc_name( $conf->{samlCommonDomainCookieName} || '_saml_idp' );
Condition50%1/2
$conf->{'samlCommonDomainCookieName'} || '_saml_idp'
l!l
01
MC/DC0%0/1
$conf->{'samlCommonDomainCookieName'} || '_saml_idp'
$conf->{'samlCommonDomainCookieName'}
Truth table50%1/2
A
A= $conf->{'samlCommonDomainCookieName'}
Aresult
11
00
51 1 $self->cdc_domain( $conf->{samlCommonDomainCookieDomain} );
52 1 $self->logger->debug( "[CDC] Cookie name: " . $self->cdc_name );
53 1 ▶ $self->logger->debug( "[CDC] Domain name: "
Branch: 1 branch
BranchTrueFalseTotal
$self->cdc_domain ? : 1 0 1
54 . ( $self->cdc_domain ? $self->cdc_domain : '<host name>' ) );
55  
56 1 foreach (qw(httpOnly cookieExpiration oldStyleUrl)) {
57 3 $self->$_( $conf->{$_} );
58 }
59  
60 # Build the trusted domains regexp used to control redirection URLs
61 # (see _isTrustedRedirectUrl()). This prevents the CDC endpoint from being
62 # used as an open redirector (CWE-601).
63 1 $self->_buildTrustedDomainsRe($conf);
64  
65 1 return 1;
66 }
67  
68 ## @method void _buildTrustedDomainsRe($conf)
69 # Build the regexp of allowed redirection hosts from the configuration.
70 # @param $conf full configuration hashref
71 1 ▶sub _buildTrustedDomainsRe {
Subroutine _buildTrustedDomainsRe: called CC=17 SCAR=31.8
72 1 my ( $self, $conf ) = @_;
73  
74 # Wildcard trustedDomains: trust any http(s) destination
75 1 ▶ if ( $conf->{trustedDomains}
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{"trustedDomains"} and $conf->{"trustedDomains"} =~ /^\s*\*\s*$/) 0 1 1
Condition33%1/3
$conf->{"trustedDomains"} and $conf->{"trustedDomains"} =~ /^\s*\*\s*$/
!ll&&!rl&&r
010
MC/DC0%0/2
$conf->{"trustedDomains"} and $conf->{"trustedDomains"} =~ /^\s*\*\s*$/
$conf->{"trustedDomains"} $conf->{"trustedDomains"} =~ /^\s*\*\s*$/
Truth table33%1/3
A and B
A= $conf->{"trustedDomains"}B= $conf->{"trustedDomains"} =~ /^\s*\*\s*$/
ABresult
0X0
100
111
76 and $conf->{trustedDomains} =~ /^\s*\*\s*$/ )
77 {
78 0 $self->trustedDomainsRe(qr#^https?://#);
79 0 return;
80 }
81  
82 1 my $re = Regexp::Assemble->new();
83 1 my $count = 0;
84  
85 # 1. Hosts of every registered SAML federation member (SP and IdP).
86 # We extract them from the raw metadata (entityID and endpoint
87 # Location URLs) with a simple scan, avoiding a full XML/Lasso parse.
88 1 foreach my $key (qw(samlSPMetaDataXML samlIDPMetaDataXML)) {
89 2 my $branch = $conf->{$key};
90 2 ▶ next unless ref($branch) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $branch eq "HASH" 0 2 2
91 2 foreach my $entry ( values %$branch ) {
92 my $xml =
93 2 ▶ ref($entry) eq 'HASH' ? ( $entry->{$key} // '' ) : ( $entry // '' );
Branch: 1 branch
BranchTrueFalseTotal
ref $entry eq 'HASH' ? : 2 0 2
Condition25%1/4
$entry->{$key} // ""
l!l
20
$entry // ""
l!l
00
MC/DC0%0/1
$entry->{$key} // ""
$entry->{$key}
MC/DC0%0/1
$entry // ""
$entry
Truth table50%1/2
A
A= $entry->{$key}
Aresult
11
00
Truth table0%0/2
A
A= $entry
Aresult
11
00
94 2 ▶ next unless $xml;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml 0 2 2
95 2 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ m[\b(?:Location|entityID)\s*=\s*"https?://([^"/:]+)]gi) 19 2 21
96 $xml =~ /\b(?:Location|entityID)\s*=\s*"https?:\/\/([^"\/:]+)/gi )
97 {
98 19 $re->add( quotemeta($1) );
99 19 $count++;
100 }
101 }
102 }
103  
104 # 2. Explicitly trusted domains
105 1 ▶ if ( my $td = $conf->{trustedDomains} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $td = $conf->{"trustedDomains"}) 1 0 1
106 1 $td =~ s/^\s*(.*?)\s*$/$1/;
107 1 foreach ( split( /\s+/, $td ) ) {
108 1 ▶ next unless ($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 1 1
109 1 s#^\.#([^/]+\.)?#;
110 1 s/\./\\./g;
111 1 s/\*\\\./(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9]\\.)*/g;
112 1 $re->add($_);
113 1 $count++;
114 }
115 }
116  
117 # 3. The portal hosting this CDC
118 1 ▶ if ( my $portal = $conf->{portal} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $portal = $conf->{"portal"}) 1 0 1
119 1 $portal =~ s#https?://([^/]*).*$#$1#;
120 1 ▶ if ($portal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($portal) 1 0 1
121 1 $re->add( quotemeta($portal) );
122 1 $count++;
123 }
124 }
125  
126 1 ▶ unless ($count) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($count) 0 1 1
127  
128 # Nothing is trusted: only local relative paths will be allowed
129 0 $self->trustedDomainsRe(undef);
130 0 return;
131 }
132  
133 1 my $tmp = '^https?://' . $re->as_string . '(?::\d+)?(?:/|$)';
134 1 $self->trustedDomainsRe(qr/$tmp/);
135 1 return;
136 }
137  
138 ## @method boolean _isTrustedRedirectUrl($url)
139 # @param $url decoded (and CRLF-stripped) destination URL
140 # @return boolean
141 11 ▶sub _isTrustedRedirectUrl {
Subroutine _isTrustedRedirectUrl: called CC=5 SCAR=17.2
142 11 my ( $self, $url ) = @_;
143 11 ▶ return 0 unless defined $url and length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 11 11
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $url and length $url
defined $url length $url
Truth table33%1/3
A and B
A= defined $urlB= length $url
ABresult
0X0
100
111
144 11 my $re = $self->trustedDomainsRe;
145 11 ▶ return 0 unless $re;
Branch: 1 branch
BranchTrueFalseTotal
unless $re 0 11 11
146 11 ▶ return $url =~ $re ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /$re/ ? : 5 6 11
147 }
148  
149 ## @method int process()
150 # Main method to process CDC requests
151 # @return portal error code
152 11 ▶sub handler {
Subroutine handler: called CC=17 SCAR=44.6
Pod undocumented
153 11 my ( $self, $req ) = @_;
154 11 my $cdc_idp = "";
155 11 my $cdc_cookie = "";
156  
157 # Default values
158 11 ▶ my $cdc_domain = $self->cdc_domain || $req->hostname;
Condition33%1/3
$self->cdc_domain || $req->hostname
l!l&&r!l&&!r
1100
MC/DC0%0/2
$self->cdc_domain || $req->hostname
$self->cdc_domain $req->hostname
Truth table33%1/3
A || B
A= $self->cdc_domainB= $req->hostname
ABresult
1X1
011
000
159  
160 # Request parameter
161 11 ▶ my $action = $req->param('action') || ""; # What we do
Condition50%1/2
$req->param('action') || ''
l!l
011
MC/DC0%0/1
$req->param('action') || ''
$req->param('action')
Truth table50%1/2
A
A= $req->param('action')
Aresult
11
00
162 11 my $idp = $req->param('idp'); # IDP ID in write mode
163  
164 # Get cookie
165 11 $cdc_cookie = $req->cookies->{ $self->cdc_name };
166  
167 11 ▶ if ($cdc_cookie) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cdc_cookie) 0 11 11
168 0 $self->logger->debug("[CDC] Cookie found with value $cdc_cookie");
169 }
170  
171 # Write request
172 # Called in an iFrame
173 # Get or build common domain cookie
174 # Append IDP to common domain cookie
175 11 ▶ if ( $action eq 'write' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'write') { } 0 11 11
elsif ($action eq 'read') { } 0 11 11
176  
177 0 $self->logger->debug("[CDC] Write request detected");
178  
179 # Check IDP value
180 0 ▶ unless ($idp) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($idp) 0 0 0
181 0 return $self->sendError( $req, "[CDC] No IDP given", 400 );
182 }
183  
184 # Add IDP value
185 0 $self->logger->debug("[CDC] Will add IDP $idp to IDP list");
186  
187 0 my $encoded_idp = encode_base64( $idp, '' );
188  
189 # Remove IDP value if already present
190 0 ▶ $cdc_cookie =~ s/$encoded_idp(\s+)?//g if ($cdc_cookie);
Branch: 1 branch
BranchTrueFalseTotal
if $cdc_cookie 0 0 0
191  
192 # Add a space separator
193 0 ▶ $cdc_cookie .= ( $cdc_cookie ? " " : "" );
Branch: 1 branch
BranchTrueFalseTotal
$cdc_cookie ? : 0 0 0
194 0 $cdc_cookie .= $encoded_idp;
195  
196 0 $self->logger->debug(
197 "[CDC] Build cookie $self->{cdc_name} with value $cdc_cookie");
198  
199 # Build cookie
200 0 push @{ $req->respHeaders },
201 'Set-Cookie' => $self->cdc_name . '='
202 . $cdc_cookie
203 . "; domain=$cdc_domain; secure=1";
204 }
205  
206 # Read request
207 # Get last IDP from domain cookie
208 # Return on SP with idp as parameter
209  
210 elsif ( $action eq 'read' ) {
211  
212 0 $self->logger->debug("[CDC] Read request detected");
213  
214 # Get last IDP from cookie
215 0 ▶ if ($cdc_cookie) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cdc_cookie) { } 0 0 0
216 0 $cdc_idp = decode_base64( ( split /\s+/, $cdc_cookie )[-1] );
217 0 $self->logger->debug("[CDC] Get value $cdc_idp");
218 }
219 else {
220 0 $self->logger->debug("[CDC] No cookie, set a default value");
221 0 $cdc_idp = 'notfound';
222 }
223 }
224  
225 # Redirect if needed
226 11 ▶ if ( my $url = $req->param('url') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $url = $req->param("url")) 11 0 11
227  
228 # Decode URL
229 11 ▶ if ( $url =~ m#[^A-Za-z0-9\+/=]# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[[^A-Za-z0-9\+/=]]) 0 11 11
230 0 return $self->sendError( $req, "Bad URL", 400 );
231 }
232 11 my $urldc = decode_base64($url);
233  
234 # Strip CR/LF to prevent HTTP response splitting in the Location header
235 11 $urldc =~ s/[\r\n]//g;
236  
237 # Control URL origin to avoid being used as an open redirector
238 # (CWE-601). Only local paths and trusted hosts are allowed.
239 11 ▶ unless ( $self->_isTrustedRedirectUrl($urldc) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_isTrustedRedirectUrl($urldc)) 6 5 11
240 6 $self->logger->error(
241 "[CDC] Refusing redirection to untrusted URL: $urldc");
242 6 return $self->sendError( $req, "Bad URL", 400 );
243 }
244  
245 # Add CDC IDP in return URL if needed
246 # olStyleUrl can be set to 1 to use & instead of ;
247 $urldc .= (
248 $cdc_idp
249 ? ( (
250 $urldc =~ /\?/
251 5 ▶ ? ( $self->{oldStyleUrl} ? '&' : ';' )
Branch: 3 branches
BranchTrueFalseTotal
$cdc_idp ? : 0 5 5
$urldc =~ /\?/ ? : 0 0 0
$self->{'oldStyleUrl'} ? : 0 0 0
252 : '?'
253 )
254 . build_urlencoded( idp => $cdc_idp )
255 )
256 : ''
257 );
258  
259 # Redirect
260 5 return $self->sendRedirection( $req, URI->new($urldc)->as_string, );
261 }
262  
263 0 ▶ if ($cdc_cookie) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cdc_cookie) 0 0 0
264  
265 # Parse cookie to display it if not redirected
266 0 my @cdc_values =
267 map( decode_base64($_), ( split( /\s+/, $cdc_cookie ) ) );
268 0 $self->{cdc_values} = \@cdc_values;
269 }
270  
271 0 return $self->sendTextResponse( $req, 'OK', type => 'text/plain' );
272 }
273  
274 1 1;
275  
276 __END__
« blib/lib/Lemonldap/NG/Portal/Auth/REST.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/WebAuthn.pm »