Skip to content

Issue 507 Restored Tab Refused Every Save

Ed Mozley edited this page Dec 5, 2026 · 1 revision

πŸ”„ #507 A tab the browser restored refused every save

GitHub #507, reported by @pappas81 Β· Tickets, and every page Β· fixed after 5.19.0, in 6.0.0

What you saw

The browser (Brave, set to reopen its tabs when it starts) brought back the Tickets inbox after the computer had been off. The page looked fine, and the list even kept itself up to date - but clicking the mail icon did nothing: no message, no spinner, no new mail. The only sign was a red line in the browser's developer tools:

{"success": false, "error": "Your page is out of date or your session changed. Reload the page and try again.", "code": "csrf_token"}

Reloading the page fixed it, every time.

What was wrong

It was two faults, one hiding the other.

1. The page's security token had gone out of date. Every page gets a CSRF token when it is drawn - a secret that proves a request to change something came from FreeITSM's own page and not from another website. assets/js/csrf.js adds it to every request that changes something, and the server refuses any request whose token does not match the session's. The page read its token once:

var meta = document.querySelector('meta[name="csrf-token"]');
var TOKEN = meta ? meta.getAttribute('content') : '';

A browser that restores its tabs shows each page from its cache, as it was. Behind it, the old session had ended while the computer was off, and Keep me signed in started a new one on the page's first request - with a new token. From then on the page sent the old token, and every Check mail, Save and Assign was refused with 403. The live updates kept working because they only read (GETs carry no token), which is what made the page look healthy.

2. The inbox swallowed the refusal. The mail check threw every failure away, even when somebody had just clicked the button:

const data = await res.json();
if (data.success) totalNew += data.details?.emails_saved || 0;
} catch (e) { /* skip */ }

So nothing appeared on screen at all.

It was every page, not one

The out-of-date token was never about mail. Any page left open while its session was replaced - a restored tab, a laptop that slept past the session, Keep me signed in after a server restart - refused every save until a reload. The inbox was just where it showed first, because it saves something every minute.

The fix

The page fetches the new token by itself. When a request csrf.js stamped comes back refused for its token (403, X-CSRF-Refused: token), it asks a new endpoint, api/system/csrf_token.php, for the session's current token, starts using it, and sends a fetch() again once:

var sent = origFetch.call(self, input, init);
if (!stamped) return sent;
return sent.then(function (res) {
    if (!refusedForToken(res.status, res.headers.get('X-CSRF-Refused'))) return res;
    return refreshToken(origFetch).then(function (changed) {
        if (!changed || !replayable) return res;
        var again = Object.assign({}, init);
        var h2 = new Headers(again.headers);
        h2.set(HEADER, TOKEN);
        again.headers = h2;
        return origFetch.call(self, input, again);
    });
});
  • Requests refused together share one token request.
  • An XMLHttpRequest is not sent again (its body cannot be replayed safely), but it fetches the new token, so the next click works.
  • Never retried: a refusal because the request came from another website, a signed-out session, a token the code set itself, and a body that cannot be sent twice.

The new endpoint only answers FreeITSM's own pages. It hands out the token, so it is the part that needed the most care:

if (csrfSameOrigin() === false) csrfRefuse('origin');

if (!csrfSessionIsAuthenticated()) {
    http_response_code(401);
    echo json_encode(['success' => false, 'error' => 'Not authenticated']);
    exit;
}

Another site's page is refused before anything is read, the reply carries no CORS header (so another site could not read it anyway), and a TRAP: comment says it must never be added to the CSRF exemptions.

A clicked mail check says why it failed. If a mailbox cannot be checked when you click the mail icon, a message names it and gives the error. The check every 60 seconds stays quiet, so a mailbox that is down does not pop up a message every minute.

Before - a clicked check, nothing on screen After - the mailbox's own error
Before: nothing After: "ZZ507 Support mailbox: IMAP connection ... failed"

One more place, same rule. assets/js/email-report.js (Email me this report) set the token header itself. csrf.js leaves a header the code set alone, so that request would not have been retried. It now lets csrf.js add it, like every other request. That rule is now a code trap: never set X-CSRF-Token yourself, and never keep a copy of the token.

How it was proved

On a test install, the reporter's situation was rebuilt: the Tickets inbox open, then the session given a new token behind it, as a new session would have.

Before After
Check mail 403 "Your page is out of date" 200 - reached the mailbox check
The next save on the same page 403 200
An XMLHttpRequest save, twice 403, 403 403, then 200
Clicking the mail icon with a mailbox that cannot be reached nothing on screen a message naming the mailbox and the error

The new endpoint, called directly: 200 with the token from the same site; 403 from another site and from a sibling subdomain; 401 signed out; no Access-Control-Allow-Origin in any reply.

tests/csrf-token-refresh.php (new, 11 checks) runs the shipped csrf.js in Chrome against a stand-in server: refresh and resend once with the same body, one token request for two refusals, the new token used straight away, and every "never retried" case. Control: with the csrf.js from before the fix, 6 of the 11 fail. tests/csrf.php (30 checks, one new), keep-signed-in (43), live-pulse (22), live-pulse-signed-out (9) and get-must-not-write (18) all pass.

Not proven: the test reproduces the new session by changing the token in the session file, not by restarting Brave - the effect on the page is the same, but Brave's own restore was not run.

πŸ“ Files

File Change
πŸ”΄ assets/js/csrf.js Fetch the current token and resend a refused fetch() once; XHR fetches it for next time
πŸ”΄ api/system/csrf_token.php New - the session's current token, for this site's own signed-in pages only
🟑 includes/csrf.php csrf-refresh meta tag: where to ask
🟑 tickets/index.php A clicked mail check shows why a mailbox failed
🟑 assets/js/email-report.js Lets csrf.js add the token (and retry)
🟒 tests/csrf-token-refresh.php, tests/csrf.php New test; the order check allows the new tag

What this means for you

Upgrade and hard-refresh (Ctrl-F5) once. After that, a tab your browser brings back - or one left open while your session changed - keeps working without a reload, and a mail check you click tells you when a mailbox has a problem.

Related

FreeITSM

Getting Started

Modules

Multi-tenancy (planned)

Responses to feedback

Blue sky thinking

Bugs resolved

Pull requests

Links

Clone this wiki locally