Is exposing a session's CSRF-protection token safe?

前端 未结 3 723
情话喂你
情话喂你 2020-12-08 02:58

Django comes with CSRF protection middleware, which generates a unique per-session token for use in forms. It scans all incoming POST requests for the correct t

3条回答
  •  长情又很酷
    2020-12-08 03:51

    If you know you're going to need the CSRF token for AJAX requests, you can always embed it in the HTML somewhere; then you can find it through Javascript by traversing the DOM. This way, you'll still have access to the token, but you're not exposing it via an API.

    To put it another way: do it through Django's templates -- not through the URL dispatcher. It's much more secure this way.

提交回复
热议问题