Loading mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapResponseParser.java +14 −3 Original line number Diff line number Diff line Loading @@ -339,7 +339,18 @@ class ImapResponseParser { */ private Object parseLiteral() throws IOException { expect('{'); int size = Integer.parseInt(readStringUntil('}')); int size; try { size = Integer.parseInt(readStringUntil('}')); } catch (NumberFormatException e) { throw new ImapResponseParserException("Invalid value for size of literal string", e); } if (size < 0) { throw new ImapResponseParserException("Invalid value for size of literal string"); } expect('\r'); expect('\n'); Loading Loading @@ -479,9 +490,9 @@ class ImapResponseParser { } private void checkTokenIsString(Object token) throws IOException { private void checkTokenIsString(Object token) { if (!(token instanceof String)) { throw new IOException("Unexpected non-string token: " + token.getClass().getSimpleName() + " - " + token); throw new ImapResponseParserException("Unexpected non-string token"); } } } mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapResponseParserException.java +4 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,10 @@ package com.fsck.k9.mail.store.imap; public class ImapResponseParserException extends RuntimeException { public ImapResponseParserException(String message) { super(message); } public ImapResponseParserException(String message, Throwable cause) { super(message, cause); } Loading mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapResponseParserTest.kt +46 −2 Original line number Diff line number Diff line Loading @@ -236,6 +236,39 @@ class ImapResponseParserTest { assertThatAllInputWasConsumed() } @Test fun `readResponse() with literal containing negative size`() { val parser = createParserWithResponses("* {-1}") assertThat { parser.readResponse() }.isFailure() .isInstanceOf(ImapResponseParserException::class) .hasMessage("Invalid value for size of literal string") } @Test fun `readResponse() with literal size exceeding Int`() { val parser = createParserWithResponses("* {2147483648}") assertThat { parser.readResponse() }.isFailure() .isInstanceOf(ImapResponseParserException::class) .hasMessage("Invalid value for size of literal string") } @Test fun `readResponse() with invalid characters for literal size`() { val parser = createParserWithResponses("* {invalid}") assertThat { parser.readResponse() }.isFailure() .isInstanceOf(ImapResponseParserException::class) .hasMessage("Invalid value for size of literal string") } @Test fun `readResponse() should throw when end of stream is reached while reading literal`() { val parser = createParserWithData("* {4}\r\nabc") Loading Loading @@ -452,6 +485,17 @@ class ImapResponseParserTest { assertThatAllInputWasConsumed() } @Test fun `readResponse() with tagged response missing completion code should throw`() { val parser = createParserWithResponses("tag ") assertThat { parser.readResponse() }.isFailure() .isInstanceOf(ImapResponseParserException::class) .hasMessage("Unexpected non-string token") } @Test fun `readResponse() with list as first token should throw`() { val parser = createParserWithResponses("* [1 2] 3") Loading @@ -459,8 +503,8 @@ class ImapResponseParserTest { assertThat { parser.readResponse() }.isFailure() .isInstanceOf(IOException::class) .hasMessage("Unexpected non-string token: ImapList - [1, 2]") .isInstanceOf(ImapResponseParserException::class) .hasMessage("Unexpected non-string token") } @Test Loading Loading
mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapResponseParser.java +14 −3 Original line number Diff line number Diff line Loading @@ -339,7 +339,18 @@ class ImapResponseParser { */ private Object parseLiteral() throws IOException { expect('{'); int size = Integer.parseInt(readStringUntil('}')); int size; try { size = Integer.parseInt(readStringUntil('}')); } catch (NumberFormatException e) { throw new ImapResponseParserException("Invalid value for size of literal string", e); } if (size < 0) { throw new ImapResponseParserException("Invalid value for size of literal string"); } expect('\r'); expect('\n'); Loading Loading @@ -479,9 +490,9 @@ class ImapResponseParser { } private void checkTokenIsString(Object token) throws IOException { private void checkTokenIsString(Object token) { if (!(token instanceof String)) { throw new IOException("Unexpected non-string token: " + token.getClass().getSimpleName() + " - " + token); throw new ImapResponseParserException("Unexpected non-string token"); } } }
mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapResponseParserException.java +4 −0 Original line number Diff line number Diff line Loading @@ -2,6 +2,10 @@ package com.fsck.k9.mail.store.imap; public class ImapResponseParserException extends RuntimeException { public ImapResponseParserException(String message) { super(message); } public ImapResponseParserException(String message, Throwable cause) { super(message, cause); } Loading
mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/ImapResponseParserTest.kt +46 −2 Original line number Diff line number Diff line Loading @@ -236,6 +236,39 @@ class ImapResponseParserTest { assertThatAllInputWasConsumed() } @Test fun `readResponse() with literal containing negative size`() { val parser = createParserWithResponses("* {-1}") assertThat { parser.readResponse() }.isFailure() .isInstanceOf(ImapResponseParserException::class) .hasMessage("Invalid value for size of literal string") } @Test fun `readResponse() with literal size exceeding Int`() { val parser = createParserWithResponses("* {2147483648}") assertThat { parser.readResponse() }.isFailure() .isInstanceOf(ImapResponseParserException::class) .hasMessage("Invalid value for size of literal string") } @Test fun `readResponse() with invalid characters for literal size`() { val parser = createParserWithResponses("* {invalid}") assertThat { parser.readResponse() }.isFailure() .isInstanceOf(ImapResponseParserException::class) .hasMessage("Invalid value for size of literal string") } @Test fun `readResponse() should throw when end of stream is reached while reading literal`() { val parser = createParserWithData("* {4}\r\nabc") Loading Loading @@ -452,6 +485,17 @@ class ImapResponseParserTest { assertThatAllInputWasConsumed() } @Test fun `readResponse() with tagged response missing completion code should throw`() { val parser = createParserWithResponses("tag ") assertThat { parser.readResponse() }.isFailure() .isInstanceOf(ImapResponseParserException::class) .hasMessage("Unexpected non-string token") } @Test fun `readResponse() with list as first token should throw`() { val parser = createParserWithResponses("* [1 2] 3") Loading @@ -459,8 +503,8 @@ class ImapResponseParserTest { assertThat { parser.readResponse() }.isFailure() .isInstanceOf(IOException::class) .hasMessage("Unexpected non-string token: ImapList - [1, 2]") .isInstanceOf(ImapResponseParserException::class) .hasMessage("Unexpected non-string token") } @Test Loading