Skip to content

Conversation

@f2cmb
Copy link
Contributor

@f2cmb f2cmb commented Oct 22, 2025

Fix AJAX requests returning HTTP 500 instead of 401 when session expires.

The AccessErrorListener had an early return for AJAX requests that prevented handling of SessionExpiredException. This has been fixed by processing SessionExpiredException before the
AJAX check.

For AJAX requests with expired sessions, the listener now returns a proper JSON 401 response:

  {
    "error": "Your session has expired",
    "message": "Please log in again",
    "code": "ERROR_SESSION_EXPIRED"
  }

HTML requests continue to redirect to the login page as before. Other exception handling remains unchanged.

Warning : This fix applies to all 122 AJAX endpoints in the application. Low risk of impact, though frontend handling of 401 responses should be monitored after deployment.

@f2cmb
Copy link
Contributor Author

f2cmb commented Oct 22, 2025

No tests added for the moment as i am not sure about potential impact : need pre-review first.

@trasher
Copy link
Contributor

trasher commented Oct 23, 2025

I'm not confident enough with new routiong system, I'll wait for @cedric-anne review.

Comment on lines 83 to 88
// For AJAX/JSON requests, return a JSON response with 401 status
$response = new JsonResponse([
'error' => __('Your session has expired'),
'message' => __('Please log in again'),
'code' => 'ERROR_SESSION_EXPIRED',
], 401);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing back a HttpException should permit to have it correctly handled by the ErrorController.

Suggested change
// For AJAX/JSON requests, return a JSON response with 401 status
$response = new JsonResponse([
'error' => __('Your session has expired'),
'message' => __('Please log in again'),
'code' => 'ERROR_SESSION_EXPIRED',
], 401);
// For AJAX/JSON requests, convert the error into a HttpException
$http_exception = new \Glpi\Exception\Http\HttpException(403, 'Session expired.');
$http_exception->setMessageToDisplay(__('Your session has expired. Please log in again.'));
throw $http_exception;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed here 2d6d33b but i kept 401 Response Code.

@f2cmb f2cmb marked this pull request as ready for review October 28, 2025 08:15
Copy link
Contributor

@trasher trasher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the $event->setResponse($response); is wrongly conditionned

@trasher
Copy link
Contributor

trasher commented Oct 28, 2025

Seems like the $event->setResponse($response); is wrongly conditionned

Nevermind, it's OK

@cedric-anne cedric-anne merged commit 046ea30 into glpi-project:11.0/bugfixes Oct 29, 2025
10 checks passed
@cedric-anne cedric-anne added this to the 11.0.2 milestone Oct 29, 2025
@f2cmb f2cmb deleted the 11.0/21557 branch October 29, 2025 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Requests to /ajax/notifications_ajax.php return a 500 error when session is expired

4 participants