Skip to content

Commit 3085cc1

Browse files
authored
core (fix): Fix MessageUnpacker.unpackValue to check the custom stringSizeLimit (#753)
* core (fix): Fix MessageUnpacker.unpackValue to check the custom stringSizeLimit * Cover unpackVariable(var)
1 parent 2465fd3 commit 3085cc1

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,9 @@ public ImmutableValue unpackValue()
624624
return ValueFactory.newFloat(unpackDouble());
625625
case STRING: {
626626
int length = unpackRawStringHeader();
627+
if (length > stringSizeLimit) {
628+
throw new MessageSizeException(String.format("cannot unpack a String of size larger than %,d: %,d", stringSizeLimit, length), length);
629+
}
627630
return ValueFactory.newString(readPayload(length), true);
628631
}
629632
case BINARY: {
@@ -689,6 +692,9 @@ public Variable unpackValue(Variable var)
689692
return var;
690693
case STRING: {
691694
int length = unpackRawStringHeader();
695+
if (length > stringSizeLimit) {
696+
throw new MessageSizeException(String.format("cannot unpack a String of size larger than %,d: %,d", stringSizeLimit, length), length);
697+
}
692698
var.setStringValue(readPayload(length));
693699
return var;
694700
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.msgpack.core
2+
3+
import org.msgpack.core.MessagePack.UnpackerConfig
4+
import org.msgpack.value.Variable
5+
import wvlet.airspec.AirSpec
6+
7+
class StringLimitTest extends AirSpec {
8+
9+
test("throws an exception when the string size exceeds a limit") {
10+
val customLimit = 100
11+
val packer = MessagePack.newDefaultBufferPacker()
12+
packer.packString("a" * (customLimit + 1))
13+
val msgpack = packer.toByteArray
14+
15+
test("unpackString") {
16+
val unpacker = new UnpackerConfig().withStringSizeLimit(customLimit).newUnpacker(msgpack)
17+
intercept[MessageSizeException] {
18+
unpacker.unpackString()
19+
}
20+
}
21+
22+
test("unpackValue") {
23+
val unpacker = new UnpackerConfig().withStringSizeLimit(customLimit).newUnpacker(msgpack)
24+
intercept[MessageSizeException] {
25+
unpacker.unpackValue()
26+
}
27+
}
28+
29+
test("unpackValue(var)") {
30+
val unpacker = new UnpackerConfig().withStringSizeLimit(customLimit).newUnpacker(msgpack)
31+
intercept[MessageSizeException] {
32+
val v = new Variable()
33+
unpacker.unpackValue(v)
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)