Skip to content

Illuminate\Support\Str::contains crashes on php 8.0.1 #35891

@jesseheidstra

Description

@jesseheidstra
  • Laravel Version: 8.22.1
  • PHP Version: 8.0.1
  • Database Driver & Version: mysql 15.1/mariaDB 10.2.36

Description:

The string support method contains throws an exception when $haystack is not a string.

Example:
Running any request that interacts with the InteractsWithContentTypes trait and uses the isJson method crashes. Make sure to send the request without a content-type header, this causes the $haystack variable to be null and throws an exception.

Php 8.0.0 and older always returned false
Php 8.0.1 throws an exception

Steps To Reproduce:

Install the above php version and laravel version. Request an API without a content-type header. Response should be a 500 server error.

The following code is what crashes.

public static function contains($haystack, $needles)
    {
        foreach ((array) $needles as $needle) {
            if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
                return true;
            }
        }

        return false;
    }

Exception: Uncaught TypeError: mb_strpos(): Argument #1 ($haystack) must be of type string, null given

The contains method should either type-hint a string or internally check if its a string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions