From b353858b3b7c47fe67a4cf353ff7107ed93257b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Can=20Berk=20G=C3=BCder?= Date: Fri, 8 Nov 2024 15:26:10 -0800 Subject: [PATCH] Gracefully handle duplicate query keys in contextual services pipeline (#2697) Fixes #2696 --- .../contextualservices/BuildReportingUrl.java | 16 ++++++++++------ .../BuildReportingUrlTest.java | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 ingestion-beam/src/test/java/com/mozilla/telemetry/contextualservices/BuildReportingUrlTest.java diff --git a/ingestion-beam/src/main/java/com/mozilla/telemetry/contextualservices/BuildReportingUrl.java b/ingestion-beam/src/main/java/com/mozilla/telemetry/contextualservices/BuildReportingUrl.java index 69cf6df7..b5a91757 100644 --- a/ingestion-beam/src/main/java/com/mozilla/telemetry/contextualservices/BuildReportingUrl.java +++ b/ingestion-beam/src/main/java/com/mozilla/telemetry/contextualservices/BuildReportingUrl.java @@ -54,12 +54,16 @@ public class BuildReportingUrl { throw new InvalidUrlException("Reporting URL null or missing: " + reportingUrl); } - if (this.reportingUrl.getQuery() == null) { - queryParams = new HashMap<>(); - } else { - queryParams = Arrays.stream(this.reportingUrl.getQuery().split("&")) - .map(param -> param.split("=")) - .collect(Collectors.toMap(item -> item[0], item -> item.length > 1 ? item[1] : "")); + try { + if (this.reportingUrl.getQuery() == null) { + queryParams = new HashMap<>(); + } else { + queryParams = Arrays.stream(this.reportingUrl.getQuery().split("&")) + .map(param -> param.split("=")) + .collect(Collectors.toMap(item -> item[0], item -> item.length > 1 ? item[1] : "")); + } + } catch (IllegalStateException e) { + throw new InvalidUrlException("Reporting URL contains duplicate query keys: " + reportingUrl); } } diff --git a/ingestion-beam/src/test/java/com/mozilla/telemetry/contextualservices/BuildReportingUrlTest.java b/ingestion-beam/src/test/java/com/mozilla/telemetry/contextualservices/BuildReportingUrlTest.java new file mode 100644 index 00000000..eb0dca84 --- /dev/null +++ b/ingestion-beam/src/test/java/com/mozilla/telemetry/contextualservices/BuildReportingUrlTest.java @@ -0,0 +1,15 @@ +package com.mozilla.telemetry.contextualservices; + +import static org.junit.Assert.assertThrows; + +import org.junit.Test; + +public class BuildReportingUrlTest { + + @Test + public void testDuplicateQueryKeys() { + assertThrows(BuildReportingUrl.InvalidUrlException.class, + () -> new BuildReportingUrl("https://example.com?foo=bar&foo=bar")); + } + +}