Skip to content

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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -38,22 +41,23 @@ 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)
}
}

// 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)
}

Expand All @@ -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()
}
Expand Down
Loading