Add session-backed CSRF token generation and verification helpers

Aperta
#204 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
sqlite

Direzione di ricerca

Inizia leggendo le issue sulle dipendenze collegate #198, #203 e #199, quindi segui la funzionalità di sessione SQLite e il relativo esempio con un listener reale. Il lavoro è completo quando le applicazioni possono generare, memorizzare, visualizzare e verificare token di sincronizzazione in formato fisso, distinguere i casi di errore senza leak, estrarre un header senza consumare il body e dimostrare l’invio di un form protetto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

Add a focused Csrf module implementing the synchronizer-token building blocks needed by HTML forms and browser APIs.

Illustrative API:

token = Csrf.generate!()       # persist in the server-side session
form_value = token.to_str()

match Csrf.verify({
    expected: Csrf.from_stored(session.csrf_token)?,
    presented: submitted_value,
}) {
    Valid => handle_state_change()
    Invalid(reason) => forbidden(reason)
}

Dependencies

  • #198 for OS-backed cryptographic random bytes
  • #203 for the intended server-side session integration
  • #199 for the surrounding session-cookie flow

Why this should not be generic middleware

CSRF enforcement depends on route semantics, authentication, and where the application obtains the submitted token. A form handler may need to parse a bounded URL-encoded or multipart body before verification, while a JSON endpoint normally reads a header.

Automatically consuming request bodies or guessing which methods and routes are safe would conflict with the explicit request-body and routing model. The platform should instead provide a difficult-to-misuse token type and verification function.

OWASP recommends server-generated, secret, unpredictable synchronizer tokens that are compared with session state. It also warns against placing synchronizer tokens in cookies, URLs, or logs.

Required contract

  • Generate tokens from at least 32 bytes returned by Random.bytes!.
  • Use a fixed cookie/form-safe representation with strict parsing.
  • Make the token nominal and redact its normal inspection.
  • Verify token values with a constant-time comparison after validating their fixed length and encoding.
  • Distinguish missing, malformed, and mismatched tokens.
  • Provide a helper for extracting exactly one configured request header without consuming the body.
  • Form handlers parse their own bounded body and pass the presented value explicitly.
  • Document that the application stores the expected token in its server-side session.
  • Rotate the token on session creation, authentication, and session-identifier rotation.
  • Never emit token values in error messages, access logs, or metrics.
  • Provide an end-to-end form example using the SQLite session facility.

Non-goals

  • Automatic protection of all POST requests
  • Guessing safe routes or methods
  • Consuming form or multipart bodies behind the application
  • CORS policy
  • A naive double-submit cookie design
  • Stateless signed tokens or general HMAC APIs
  • Origin/Referer validation based on untrusted forwarded headers

Acceptance criteria

  • Applications can generate, persist, render, and verify synchronizer tokens.
  • Verification uses fixed-format parsing and constant-time comparison.
  • Missing, malformed, and mismatched values are distinguishable without leaking secrets.
  • Header extraction never consumes the request body.
  • A real-listener example demonstrates session cookie plus protected form submission.

References

Lingua principale
HTML
Stelle
110
Fork
25
Merge medio
18h 28m
PR unite (30g)
16

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di roc-lang/basic-webserver

Tutte le issue di roc-lang/basic-webserver

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.