fix: production deployment hardening for Cloudflare Tunnel + WJ auth flow - #57
Open
JackyTJie wants to merge 4 commits into
Open
fix: production deployment hardening for Cloudflare Tunnel + WJ auth flow#57JackyTJie wants to merge 4 commits into
JackyTJie wants to merge 4 commits into
Conversation
… volume path - gcr.io distroless base is unreachable behind GFW; reuse ghcr uv image - add cloudflared tunnel service reading TUNNEL_TOKEN from .env - force http2 protocol (QUIC connections get dropped on this network) - mount config.yaml and .env into container for live config updates - rename service to CourseReview - fix PostgreSQL 18 volume mount path
- add CSRF_TRUSTED_ORIGINS config for coursesel.gcers.org / api.gcers.org - add SECURE_PROXY_SSL_HEADER so Django detects HTTPS behind tunnel - set csrftoken cookie on /api/user/status/ so browser-side POSTs pass CSRF
The course catalog moved from www.ji.sjtu.edu.cn to gc.sjtu.edu.cn and the old domain's TLS certificate has expired.
- cast OTP_TIMEOUT to int (env override injects a string, breaking float comparisons) - retry Turnstile siteverify on transient network failures with short timeout - handle non-JSON siteverify responses (rate limit 429 HTML) - tolerate 60s negative timestamp offset (WJ server clock measured ~39s slow) - fix Python 2 style except clauses that never caught TypeError - add diagnostic logging for submission timestamp failures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Deploys the backend to production behind Cloudflare Tunnel (
[api.gcers.org](http://api.gcers.org/)) and fixes every issue hit while bringing up the live environment: container builds behind GFW, CSRF/cookie handling through the Worker proxy, the ORC crawler's dead domain, and several auth bugs surfaced by real user testing of the WJ (问卷) OTP flow.The frontend (Tech-JI/CourseFront, served from Cloudflare Pages) proxies
/api/*to this backend via a Worker; all changes here assume that topology.Changes
1. Deployment / infrastructure (
fix(deploy))[gcr.io/distroless/base-debian13:nonroot](http://gcr.io/distroless/base-debian13:nonroot)(unreachable from mainland China) with the[ghcr.io/astral-sh/uv](http://ghcr.io/astral-sh/uv)base image; add anonrootuser for parity.cloudflaredtunnel service, token read from.env(TUNNEL_TOKEN), forced to--protocol http2— QUIC connections to the Cloudflare edge were being dropped by the network every ~30 min, causing intermittent1033errors on the public hostname../config.yamland./.envread-only into the container so config changes no longer require an image rebuild.backend→CourseReview(matches the tunnel's public hostname service URL)./var/lib/postgresqlinstead of/var/lib/postgresql/data.2. Security / request plumbing (
fix(security))CSRF_TRUSTED_ORIGINSconfig (defaults empty; production values inconfig.yaml).SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")— requests arrive via HTTPS at Cloudflare but plain HTTP at gunicorn; without this, Django treats secure requests as insecure andSESSION_COOKIE_SECUREbreaks cookie setting.@ensure_csrf_cookietouser_status. The frontend callsGET /api/user/status/on every page load; previously thecsrftokencookie was only set on OTP endpoints, so password-login users could never make CSRF-protected POSTs (votes, reviews, logout).3. ORC crawler (
fix(spider))BASE_URL/COURSE_DETAIL_URL_PREFIXfrom[www.ji.sjtu.edu.cn](http://www.ji.sjtu.edu.cn/)to[gc.sjtu.edu.cn](http://gc.sjtu.edu.cn/). The old domain's TLS certificate has expired and the catalog redirects to the new host. Verified the new site's page structure (h2 headings,et_pb_text_innerblocks) still matches the parser.4. Auth fixes (
fix(auth))AUTH__OTP_TIMEOUT=300set via.envinjects a string into the config, and the config system does not cast it.OTP_TIMEOUTis used in a float comparison inverify_callback_api—float > "300"raisedTypeError, which the surroundingexceptswallowed and surfaced to users as"Invalid submission timestamp". Bothapps/auth/views.pyandapps/auth/utils.pynow cast withint(...).apps/auth/utils.py):TimeoutException/HTTPError— transient connect failures to[challenges.cloudflare.com](http://challenges.cloudflare.com/)were observed in production logs.json()exception.apps/auth/views.py): the questionnaire platform's server clock was measured ~39s slow (via its HTTPDateheader). Submissions made seconds after initiation were rejected as "outside validity window" because the recorded timestamp fell before the initiation time. Added a 60s tolerance on the lower bound; the upper bound (OTP window) is unchanged.except ValueError, TypeError:is valid Python 3 syntax but means "catch ValueError as TypeError" —TypeErrorwas never caught. Fixed in both auth modules. (# fmt: skiprequired: ruff-format incorrectly strips the tuple parens, which changes semantics.)Verification
course_codevarchar(10) limit).
cookie set,
GET /api/user/status/returnskie issued.questionnaires forsignup/login/reset are configured in.env`).ruff format+ruff checkpass; pre-coNotes for reviewers
celeryis imported byapps/spider/task inpyproject.toml`; the crawler was runwith a celery-free path in this deployment. Out of scope for this PR, but worth a follow-up.Course.course_code` max_length=10 and areskipped on import.config.yamland.envare gitignored; server.