freescout-help-desk/freescout

mb_convert_encoding() error while fetching certain emails

Closed this issue · 8 comments

While fetching certain emails, the following error is logged in laravel log:

mb_convert_encoding(): Argument #3 ($from_encoding) contains invalid encoding "windows-1257" {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): mb_convert_encoding(): Argument #3 ($from_encoding) contains invalid encoding \"windows-1257\" at /srv/freescout/overrides/webklex/php-imap/src/Message.php:748)
[stacktrace]
#0 /srv/freescout/overrides/webklex/php-imap/src/Message.php(748): mb_convert_encoding()
#1 /srv/freescout/overrides/webklex/php-imap/src/Message.php(570): Webklex\\PHPIMAP\\Message->convertEncoding()
#2 /srv/freescout/overrides/webklex/php-imap/src/Message.php(542): Webklex\\PHPIMAP\\Message->fetchPart()
#3 /srv/freescout/overrides/webklex/php-imap/src/Message.php(526): Webklex\\PHPIMAP\\Message->fetchStructure()
#4 /srv/freescout/overrides/webklex/php-imap/src/Message.php(272): Webklex\\PHPIMAP\\Message->parseRawBody()
#5 /srv/freescout/vendor/webklex/php-imap/src/Query/Query.php(259): Webklex\\PHPIMAP\\Message::make()
#6 /srv/freescout/vendor/webklex/php-imap/src/Query/Query.php(347): Webklex\\PHPIMAP\\Query\\Query->make()
#7 /srv/freescout/vendor/webklex/php-imap/src/Query/Query.php(313): Webklex\\PHPIMAP\\Query\\Query->populate()
#8 /srv/freescout/vendor/webklex/php-imap/src/Query/Query.php(368): Webklex\\PHPIMAP\\Query\\Query->curate_messages()
#9 /srv/freescout/app/Console/Commands/FetchEmails.php(223): Webklex\\PHPIMAP\\Query\\Query->get()
#10 /srv/freescout/app/Console/Commands/FetchEmails.php(128): App\\Console\\Commands\\FetchEmails->fetch()
#11 [internal function]: App\\Console\\Commands\\FetchEmails->handle()
#12 /srv/freescout/overrides/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array()
#13 /srv/freescout/overrides/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#14 /srv/freescout/overrides/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod()
#15 /srv/freescout/overrides/laravel/framework/src/Illuminate/Container/Container.php(549): Illuminate\\Container\\BoundMethod::call()
#16 /srv/freescout/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\\Container\\Container->call()
#17 /srv/freescout/vendor/symfony/console/Command/Command.php(255): Illuminate\\Console\\Command->execute()
#18 /srv/freescout/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\\Component\\Console\\Command\\Command->run()
#19 /srv/freescout/vendor/symfony/console/Application.php(992): Illuminate\\Console\\Command->run()
#20 /srv/freescout/vendor/symfony/console/Application.php(255): Symfony\\Component\\Console\\Application->doRunCommand()
#21 /srv/freescout/vendor/symfony/console/Application.php(148): Symfony\\Component\\Console\\Application->doRun()
#22 /srv/freescout/vendor/laravel/framework/src/Illuminate/Console/Application.php(88): Symfony\\Component\\Console\\Application->run()
#23 /srv/freescout/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(121): Illuminate\\Console\\Application->run()
#24 /srv/freescout/artisan(62): Illuminate\\Foundation\\Console\\Kernel->handle()
#25 {main}
"}

What is the worst, is that other emails (not problematic otherwise) that arrive at the same time (fetched together) do not get processed (converted to issues), but are marked as read on IMAP.

The relevant headers of the email causing the problem (sensitive information removed):

From: =?windows-1257?Q?=DEi********s?= <********>
Subject:
    =?windows-1257?Q?RE:_N********a=F0o_p********s?=
    =?windows-1257?Q?_2024-05?=
Thread-Topic:
    =?windows-1257?Q?N********a=F0o_p********s_202?=
    =?windows-1257?Q?4-05?=

A possible fix could be to use mb_list_encodings() to check if an encoding is supported before doing the conversion.

PHP version: 8.1.28
FreeScout version: 1.8.141
Database: PostgreSQL 13.7
Are you using CloudFlare: No
Are you using non-official modules: No

Can you share here the EML file of the email or send it to support@freescout.net

Unfortunately, we are not allowed to distribute emails from customers, but I could try to reproduce the error by creating an email with the unsupported windows-1257 encoding in the headers myself, if that would be helpful.

We can't reproduce the issue on our end. Is everything OK on your "Manage > Status" page?

PHP version: 8.1.28

It looks like your are using PHP 8.1

As it's said in the Installation Guide: https://github.com/freescout-helpdesk/freescout/wiki/Installation-Guide
2024-06-01_18-39-43

In fact, it is not about the headers, it is about the body, my bad. Please find the sample attached:
Test.zip

Please note that the most serious part of this issue is that an exception during processing of one email (which can be just technically incorrect, or crafted on purpose) cancels the processing of other emails fetched at the same time, which means a loss of data.

It looks like your are using PHP 8.1

Yes. I wasn't aware of that warning (maybe it was not known at that time, or I just overlooked). Will upgrade to 8.2, and check the results.

Fixed in the master branch and will be published in the next release.

@freescout-helpdesk thanks!
Just for the record - upgraded to PHP 8.2.19, the result was the same.