Skip to content

[5.x]: Invalid token "An internal server error occurred." instead of 400 bad request #16630

@jeroenlammerts

Description

@jeroenlammerts

What happened?

Description

When having an invalid token in the url https://craftcms.com/?token=! you get a 500 server error in production mode instead of 400 bad request.

After debugging vendor/yiisoft/yii2/base/ErrorHandler.php:164

 catch (\Throwable $e) {
     echo $e->getMessage() . "\n";
     echo $e->getTraceAsString() . "\n";
     exit;
     // additional check for \Throwable introduced in PHP 7
     $this->handleFallbackExceptionMessage($e, $exception);
}

I get the error Typed property craft\web\Application::$edition must not be accessed before initialization
It has something to do with vendor/craftcms/cms/src/base/ApplicationTrait.php _preInit() where the edition is set after $this->getLog();
Setting the edition before $this->getLog(); looks like it solved the problem.

Steps to reproduce

  1. Set CRAFT_ENVIRONMENT=production in .env file
  2. Go to your site https://mysite.test/?token=!

Expected behavior

A styled page saying
Bad Request
Invalid token

Actual behavior

In dev mode you get a HTTP 400 – Bad Request
In production mode you get an unstyled "An internal server error occurred." 500 error

Craft CMS version

5.6.4

PHP version

8.3

Operating system and version

No response

Database type and version

No response

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions