fix: Ignore invalid JSON messages (like NODE_ENV undefined) (#6576)

Fixes: https://continue-xd.sentry.io/issues/6731751693

Co-authored-by: Patrick Erichsen <patrick.a.erichsen@gmail.com>
This commit is contained in:
Jakub Błach 2025-07-21 23:37:06 +02:00 • committed by GitHub
parent 9713651716
commit 52e2f3c4e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,9 +7,11 @@ import com.github.continuedev.continueintellijextension.`continue`.process.Conti
import com.github.continuedev.continueintellijextension.services.ContinuePluginService
import com.github.continuedev.continueintellijextension.utils.uuid
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
class CoreMessenger(
private val project: Project,
@ -20,6 +22,7 @@ class CoreMessenger(
private val gson = Gson()
private val responseListeners = mutableMapOf<String, (Any?) -> Unit>()
private val process = startContinueProcess()
private val log = Logger.getInstance(CoreMessenger::class.java)
fun request(messageType: String, data: Any?, messageId: String?, onResponse: (Any?) -> Unit) {
val id = messageId ?: uuid()
@ -38,13 +41,13 @@ class CoreMessenger(
}
private fun handleMessage(json: String) {
val responseMap = gson.fromJson(json, Map::class.java)
val responseMap = tryToParse(json) ?: return
val messageId = responseMap["messageId"].toString()
val messageType = responseMap["messageType"].toString()
val data = responseMap["data"]
// IDE listeners
if (MessageTypes.IDE_MESSAGE_TYPES.contains(messageType)) {
if (messageType in MessageTypes.IDE_MESSAGE_TYPES) {
ideProtocolClient.handleMessage(json) { data ->
val message = gson.toJson(mapOf("messageId" to messageId, "messageType" to messageType, "data" to data))
process.write(message)
@ -52,8 +55,9 @@ class CoreMessenger(
}
// Forward to webview
if (MessageTypes.PASS_THROUGH_TO_WEBVIEW.contains(messageType)) {
if (messageType in MessageTypes.PASS_THROUGH_TO_WEBVIEW) {
val continuePluginService = project.service<ContinuePluginService>()
// todo: is this a bug below (messageType = ID)? verify
continuePluginService.sendToWebview(messageType, responseMap["data"], messageType)
}
@ -68,6 +72,15 @@ class CoreMessenger(
}
}
// todo: map<*, *> = code smell
private fun tryToParse(json: String): Map<*, *>? =
try {
gson.fromJson(json, Map::class.java)
} catch (_: JsonSyntaxException) {
log.warn("Invalid message JSON: $json") // example: NODE_ENV undefined
null
}
fun killSubProcess() {
process.close()
}