Skip to content

Commit 1d615b4

Browse files
authored
Properly escape JS string passed to WebView, it was crashing before when single quote in payload (#42)
1 parent 0f641bd commit 1d615b4

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

ORLib/src/main/java/io/openremote/orlib/ui/OrMainActivity.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -949,14 +949,11 @@ open class OrMainActivity : Activity() {
949949
var jsonString = mapper.writeValueAsString(data)
950950
LOG.info("Sending response to client: $jsonString")
951951

952-
// Double escape quotes (this is needed for browsers to be able to parse the response)
953-
jsonString = jsonString.replace("\\\"", "\\\\\"")
954-
955952
runOnUiThread {
956953
binding.webView.evaluateJavascript(
957954
String.format(
958955
"OpenRemoteConsole._handleProviderResponse('%s')",
959-
jsonString
956+
jsonString.escapeForJavaScript()
960957
), null
961958
)
962959
}
@@ -965,6 +962,16 @@ open class OrMainActivity : Activity() {
965962
}
966963
}
967964

965+
private fun String.escapeForJavaScript(): String {
966+
return this
967+
.replace("\\", "\\\\")
968+
.replace("'", "\\'")
969+
.replace("\"", "\\\"")
970+
.replace("\n", "\\n")
971+
.replace("\r", "\\r")
972+
.replace("\t", "\\t")
973+
}
974+
968975
private fun onConnectivityChanged(connectivity: Boolean) {
969976
LOG.info("Connectivity changed: $connectivity")
970977
if (connectivity && !webViewIsLoading && !lastConnectivity) {

0 commit comments

Comments
 (0)