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 — Avoid repeated closing-tag scans in XmlFrameDecoder · netty/netty@efb0044

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-xml/src
    • main/java/io/netty/handler/codec/xml
      • XmlFrameDecoder.java
    • test/java/io/netty/handler/codec/xml
      • XmlFrameDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-xml/src/main/java/io/netty/handler/codec/xml/XmlFrameDecoder.java‎

Copy file name to clipboardExpand all lines: codec-xml/src/main/java/io/netty/handler/codec/xml/XmlFrameDecoder.java
+10-10Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
8888
boolean openingBracketFound = false;
8989
boolean atLeastOneXmlElementFound = false;
9090
boolean inCDATASection = false;
91+
boolean inClosingTag = false;
9192
long openBracketsCount = 0;
9293
int length = 0;
9394
int leadingWhiteSpaceCount = 0;
@@ -110,22 +111,18 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
110111
fail(ctx);
111112
in.skipBytes(in.readableBytes());
112113
return;
114+
} else if (inClosingTag && readByte == '<') {
115+
fail(ctx);
116+
in.skipBytes(in.readableBytes());
117+
return;
113118
} else if (!inCDATASection && readByte == '<') {
114119
openingBracketFound = true;
115120

116121
if (i < bufferLength - 1) {
117122
final byte peekAheadByte = in.getByte(i + 1);
118123
if (peekAheadByte == '/') {
119124
// found </, we must check if it is enclosed
120-
int peekFurtherAheadIndex = i + 2;
121-
while (peekFurtherAheadIndex <= bufferLength - 1) {
122-
//if we have </ and enclosing > we can decrement openBracketsCount
123-
if (in.getByte(peekFurtherAheadIndex) == '>') {
124-
openBracketsCount--;
125-
break;
126-
}
127-
peekFurtherAheadIndex++;
128-
}
125+
inClosingTag = true;
129126
} else if (isValidStartCharForXmlElement(peekAheadByte)) {
130127
atLeastOneXmlElementFound = true;
131128
// char after < is a valid xml element start char,
@@ -156,7 +153,10 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
156153
if (i - 1 > -1) {
157154
final byte peekBehindByte = in.getByte(i - 1);
158155

159-
if (!inCDATASection) {
156+
if (inClosingTag) {
157+
openBracketsCount--;
158+
inClosingTag = false;
159+
} else if (!inCDATASection) {
160160
if (peekBehindByte == '?') {
161161
// an <?xml ?> tag was closed
162162
openBracketsCount--;
Collapse file

‎codec-xml/src/test/java/io/netty/handler/codec/xml/XmlFrameDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-xml/src/test/java/io/netty/handler/codec/xml/XmlFrameDecoderTest.java
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,31 @@ public void testDecodeInvalidXml() {
133133
testDecodeWithXml("<a></a", new Object[0]);
134134
}
135135

136+
@Test
137+
public void testDecodeInvalidNestedClosingTag() {
138+
XmlFrameDecoder decoder = new XmlFrameDecoder(1048576);
139+
final EmbeddedChannel ch = new EmbeddedChannel(decoder);
140+
assertThrows(CorruptedFrameException.class, new Executable() {
141+
@Override
142+
public void execute() {
143+
ch.writeInbound(Unpooled.copiedBuffer("<a></</a>", CharsetUtil.UTF_8));
144+
}
145+
});
146+
}
147+
148+
@Test
149+
public void testDecodeInvalidRepeatedClosingTags() {
150+
XmlFrameDecoder decoder = new XmlFrameDecoder(1048576);
151+
final EmbeddedChannel ch = new EmbeddedChannel(decoder);
152+
ch.writeInbound(Unpooled.copiedBuffer("</", CharsetUtil.UTF_8));
153+
assertThrows(CorruptedFrameException.class, new Executable() {
154+
@Override
155+
public void execute() {
156+
ch.writeInbound(Unpooled.copiedBuffer("</", CharsetUtil.UTF_8));
157+
}
158+
});
159+
}
160+
136161
@Test
137162
public void testDecodeWithCDATABlock() {
138163
final String xml = "<book>" +
@@ -173,6 +198,11 @@ public void testFraming() {
173198
testDecodeWithXml(Arrays.asList("<abc", ">123</a", "bc>"), "<abc>123</abc>");
174199
}
175200

201+
@Test
202+
public void testFramingWithSplitClosingTag() {
203+
testDecodeWithXml(Arrays.asList("<abc>", "123</", "abc>"), "<abc>123</abc>");
204+
}
205+
176206
@Test
177207
public void testDecodeWithSampleXml() {
178208
for (final String xmlSample : xmlSamples) {

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.