Skip to content

Control automatic port forwarding with a devcontainer.json file #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 27, 2025
Prev Previous commit
Next Next commit
Validate single port or range endpoint is between 0 and 65535
  • Loading branch information
aaronlehmann committed Feb 26, 2025
commit ae1df2f2eaf898fba7d011488b8cc37c0053f82f
14 changes: 13 additions & 1 deletion src/main/kotlin/com/coder/jetbrains/matcher/PortMatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ class PortMatcher(private val rule: String) {
return when {
// Try parsing as single port
portPart.all { it.isDigit() } -> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non Blocking Nit/Question: Should we validate the port is between 0 and 65535 for the SinglePort and PortRange parsing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this validation

MatchRule.SinglePort(portPart.toInt())
val port = portPart.toInt()
validatePort(port)
MatchRule.SinglePort(port)
}
// Try parsing as port range (e.g., "40000-55000")
portPart.matches("^\\d+-\\d+$".toRegex()) -> {
val (start, end) = portPart.split('-')
.map { it.trim().toInt() }
validatePort(start)
validatePort(end)
require(start <= end) { "Invalid port range: start must be less than or equal to end" }
MatchRule.PortRange(start, end)
}
Expand All @@ -47,4 +51,12 @@ class PortMatcher(private val rule: String) {
}
}
}

private fun validatePort(port: Int) {
require(port in 0..65535) { "Port number must be between 0 and 65535, got: $port" }
}

companion object {
const val MAX_PORT = 65535
}
}
21 changes: 21 additions & 0 deletions src/test/kotlin/com/coder/jetbrains/matcher/PortMatcherTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.coder.jetbrains.matcher
import org.junit.Test
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assert.assertThrows

class PortMatcherTest {

Expand Down Expand Up @@ -40,4 +41,24 @@ class PortMatcherTest {
assertTrue(matcher.matches(8009))
assertFalse(matcher.matches(8010))
}

@Test
fun `test invalid port numbers`() {
assertThrows(IllegalArgumentException::class.java) { PortMatcher("65536") }
assertThrows(IllegalArgumentException::class.java) { PortMatcher("0-65536") }
assertThrows(IllegalArgumentException::class.java) { PortMatcher("70000") }
}

@Test
fun `test edge case port numbers`() {
// These should work
PortMatcher("0")
PortMatcher("65535")
PortMatcher("0-65535")

// These combinations should work
val matcher = PortMatcher("0-65535")
assertTrue(matcher.matches(0))
assertTrue(matcher.matches(65535))
}
}