Skip to content

Instantly share code, notes, and snippets.

@codephillip
Created November 30, 2021 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codephillip/ec95145981a63c21a6bf78f6dd31addd to your computer and use it in GitHub Desktop.
Save codephillip/ec95145981a63c21a6bf78f6dd31addd to your computer and use it in GitHub Desktop.
Custom CORS and CSRF middleware bypasser(used in microservices)
from django.utils.deprecation import MiddlewareMixin
# NOTE: This is not best practise, however, api requests from some browser would fail continuous so explains the use
# of this. Enhance this CSRF solution if necessary
class DisableCsrfCheck(MiddlewareMixin):
def process_request(self, req):
attr = '_dont_enforce_csrf_checks'
if not getattr(req, attr, False):
setattr(req, attr, True)
class CorsMiddleware(object):
def process_response(self, req, resp):
resp["Access-Control-Allow-Origin"] = "*"
return resp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment