From ebc98070d6b483b636a6060643020f9731d9e1d3 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Fri, 8 Sep 2023 14:33:50 +0000 Subject: [PATCH] Bug 34587: Fix url validation We need to check whether the url ends in a trailing "/" and also whether it ends in "/reports" for a SUSHI harvest. Two new functions are added to manage this and prevent failed harvests. report_type is also now lowercase in the url and the dates are truncated to YYYY-MM format Signed-off-by: Jessica Zairo Signed-off-by: Michaela Sieber Signed-off-by: Nick Clemens Signed-off-by: Tomas Cohen Arazi --- Koha/ERM/EUsage/UsageDataProvider.pm | 58 ++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/Koha/ERM/EUsage/UsageDataProvider.pm b/Koha/ERM/EUsage/UsageDataProvider.pm index 00d3e8f7e6..1c7ae1443b 100644 --- a/Koha/ERM/EUsage/UsageDataProvider.pm +++ b/Koha/ERM/EUsage/UsageDataProvider.pm @@ -266,7 +266,7 @@ Tests the connection of the harvester to the SUSHI service and returns any alert sub test_connection { my ($self) = @_; - my $url = $self->service_url; + my $url = _validate_url($self->service_url, 'status'); $url .= 'status'; $url .= '?customer_id=' . $self->customer_id; $url .= '&requestor_id=' . $self->requestor_id if $self->requestor_id; @@ -383,17 +383,59 @@ sub _build_url_query { $self->erm_usage_data_provider_id; } - # FIXME: service_url needs to end in 'reports/' - # below concat will result in a badly formed URL otherwise - # Either validate this on UI form, here, or both - my $url = $self->service_url; + my $url = _validate_url($self->service_url, 'harvest'); - $url .= $self->{report_type}; + $url .= lc $self->{report_type}; $url .= '?customer_id=' . $self->customer_id; $url .= '&requestor_id=' . $self->requestor_id if $self->requestor_id; $url .= '&api_key=' . $self->api_key if $self->api_key; - $url .= '&begin_date=' . $self->{begin_date} if $self->{begin_date}; - $url .= '&end_date=' . $self->{end_date} if $self->{end_date}; + $url .= '&begin_date=' . substr $self->{begin_date}, 0, 7 if $self->{begin_date}; + $url .= '&end_date=' . substr $self->{end_date}, 0, 7 if $self->{end_date}; + + return $url; +} + + +=head3 _validate_url + +Checks whether the url ends in a trailing "/" and adds one if not + +my $url = _validate_url($url, 'harvest') + +$caller is either the harvest_sushi function ("harvest") or the test_connection function ("status") + +=cut + +sub _validate_url { + my ( $url, $caller ) = @_; + + if($caller eq 'harvest') { + # Not all urls will end in "/" - add one so they are standardised + $url = _check_trailing_character($url); + # All SUSHI report requests should be to the "/reports" endpoint + # Not all providers in the counter registry include this in their data so we need to check and add it + my $reports_param = substr $url, -8; + $url .= 'reports/' if $reports_param ne 'reports/'; + } else { + $url = _check_trailing_character($url); + } + + return $url; +} + +=head3 _check_trailing_character + +Checks whether a url string ends in a "/" before we concatenate further params to the end of the url + +=cut + +sub _check_trailing_character { + my ( $url ) = @_; + + my $trailing_char = substr $url, -1; + if ( $trailing_char ne '/' ) { + $url .= '/'; + } return $url; } -- 2.20.1