Skip to content

Instantly share code, notes, and snippets.

@Wicker25
Last active September 12, 2022 12:10
Show Gist options
  • Save Wicker25/61852ffd735f048ce0d7b4676493289e to your computer and use it in GitHub Desktop.
Save Wicker25/61852ffd735f048ce0d7b4676493289e to your computer and use it in GitHub Desktop.
Mitmproxy's addon for handling CORS preflight requests
# Title: Mitmproxy's addon for handling CORS preflight requests (tested with version 8.1.1)
# Usage: $ mitmproxy -s ./mitmproxy_CORSPreflight.py -p 8080 --ssl-insecure --mode reverse:https://www.google.com/
from mitmproxy import http
class CORSPreflight:
def response(self, flow: http.HTTPFlow):
origin = flow.request.headers.get('origin', '*')
flow.response.headers['Access-Control-Allow-Origin'] = origin
flow.response.headers['Access-Control-Allow-Credentials'] = 'true'
if flow.request.method == "OPTIONS":
flow.response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
flow.response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
flow.response.headers['Access-Control-Max-Age'] = '86400'
flow.response.status_code = 204
flow.response.content = b''
addons = [
CORSPreflight()
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment