All Projects → ring-clojure → ring-anti-forgery

ring-clojure / ring-anti-forgery

Licence: other
Ring middleware to prevent CSRF attacks

Projects that are alternatives of or similar to ring-anti-forgery

Okta Spring Boot React Crud Example
Simple CRUD with React and Spring Boot 2.0
Stars: ✭ 176 (+53.04%)
Mutual labels:  csrf
laravel-stateless-session
CSRF verification and session persistent through request/response headers.
Stars: ✭ 33 (-71.3%)
Mutual labels:  csrf
next.js-boilerplate
next.js bolierplate, next.js 的开发模板
Stars: ✭ 28 (-75.65%)
Mutual labels:  csrf
Csrf Protector Php
CSRF Protector library: standalone library for CSRF mitigation
Stars: ✭ 178 (+54.78%)
Mutual labels:  csrf
Csrf
Logic behind CSRF token creation and verification.
Stars: ✭ 226 (+96.52%)
Mutual labels:  csrf
Formidable
The PHP pragmatic forms library
Stars: ✭ 116 (+0.87%)
Mutual labels:  csrf
Hacker101
Source code for Hacker101.com - a free online web and mobile security class.
Stars: ✭ 12,246 (+10548.7%)
Mutual labels:  csrf
okta-spring-boot-react-crud-example
Simple CRUD with React and Spring Boot 2.0
Stars: ✭ 214 (+86.09%)
Mutual labels:  csrf
CSRF-tutorial
Use Django To Introduce CSRF and Cookies , Session 📝
Stars: ✭ 49 (-57.39%)
Mutual labels:  csrf
Slim-Auth
A Slim 4 Skeleton.
Stars: ✭ 22 (-80.87%)
Mutual labels:  csrf
Javasecurity
Java web and command line applications demonstrating various security topics
Stars: ✭ 182 (+58.26%)
Mutual labels:  csrf
Web Security Fundamentals
👨‍🏫 Mike's Web Security Course
Stars: ✭ 195 (+69.57%)
Mutual labels:  csrf
asgi-csrf
ASGI middleware for protecting against CSRF attacks
Stars: ✭ 43 (-62.61%)
Mutual labels:  csrf
Csurf
CSRF token middleware
Stars: ✭ 2,183 (+1798.26%)
Mutual labels:  csrf
www-project-csrfguard
The aim of this project is to protect Java applications against CSRF attacks with the use of Synchronizer Tokens
Stars: ✭ 43 (-62.61%)
Mutual labels:  csrf
Xssor2
XSS'OR - Hack with JavaScript.
Stars: ✭ 1,969 (+1612.17%)
Mutual labels:  csrf
solutions-bwapp
In progress rough solutions to bWAPP / bee-box
Stars: ✭ 158 (+37.39%)
Mutual labels:  csrf
koa-restful-boilerplate
A boilerplate for koa2 RESTful API development
Stars: ✭ 31 (-73.04%)
Mutual labels:  csrf
fastify-csrf
A fastify csrf plugin.
Stars: ✭ 88 (-23.48%)
Mutual labels:  csrf
noire-server
Hapi Boilerplate
Stars: ✭ 20 (-82.61%)
Mutual labels:  csrf

Ring-Anti-Forgery

Build Status

Ring middleware that prevents CSRF attacks. By default this uses the synchronizer token pattern.

Install

Add the following dependency to your project.clj:

[ring/ring-anti-forgery "1.3.0"]

Usage

The wrap-anti-forgery middleware function should be applied to your Ring handler.

Once applied, any request that isn't a HEAD or GET request will now require an anti-forgery token, or a 403 "access denied" response will be returned.

By default, the request is validated via the synchronizer token pattern, which requires the session middleware to be in place:

(require '[ring.middleware.anti-forgery :refer :all]
         '[ring.middleware.session :refer :all])

(def app
  (-> handler
      wrap-anti-forgery
      wrap-session))

The token will be used to validate the request is accessible via the *anti-forgery-token* var. The token is also placed in the request under the :anti-forgery-token key.

Custom token reader

By default the middleware looks for the anti-forgery token in the __anti-forgery-token form parameter, which can be added to your forms as a hidden field. For convenience, this library provides a function to generate the HTML of that hidden field:

(use 'ring.util.anti-forgery)

(anti-forgery-field)  ;; returns the HTML for the anti-forgery field

The middleware also looks for the token in the X-CSRF-Token and X-XSRF-Token header fields, which are commonly used in AJAX requests.

This behavior can be customized further by supplying a function to the :read-token option. This function is passed the request map, and should return the anti-forgery token found in the request.

(defn get-custom-token [request]
  (get-in request [:headers "x-forgery-token"]))

(def app
  (-> handler
      (wrap-anti-forgery {:read-token get-custom-token})
      (wrap-session)))

Custom error handling

It's also possible to customize the error response returned when the token is invalid or missing:

(def custom-error-response
  {:status 403
   :headers {"Content-Type" "text/html"}
   :body "<h1>Missing anti-forgery token</h1>"})

(def app
  (-> handler
      (wrap-anti-forgery {:error-response custom-error-response})
      (wrap-session)))

Or, for more control, an error handler can be supplied:

(defn custom-error-handler [request]
  {:status 403
   :headers {"Content-Type" "text/html"}
   :body "<h1>Missing anti-forgery token</h1>"})

(def app
  (-> handler
      (wrap-anti-forgery {:error-handler custom-error-handler})
      (wrap-session)))

Custom token strategy

The synchronizer pattern is not the only way of preventing CSRF attacks. There a number of different strategies, and the middleware in this library can support them through the :strategy option:

(def app
  (wrap-anti-forgery handler {:strategy custom-strategy}))

The custom strategy must satisfy the Strategy protocol. Some third-party strategies already exist:

Further Documentation

Caveats

This middleware will prevent all HTTP methods except for GET and HEAD from accessing your handler without a valid anti-forgery token.

You should therefore only apply this middleware to the parts of your application designed to be accessed through a web browser. This middleware should not be applied to handlers that define web services.

License

Copyright © 2018 James Reeves

Distributed under the MIT License, the same as Ring.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].