Skip to content

Commit

Permalink
Fix AddressCandidateHelper issues with empty input and invalid ports
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsvanvelzen committed Sep 25, 2022
1 parent f0a6f35 commit 48c2e4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,22 @@ public class AddressCandidateHelper(
logger.debug { "Input is $input" }

// Add the input as initial candidate
candidates.add(URLBuilder().apply {
// Ktor doesn't like urls not starting with a protocol
// so we default to a http prefix
if (!input.startsWith(PROTOCOL_HTTP) && !input.startsWith(PROTOCOL_HTTPS))
takeFrom(PROTOCOL_HTTP + input)
else
takeFrom(input)
}.build())
if (input.isNotBlank()) {
candidates.add(URLBuilder().apply {
// Ktor doesn't like urls not starting with a protocol
// so we default to a http prefix
if (!input.startsWith(PROTOCOL_HTTP) && !input.startsWith(PROTOCOL_HTTPS))
takeFrom(PROTOCOL_HTTP + input)
else
takeFrom(input)
}.build())
}
} catch (error: URLParserException) {
// Input can't be parsed
logger.error(error) { "Input $input could not be parsed" }
} catch (error: IllegalArgumentException) {
// Input can't be parsed
logger.error(error) { "Input $input could not be parsed" }
}
}

Expand Down Expand Up @@ -88,6 +93,7 @@ public class AddressCandidateHelper(
URLProtocol.HTTP -> {
candidates.add(it.copy(specifiedPort = JF_HTTP_PORT))
}

URLProtocol.HTTPS -> {
candidates.add(it.copy(specifiedPort = JF_HTTP_PORT))
candidates.add(it.copy(specifiedPort = JF_HTTPS_PORT))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,17 @@ class DiscoveryServiceTests : FunSpec({
instance.getAddressCandidates("[0:0:0:0:0:0:0:1]:8096") shouldContain "http://[0:0:0:0:0:0:0:1]:8096"
}

test("getAddressCandidates fails on bad input") {
test("getAddressCandidates returns empty on bad input") {
val instance = getInstance()

// Invalid host
instance.getAddressCandidates("::").shouldBeEmpty()

// Empty input
instance.getAddressCandidates("").shouldBeEmpty()

// Port out of range
instance.getAddressCandidates("localhost:65536").shouldBeEmpty()
instance.getAddressCandidates("localhost:999999").shouldBeEmpty()
}
})

0 comments on commit 48c2e4c

Please sign in to comment.