Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 0d24dcd2 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #6836 from thundernest/fix_ImapResponseParser

Fix bugs in `ImapResponseParser`
parents 7679132e 98b59f17
Loading
Loading
Loading
Loading
+14 −3
Original line number Diff line number Diff line
@@ -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");
        }
    }
}
+4 −0
Original line number Diff line number Diff line
@@ -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);
    }
+46 −2
Original line number Diff line number Diff line
@@ -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