0
0
mirror of https://github.com/thunderbird/thunderbird-android.git synced 2024-09-20 04:02:14 +02:00

Merge pull request #6836 from thundernest/fix_ImapResponseParser

Fix bugs in `ImapResponseParser`
This commit is contained in:
cketti 2023-04-20 16:04:59 +02:00 committed by GitHub
commit 0d24dcd28a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 5 deletions

View File

@ -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');
@ -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");
}
}
}

View File

@ -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);
}

View File

@ -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")
@ -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")
@ -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