@@ -56,6 +56,8 @@ class CoderPortForwardService(
56
56
poller?.cancel()
57
57
}
58
58
59
+ class InvalidJsonTypeException (message : String ) : Exception(message)
60
+
59
61
private fun start () {
60
62
val devcontainerFile = CoderBackendSettings .getDevcontainerFile()
61
63
if (devcontainerFile.exists()) {
@@ -66,19 +68,27 @@ class CoderPortForwardService(
66
68
val portsAttributes = obj.optJSONObject(" portsAttributes" ) ? : JSONObject ()
67
69
portsAttributes.keys().forEach { spec ->
68
70
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" ) {
71
77
logger.info(" found ignored port specification $spec in devcontainer.json" )
72
78
rules.add(0 , PortRule (PortMatcher (spec), false ))
73
- } else if (onAutoForward != " " ) {
79
+ } else if (onAutoForwardStr != " " ) {
74
80
logger.info(" found auto-forward port specification $spec in devcontainer.json" )
75
81
rules.add(0 , PortRule (PortMatcher (spec), true ))
76
82
}
77
83
}
78
84
}
79
85
80
86
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" ) {
82
92
logger.info(" found ignored setting for otherPortsAttributes in devcontainer.json" )
83
93
defaultForward = false
84
94
}
@@ -141,4 +151,8 @@ class CoderPortForwardService(
141
151
}
142
152
}
143
153
}
154
+
155
+ private fun isValidString (value : Any? ): Boolean {
156
+ return value != null && value is String
157
+ }
144
158
}
0 commit comments