Laravel 8 - Getting randomly "No application encryption key has been specified"

Viewed 347

Please try to read & answer to the question asked, instead of suggesting improvements to public-routing which isn't there by an accident.


Edit #5 (updated @ 20.10.2021) Unfortunately the question remains unanswered. I'm really baffled by this, but even more so, this error hasn't been showing up for past few days, and I have no idea what happened.

I used to have custom static "User::hasRole(['...']"-method which fetched users userRoles everytime roles were checked. Biggest change I made was caching user-roles to session, so that there wouldn't be need to fetch 'em every time (as dozens of rest-queries were referring to checking roles from DB over and over again as part of querybuilders filters).

$userRoles = Session()->get('userRoles', function() {
    $user = User::with(['userRoles'])->find(Auth::user()->id);
    Session()->put('userRoles', $user->userRoles);
    return $user->userRoles;
});

I know this solution could cause an issue at certain points, but due to the nature of my project it's almost nonexistent risk.

These random errors never seemed to point to one specific method and nothing pointed this direction. It looks like this error will remain somewhat a mystery, but at the time being it's not causing issues anymore. Best I can guess is that there were too many parallel PHP/SQL-threads, which somehow lead to kernel pipeline failing at random states when authentication somehow failed.

Thanks everyone who tried to help.

Edit #4 (updated @ 14.9.2021) Changing cache driver from file to array didn't resolve issue. Single endpoint just returned "unauthenticated"-message, even though user is authenticated.

Edit #3, Problem persists, trying to sum up:

  • Key HAS BEEN generated, even regenerated multiple times
  • Laravel 8, XAMPP
  • Have run config:cache multiple times. If config-cache has been cleared the problem happens more often, but currently maybe 1/100-200 REST calls end up failing more or less randomly. This doesn't happen on LAMP production-server.
  • This problem causes randomly different errors, all somehow related to user session
  • No application encryption key
  • 401 - unauthenticated
  • SQL Connection error (even though either refreshing page / rerunning post works like a charm, and only 1 of ~20 REST-actions on single page load might end up to this state).
  • Doesn't seem to be CSRF-token related error since sessions are live (as other REST-calls are not failing), but for some reason Axios doesn't report XSRF-token on RESPONSE-headers whenever this error occurs. Token is present on REQUEST-headers... still looks like backend is unable to find session?
  • Currently using file-based session handling, going to try memory-based

--

I'v been having an issue with Laravel producing HTTP code 500 - "No application encryption key has been specified" -error randomly on my rest-routes. Of course I have set, and even reset the key with Artisan, so that shouldn't be the issue.

This issue has always been present, ever since I started my project on Laravel 6. Quite quickly I was able to track issue to cache. Basically when cache was wiped with "php artisan cache:clear" REST-routes started to fail, about 5% of all the requests. When cache was generated again with "php artisan config:cache" the issue was resolved. For some reason it seems that .env -variables are not registered or read correctly when cache is wiped? Still, wasn't an issue when developing and cache was configured.

I'v upgraded my project from Laravel 6 to 8 and now it seems that this issue has started to reproduce and I'm unable to find the reason.

My application has ~10-20 initial atomic REST-endpoint accesses for providing basic information for Vue SPA initialization. All of these requests happen asynchronously and every-now-and-then (maybe once per 10 page refreshes / reinitializations) one of those calls might fail.

Running things over latest XAMPP. Not using XDebug or anything else which might block calls. Wondering if anyone else has stumbled to this problem?

EDIT:

Additionally I might get very random "401 - unauthenticated" -errors. The problem is very similar to application key -error. For an example this picture was taken after ~20 minutes activity on web-browser while jumping from one page to another. All the other calls before and after were behind authentication as well, but this single 401 came out of nowhere.

enter image description here

Edit 2:

Changing lottery-number didn't fix.

2 Answers

EDIT: Unfortunately this wasn't the solution. I thought that maybe sessions were mixing or something similar, but nope...

I believe I finally found answer.

For previous Laravel version (5 & 6) it seems that problem was always with cache, as clearing it had resolved this problem, but moving from 6 -> 8 really brought this issue up.

I had different session-related problem at hand, which finally lead to detecting that something had changed on Laravel 8. My previous logout was

public function logout(Request $request) {
    Auth::logout();
    return redirect('/login');
}

... but official documentation pointed out that it should be:

public function logout(Request $request) {
    Auth::logout();

    $request->session()->invalidate();
    $request->session()->regenerateToken();

    return redirect('/login');
}

Apparently session needs to be manually invalidated, as well token being regenerated. I don't know the exact reason, but these two additional lines seemed to remove previous random errors. Most likely some old sessions were active and were brought up at random times on REST-API-routes.

I'll continue monitoring situation and will report if the problem persists, but as of now it looks like issue has been resolved.

Edit, additional details:

My LoginController contains logout-method, which is most likely an overwrite of "AuthenticatesUsers.php". The original method looks like following:

public function logout(Request $request) {
    $this->guard()->logout();

    $request->session()->invalidate();

    $request->session()->regenerateToken();

    if ($response = $this->loggedOut($request)) {
        return $response;
    }

    return $request->wantsJson()
        ? new JsonResponse([], 204)
        : redirect('/');
}

Basically keep using original logout-method or use comprehensive enough solution when overwriting. Can't really remember why I simplified this with more straight-forward overwriting method, but this was most likely the source of weird behaviour.

Seems you have defined the root of your application incorrectly.. configure your web-server so that the root is

/path/to/your/laravel/application/public

Note: /public at the end

Related