Files
vibpe 7a1eb67db2 Send the 419 status with a reason phrase so Apache does not answer 500 (#1209)
Fixes #1208 - add reason phrase with http 419 response (used with  invalid or missing CSRF token is answered with the "Session expired"), otherwise Apache returns a http 500 response instead.

thanks @vibpe
2026-11-20 13:56:53 +00:00

30 lines
1.6 KiB
PHP

<?php
require_once(dirname(__FILE__) . '/../common.php');
set_exception_handler(function ($exception) {
// we could do something much better here - e.g. if the user is logged in, and an admin, show a full stack trace etc.
if ($exception instanceof CsrfInvalidException) {
// 419 is not a registered status code: without an explicit reason phrase
// Apache replaces the status line with "500 Internal Server Error".
// Always HTTP/1.1: mod_php only applies a status line starting with "HTTP/1.".
header('HTTP/1.1 419 Page Expired', true, 419);
$title = Config::lang('session_expired_title') ?: 'Session expired';
$message = Config::lang('session_expired_message') ?: 'The page token is no longer valid. Please return and try again.';
$button = Config::lang('session_expired_button') ?: 'Continue';
echo "<!doctype html>\n";
echo "<html><head><meta charset=\"utf-8\"><title>" . htmlentities($title) . "</title></head><body>\n";
echo "<p><strong>" . htmlentities($title) . "</strong></p>\n";
echo "<p>" . htmlentities($message) . "</p>\n";
echo "<p><a href=\"main.php\">" . htmlentities($button) . "</a></p>\n";
echo "</body></html>\n";
exit(0);
}
error_log("PostfixAdmin : untrapped exception. " . $exception->getMessage() . " - " . $exception->getTraceAsString());
http_response_code(500);
echo "<p><strong>PostfixAdmin Error:</strong></p><p>PostfixAdmin encountered an error. More details are in the server log files (e.g. /var/log/apache2/error.log).</p>";
});