This covers the security model as it's actually implemented: auth, authorization, transport, input handling, rate limiting, and a few incidents from this codebase's own history that shaped how things work now. Where something's unfinished, it says so — no point writing this up as more complete than it is.
A chunk of this is sourced from inline comments in auth.py that reference a numbered series of
fixes ("Finding #1" through "Finding #8") and similar audit-style comments elsewhere. Those read
like the record of an actual security review, not planning notes, so where a comment describes
what a bug used to be and how it got closed, that's treated as the authoritative account below.
The "Finding #N" references throughout this document come from a structured security review I ran against this codebase, cataloguing authentication and session-handling gaps. Where a finding is cited, the fix it produced is described inline.
Passwords are hashed with Werkzeug's generate_password_hash (auth_service.py) and checked with
check_password_hash (auth.py). No custom hashing, nothing reversible. Minimum length is 6
characters, enforced where passwords get set (auth.py::set_password). That's a low bar by
current standards — worth saying plainly rather than pretending otherwise.
New accounts get a sentinel value (pin = "PENDING_VERIFICATION") instead of a real password hash
until registration finishes. check_password_hash will never match against that string, so a
half-registered account has no working password, full stop. It also doubles as the signal
login() checks to give someone a "finish setting up your account" message instead of a generic
"wrong password."
google_callback() in auth.py resolves the account by Google's sub claim — the stable,
non-reassignable ID from the v2 userinfo response — not by email address. This is the fix for
Finding #2 (marked Critical in the code). The original version logged into any account matching
the OAuth-returned email, including accounts created with a password that had nothing to do with
that Google identity. A shared or former mailbox is the obvious case: whoever controls that inbox
today could get Google to authenticate the address and walk straight into someone else's account,
no password required.
The current version checks account state before allowing a Google login:
google_idis already set and matches the incomingsub→ genuine returning user, proceed.google_idisNULLandpinis still the untouched"PENDING_VERIFICATION"sentinel → a legacy Google-created account from before this column existed. No real password was ever set, so there's nothing to bypass — backfillinggoogle_idhere isn't a new grant of access.google_idisNULLandpinis a real hash → this account was created with a password. Google auth is refused, and the client gets redirected with?error=account_exists_use_password.
There's a fourth case — google_id set but not matching the incoming sub — which should be
close to impossible under normal Google behavior. It's still treated as a refusal rather than an
auto-correction. The code doesn't try to guess when the identity signal disagrees with itself.
Two token types, different shapes because they cover different threats.
access_token is a stateless JWT (HS256, signed with SECRET_KEY), good for 30 minutes,
carrying user_id and role. There's no server-side revocation for it — the short lifetime is
the mitigation if one leaks.
refresh_token is DB-backed (RefreshToken model), good for 7 days, hashed at rest (SHA-256
— the raw value never touches the database, so a DB read alone can't mint sessions), rotated on
every use, and revocable. A family_id ties together every token descended from one login, which
is what makes revoking a whole family possible.
Rotation with reuse detection works like this: every /refresh-token call revokes the presented
token and issues a new one in the same family. Revocation is a single compare-and-set
(UPDATE ... WHERE revoked = false), so two requests presenting the same token at the same moment
(two tabs, or two app instances) cannot both rotate it. The loser is handled like any reuse:
auth_service.rotate_refresh_token() checks the timing. Within 10 seconds of the rotation it is
treated as two open tabs racing — the caller gets a fresh access token without a new refresh
rotation. Outside that window it is treated as a compromised token being replayed, and the entire
family is revoked server-side. Every session descended from that login has to log in again.
The account is checked before the token is rotated (a rejected account does not burn its token). Accounts that are still registering with a verified email may refresh, so a 30-minute access token does not strand someone half-way through onboarding; what such users may call is limited by the account-status gate (§1.7). A password reset revokes every refresh token of the user.
Both flows use opaque, single-use, database-backed tokens (EmailVerificationToken,
PasswordResetToken) instead of a signed JWT the server can't invalidate individually.
- Hashed at rest. Only the SHA-256 of the token is stored (
token_hash); the raw value exists only in the emailed link, so a database leak does not yield usable links. - Atomic single use. Spending a token is one conditional
UPDATE ... WHERE used = false AND expires_at > now. Two simultaneous requests (double click, two instances) cannot both succeed. The consume functions do not commit: the password change, the token use and the revocation of sessions share one transaction, so a failure leaves the link unspent instead of burned. - One live token per user and purpose. Issuing a token invalidates the user's earlier unused ones; a successful reset invalidates the rest.
- Lifetimes. Reset links: 1 hour. Verification links: 5 hours.
- Peek vs consume.
GET/POST /verify-reset/<token>is a read-only check, so opening or previewing a link does not burn it (mail clients and chat apps pre-fetch links). The token is spent byset_password()when the new password is written. - A reset ends all sessions.
set_password()revokes every refresh token of the user. - Replayed verification links only get a friendly answer in the browser that is already signed in as that user; anyone else is told to sign in or request a new link.
- Links are built from
PUBLIC_BASE_URL, never from request headers (helpers.public_url), so a forgedHost/X-Forwarded-Hostcannot make us email a link to another domain.
Uniform answers (no account enumeration). POST /register answers identically for a new
address, an unfinished registration and an active account; "forgot password" answers identically
for known and unknown accounts; login answers "Invalid credentials" for an unknown account, a wrong
password and an account that has no password, and does a dummy hash for unknown accounts so timing
is similar. An account's state ("verify your email") is only described to someone who proved the
password. (check-username is an availability oracle by nature and has its own rate tier.)
Recovery. Signing up again with an address whose registration was never finished sends a fresh
link (earlier links stop working); POST /resend-verification does the same. A user who verified
their email but never chose a username can use "forgot password" and then log in: a correct
password for a verified, still-registering account signs them in and sends them to
/student/complete-registration. Nobody is locked out of their own registration.
Partial failures. User, profile and token are committed together and only then is the email
queued. If the queue is down the request fails with 503 and the account is not stranded: signing up
again sends a new link. Abandoned sign-ups are purged after 7 days (see BACKGROUND_JOBS.md),
which also frees the address for its real owner.
Google sign-in.
- Addresses Google reports as unverified are refused.
- A Google login may claim an unfinished password sign-up (no password yet) — Google just proved
control of the mailbox, so the email counts as verified. A password-created account is never
taken over (
account_exists_use_password); agoogle_idmismatch is refused; suspended accounts get neither a session nor a proof. - The "proof" that lets a brand-new Google user finish onboarding before they hold a JWT is the
signed Flask session (
google_emailplusgoogle_proof_at). It expires after 30 minutes (GOOGLE_PROOF_TTL_SECONDS) because a client-side session cookie cannot be revoked server-side, and it is cleared on every completed auth event. - The client-supplied
google_verifiedflag no longer exists.
POST /onboard/<email> and POST /complete-registration require proof that the caller owns the
email: a fresh Google session for it, or an access cookie belonging to that user (emails compared
case-insensitively). They also check the account state:
complete-registrationonly works for users who are still registering (verified email, pending status). Approved accounts get 409 — a session cookie can never be used to overwrite an existing account's username or password.onboardlets approved users update their preferences, but never changes their status or issues tokens; onboarding is optional for becoming approved.- Request bodies must be
application/json(other content types are not parsed), because a cross-site HTML form can sendtext/plainwithout a CORS preflight.
Flask-SocketIO's connect handler runs inside a real Flask request context, so request.cookies
is available exactly like in an ordinary HTTP route. StudyHub uses that directly:
websocket_messages.py::handle_connect reads the access_token cookie straight from the
handshake request when ACCESS_TOKEN_HTTPONLY is on (the default — see §2.1), decodes and
validates the JWT, and returns False to reject the connection if the token's missing, expired,
or invalid. The client gets disconnected before any event handler runs — it's not left connected
and merely unauthenticated. There's also a secondary authenticate event as a fallback for the
non-httponly config, where the token has to travel in the handshake payload instead of a cookie.
Its own docstring calls the connect-based check the primary path.
A valid JWT is not enough. role_required loads the user and rejects every account whose
status is not approved with 403 registration_incomplete. Users who are still registering
hold a JWT (issued after email verification or onboarding), so routes they legitimately need opt in
with token_required_allow_pending — currently /auth/me, /users/me and the onboarding
suggestion/connect routes — and even then only for pending_onboarding/pending_verification
accounts with a verified email. Any other status (for example suspended) is always rejected. The
WebSocket handshake applies the same rule, and a logout disconnects the user's sockets on every
instance (the Redis presence index plus the Socket.IO message queue).
The suggestion and connect endpoints under /connections/onboarding/ are authenticated; the user
comes from the access token, never from an email in the URL (the old -by-email routes accepted an
email as proof of identity and allowed anyone to create connections for anyone). Connecting sends a
normal pending request — nobody becomes connected without consent — and existing rows (blocked,
rejected, pending, accepted) are never overwritten. Bulk requests are capped at 25 ids, validated
and batch-loaded. Suggestions come from a candidate pool bounded in SQL.
| Cookie | HttpOnly | Secure | SameSite | Lifetime |
|---|---|---|---|---|
access_token |
ACCESS_TOKEN_HTTPONLY (default true) |
SESSION_COOKIE_SECURE config |
Lax |
30 minutes |
refresh_token |
always true |
SESSION_COOKIE_SECURE config |
Lax |
7 days |
csrf_token |
always false (has to be JS-readable) |
SESSION_COOKIE_SECURE config |
Lax |
30 minutes, reissued with access_token |
csrf_token only gets issued when ACCESS_TOKEN_HTTPONLY is on — which, since that flag defaults
to true, means it's issued by default. (Audit correction: an earlier version of this section
said the cookie is issued but never validated against a submitted header. That was wrong —
routes/student/__init__.py::enforce_csrf() is registered as a before_request hook on the
entire student_bp blueprint and does exactly that check on every mutating request, 403'ing on a
missing or mismatched X-CSRF-Token header. See §7 below for the corrected description and a note
on how this was verified.)
SameSite=Lax on all three auth cookies is additional, real protection against cross-site
POST-with-cookie forgery in every modern browser — on top of, not instead of, the double-submit
check above.
supports_credentials=True isn't optional here — the app authenticates via cookies, not an
Authorization header, for browser clients. A cross-origin frontend can't complete a request at
all unless the browser's told it can send/receive those cookies. That has a real consequence:
CORS_ALLOWED_ORIGINS can't sit at a wildcard once credentials are involved. Browsers won't honor
Access-Control-Allow-Credentials: true paired with a wildcard origin — they reject the request
at the browser layer before this app's logic ever runs. Cross-origin access is therefore opt-in:
the default is an empty list (no cross-origin requests), development sets * explicitly, and
production refuses to start with * (see §2.5). Set CORS_ALLOWED_ORIGINS to the real
frontend origin(s) in any environment where frontend and API aren't same-origin.
Set on every response via a Flask after_request hook, with a guard to skip WebSocket upgrade
connections (setting headers on an already-upgraded connection throws an AssertionError under
threading mode, and the hook still fires for those requests otherwise):
X-Content-Type-Options: nosniffX-Frame-Options: DENYX-XSS-Protection: 1; mode=blockStrict-Transport-Security: max-age=31536000; includeSubDomains— only set when the app isn't in debug mode andSESSION_COOKIE_SECUREis on, so HSTS never gets advertised for a deployment that isn't actually serving HTTPS.
POST /logout does three things. Each is best-effort and wrapped independently, so one failing
doesn't block the others or the response itself.
First, it disconnects any live WebSocket session tied to the user. This came out of a real gap —
access_token doubles as the WebSocket credential now, and a socket that stayed connected through
a logout would keep working.
Second, it revokes the whole refresh-token family server-side. This is a side effect of Finding #6. Before the fix, logout only cleared cookies on the client, while the refresh token — a stateless JWT with nothing to revoke, at the time — stayed valid for the rest of its 7-day life. A copy of that cookie sitting in another tab, another device, or an attacker's hands would keep minting new access tokens after "logout."
Third, it clears all three auth cookies.
GET-based logout was removed entirely — Finding #8. A GET logout can be forged with a plain
<img src="..."> tag on some other site, since browsers don't apply CSRF protections to simple
cross-origin GET navigations the way they effectively do for POST under this app's cookie/CORS
setup.
ProductionConfig.validate() runs in create_app() and refuses to start the app (listing every
problem at once) when:
- rate limiting is enabled but its storage is
memory://— in-memory counters are per process, so every extra instance multiplies the limits (RATE_LIMIT_STORAGE_URIorREDIS_URL) PUBLIC_BASE_URLis missing or nothttps://— emailed verification/reset links are built from it, never from request headersCORS_ALLOWED_ORIGINScontains*SESSION_COOKIE_SECUREis false (it now defaults to true in production)
OAUTHLIB_INSECURE_TRANSPORT (which lets oauthlib accept an http:// OAuth callback) is only set
for development and tests.
role_required(*allowed_roles) in helpers.py is the single authorization gate for the HTTP API.
It takes credentials from either an Authorization: Bearer <token> header or the access_token
cookie (header checked first), decodes and validates the JWT, loads the User, and checks
user.role against the allowed set.
token_required is just role_required("student") under another name. It's kept as its own alias
specifically so the roughly 280 existing @token_required routes kept working unchanged when the
role check got generalized — the fix went into how the decorator's defined, not into touching
every call site. admin_required = role_required("admin", "system") is the parameterized version
for the handful of privileged routes.
Failure responses are specific, not one generic 401: no token → 401 "Authentication required.
Please login." User row missing (deleted account, still-valid token) → 401 "User not found."
Expired signature → 401 "Token expired. Please refresh your session." Malformed/invalid token →
401 "Invalid token." Role mismatch → 403 via AuthorizationError, "Access denied for this role."
The role decorator answers "is this caller a student/admin at all" — it doesn't answer "does this
caller own this specific resource." That check happens per-domain in services instead:
connection_service.can_message gates direct messaging behind a mutual-accept Connection;
thread_authorization.py gates moderation actions inside a thread on membership/role; similar
ownership checks exist per-resource for posts and homework assignments.
These are two deliberately separate layers. A valid student token proves who you are. It doesn't prove you're allowed to act on an arbitrary resource ID — that's a second check, every time.
Every query path reviewed goes through SQLAlchemy's ORM and parameterized query construction. The
only raw text() calls found are static, literal SQL with nothing request-derived concatenated in
— health-check SELECT 1 statements (app.py, admin.py) and two static partial-index WHERE
predicates in models.py (is_deleted = FALSE, ai_personality IS NOT NULL). No string-formatted
or f-string-interpolated SQL anywhere in the routes or services.
HTTP-layer rate limiting (rate_limit_service.py) runs on Flask-Limiter, fixed-window strategy,
Redis-backed in production and falling back to in-memory in dev/test. Tiers are chosen per route
by risk and cost, not one global number:
| Tier | Limit | Applied to |
|---|---|---|
SENSITIVE_AUTH |
5/minute | Password-reset request/check/set, email verification, resend-verification — keyed by IP |
AUTH_ATTEMPT |
20/minute | Login, keyed by IP. Real brute-force protection is the per-account counter below |
REGISTRATION |
10/minute | Sign-up, onboard and complete-registration writes, keyed by IP |
USERNAME_CHECK |
30/minute | The live "is this username free" check |
WRITE_HEAVY |
30/minute | Create/update/delete on posts, threads, comments, connections |
AI_EXPENSIVE |
10/hour | AI chat, refinement, meeting-notes generation — these cost real money per call |
BURST_OK |
60/minute | Reactions, likes, other low-risk high-frequency actions |
PUBLIC_READ |
300/minute | Read-only, often-unauthenticated endpoints |
WEBHOOK |
100/minute | External-service callback endpoints, if any are exposed |
ADMIN |
20/minute | Operational/admin endpoints — low volume by nature, privileged callers only |
Per-account/per-email counters. IP limits cannot stop one victim's inbox being flooded or one
account being guessed from many addresses, so allow_action() (Redis, shared by every instance,
identifier hashed, fails open) also counts: failed logins per account (10 per 15 minutes, reset on
success), registrations per email (5/hour), resend-verification per email (3/hour) and password-reset
requests per account (3/hour). Token refresh uses BURST_OK (60/min) so many students behind one
campus address are not locked out of refreshing.
A global default (200/day, 50/hour) covers any route that doesn't opt into a specific tier.
/health, /ping, and /ready are exempt from rate limiting entirely. Authenticated routes key
by user_or_ip_key() — per-user once g.current_user_id is set by the auth decorator, falling
back to IP otherwise — rather than IP alone. Straight IP limiting would incorrectly throttle
unrelated students sharing the same campus network.
Fails open, on purpose. If Redis errors, the limiter swallows it
(RATELIMIT_SWALLOW_ERRORS=True) and falls back to in-memory
(RATELIMIT_IN_MEMORY_FALLBACK_ENABLED=True) instead of raising or blocking requests. A rate
limiter that takes the whole app down when its own backing store goes away is a worse failure mode
than temporarily looser limits. A short connect/socket timeout (1 second) on the Redis storage
options means a down Redis fails fast instead of hanging every request for the default multi-second
timeout.
WebSocket-layer rate limiting is separate, and Redis-backed for correctness across instances —
thread-message sends and thread-level AI actions each have their own per-user cap. Full mechanics
in ARCHITECTURE.md §13.
Every image upload — avatars, post attachments, thread images — goes through
upload_validation_service.validate_and_normalize_image() rather than trusting the file extension
or declared MIME type. The route layer does a cheap extension allowlist check first
({jpg, jpeg, png, gif, webp}), but that's early rejection only, not the real gate. The actual
enforcement: every accepted file gets opened with Pillow, structurally verified, fully decoded
(which catches a truncated or corrupt file that a shallow header check would miss), and
re-encoded into a fresh output buffer before it's ever forwarded to storage.
Re-encoding from genuinely decoded pixel data is what makes it structurally impossible to smuggle
a polyglot file through — an SVG or an embedded script payload wearing a .jpg extension, say.
The bytes that reach Cloudinary or local storage were generated by Pillow from decoded image data.
Nothing from the original upload gets copied through as-is.
Non-image document uploads are checked against a small magic-byte signature table instead of being trusted by filename extension alone.
A typed exception hierarchy (errors.py: ValidationError 400, NotFoundError 404,
AuthorizationError 403, ConflictError 409, RateLimitedError 429, ExternalServiceError 502)
feeds one centralized Flask error handler. It produces {"status": "error", "message": "...", "errors": {...}}, with errors present only when there's structured validation detail to show.
Several routes explicitly avoid putting a raw caught exception's message into the response — marked
inline with # FIX: no str(e) leak at the specific spots this got corrected. Registration and
login failures return a fixed, generic message no matter what the underlying exception says, so
internal detail — stack traces, SQL fragments, library-specific error text — never reaches the
client on those paths. Anything with status_code >= 500 gets logged server-side with
exc_info=True and forwarded to Sentry. Nothing below 500 triggers a Sentry capture.
Sentry itself (error_tracking.py) fails open at every stage. A missing DSN, an init failure, a
runtime capture error — all of it degrades to "no error tracking," never to an app-breaking
exception. The capture call is wrapped in a bare except: pass so a failure in the error-reporting
path can't become the source of a second, unrelated error. A before_send hook strips the three
auth cookie names (access_token, refresh_token, csrf_token) and password-hash-related field
names, by name, from any event before it leaves the process. send_default_pii=False is set
explicitly rather than left to the library default.
Naming what isn't finished, same as everywhere else in this doc:
CSRF double-submit is half-wiredAudit correction: this is implemented and enforced, not a gap.routes/student/__init__.py::enforce_csrf()runs as abefore_requesthook on the wholestudent_bpblueprint — not a per-route decorator, so new routes are protected by default — and compares thecsrf_tokencookie against a submittedX-CSRF-Tokenheader on every mutating request (GET/HEAD/OPTIONS and a documented list of pre-auth routes, e.g./student/login,/student/verify-email/<token>, are exempt since there's no session cookie yet to protect on those). A missing or mismatched token raises a 403. This is genuinely exercised by the integration suite (tests/integration/conftest.py'scsrf_headersfixture, which itself documents a prior bug where the fixture silently produced an empty header and masked whetherenforce_csrfwas actually being hit — see that fixture's own comments).SameSite=Laxis still additional, real protection on top of this, not a replacement for it. The registration-step routes (/student/onboard/*,/student/complete-registration) are exempt only when the request has noaccess_tokencookie (a brand-new Google user has no CSRF cookie yet); with a cookie they go through the normal check.- Access tokens cannot be revoked and WebSockets are checked once. A logout or suspension does not kill an access JWT before its 30 minutes are up, and a socket's JWT is validated at connect time only. Logout does disconnect the user's sockets on every instance, and non-approved accounts cannot connect or call product routes, but a socket that is already open when an account is suspended stays open until it disconnects.
- The Google session proof is a signed client-side cookie. It expires after 30 minutes and is cleared server-side on completion, but an older copy of the cookie remains valid until it expires.
- Account lockout is per account, not per (account, IP). Ten failed logins lock an account's sign-in for the rest of a 15-minute window, so someone can briefly lock another user out. That is the usual trade-off against credential stuffing; the window is short.
- The full schema is not migration-managed. Only the authentication/onboarding tables and two earlier features have Alembic migrations; a baseline for the rest of the schema is still open.
- Password minimum is 6 characters, no complexity requirement beyond that. This is a real current limit, not a placeholder waiting to be filled in.
_auto_reply_buckets— the limiter gating Learnora's unprompted auto-replies (no@mentionneeded) in threads — is still process-local instead of Redis-coordinated (seeARCHITECTURE.md§11.8). Under multiple app instances, a user's cap on triggering unprompted AI replies can reset if their connection lands on a different instance between messages. That's a cost-control gap, not an auth or data-exposure one.- Search runs on unindexed
ILIKEagainst live tables instead of a dedicated search index (ARCHITECTURE.md§5.6). Not a security issue by itself, but worth knowing here too since it affects how much load a low-privilege search query can generate.
For the broader system this sits inside, see ARCHITECTURE.md. For the HTTP
surface these mechanisms protect, see openapi.yml.