Skip to content

Commit

Permalink
Add check for invalid uris containing multiple protocols (#2962)
Browse files Browse the repository at this point in the history
Co-authored-by: Sam <[email protected]>
  • Loading branch information
felixbr and sksamuel authored Nov 30, 2023
1 parent 12bf868 commit 6fa102e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ object ElasticProperties {

private val Regex = "(http|https)://(.*?)/?(/.*?)?(\\?.*?)?".r
private val EndpointRegex = "(.*?)(:\\d+)?".r
private val ContainsProtocol = "://".r

/**
* Creates [[ElasticProperties]] from an URI. The general format of the URI is:
Expand All @@ -17,6 +18,12 @@ object ElasticProperties {
*/
def apply(str: String): ElasticProperties = {
str match {
// It's much easier to validate this in a separate check instead of making the main regex much more complex
case str if ContainsProtocol.findAllMatchIn(str).size > 1 =>
sys.error(
s"Invalid uri (multiple protocols) $str, must be in format http(s)://host:port,host:port(/prefix)(?querystr)"
)

case Regex(protocol, hoststr, prefix, query) =>
val hosts = hoststr.split(',').toSeq collect {
case EndpointRegex(host, port) => ElasticNodeEndpoint(protocol, host, Option(port).map(_.drop(1).toInt).getOrElse(9200), Option(prefix).map(_.stripSuffix("/")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ class ElasticPropertiesTest extends AnyFlatSpec with Matchers {
} should not be null
}

it should "error if multiple protocols are specificed" in {
intercept[RuntimeException] {
ElasticProperties.apply("http://host1:1234,http://host2:9999")
} should not be null
}

it should "support https protocol" in {
ElasticProperties("https://host1:1234,host2:2345") shouldBe
ElasticProperties(Seq(ElasticNodeEndpoint("https", "host1", 1234, None), ElasticNodeEndpoint("https", "host2", 2345, None)))
Expand Down

0 comments on commit 6fa102e

Please sign in to comment.