Skip to main content

Request origin protection (Sec-Fetch-Site)

How Jethro rejects state-changing requests that a browser reports as coming from another origin. Implemented by enforce_request_origin() in include/general.php, called from include/init.php before the database and session are set up.

Why this exists​

Jethro has no general application-level CSRF middleware. Before this check the only request-forgery controls were:

  • the session cookie's SameSite=Lax attribute, set in include/init.php;
  • session-stored synchronizer tokens on a few forms: login_key (staff and member login), person_form_token (person edit form) and enter_attendance_token (attendance form).

That leaves gaps:

  • SameSite=Lax does not stop same-site requests. jethro.church.org and www.church.org are same-site with another.
  • Browsers that do not implement SameSite (or deployments that relax the cookie) allow cross-site form posts to edit groups, rosters, services, SMS settings, and so on.
  • Session-stored tokens break whenever the session is not available when the form is rendered or posted: after a login regenerated the session, after the browser dropped the cookie, or when the form is shown in a cross-site iframe (the SameSite=Lax cookie is not stored there, so every first login attempt from an embedded member login form failed with "Login form expired").

An attacker page can auto-submit a form to a Jethro URL. Where the cookie is still sent (no/relaxed SameSite, or a same-site origin), the request is indistinguishable from a normal POST and Jethro applies the change without any token check.

The origin check covers every unsafe request, so the per-form CSRF tokens have been removed. enter_attendance_token stays, as a duplicate-submit guard only.

What the header does​

Sec-Fetch-Site is set by the browser on every request and cannot be overridden by page script. Values:

ValueMeaning
same-originInitiator has the same scheme, host and port as the target
same-siteSame registrable domain, different origin (e.g. sibling tenant)
cross-siteDifferent site
noneUser-initiated (address bar, bookmark), or no initiator

Jethro only accepts same-origin for unsafe methods. same-site is deliberately not accepted.

Enforcement​

SituationResult
GET, HEAD, OPTIONS, TRACENot checked
POST/PUT/PATCH/DELETE, Sec-Fetch-Site: same-originAllowed
Same methods, same-site / cross-site / none403, logged and exited before dispatch
Header absent, Origin presentAllowed only if Origin matches the request's scheme and host
Header absent, Origin absent, Referer presentAllowed only if Referer's origin matches
No header, no Origin, no RefererAllowed — legacy path (CLI, curl, old browsers); relies on SameSite=Lax

request_origin_verdict() holds the policy and urls_have_same_origin() does the scheme/host/port comparison (default ports normalised). Both are pure and unit-tested.

A rejection is a plain 403 plus an error_log() line. It is not an application error: no stacktrace, no 500, no email.

Login POSTs​

The staff and member login handlers apply a stricter check before looking at the password: request_origin_is_proven() must be TRUE, meaning Sec-Fetch-Site: same-origin, or (header absent) an Origin that matches. Referer is not accepted, and a request with no signal is refused. Refusals re-show the login form with an error and write an error_log() line.

This replaces the cheap bot filter that login_key used to provide. In production access logs (which record Referer but not Origin or Sec-Fetch-Site), about half of scanner login POSTs sent no Referer and the rest forged a same-host Referer. All would have reached the password check under the general rule above. Their user agents (proof-of-concept scripts, WordPress/6.4.3) are non-browser clients that are not expected to send Origin, so this check should refuse them; that is not measured. A client that sends a forged Origin still gets through, as one that fetched the form first got past login_key; rate limiting is the defence there.

Browsers that send neither header on a form POST cannot log in. Current Chrome, Firefox and Safari send Origin on every form POST.

A login POST that logs the user in, or that leads to the 2FA or password-upgrade form, ends in a 303 redirect. The browser's history therefore never holds a POST that would re-send the password on reload or tab restore. (A correct 2FA code is the exception: when a password upgrade is still pending, the upgrade form is the response to that POST.) The 2FA and password-upgrade handlers only act on POSTs; a GET shows the pending form.

Attendance form​

views/view_6_attendance__1_record.class.php keeps enter_attendance_token as a one-time duplicate-submit guard. It is not a CSRF control:

  • token present in the session and matching → process;
  • token consumed (NULL) or not matching → existing duplicate-submit message (e.g. a double click, or a second tab);
  • token key absent from the session, and the request is a same-origin POST → process. This is the stale-form-after-login case: the session was regenerated while the form was open. The read uses array_key_exists() + hash_equals(), so it no longer emits a warning when the key is missing. Anything else with no session token (a GET, a cross-origin request, or one with no provenance signal) gets the duplicate-submit message: the view reads $_REQUEST, so without this condition a plain link could submit an empty attendance set and save() would delete that cohort's records for the date.

Embedding in an iframe​

Jethro sends no X-Frame-Options or frame-ancestors, so a church website can show the member login in an iframe, e.g. <iframe src="https://jethro.example.org/members/">. The member login form posts with target="_top". That POST is initiated by the framed Jethro document, so the browser labels it Sec-Fetch-Site: same-origin (the value ignores frame ancestors) and it is accepted. The response is a top-level page, where the session cookie is stored normally.

A login form copied into the church's own page and posting to Jethro is cross-site and is rejected.

Caveats​

  • HTTPS. Some engines only send Fetch Metadata over secure connections. The Origin/Referer fallback, then the legacy path, cover the rest.
  • Browser support. Chrome 76+, Firefox 90+, Safari 16.4+. Older browsers fall through to Origin/Referer, then to the legacy path.
  • Proxies. nginx maps the header to HTTP_SEC_FETCH_SITE; Cloudflare passes it through. TRUST_X_FORWARDED_HEADERS must be set when TLS terminates upstream, so request_scheme_and_host() sees the public scheme/host.
  • Non-browser clients. curl, scripts, and server-to-server webhooks do not send the header and are unaffected. Do not treat this check as authentication or authorization.
  • Not a duplicate guard. Same-origin requests pass however often they are sent; keep one-time-submission guards separate from provenance checks.
  • Not a clickjacking defence. A form inside a framed Jethro page posts as same-origin. Logged-in pages are protected in a cross-site frame only because the SameSite=Lax session cookie is not sent there.

Testing​

Unit tests live in tests/unit/test_general.php (header parsing, origin normalisation, verdict policy). Smoke-test with, e.g.:

curl -i -X POST -H 'Sec-Fetch-Site: cross-site' http://instance.internal/
curl -i -X POST -H 'Sec-Fetch-Site: same-origin' http://instance.internal/
curl -i -X POST -H 'Origin: https://evil.example' http://instance.internal/