@@ -67,6 +67,9 @@ public class SimpleSerializable {
6767 } else {
6868 var l4 = this[name].length;
6969 if (l4 > 52) {
70+ if (l4 > 0x4000) { // 16 * 1024
71+ throw new RuntimeException("Array size reaches the limit of Java2Script Simple RPC!");
72+ }
7073 buffer[buffer.length] = String.fromCharCode (baseChar - 2);
7174 var value = "" + l4;
7275 buffer[buffer.length] = String.fromCharCode (baseChar + value.length);
@@ -98,7 +101,11 @@ public class SimpleSerializable {
98101 }
99102 }
100103}
101- return buffer.join ('');
104+ var strBuf = buffer.join ('');
105+ if (strBuf.length > 0x1000000) { // 16 * 1024 * 1024
106+ throw new RuntimeException("Data size reaches the limit of Java2Script Simple RPC!");
107+ }
108+ return strBuf;
102109 */
103110 public String serialize () {
104111 char baseChar = 'B' ;
@@ -315,6 +322,9 @@ public String serialize() {
315322 } catch (UnsupportedEncodingException e ) {
316323 e .printStackTrace ();
317324 }
325+ if (buffer .length () > 0x1000000 ) { // 16 * 1024 * 1024
326+ throw new RuntimeException ("Data size reaches the limit of Java2Script Simple RPC!" );
327+ }
318328 return buffer .toString ();
319329 }
320330
@@ -327,6 +337,9 @@ public String serialize() {
327337 private void serializeLength (StringBuffer buffer , int length ) {
328338 char baseChar = 'B' ;
329339 if (length > 52 ) {
340+ if (length > 0x4000 ) { // 16 * 1024
341+ throw new RuntimeException ("Array size reaches the limit of Java2Script Simple RPC!" );
342+ }
330343 buffer .append ((char ) (baseChar - 2 ));
331344 String value = "" + length ;
332345 buffer .append ((char ) (baseChar + value .length ()));
@@ -429,6 +442,9 @@ private void serializeString(StringBuffer buffer, String s) throws UnsupportedEn
429442 var c4 = str.charCodeAt(index++);
430443 var l3 = c4 - baseChar;
431444 l2 = parseInt(str.substring(index, index + l3));
445+ if (l2 > 0x4000) { // 16 * 1024
446+ throw new RuntimeException("Array size reaches the limit of Java2Script Simple RPC!");
447+ }
432448 index += l3;
433449 }
434450 var arr = new Array (l2);
@@ -563,6 +579,13 @@ public void deserialize(String str) {
563579 char c4 = str .charAt (index ++);
564580 int l3 = c4 - baseChar ;
565581 l2 = Integer .parseInt (str .substring (index , index + l3 ));
582+ if (l2 > 0x4000 ) { // 16 * 1024
583+ /*
584+ * Some malicious string may try to allocate huge size of array!
585+ * Limit the size of array here!
586+ */
587+ throw new RuntimeException ("Array size reaches the limit of Java2Script Simple RPC!" );
588+ }
566589 index += l3 ;
567590 }
568591 String [] ss = new String [l2 ];
0 commit comments