From a25f2cf5f7a619911a2bb2e515f628f0c2e91aee Mon Sep 17 00:00:00 2001 From: Kirill Solomko Date: Wed, 21 Jan 2026 14:17:23 +0100 Subject: [PATCH] MT#64295 fix /api/peeringservers via_route validation * NGCP::Form::Peering::Server rework via_route URI check from a comma separated list to a single value as well as improve value transformation and validation logic * /api/peeringservers add via_route value transformation as pre_proccess_form_resource() Change-Id: I742a40534a4861dd30e0ccbc6684c02d2386f642 (cherry picked from commit 638b2f20aefbf2b252b38260f82eb01098108e99) (cherry picked from commit 02124f51af7934c52aa4b43248bf9efc9afae829) --- .../Panel/Controller/API/PeeringServers.pm | 4 ++ lib/NGCP/Panel/Form/Peering/Server.pm | 44 ++++++++++++------- lib/NGCP/Panel/Role/API/PeeringServers.pm | 21 +++++++++ 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/lib/NGCP/Panel/Controller/API/PeeringServers.pm b/lib/NGCP/Panel/Controller/API/PeeringServers.pm index 4b4caff85f..615b38948d 100644 --- a/lib/NGCP/Panel/Controller/API/PeeringServers.pm +++ b/lib/NGCP/Panel/Controller/API/PeeringServers.pm @@ -132,9 +132,13 @@ sub POST :Allow { media_type => 'application/json', ); last unless $resource; + my $item; my $form = $self->get_form($c); + + $self->pre_process_form_resource($c, undef, undef, $resource, $form, undef); + last unless $self->validate_form( c => $c, resource => $resource, diff --git a/lib/NGCP/Panel/Form/Peering/Server.pm b/lib/NGCP/Panel/Form/Peering/Server.pm index 24b8b060ac..787e16857a 100644 --- a/lib/NGCP/Panel/Form/Peering/Server.pm +++ b/lib/NGCP/Panel/Form/Peering/Server.pm @@ -56,6 +56,9 @@ has_field 'via_route' => ( type => '+NGCP::Panel::Field::Select', label => 'Via Route', options_method => \&build_via_routes, + apply => [ + { transform => \&transform_via_route }, + ], translate => 0, ); @@ -127,28 +130,35 @@ sub validate_name { } } -sub validate_via_route { +sub transform_via_route { my ($self, $field) = @_; - my @hops = split /,/, $field->value; - my $err = 0; - foreach my $hop(@hops) { - $hop =~ s/^\s*([^\s]+)\s*$/$1/; - # TODO: is there a proper sip uri check? - unless($hop =~ /^$/) { - $err = 1; last; - } + my $uri = $field->value; + + $uri =~ s/^\s*([^\s]+)\s*$/$1/g; + + unless ($uri =~ /^'; } - if($err) { - $field->add_error("Invalid SIP URI, must be (comma-separated) SIP URI(s) in form sip:ip:port"); + + unless ($uri =~ /;lr>$/) { + $uri =~ s/>$//; + $uri = $uri . ';lr>'; } + + return $uri; } -#sub validate { -# my ($self) = @_; -# my $c = $self->ctx; -# return unless $c; -# my $model = $c-> -#} + +sub validate_via_route { + my ($self, $field) = @_; + + my $uri = $field->value; + + unless ($field->value =~ /^?$/) { + $field->add_error("Invalid SIP URI, must be a valid SIP URI in the form of sip:ip:port"); + } +} + 1; __END__ diff --git a/lib/NGCP/Panel/Role/API/PeeringServers.pm b/lib/NGCP/Panel/Role/API/PeeringServers.pm index 4ac5554278..5892d57cfb 100644 --- a/lib/NGCP/Panel/Role/API/PeeringServers.pm +++ b/lib/NGCP/Panel/Role/API/PeeringServers.pm @@ -65,9 +65,30 @@ sub item_by_id { return $item_rs->find($id); } +sub pre_process_form_resource { + my($self,$c, $item, $old_resource, $resource, $form, $process_extras) = @_; + + my $uri = $resource->{via_route} // return; + + $uri =~ s/^\s*([^\s]+)\s*$/$1/g; + + unless ($uri =~ /^'; + } + + unless ($uri =~ /;lr>$/) { + $uri =~ s/>$//; + $uri = $uri . ';lr>'; + } + + $resource->{via_route} = $uri; +} + sub update_item { my ($self, $c, $item, $old_resource, $resource, $form) = @_; + $self->pre_process_form_resource($c, $item, $old_resource, $resource, $form, undef); + $form //= $self->get_form($c); return unless $self->validate_form( c => $c,