From a35314d0aa9a7da5c280fe42166e08bf819d8768 Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Fri, 20 Mar 2015 14:50:20 +0100 Subject: [PATCH] MT#11827 validate sip uri Change-Id: I19281ec28eb6708e824963bef8a4e0523499add8 --- lib/NGCP/Panel/Field/URI.pm | 24 +++++---- t/lib/Test/WebDriver/Sipwise.pm | 18 ++++--- t/unit-formhandler-field-uri.t | 88 +++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 t/unit-formhandler-field-uri.t diff --git a/lib/NGCP/Panel/Field/URI.pm b/lib/NGCP/Panel/Field/URI.pm index 5209a12912..b0d9e35721 100644 --- a/lib/NGCP/Panel/Field/URI.pm +++ b/lib/NGCP/Panel/Field/URI.pm @@ -13,8 +13,8 @@ sub get_class_messages { my $self = shift; return { %{ $self->next::method }, - %$class_messages, - } + %{ $class_messages }, + }; } apply( @@ -22,7 +22,7 @@ apply( { transform => sub { lc($_[0]); - } + }, }, { transform => sub { @@ -40,19 +40,25 @@ apply( if($c->user->roles eq "subscriberadmin" || $c->user->roles eq "subscriber") { $user = NGCP::Panel::Utils::Subscriber::apply_rewrite( - c => $c, subscriber => $sub, number => $user, direction => 'callee_in' + c => $c, subscriber => $sub, number => $user, direction => 'callee_in', ); } $uri = 'sip:' . $user . '@' . $domain; return $uri; - } + }, }, { check => sub { my ( $value, $field ) = @_; my ($user, $domain) = split(/\@/, $value); - my $checked = $value if $user && $domain; # TODO: proper check + my $checked; + if ($user && $domain) { + if ( $user =~ m/^(sip:)?[a-zA-Z0-9\+\-\.]*$/ && + $domain =~ m/^[^;\?:]*$/ ) { + $checked = $value; + } + } $field->value($checked) if $checked; }, @@ -60,8 +66,8 @@ apply( my ( $value, $field ) = @_; return $field->get_message('uri_format'); }, - } - ] + }, + ], ); has '+deflate_method' => ( default => sub { \&uri_deflate } ); @@ -80,7 +86,7 @@ sub uri_deflate { my ($user, $domain) = split(/\@/, $v); if($c->user->roles eq "subscriberadmin" || $c->user->roles eq "subscriber") { $user = NGCP::Panel::Utils::Subscriber::apply_rewrite( - c => $c, subscriber => $sub, number => $user, direction => 'caller_out' + c => $c, subscriber => $sub, number => $user, direction => 'caller_out', ); } if($domain eq $sub->domain->domain) { diff --git a/t/lib/Test/WebDriver/Sipwise.pm b/t/lib/Test/WebDriver/Sipwise.pm index 7bf0bf362c..dce9df7bdf 100644 --- a/t/lib/Test/WebDriver/Sipwise.pm +++ b/t/lib/Test/WebDriver/Sipwise.pm @@ -1,5 +1,8 @@ package Test::WebDriver::Sipwise; -use Sipwise::Base; +use warnings; +use strict; +use Moo; +use MooseX::Method::Signatures; extends 'Test::WebDriver'; method find(Str $scheme, Str $query) { @@ -30,12 +33,11 @@ method findtext(Str $text, Any $ignore) { method save_screenshot() { use MIME::Base64; - local *FH; - open(FH,'>','screenshot.png'); - binmode FH; + open(my $FH,'>','screenshot.png'); + binmode $FH; my $png_base64 = $self->screenshot(); - print FH decode_base64($png_base64); - close FH; + print $FH decode_base64($png_base64); + close $FH; } method fill_element(ArrayRef $options, Any $ignore) { @@ -51,5 +53,7 @@ method fill_element(ArrayRef $options, Any $ignore) { sub browser_name_in { my ($self, @names) = @_; my $browser_name = $self->get_capabilities->{browserName}; - return $browser_name ~~ @names; + return scalar grep {/^$browser_name$/} @names; } + +1; \ No newline at end of file diff --git a/t/unit-formhandler-field-uri.t b/t/unit-formhandler-field-uri.t new file mode 100644 index 0000000000..3226c27662 --- /dev/null +++ b/t/unit-formhandler-field-uri.t @@ -0,0 +1,88 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use NGCP::Panel::Form::SubscriberCFSimple; + +my $configs = [ + { + works => 1, + config => { + destination => { + destination => 'uri', + uri => { destination => 'sip:foo@bar.com' }, + }, + }, + }, + { + works => 1, + config => { + destination => { + destination => 'uri', + uri => { destination => 'foo@bar.com' }, + }, + }, + }, + { + works => 1, + config => { + destination => { + destination => 'uri', + uri => { destination => 'alice@10.0.0.1' }, + }, + }, + }, + { + works => 1, + config => { + destination => { + destination => 'uri', + uri => { destination => '12345@a' }, # we set a domain here, because on tests it cannot be automatically deduced from stash + }, + }, + }, + { + works => 1, + config => { + destination => { + destination => 'uri', + uri => { destination => '+12345@a' }, + }, + }, + }, + { + works => 0, + config => { + destination => { + destination => 'uri', + uri => { destination => '12345[678]@a' }, + }, + }, + }, + { + works => 0, + config => { + destination => { + destination => 'uri', + uri => { destination => '+49(0)123456789@a' }, + }, + }, + }, +]; + +for my $conf (@{$configs}) { + my $form = NGCP::Panel::Form::SubscriberCFSimple->new; + $form->process( + posted => 1, + params => $conf->{config}, + ); + my $uri = $form->value->{destination}{uri}{destination}; + if ($conf->{works}) { + ok($form->validated, "Should validate: $uri"); + } else { + ok(!$form->validated, "Should not validate: $uri"); + } +} + +done_testing();