Skip to content

Commit df17506

Browse files
author
jossonsmith
committed
Limit the data size of Simple RPC to 16M and the array size to 16K.
1 parent 1debf68 commit df17506

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleRPCHttpServlet.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,21 @@ public Object getNewInstanceByClassName(String className) {
105105
}
106106
return null;
107107
}
108-
public static String readAll(InputStream res) {
108+
private String readAll(InputStream res) {
109109
try {
110110
ByteArrayOutputStream baos = new ByteArrayOutputStream();
111111
byte[] buf = new byte[1024];
112112
int read = 0;
113113
while ((read = res.read(buf)) != -1) {
114114
baos.write(buf, 0, read);
115+
if (baos.size() > 0x1000000) { // 16 * 1024 * 1024 // 16M!
116+
/*
117+
* Some malicious request may try to allocate huge size of memory!
118+
* Limit the data size of HTTP request!
119+
*/
120+
res.close();
121+
throw new RuntimeException("Data size reaches the limit of Java2Script Simple RPC!");
122+
}
115123
}
116124
res.close();
117125
return baos.toString();

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleSerializable.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)