Simple Machines Community Forum

SMF Support => SMF 2.1.x Support => Topic started by: sah62 on February 04, 2023, 08:18:39 AM

Title: Another "Trying to access array offset on value of type null" Situation
Post by: sah62 on February 04, 2023, 08:18:39 AM
This is with SMF 2.1.3 and PHP 8.1.2. The only mod installed is "Forum Width Setting".

Can someone help identify the cause of these errors? They occur in these batches of three, in this order, associated with a "Guest" user:

Quotehttps://www.mysite.com/smf/index.php?action=dlattach;attach=110599
/var/www/mysite/smf/Sources/Security.php (Line 315)  Backtrace information

Type of error: General
Error messageSelect
2: Trying to access array offset on value of type null

314: // 'Log' the user out.  Can't have any funny business... (save the name!)
==>315: $old_name = isset($user_info['name']) && $user_info['name'] != '' ? $user_info['name'] : $txt['guest_title'];
316: $user_info['name'] = '';

Quotehttps://www.mysite.com/smf/index.php?action=dlattach;attach=110599
/var/www/mysite/smf/Sources/Security.php (Line 325)  Backtrace information

Type of error: General
Error messageSelect
2: Trying to access array offset on value of type null

322: $context['user'] = array(
323: 'id' => 0,
324: 'username' => '',
==>325: 'name' => $txt['guest_title'],
326: 'is_guest' => true,
327: 'is_logged' => false,
328: 'is_admin' => false,
329: 'is_mod' => false,
330: 'can_mod' => false,
331: 'language' => $user_info['language'],
332: );

Quotehttps://www.mysite.com/smf/index.php?action=dlattach;attach=110599
/var/www/mysite/smf/Sources/Security.php (Line 348)  Backtrace information

Type of error: General
Error messageSelect
2: Trying to access array offset on value of type null

347: // You banned, sucka!
==>348: fatal_error(sprintf($txt['your_ban'], $old_name) . (empty($_SESSION['ban']['cannot_access']['reason']) ? '' : '<br>' . $_SESSION['ban']['cannot_access']['reason']) . '<br>' . (!empty($_SESSION['ban']['expire_time']) ? sprintf($txt['your_ban_expires'], timeformat($_SESSION['ban']['expire_time'], false)) : $txt['your_ban_expires_never']), false, 403);

Any clues? How can a value like $txt['guest_title'] not be set properly?
Title: Re: Another "Trying to access array offset on value of type null" Situation
Post by: sah62 on February 10, 2023, 07:26:42 AM
This forum is using a custom theme. The value of $txt['guest_title'] is set in the index.english.php file that exists in the "languages" directory associated with the theme. Could this be a theme issue?
Title: Re: Another "Trying to access array offset on value of type null" Situation
Post by: Sesquipedalian on February 10, 2023, 01:32:58 PM
Quite possibly.

If the theme has its own copy of index.english.php (replace "english" with whatever language you're using), and its copy is missing that string, that would be one way for this to happen.

Another possibility is that the theme implements some custom code that creates a way for a login error to occur before the language files have been loaded.
Title: Re: Another "Trying to access array offset on value of type null" Situation
Post by: sah62 on February 22, 2023, 09:30:41 AM
I see this code in the index.php file in the forum's root directory:

        // Attachments don't require the entire theme to be loaded.
        if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'dlattach' && empty($maintenance))
                detectBrowser();
        // Load the current theme.  (note that ?theme=1 will also work, may be used for guest theming.)
        else
                loadTheme();

        // Check if the user should be disallowed access.
        is_not_banned();

As noted in my original post, "action=dlattach" is being passed as a query parameter, so it looks like
loadTheme(); isn't called. Is that an error, or is this user doing something that they shouldn't be able to do under normal circumstances? The call to
is_not_banned(); leads to this error.
Title: Re: Another "Trying to access array offset on value of type null" Situation
Post by: Arantor on February 22, 2023, 10:37:06 AM
Deliberate.

The dlattach action is the one reserved for serving attachments, where there is no need to load the theme normally because even a file-not-found case returns a minimal bare page (as expected).

is_not_banned probably should call loadTheme() at line 314 if it notes that $txt is null.
Title: Re: Another "Trying to access array offset on value of type null" Situation
Post by: Diego Andrés on February 22, 2023, 10:41:24 AM
Something like this happened with a setting too.
See: https://github.com/SimpleMachines/SMF/pull/7589

It's possible this is similar and could be fixed the same way, just like you are proposing.
Title: Re: Another "Trying to access array offset on value of type null" Situation
Post by: sah62 on February 22, 2023, 10:53:25 AM
Is this worth reporting as a bug?
Title: Re: Another "Trying to access array offset on value of type null" Situation
Post by: Diego Andrés on February 22, 2023, 10:54:49 AM
Yes of course.
Make sure you provide the steps to reproduce it.
Title: Re: Another "Trying to access array offset on value of type null" Situation
Post by: sah62 on February 22, 2023, 10:59:17 AM
Quote from: Diego Andrés on February 22, 2023, 10:54:49 AMYes of course.
Make sure you provide the steps to reproduce it.

I don't know exactly what this guest user is doing to cause the error. All I can see is what I have in my logs.
Title: Re: Another "Trying to access array offset on value of type null" Situation
Post by: Arantor on February 22, 2023, 11:04:42 AM
They're banned and going directly to an attachment URL somehow.