Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error token length mismatch in readResponse #400

Open
michalkkkd opened this issue Apr 26, 2023 · 1 comment
Open

Error token length mismatch in readResponse #400

michalkkkd opened this issue Apr 26, 2023 · 1 comment
Labels
bug Something isn't working validated

Comments

@michalkkkd
Copy link

Describe the bug
readResponse method in ImapProtocol class, parses token by 2 chars, but later checks for 2 & 3 chars.
I'm not sure how to fix this in accordance with the protocol

Code to Reproduce
See comments with // PROBLEM

public function readResponse(Response $response, string $tag, bool $dontParse = false): array {
        $lines = [];
        $tokens = ""; // define $tokens variable before first use
        do {
            $readAll = $this->readLine($response, $tokens, $tag, $dontParse);
            $lines[] = $tokens;
        } while (!$readAll);

        if ($dontParse) {
            // First two chars are still needed for the response code
            $tokens = [substr($tokens, 0, 2)];
            // PROBLEM this will always result in $tokens[0], having 2 chars
        }

        // last line has response code
        if ($tokens[0] == 'OK') {
            return $lines ?: [true];
            // PROBLEM So this check for BAD or BYE will never happen
        } elseif ($tokens[0] == 'NO' || $tokens[0] == 'BAD' || $tokens[0] == 'BYE') {
            throw new ImapServerErrorException();
        }

        throw new ImapBadRequestException();
    }

Expected behavior
Correct handling of error tokens.

Desktop / Server (please complete the following information):

  • Version 5.2.0
@Webklex Webklex added bug Something isn't working validated labels Jun 27, 2023
@Webklex
Copy link
Owner

Webklex commented Jun 27, 2023

Hi @michalkkkd,
thanks a lot for reporting this issue. I really appreciate it!

I just pushed a fix. I've decided to use:

$tokens = [trim(substr($tokens, 0, 3))];

Since the $tokens variable could contain string like thsese:

OK [UIDNEXT 1] Predicted next UID
NO [UNAVAILABLE] User's backend down for maintenance
BAD [COMMAND] Error

Once again, thanks for taking the time and effort to make this library better!

Best regards and happy coding,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working validated
Projects
None yet
Development

No branches or pull requests

2 participants