Fix native login form origin policy and log origin rejections
This commit is contained in:
1 parent
2b8ea9dff4
commit
84d61846d3
3 files changed
+43
-7
No files matched your search
@@ -277,3 +277,14 @@ docker compose up --build -d
|
||||
Compose uses named volumes for `/app/data` and `/app/reports`. On first creation, Docker seeds them from the files included in the image; later restarts and image rebuilds keep the server's updated price archive, portfolio database and reports. Use `docker compose down` to stop the app while keeping its data. The port is bound to localhost so an HTTPS reverse proxy can sit in front of it.
|
||||
|
||||
For automatic refresh diagnostics, view the container log with `docker compose logs -f primethai`. Failed-source entries include the provider, failed stage, exception type and message; retry entries summarize SiamChart, Yahoo Finance, archive reading and signal calculation status.
|
||||
|
||||
### Login origin troubleshooting
|
||||
|
||||
The login form requires `Referrer-Policy: same-origin`. The old `no-referrer` policy makes browsers send `Origin: null` for native form POSTs, which the app rejects even if the public domain is listed in `PRIMETHAI_ALLOWED_ORIGINS`. The app now sets `same-origin` in its response header and login page metadata. After updating, rebuild/recreate the container and reload `/login` before submitting; an already open page retains its old policy:
|
||||
|
||||
```sh
|
||||
git pull --ff-only
|
||||
docker compose up -d --build --force-recreate primethai
|
||||
```
|
||||
|
||||
If login still returns 403, look for `[auth]` entries with `event: origin_rejected` in `docker compose logs --tail=100 primethai`. These show the reason, received Origin, expected origin, configured origins and selected proxy headers. `null_origin_check_page_referrer_policy` indicates a hidden/opaque origin; reload the page and check browser/privacy or proxy referrer-policy settings. `origin_mismatch` indicates a public scheme/host mismatch; compare the logged headers with the public URL and the configured allowed origins. Logs exclude passwords, session cookies and request bodies. Missing, null and unrelated origins remain rejected.
|
||||
@@ -2,6 +2,7 @@
|
||||
<html lang="th">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="referrer" content="same-origin">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="color-scheme" content="dark">
|
||||
<title>เข้าสู่ระบบ · PrimeThai</title>
|
||||
|
||||
+31
-7
@@ -675,16 +675,39 @@ def _handler_factory(archives: tuple[Path, ...], report_dir: Path,
|
||||
return forwarded == "https"
|
||||
|
||||
def _same_origin(self) -> bool:
|
||||
origin = normalize_origin(self.headers.get("Origin", ""))
|
||||
if origin is None:
|
||||
return False
|
||||
if origin in auth.allowed_origins:
|
||||
return True
|
||||
raw_origin = self.headers.get("Origin", "")
|
||||
origin = normalize_origin(raw_origin)
|
||||
expected_host = (self.headers.get("X-Forwarded-Host") or self.headers.get("Host", ""))
|
||||
expected_host = expected_host.split(",", 1)[0].strip().casefold()
|
||||
forwarded_proto = self.headers.get("X-Forwarded-Proto", "").split(",", 1)[0].strip().casefold()
|
||||
expected_scheme = forwarded_proto or "http"
|
||||
return origin == normalize_origin(f"{expected_scheme}://{expected_host}")
|
||||
expected_origin = normalize_origin(f"{expected_scheme}://{expected_host}")
|
||||
if origin is not None and (origin in auth.allowed_origins or origin == expected_origin):
|
||||
return True
|
||||
if not raw_origin:
|
||||
reason = "missing_origin"
|
||||
elif raw_origin.strip() == "null":
|
||||
reason = "null_origin_check_page_referrer_policy"
|
||||
elif origin is None:
|
||||
reason = "invalid_origin"
|
||||
else:
|
||||
reason = "origin_mismatch"
|
||||
# Log only selected routing headers, never credentials, cookies or request bodies.
|
||||
# JSON escaping and length limits keep untrusted header values on one bounded line.
|
||||
details = {
|
||||
"event": "origin_rejected",
|
||||
"reason": reason,
|
||||
"path": self._request_path()[:256],
|
||||
"origin": raw_origin[:512],
|
||||
"expected_origin": expected_origin[:512] if expected_origin else None,
|
||||
"allowed_origins": sorted(auth.allowed_origins),
|
||||
"headers": {
|
||||
name: self.headers.get(name, "")[:512]
|
||||
for name in ("Host", "X-Forwarded-Host", "X-Forwarded-Proto", "Sec-Fetch-Site")
|
||||
},
|
||||
}
|
||||
print("[auth] " + json.dumps(details, ensure_ascii=True), flush=True)
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def _safe_next(value: str) -> str:
|
||||
@@ -719,7 +742,8 @@ def _handler_factory(archives: tuple[Path, ...], report_dir: Path,
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.send_header("Cache-Control", "no-store")
|
||||
self.send_header("X-Content-Type-Options", "nosniff")
|
||||
self.send_header("Referrer-Policy", "no-referrer")
|
||||
# Native form POSTs use Origin: null under no-referrer, breaking login.
|
||||
self.send_header("Referrer-Policy", "same-origin")
|
||||
self.send_header("X-Frame-Options", "DENY")
|
||||
self.send_header(
|
||||
"Content-Security-Policy",
|
||||
|
||||
Reference in new issue
Block a user