GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Redis: Limit the maximum number of nested arrays (#16882) · netty/netty@728c98b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-redis/src
    • main/java/io/netty/handler/codec/redis
      • RedisArrayAggregator.java
    • test/java/io/netty/handler/codec/redis
      • RedisArrayAggregatorTest.java
      • RedisDecoderTest.java
  • example/src/main/java/io/netty/example/redis
    • RedisClient.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-redis/src/main/java/io/netty/handler/codec/redis/RedisArrayAggregator.java‎

Copy file name to clipboardExpand all lines: codec-redis/src/main/java/io/netty/handler/codec/redis/RedisArrayAggregator.java
+15-3Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
public final class RedisArrayAggregator extends MessageToMessageDecoder<RedisMessage> {
3737

3838
private static final int DEFAULT_MAX_ARRAY_LENGTH = RedisConstants.REDIS_MAX_ARRAY_LENGTH;
39+
private final int maxNestedArrayDepth;
3940
private final Deque<AggregateState> depths = new ArrayDeque<AggregateState>(4);
4041
private final int maxElements;
4142

@@ -46,11 +47,12 @@ public final class RedisArrayAggregator extends MessageToMessageDecoder<RedisMes
4647
* This constructor specifies a maximum number of elements of 1.000.000,
4748
* but this default can be increased with the {@value RedisConstants#PROP_REDIS_MAX_ARRAY_LENGTH} system property.
4849
*
49-
* @deprecated Use {@link #RedisArrayAggregator(int)} instead to define a max size of the array to aggregate.
50+
* @deprecated Use {@link #RedisArrayAggregator(int, int)} instead to define a max size of the array to aggregate.
5051
*/
5152
@Deprecated
5253
public RedisArrayAggregator() {
53-
this(DEFAULT_MAX_ARRAY_LENGTH);
54+
// Let's impose some limit at least by default.
55+
this(DEFAULT_MAX_ARRAY_LENGTH, 1024);
5456
}
5557

5658
/**
@@ -60,10 +62,12 @@ public RedisArrayAggregator() {
6062
* A {@link CodecException} will be thrown if the array header specify a length greater than
6163
* the given number of max elements.
6264
* @param maxElements The maximum number of elements to aggregate in a single message.
65+
* @param maxNestedArrayDepth the maximum depth of the nested array before an exception will be thrown
6366
*/
64-
public RedisArrayAggregator(int maxElements) {
67+
public RedisArrayAggregator(int maxElements, int maxNestedArrayDepth) {
6568
super(RedisMessage.class);
6669
this.maxElements = ObjectUtil.checkPositive(maxElements, "maxElements");
70+
this.maxNestedArrayDepth = ObjectUtil.checkPositive(maxNestedArrayDepth, "maxNestedArrayDepth");
6771
}
6872

6973
@Override
@@ -105,6 +109,10 @@ private RedisMessage decodeRedisArrayHeader(ArrayHeaderRedisMessage header) {
105109
throw new CodecException("this codec doesn't support longer length than " + maxElements);
106110
}
107111

112+
if (depths.size() >= maxNestedArrayDepth) {
113+
releaseAndClearDepths();
114+
throw new CodecException("max nested array depth exceeded: " + maxNestedArrayDepth);
115+
}
108116
// start aggregating array
109117
depths.push(new AggregateState((int) header.length()));
110118
return null;
@@ -125,6 +133,10 @@ private static final class AggregateState {
125133
@Override
126134
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
127135
super.handlerRemoved(ctx);
136+
releaseAndClearDepths();
137+
}
138+
139+
private void releaseAndClearDepths() {
128140
for (AggregateState state : depths) {
129141
for (RedisMessage message : state.children) {
130142
ReferenceCountUtil.safeRelease(message);
Collapse file

‎codec-redis/src/test/java/io/netty/handler/codec/redis/RedisArrayAggregatorTest.java‎

Copy file name to clipboardExpand all lines: codec-redis/src/test/java/io/netty/handler/codec/redis/RedisArrayAggregatorTest.java
+22-1Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
* or implied. See the License for the specific language governing permissions and limitations under
1313
* the License.
1414
*/
15-
1615
package io.netty.handler.codec.redis;
1716

1817
import io.netty.buffer.Unpooled;
1918
import io.netty.channel.embedded.EmbeddedChannel;
2019
import io.netty.handler.codec.PrematureChannelClosureException;
20+
import io.netty.handler.codec.CodecException;
21+
import io.netty.util.CharsetUtil;
2122
import org.junit.jupiter.api.Test;
2223
import org.junit.jupiter.api.function.Executable;
2324

@@ -59,4 +60,24 @@ void testDoesNotLeakOnRemoval() {
5960
assertEquals(0, redisMessage.refCnt());
6061
assertFalse(ch.finish());
6162
}
63+
64+
@Test
65+
public void testLimitNested() {
66+
final byte[] arrayHeader = "*1\r\n".getBytes(CharsetUtil.US_ASCII);
67+
int maxNestedDepth = 100;
68+
final EmbeddedChannel channel = new EmbeddedChannel(new RedisDecoder(),
69+
new RedisArrayAggregator(RedisConstants.REDIS_MAX_ARRAY_LENGTH, maxNestedDepth));
70+
for (int i = 0; i < maxNestedDepth; i++) {
71+
assertFalse(channel.writeInbound(Unpooled.wrappedBuffer(arrayHeader)));
72+
}
73+
74+
// Next write should trigger an exception.
75+
assertThrows(CodecException.class, new Executable() {
76+
@Override
77+
public void execute() throws Throwable {
78+
channel.writeInbound(Unpooled.wrappedBuffer(arrayHeader));
79+
}
80+
});
81+
assertFalse(channel.finishAndReleaseAll());
82+
}
6283
}
Collapse file

‎codec-redis/src/test/java/io/netty/handler/codec/redis/RedisDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-redis/src/test/java/io/netty/handler/codec/redis/RedisDecoderTest.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private static EmbeddedChannel newChannel(boolean decodeInlineCommands) {
5858
return new EmbeddedChannel(
5959
new RedisDecoder(decodeInlineCommands),
6060
new RedisBulkStringAggregator(),
61-
new RedisArrayAggregator(100));
61+
new RedisArrayAggregator(100, 1024));
6262
}
6363

6464
@AfterEach
Collapse file

‎example/src/main/java/io/netty/example/redis/RedisClient.java‎

Copy file name to clipboardExpand all lines: example/src/main/java/io/netty/example/redis/RedisClient.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected void initChannel(SocketChannel ch) throws Exception {
5252
ChannelPipeline p = ch.pipeline();
5353
p.addLast(new RedisDecoder());
5454
p.addLast(new RedisBulkStringAggregator());
55-
p.addLast(new RedisArrayAggregator(1000000));
55+
p.addLast(new RedisArrayAggregator(1000000, 1024));
5656
p.addLast(new RedisEncoder());
5757
p.addLast(new RedisClientHandler());
5858
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.