diff --git a/lib/WebGUI/Session/Url.pm b/lib/WebGUI/Session/Url.pm index 770fc08a8..a963e38f7 100644 --- a/lib/WebGUI/Session/Url.pm +++ b/lib/WebGUI/Session/Url.pm @@ -405,9 +405,8 @@ sub makeCompliant { $url =~ s{\s+$}{}; # remove trailing whitespace $url =~ s{^/+}{}; # remove leading slashes $url =~ s{/+$}{}; # remove trailing slashes - $url =~ s{[^\w/:._-]+}{-}g; # replace anything aside from word or other allowed characters with dashes - $url =~ s{//+}{/}g; # remove double slashes - $url =~ s{--+}{-}g; # remove double dashes + $url =~ s{[^\w/:.-]+}{-}g; # replace anything aside from word or other allowed characters with dashes + $url =~ tr{/-}{/-}s; # replace multiple slashes and dashes with singles. return $url; } @@ -530,16 +529,14 @@ The string to urlize. =cut sub urlize { - my $self = shift; - my ($value); - $value = lc(shift); #lower cases whole string + my $self = shift; + my $value = lc(shift); #lower cases whole string $value = $self->makeCompliant($value); # remove /./ or /../ $value =~ s{(^|/)(?:\.\.?/)*}{$1}g; # remove trailing slashes - $value =~ s/\/$//; return $value; } diff --git a/t/Session/Url.t b/t/Session/Url.t index 8c5d1d838..ee863ce76 100644 --- a/t/Session/Url.t +++ b/t/Session/Url.t @@ -52,7 +52,7 @@ my @getRefererUrlTests = ( use Test::More; use Test::MockObject::Extends; -plan tests => 85 + scalar(@getRefererUrlTests); +plan tests => 86 + scalar(@getRefererUrlTests); my $session = WebGUI::Test->session; @@ -172,10 +172,25 @@ $session->url->setSiteURL('http://'.$sitename); is( $session->url->getSiteURL, 'http://'.$sitename, 'restore config setting'); $session->setting->set('hostToUse', $setting_hostToUse); +####################################### +# +# makeCompliant +# +####################################### + $url = 'level1 /level2/level3 '; $url2 = 'level1-/level2/level3'; - -is( $session->url->makeCompliant($url), $url2, 'language specific URL compliance'); +is $session->url->makeCompliant($url), $url2, 'internal spaces encoded, trailing spaces removed'; +is $session->url->makeCompliant('home/'), 'home', '... trailing slashes removed'; +is $session->url->makeCompliant('home is where the heart is'), 'home-is-where-the-heart-is', '... makeCompliant translates spaces to dashes'; +is $session->url->makeCompliant('/home'), 'home', '... removes initial slash'; +is $session->url->makeCompliant('home -- here'), 'home-here', 'multiple dashes collapsed'; +is $session->url->makeCompliant('home!@#$%^&*here'), 'home-here', 'non-word characters collapsed to single dash'; +is $session->url->makeCompliant("home\x{2267}here"), 'home-here', 'non-word international characters removed'; +is $session->url->makeCompliant("home\x{1EE9}here"), "home\x{1EE9}here", 'word international characters not removed'; +my $character = "\x{00C0}"; +utf8::upgrade($character); +is( $session->url->makeCompliant($character), $character, 'utf8 allowed in URLs'); ####################################### @@ -340,17 +355,10 @@ is($unEscapedString, '10% is enough;', 'unescape method'); ####################################### is($session->url->urlize('HOME/PATH1'), 'home/path1', 'urlize: urls are lower cased'); -is($session->url->urlize('home/'), 'home', '... trailing slashes removed'); -is($session->url->urlize('home is where the heart is'), 'home-is-where-the-heart-is', '... makeCompliant translates spaces to dashes'); -is($session->url->urlize('/home'), 'home', '... removes initial slash'); -is($session->url->urlize('home/../out-of-bounds'), 'home/out-of-bounds', '... removes ../'); -is($session->url->urlize('home/./here'), 'home/here', '... removes ./'); -is($session->url->urlize('home/../../out-of-bounds'), 'home/out-of-bounds', '... removes multiple ../'); -is($session->url->urlize('home/././here'), 'home/here', '... removes multiple ./'); -is($session->url->urlize('home -- here'), 'home-here', 'multiple dashes collapsed'); -is($session->url->urlize('home!@#$%^&*here'), 'home-here', 'non-word characters collapsed to single dash'); -is($session->url->urlize("home\x{2267}here"), 'home-here', 'non-word international characters removed'); -is($session->url->urlize("home\x{1EE9}here"), "home\x{1EE9}here", 'word international characters not removed'); +is $session->url->urlize('home/../out-of-bounds'), 'home/out-of-bounds', '... removes ../'; +is $session->url->urlize('home/./here'), 'home/here', '... removes ./'; +is $session->url->urlize('home/../../out-of-bounds'), 'home/out-of-bounds', '... removes multiple ../'; +is $session->url->urlize('home/././here'), 'home/here', '... removes multiple ./'; ####################################### #