lemonldap-ng/lemonldap-ng-portal/t/03-XSS-protection.t

185 lines
4.8 KiB
Perl
Raw Permalink Normal View History

2016-05-23 13:53:09 +02:00
use Test::More;
2009-02-17 20:37:06 +01:00
use strict;
2016-05-23 13:53:09 +02:00
use IO::String;
2016-05-23 13:53:09 +02:00
require 't/test-lib.pm';
2009-02-17 20:37:06 +01:00
2019-02-07 09:27:56 +01:00
my $client = LLNG::Manager::Test->new( {
2016-11-14 13:34:46 +01:00
ini => {
logLevel => 'error',
useSafeJail => 1,
trustedDomains => 'example3.com *.example2.com'
}
2016-05-23 18:55:23 +02:00
}
);
2009-02-17 20:37:06 +01:00
2016-05-23 13:53:09 +02:00
my @tests = (
2009-02-17 20:37:06 +01:00
2016-05-23 13:53:09 +02:00
# 1 No redirection
'' => 0, 'Empty',
2009-02-17 20:37:06 +01:00
2016-05-23 13:53:09 +02:00
# 2 http://test1.example.com/
'aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tLw==' => 1, 'Protected virtual host',
2009-02-19 09:06:59 +01:00
2016-05-23 13:53:09 +02:00
# 3 http://test1.example.com
'aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29t' => 1, 'Missing / in URL',
2009-02-17 20:37:06 +01:00
2016-05-23 13:53:09 +02:00
# 4 http://test1.example.com:8000/test
'aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tOjgwMDAvdGVzdA==' => 1,
'Non default port',
# 5 http://test1.example.com:8000/
'aHR0cDovL3Rlc3QxLmV4YW1wbGUuY29tOjgwMDAv' => 1,
2009-02-19 09:06:59 +01:00
'Non default port with missing /',
2009-02-17 20:37:06 +01:00
2016-05-23 13:53:09 +02:00
# 6 http://t.example2.com/test
'aHR0cDovL3QuZXhhbXBsZTIuY29tL3Rlc3Q=' => 1,
'Undeclared virtual host in trusted domain',
2016-05-23 13:53:09 +02:00
# 7 http://testexample2.com/
'aHR0cDovL3Rlc3RleGFtcGxlMi5jb20vCg==' => 0,
'Undeclared virtual host in untrusted domain'
2012-07-26 04:47:27 +02:00
. ' (looks like a trusted domain, but is not)',
2016-05-23 13:53:09 +02:00
# 8 http://test.example3.com/
'aHR0cDovL3Rlc3QuZXhhbXBsZTMuY29tLwo=' => 0,
2012-07-26 04:47:27 +02:00
'Undeclared virtual host in untrusted domain (domain name'
. ' "example3.com" is trusted, but domain "*.example3.com" not)',
2016-05-23 13:53:09 +02:00
# 9 http://example3.com/
'aHR0cDovL2V4YW1wbGUzLmNvbS8K' => 1,
'Undeclared virtual host with trusted domain name',
2016-05-23 13:53:09 +02:00
# 10 http://t.example.com/test
'aHR0cDovL3QuZXhhbXBsZS5jb20vdGVzdA==' => 0,
'Undeclared virtual host in (untrusted) protected domain',
2016-05-23 13:53:09 +02:00
# 11
'http://test.com/' => 0, 'Non base64 encoded characters',
2009-02-17 20:37:06 +01:00
2016-05-23 13:53:09 +02:00
# 12 http://test.example.com:8000V
'aHR0cDovL3Rlc3QuZXhhbXBsZS5jb206ODAwMFY=' => 0,
2009-02-19 09:06:59 +01:00
'Non number in port',
2009-02-17 20:37:06 +01:00
2016-05-23 13:53:09 +02:00
# 13 http://t.ex.com/test
'aHR0cDovL3QuZXguY29tL3Rlc3Q=' => 0,
'Undeclared virtual host in untrusted domain',
2009-02-17 20:37:06 +01:00
2016-05-23 13:53:09 +02:00
# 14 http://test.example.com/%00
'aHR0cDovL3Rlc3QuZXhhbXBsZS5jb20vJTAw' => 0, 'Base64 encoded \0',
2009-02-19 09:06:59 +01:00
2016-05-23 13:53:09 +02:00
# 15 http://test.example.com/test\0
'aHR0cDovL3Rlc3QuZXhhbXBsZS5jb20vdGVzdAA=' => 0,
2009-02-19 09:06:59 +01:00
'Base64 and url encoded \0',
2016-05-23 13:53:09 +02:00
# 16
'XX%00' => 0, 'Non base64 encoded \0 ',
2009-02-19 09:06:59 +01:00
2016-05-23 13:53:09 +02:00
# 17 http://test.example.com/test?<script>alert()</script>
2009-02-19 09:06:59 +01:00
'aHR0cDovL3Rlc3QuZXhhbXBsZS5jb20vdGVzdD88c2NyaXB0PmFsZXJ0KCk8L3NjcmlwdD4='
2016-05-23 13:53:09 +02:00
=> 0,
2009-02-19 09:06:59 +01:00
'base64 encoded HTML tags',
# LOGOUT TESTS
'LOGOUT',
2016-05-23 13:53:09 +02:00
# 18 url=http://www.toto.com/, bad referer
'aHR0cDovL3d3dy50b3RvLmNvbS8=',
2016-05-23 13:53:09 +02:00
'http://bad.com/' => 0,
'Logout required by bad site',
2016-05-23 13:53:09 +02:00
# 19 url=http://www.toto.com/, good referer
'aHR0cDovL3d3dy50b3RvLmNvbS8=',
2016-05-23 18:55:23 +02:00
'http://test1.example.com/' => 1,
'Logout required by good site',
2016-05-23 13:53:09 +02:00
# 20 url=http://www?<script>, good referer
'aHR0cDovL3d3dz88c2NyaXB0Pg==',
2016-05-23 18:55:23 +02:00
'http://test1.example.com/' => 0,
'script with logout',
# 21 url=http://www.toto.com/, no referer
'aHR0cDovL3d3dy50b3RvLmNvbS8=',
'' => 1,
'Logout required by good site, empty referer',
2009-02-17 20:37:06 +01:00
);
2016-05-23 13:53:09 +02:00
my $res;
2009-02-17 20:37:06 +01:00
ok(
2016-11-14 13:34:46 +01:00
$res = $client->_post(
2016-05-30 22:20:50 +02:00
'/',
2016-05-23 13:53:09 +02:00
IO::String->new('user=dwho&password=dwho'),
2016-05-30 22:20:50 +02:00
length => 23
2009-02-17 20:37:06 +01:00
),
2016-05-23 13:53:09 +02:00
'Auth query'
2009-02-17 20:37:06 +01:00
);
2016-12-23 07:41:03 +01:00
count(1);
expectOK($res);
my $id = expectCookie($res);
2016-05-23 13:53:09 +02:00
while ( defined( my $url = shift(@tests) ) ) {
last if ( $url eq 'LOGOUT' );
my $redir = shift @tests;
my $detail = shift @tests;
ok(
2016-11-14 13:34:46 +01:00
$res = $client->_get(
2016-05-23 13:53:09 +02:00
'/',
query => "url=$url",
cookie => "lemonldap=$id",
accept => 'text/html'
),
$detail
);
ok( ( $res->[0] == ( $redir ? 302 : 200 ) ),
( $redir ? 'Get redirection' : 'Redirection dropped' ) )
or explain( $res->[0], ( $redir ? 302 : 200 ) );
count(2);
}
2009-02-17 20:37:06 +01:00
2016-05-23 18:55:23 +02:00
while ( defined( my $url = shift(@tests) ) ) {
my $referer = shift @tests;
my $redir = shift @tests;
my $detail = shift @tests;
ok(
2016-11-14 13:34:46 +01:00
$res = $client->_get(
2016-05-23 18:55:23 +02:00
'/',
query => "url=$url&logout=1",
cookie => "lemonldap=$id",
accept => 'text/html',
referer => $referer,
),
$detail
);
ok( ( $res->[0] == ( $redir ? 302 : 200 ) ),
( $redir ? 'Get redirection' : 'Redirection dropped' ) )
or explain( $res->[0], ( $redir ? 302 : 200 ) );
ok(
2016-11-14 13:34:46 +01:00
$res = $client->_post(
2016-05-30 22:20:50 +02:00
'/',
2016-05-23 18:55:23 +02:00
IO::String->new('user=dwho&password=dwho'),
2016-05-30 22:20:50 +02:00
length => 23
2016-05-23 18:55:23 +02:00
),
'Auth query'
);
2016-12-23 07:41:03 +01:00
expectOK($res);
$id = expectCookie($res);
count(3);
2016-05-23 18:55:23 +02:00
}
2016-05-23 13:53:09 +02:00
clean_sessions();
2009-02-17 20:37:06 +01:00
2016-05-23 13:53:09 +02:00
done_testing( count() );
__END__
# LOGOUT CASES
$logout = 1;
while ( defined( $url = shift(@h) ) ) {
my $referer = shift @h;
$result = shift @h;
my $text = shift @h;
$ENV{HTTP_REFERER} = $referer;
ok( $p->controlUrlOrigin() == $result, $text );
}