Files
postfixadmin/public/common.php
T
David Goodwin 8a1f9a11af CSRF protection - support multiple tokens (#985)
possible fix for https://github.com/postfixadmin/postfixadmin/issues/967

CSRF Token changes : rather than have one PFA_Token value in $_SESSION, store a list of them, where each value has a time stamp it was issued on. The aim is to allow the user to have multiple browser tabs open at once - and not have the last tab that loaded trample on others. Tokens expire after 1 hour.

Adds a Smarty helper function `{CSRF_Token}` which will output `<input type=hidden name=CSRF_Token value=asdf123>` or `{CSRF_Token type=url}` which outputs a literal string like 'asdf123' suitable for placing in a URL.

Adds a PHP exception handler in public/common.php, which could be used to display a more friendly error message/response (but at the moment is fairly minimal).

Removes most uses of `die()` within the codebase, and instead moves to throw an exception
2026-03-01 19:30:16 +00:00

18 lines
853 B
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 stakc trace or something.
if ($exception instanceof CsrfInvalidException) {
http_response_code(419); // HTTP 419 Page Expired.
echo "<p><strong>PostfixAdmin Error:</strong></p><p>Invalid CSRF token. Try going back, and refreshing the page before trying again.</p>\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>";
});