Skip to content

Instantly share code, notes, and snippets.

@mcmire
Last active October 22, 2025 21:05
Show Gist options
  • Save mcmire/1d43ce690d3a974217126cd584f79b7d to your computer and use it in GitHub Desktop.
Save mcmire/1d43ce690d3a974217126cd584f79b7d to your computer and use it in GitHub Desktop.
mitmproxy script to block requests
"""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