diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt index 061326f6..a11214e6 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt @@ -158,7 +158,11 @@ data class ClusterMetricsInput( return if (url.isEmpty()) { constructUrlFromInputs() } else { - URIBuilder(url).build() + try { + URIBuilder(url).build() + } catch (e: URISyntaxException) { + throw IllegalArgumentException("Invalid URL syntax.") + } } } @@ -243,7 +247,11 @@ data class ClusterMetricsInput( .setHost(SUPPORTED_HOST) .setPort(SUPPORTED_PORT) .setPath(path + pathParams) - uriBuilder.build() + try { + uriBuilder.build() + } catch (e: URISyntaxException) { + throw IllegalArgumentException("Invalid URL syntax.") + } } } diff --git a/src/test/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInputTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInputTests.kt index 0e739e7f..6d1c1055 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInputTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInputTests.kt @@ -555,4 +555,40 @@ class ClusterMetricsInputTests { ) } } + + @Test + fun `test url field contains invalid characters`() { + // GIVEN + path = "" + pathParams = "" + url = "http://localhost:9200/${ILLEGAL_PATH_PARAMETER_CHARACTERS.joinToString("")}" + + // WHEN + THEN + assertFailsWith("Invalid URL syntax.") { + ClusterMetricsInput( + path = path, + pathParams = pathParams, + url = url, + clusters = listOf() + ) + } + } + + @Test + fun `test URI fields provided and url contains invalid characters`() { + // GIVEN + path = "/_cluster/health" + pathParams = "index1,index2,index3,index4,index5" + url = "http://localhost:9200/${ILLEGAL_PATH_PARAMETER_CHARACTERS.joinToString("")}" + + // WHEN + THEN + assertFailsWith("Invalid URL syntax.") { + ClusterMetricsInput( + path = path, + pathParams = pathParams, + url = url, + clusters = listOf() + ) + } + } }