Skip to content

Commit 8c265e4

Browse files
authored
Escape JS string before passing it to eval() (#27)
1 parent 9cef5d5 commit 8c265e4

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* $Id: JSONValue.java,v 1.1 2006/04/15 14:37:04 platform Exp $
3+
* Created on 2006-4-15
4+
*/
5+
6+
/*
7+
* Code from the json-simple project, modified to extract only the escape() function.
8+
* https://github.com/fangyidong/json-simple/blob/351aa583745c6de31eed3ccd921c7589f27e2413/src/main/java/org/json/simple/JSONValue.java
9+
*
10+
* Original code licensed under the terms of Apache License 2.0:
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*/
13+
package com.defold.webview;
14+
15+
/**
16+
* @author FangYidong<[email protected]>
17+
*/
18+
public class JSONValue {
19+
/**
20+
* Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
21+
* @param s
22+
* @return
23+
*/
24+
public static String escape(String s){
25+
if(s==null)
26+
return null;
27+
StringBuffer sb = new StringBuffer();
28+
escape(s, sb);
29+
return sb.toString();
30+
}
31+
32+
/**
33+
* @param s - Must not be null.
34+
* @param sb
35+
*/
36+
static void escape(String s, StringBuffer sb) {
37+
final int len = s.length();
38+
for(int i=0;i<len;i++){
39+
char ch=s.charAt(i);
40+
switch(ch){
41+
case '"':
42+
sb.append("\\\"");
43+
break;
44+
case '\\':
45+
sb.append("\\\\");
46+
break;
47+
case '\b':
48+
sb.append("\\b");
49+
break;
50+
case '\f':
51+
sb.append("\\f");
52+
break;
53+
case '\n':
54+
sb.append("\\n");
55+
break;
56+
case '\r':
57+
sb.append("\\r");
58+
break;
59+
case '\t':
60+
sb.append("\\t");
61+
break;
62+
case '/':
63+
sb.append("\\/");
64+
break;
65+
default:
66+
//Reference: http://www.unicode.org/versions/Unicode5.1.0/
67+
if((ch>='\u0000' && ch<='\u001F') || (ch>='\u007F' && ch<='\u009F') || (ch>='\u2000' && ch<='\u20FF')){
68+
String ss=Integer.toHexString(ch);
69+
sb.append("\\u");
70+
for(int k=0;k<4-ss.length();k++){
71+
sb.append('0');
72+
}
73+
sb.append(ss.toUpperCase());
74+
}
75+
else{
76+
sb.append(ch);
77+
}
78+
}
79+
}//for
80+
}
81+
}

webview/src/java/com/defold/webview/WebViewJNI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public void eval(final String code, final int webview_id, final int request_id)
411411
public void run() {
412412
WebViewJNI.this.infos[webview_id].webviewClient.reset(request_id);
413413
WebViewJNI.this.infos[webview_id].webviewChromeClient.reset(request_id);
414-
String javascript = String.format("javascript:%s.returnResultToJava(eval(\"%s\"))", JS_NAMESPACE, code);
414+
String javascript = String.format("javascript:%s.returnResultToJava(eval(\"%s\"))", JS_NAMESPACE, JSONValue.escape(code));
415415
WebViewJNI.this.infos[webview_id].webview.loadUrl(javascript);
416416
}
417417
});

0 commit comments

Comments
 (0)