Giter Site home page Giter Site logo

nginx_csrf_prevent's Introduction

Prevent CSRF with nginx

This is a simple nginx module which compares either the referer or the origin header to the host header. If the domain name doesn't match, HTTP response 403 is returned. This action takes place before the request is processed and will terminate any additional processing. If the request lacks both a referer and a origin header, no action will be taken.

To activate this check, place csrf_prevent on; in the block you wish to protect. You can put it in the server, location, or if blocks. If activated in a parent block, all child blocks will also be protected. You can disable the check for a specific block with csrf_prevent off;.

Examples

Apply to all requests

location / {
	csrf_prevent on;
	proxy_pass http://localhost:5000/;
}

Only apply to POST:

location / {
	if ($request_method = POST)
	{
		csrf_prevent on;
	}
	proxy_pass http://localhost:5000/;
}

Installation

In order to install a nginx module, you'll have to compile nginx from source. Download the source from https://nginx.org/en/download.html. You'll also need to clone this repo. Inside the nginx source directory run ./configure --add-module=/path/to/my-module. If you have nginx currently installed, you can get the existing configure options with nginx -V. It's annoying that you have to recompile nginx to add a module, but I don't have any other way.

nginx_csrf_prevent's People

Contributors

gartnera avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.