Skip to content

Commit

Permalink
Gracefully handle duplicate query keys in contextual services pipeline (
Browse files Browse the repository at this point in the history
#2697)

Fixes #2696
  • Loading branch information
cbguder authored Nov 8, 2024
1 parent 5058a75 commit b353858
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ public InvalidUrlException(String message, Throwable cause) {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}

}

0 comments on commit b353858

Please sign in to comment.