Fixed php 8 + unfurl - mb_convert_encoding can throw ValueError

Xon

Well-known member
Affected version
2.2.6 Patch 2
On fetching https://www.toei-anim.co.jp:443/tv/digimon_t/chara/dukemon.html

A ValueError is thrown and the unfurl is not marked as failed.
Code:
ValueError: mb_convert_encoding(): Argument #3 ($from_encoding) contains invalid encoding "none" src/XF/Http/Metadata.php:200
Generated by: Unknown account Sep 9, 2021 at 10:47 PM
Stack trace
#0 src/XF/Http/Metadata.php(200): mb_convert_encoding('<html>
<head>
<...', 'utf-8', 'none')
#1 src/XF/Http/Metadata.php(35): XF\Http\Metadata->convertBodyCharset()
#2 src/XF/Http/MetadataFetcher.php(129): XF\Http\Metadata->__construct(Object(XF\Pub\App), '<html>
<head>
<...', Array, 'http://www.toei...')
#3 src/XF/Service/Unfurl/Fetcher.php(22): XF\Http\MetadataFetcher->fetch('http://www.toei...', NULL)
#4 unfurl.php(57): XF\Service\Unfurl\Fetcher->fetch()
#5 {main}

The URL returns the following headers;
Code:
HTTP/1.1 200 OK
Date: Thu, 09 Sep 2021 15:57:05 GMT
Content-Type: text/html; charset=none
Content-Length: 4982
Connection: keep-alive
Server: Apache
Last-Modified: Mon, 18 Nov 2013 07:27:01 GMT
ETag: "1376-4eb6e794f1b40"
Accept-Ranges: bytes

Content-Type: text/html; charset=none makes php 8 sad.
 

Xon

Well-known member
Before php 7.4, the unfurl would fail and it would just render as the title "LN^[" which isn't really correct either. But at least it didn't cause an error
 

Chris D

XenForo developer
Staff member
Content-Type: text/html; charset=none makes php 8 sad.
Me too :(

Ironically the meta tag in the HTML output has it as Shift_JIS and that would be a valid encoding. Though somewhat annoyingly if we checked that against mb_list_encodings then it wouldn't be found. Which is my current approach to protecting against this error so this may not be fixable for this site.

Alternative would be to do a try and catch the ValueError but then we have to have a different code path for PHP 8.

Final alternative would be to bail out entirely if it's an unknown charset but as I just pointed out Shift_JIS is supported, it just isn't listed. That would turn it into a basic link at least.

SAD
 

XF Bug Bot

XenForo bug fixer bot
Staff member
Thank you for reporting this issue, it has now been resolved. We are aiming to include any changes that have been made in a future XF release (2.2.7).

Change log:
When fetching metadata from a URL improve checks to ensure we get a valid charset encoding where possible and prevent against an Error exception thrown if an invalid character set encoding is encountered.
There may be a delay before changes are rolled out to the XenForo Community.
 
Top