Skip to content

Commit 02a8979

Browse files
committed
Add validation that onAutoForward is a string
1 parent 0130481 commit 02a8979

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/main/kotlin/com/coder/jetbrains/services/CoderPortForwardService.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class CoderPortForwardService(
5656
poller?.cancel()
5757
}
5858

59+
class InvalidJsonTypeException(message: String) : Exception(message)
60+
5961
private fun start() {
6062
val devcontainerFile = CoderBackendSettings.getDevcontainerFile()
6163
if (devcontainerFile.exists()) {
@@ -66,19 +68,27 @@ class CoderPortForwardService(
6668
val portsAttributes = obj.optJSONObject("portsAttributes") ?: JSONObject()
6769
portsAttributes.keys().forEach { spec ->
6870
portsAttributes.optJSONObject(spec)?.let { attrs ->
69-
val onAutoForward = attrs.optString("onAutoForward")
70-
if (onAutoForward == "ignore") {
71+
val onAutoForward = attrs.opt("onAutoForward")
72+
if (!isValidString(onAutoForward)) {
73+
throw InvalidJsonTypeException("onAutoForward for port $spec is not a string value")
74+
}
75+
val onAutoForwardStr = onAutoForward as String
76+
if (onAutoForwardStr == "ignore") {
7177
logger.info("found ignored port specification $spec in devcontainer.json")
7278
rules.add(0, PortRule(PortMatcher(spec), false))
73-
} else if (onAutoForward != "") {
79+
} else if (onAutoForwardStr != "") {
7480
logger.info("found auto-forward port specification $spec in devcontainer.json")
7581
rules.add(0, PortRule(PortMatcher(spec), true))
7682
}
7783
}
7884
}
7985

8086
val otherPortsAttributes = obj.optJSONObject("otherPortsAttributes") ?: JSONObject()
81-
if (otherPortsAttributes.optString("onAutoForward") == "ignore") {
87+
val otherPortsAutoForward = otherPortsAttributes.opt("onAutoForward")
88+
if (!isValidString(otherPortsAutoForward)) {
89+
throw InvalidJsonTypeException("otherPortsAttributes.onAutoForward is not a string value")
90+
}
91+
if ((otherPortsAutoForward as String) == "ignore") {
8292
logger.info("found ignored setting for otherPortsAttributes in devcontainer.json")
8393
defaultForward = false
8494
}
@@ -139,4 +149,8 @@ class CoderPortForwardService(
139149
}
140150
}
141151
}
152+
153+
private fun isValidString(value: Any?): Boolean {
154+
return value != null && value is String
155+
}
142156
}

0 commit comments

Comments
 (0)