Login Return PR Review - edmozley/freeitsm GitHub Wiki
Back to the page you were on after signing in - review
#407 by @cloudcogsio ยท Verdict: merge, with one small fix ยท 5.8.0 (#3032)
When a session ends while a page is open - an idle timeout, a sign-out in another tab, a server restart - the next click goes to the sign-in page. Until now, signing in then took you to your landing page, not the ticket, change or table you were on. Now it takes you back there. Opening the sign-in page yourself still goes to the landing page.
It works however the sign-in finishes: password, trusted device, MFA, single sign-on, and the forced password change.
How it works
Around a hundred pages and gates redirect to auth/login.php themselves. Rather than change each one, includes/functions.php installs one hook (includes/login_return.php):
header_register_callback(function () use ($here) {
foreach (headers_list() as $h) {
if (stripos($h, 'Location:') !== 0) continue;
$location = trim(substr($h, 9));
// Only the bare sign-in page, absolute or relative ('../auth/login.php').
if (preg_match('#(^|/)auth/login\.php$#', $location)) {
header('Location: ' . $location . '?return=' . rawurlencode($here));
}
return;
}
});
Just before the headers go out, a bare redirect to the sign-in page gains ?return=<the page asked for>. Only GET page requests get it: a JSON or XHR request, a script under auth/, and the landing page itself are left alone. auth/login.php keeps the target in the session, so it survives the POST, the MFA step (including cancelling it) and a single sign-on round trip. loginReturnTake() uses it once, so a later ordinary sign-in starts at the landing page again.
๐ด The danger, and how it was checked
An open redirect. The return value comes from the address bar, so anybody can send a sign-in link with any return in it. If FreeITSM sent people wherever it said, a phishing email could use a real FreeITSM sign-in page to land someone on a look-alike site straight after they signed in.
loginReturnSanitize() accepts only a path on this install: it must start with BASE_URL, and it is refused for a scheme or host, a leading //, any backslash or control character, a . or .. segment (also when percent-encoded), or anything under auth/, api/, setup/ or self-service/ (case-insensitive, since /AUTH/login.php is the same file on Windows). The .htaccess sign-in and sign-out aliases are refused too - otherwise signing out through /analyst_logout.php would sign you out again straight after every sign-in.
The review threw a list of known tricks at it, both for an install in a subfolder and at the web root (where BASE_URL is just /, the riskier case):
| Tried | Result |
|---|---|
https://evil.com, //evil.com, ///evil.com |
Refused |
/\evil.com, /%5C%5Cevil.com (browsers read a backslash as a slash) |
Refused |
/%2F%2Fevil.com, /./evil.com, /.//evil.com, /%2e/%2e/evil |
Refused |
| A raw tab, carriage return or null byte | Refused |
/AUTH/login.php, /Api/v1/..., /Analyst_Logout.php, /logout/ |
Refused |
/https://evil.com, /@evil.com, / /evil.com, /%09/evil.com, /x?y=//evil.com |
Accepted - and harmless: each is a path on this site. Only a leading // or a scheme can leave the site, and percent-encoded characters are not decoded in a Location header |
Then over real HTTP on a test install:
| Request | Went to |
|---|---|
Signed out, tasks/table/?q=1 |
auth/login.php?return=%2F...%2Ftasks%2Ftable%2F%3Fq%3D1 |
| Then a real password sign-in | tasks/table/?q=1 - back where they were |
A plain sign-in, no return |
index.php (the control) |
Signed in, return=//evil.com, https://evil.com, /\evil.com, .../analyst_logout.php |
index.php every time |
The branch's own tests/login-return-target.php passes 47/47, and the single sign-on, OIDC, web-exposure and .htaccess guards still pass.
What was fixed
One page the branch flagged itself: contracts/equipment-report.php sent signed-out visitors to ../login.php - a root alias - instead of ../auth/login.php, so it got no return address. It now uses the same page as every other gate, and does.
Traps
- TRAP: PHP keeps ONE header callback per request. A second
header_register_callback()anywhere replaces this one silently. Nothing else registers one; anything that ever needs to must chain tologinReturnInstallHook()'s callback instead. - A new sign-in finish must call
loginReturnTake(), or that route lands on the landing page - safe, just not helpful. A new folder of pages nobody should be returned to belongs inLOGIN_RETURN_REFUSED_FOLDERS.
๐ Files
| File | Change |
|---|---|
๐ฃ includes/login_return.php |
New: the hook, the sanitiser, remember / peek / take |
๐ฃ includes/functions.php |
Installs the hook, before the gates that redirect |
๐ฃ auth/login.php, auth/force_password_change.php |
Keep the target; go there after sign-in |
๐ฃ api/myaccount/verify_login_otp.php, api/auth/oidc_callback.php |
The MFA and single sign-on finishes |
๐ฃ contracts/equipment-report.php |
Redirects to auth/login.php like every other page |
๐งช tests/login-return-target.php |
New |
Related: Developer Guide ยท Code traps ยท CSRF protection - Developer Guide