Last active
October 22, 2025 21:05
-
-
Save mcmire/1d43ce690d3a974217126cd584f79b7d to your computer and use it in GitHub Desktop.
mitmproxy script to block requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Block requests for testing purposes.""" | |
| from mitmproxy import http | |
| class BlockRequests: | |
| def request(self, flow: http.HTTPFlow) -> None: | |
| # Handle OPTIONS requests (CORS preflight) | |
| if flow.request.method == "OPTIONS": | |
| flow.response = http.Response.make( | |
| 200, | |
| b"", | |
| { | |
| "Access-Control-Allow-Origin": "*", | |
| "Access-Control-Allow-Methods": "*", | |
| # TODO: Figure out how to add Authorization | |
| "Access-Control-Allow-Headers": "*", | |
| "Access-Control-Max-Age": "3600" | |
| } | |
| ) | |
| return | |
| # Block specific POST requests | |
| if flow.request.method == "POST" and ".infura.io" in flow.request.host: | |
| flow.response = http.Response.make( | |
| 500, | |
| b"Internal Server Error", | |
| { | |
| "Content-Type": "text/plain", | |
| "Access-Control-Allow-Origin": "*" | |
| } | |
| ) | |
| def response(self, flow: http.HTTPFlow) -> None: | |
| # Add CORS header to all responses that weren't already handled in request() | |
| if flow.response: | |
| flow.response.headers["Access-Control-Allow-Origin"] = "*" | |
| addons = [BlockRequests()] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment