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 — Port 5.0: Reject reply codes with non-digit characters in SmtpRespons… · netty/netty@9e20e73

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-smtp/src
    • main/java/io/netty/handler/codec/smtp
      • SmtpResponseDecoder.java
    • test/java/io/netty/handler/codec/smtp
      • SmtpResponseDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-smtp/src/main/java/io/netty/handler/codec/smtp/SmtpResponseDecoder.java‎

Copy file name to clipboardExpand all lines: codec-smtp/src/main/java/io/netty/handler/codec/smtp/SmtpResponseDecoder.java
+12-3Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ protected SmtpResponse decode(ChannelHandlerContext ctx, ByteBuf buffer) throws
8686
throw newDecoderException(buffer, readerIndex, readable);
8787
}
8888
final int code = parseCode(frame);
89+
if (code < 0) {
90+
reset();
91+
throw newDecoderException(buffer, readerIndex, readable);
92+
}
8993
final int separator = frame.readByte();
9094
final CharSequence detail = frame.isReadable() ? frame.toString(CharsetUtil.US_ASCII) : null;
9195

@@ -145,12 +149,17 @@ private static DecoderException newDecoderException(ByteBuf buffer, int readerIn
145149

146150
/**
147151
* Parses the io.netty.handler.codec.smtp code without any allocation, which is three digits.
152+
*
153+
* @return the code, or {@code -1} if one of the three bytes is not a digit.
148154
*/
149155
private static int parseCode(ByteBuf buffer) {
150-
final int first = parseNumber(buffer.readByte()) * 100;
151-
final int second = parseNumber(buffer.readByte()) * 10;
156+
final int first = parseNumber(buffer.readByte());
157+
final int second = parseNumber(buffer.readByte());
152158
final int third = parseNumber(buffer.readByte());
153-
return first + second + third;
159+
if (first < 0 || second < 0 || third < 0) {
160+
return -1;
161+
}
162+
return first * 100 + second * 10 + third;
154163
}
155164

156165
private static int parseNumber(byte b) {
Collapse file

‎codec-smtp/src/test/java/io/netty/handler/codec/smtp/SmtpResponseDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-smtp/src/test/java/io/netty/handler/codec/smtp/SmtpResponseDecoderTest.java
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import io.netty.handler.codec.TooLongFrameException;
2323
import io.netty.util.CharsetUtil;
2424
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.ValueSource;
2527

2628
import java.util.List;
2729

@@ -123,6 +125,17 @@ public void testDecodeInvalidCode() {
123125
assertThrows(DecoderException.class, () -> channel.writeInbound(newBuffer("xyz Ok\r\n")));
124126
}
125127

128+
@ParameterizedTest
129+
@ValueSource(strings = { "5x0 denied\r\n", "2:0 hm\r\n", "4?1 later\r\n" })
130+
public void testDecodeCodeWithNonDigit(String line) throws Exception {
131+
// Each of these used to be decoded as a valid code (490, 190 and 391).
132+
final EmbeddedChannel channel = newChannel();
133+
DecoderException exception = assertThrows(DecoderException.class,
134+
() -> channel.writeInbound(newBuffer(line)));
135+
assertNull(exception.getCause());
136+
assertFalse(channel.finishAndReleaseAll());
137+
}
138+
126139
@Test
127140
public void testDecodeLineWithOnlyCodeAndNoSeparator() throws Exception {
128141
final EmbeddedChannel channel = newChannel();

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.