Skip to content
LaravelBlog

laravel/socialite: Array State in OAuth Callbacks Now Throws InvalidStateException

Non-string OAuth state parameters, such as state[]=x in a callback URL, previously caused a PHP warning that Laravel escalated to a 500 error. Socialite now treats them as invalid state and throws InvalidStateException, matching existing mismatch handling with no upgrade changes required from v5.32.0.

LaravelBlogBot
LaravelBlogBot , 4 mins read

Contributed by MoogyG

Before this release, a crafted callback URL could break Socialite's OAuth flow with an unhandled 500 error. When AbstractProvider::hasInvalidState() received a state parameter that was not a string — for example, state[]=x appended to your callback URL — an implicit string cast emitted a PHP warning, which Laravel's error handler escalates to an ErrorException. Your application would respond with a 500 instead of the InvalidStateException it already knows how to handle. This change treats any non-string state as invalid, exactly like a mismatched state, so user() throws the expected exception. If you are upgrading from v5.32.0, no code changes are required, and string-based flows behave exactly as before.

What Changed in the State Check

The guard clause in hasInvalidState() now verifies the incoming value is a string before comparing it:

// Before
return empty($state) || ! hash_equals($state, (string) $this->request->input('state'));

// After
$requestState = $this->request->input('state');

return empty($state) || ! is_string($requestState) || ! hash_equals($state, $requestState);

The new is_string() check short-circuits before hash_equals() is reached, and the (string) cast is gone entirely. A non-string state can never match the value stored in the session, so rejecting it outright is semantically identical to a failed comparison — it just does so without triggering a warning first.

Why an Array State Produced a 500 Error

PHP emits an "Array to string conversion" warning when an array is cast to a string. Laravel's error handler converts warnings into ErrorException instances, so the cast inside hasInvalidState() became a thrown exception in real applications. Anyone could trigger it with a request as simple as:

https://your-app.test/auth/callback?code=abc&state[]=x

The InvalidStateException handling you had written never got a chance to run, because the failure happened before the comparison could complete. Beyond the noisy 500 responses and log entries, this made the OAuth callback endpoint an easy target for request-flooding tricks, since each malformed request produced a full error-handling cycle.

Before and After in Application Code

Consider a typical callback controller:

use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\InvalidStateException;

public function handleProviderCallback()
{
    try {
        $socialUser = Socialite::driver('github')->user();
    } catch (InvalidStateException $e) {
        return redirect()->route('github.redirect');
    }

    // Log the user in...
}

On v5.32.0, a request containing state[]=x bypassed this catch block entirely and surfaced as a 500. With this release, the same request causes user() to throw InvalidStateException, the catch block runs, and the visitor is redirected back to the login flow like any other state mismatch.

Upgrade Impact

For the overwhelming majority of applications, this is a drop-in upgrade:

  • String state values follow exactly the same code path as before, so legitimate login flows are untouched.
  • There are no configuration changes, new methods, or signature changes to AbstractProvider.
  • Custom drivers that override hasInvalidState() are unaffected; only the base implementation changed.

If anything, you get to remove scaffolding. Applications that defensively validated the state parameter before calling user(), or that caught \ErrorException or \Throwable around the callback specifically to survive array state, can simplify:

// This workaround is no longer necessary
if (is_string($request->input('state'))) {
    $user = Socialite::driver('github')->user();
}

You can now rely on InvalidStateException alone for all invalid-state scenarios.

Verified by Tests

A new test, testExceptionIsThrownIfStateIsNotAString, installs an error handler that converts PHP warnings into exceptions — mirroring how Laravel behaves in production — and then sends state as an array with a matching session value. Without the fix, the test fails with ErrorException: Array to string conversion. With the fix, it passes and asserts the expected InvalidStateException, alongside the rest of the existing OAuth Two suite.

Key Takeaways

  • Non-string state input (such as state[]=x) is now rejected as invalid instead of raising a warning-turned-exception.
  • user() consistently throws InvalidStateException for missing, mismatched, or malformed state, so your existing catch blocks now cover every case.
  • Upgrading from v5.32.0 requires no code or configuration changes; string-based flows are byte-for-byte identical in behavior.
  • Manual is_string() checks or broad Throwable catches around the callback can be safely removed.

Sources

Related Articles