csrf: session-bound CSRF primitive for app POST routes (Jinja global + csrf_protect)

Open
#43 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
flask, python

Research direction

Start by reading the worked example in nyjc-computing/campus-profile PR #67 and the flask_campus login flow that stores OAuth state. Identify where the Jinja environment and auth.logout route are registered, then check the app’s existing tests for route and session behavior. Done means unsafe methods require a matching session token, logout uses POST with protection, and the consumer migration path is documented.

Written by the indexing model from the issue text.

Description

enhancement

Every campus-family Flask consumer with a POST route currently rolls its own
CSRF. flask_campus owns the session and already handles the login flow's CSRF
(OAuth state), but app POST routes are on their own — so apps invent
per-app idioms that drift.

Worked example: nyjc-computing/campus-profile#61, implemented in PR
nyjc-computing/campus-profile#67: a session-bound synchronizer token (~20
lines — mint-or-return secrets.token_urlsafe() into the session, hidden
form field, secrets.compare_digest on POST, 400 on mismatch/absence). The
same app already carries a second bespoke session nonce (classroom connect,
CLASSROOM_CONNECT_STATE); the next consumer app will invent a third
pattern.

Proposal — a primitive, not a form framework (no Flask-WTF/WTForms; the
ecosystem's wheel-pinning model favours a small dependency-free primitive):

  • csrf_token(): mint-or-return session token, registered as a Jinja global
    so templates render {{ csrf_token() }} directly. This kills the
    "forgot to pass it to render_template" failure mode — campus-profile had to
    thread the token explicitly through both render paths of the page.
  • csrf_protect (decorator or before_request check for unsafe methods —
    POST/PUT/PATCH/DELETE): require a non-empty form field and non-empty
    session token, compare with secrets.compare_digest, abort 400 otherwise.

Companion fix in the same change: auth.logout is currently a GET route
(@bp.get("/logout")), which is logout-CSRF-able (a cross-site <img> or
link can sign a user out — nuisance-grade, not a breach). Moving logout to
POST + CSRF token is only possible here; flask_campus owns the route.

Migration: consumers migrate to the primitive and delete their local
implementations (campus-profile's is written and reviewed — use PR #67's
shape as the reference). Ship alongside other changes in the next
flask-campus release so consumers bump the wheel pin once.

Dominant language
Python
Stars
0
Forks
0
Avg merge
9m
Merged PRs (30d)
40

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nyjc-computing/flask-campus

All issues in nyjc-computing/flask-campus

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.