Skip to content

Commit 495b832

Browse files
authored
Copy CORS headers when subsituting responses (#178)
... otherwise the client won't be able to read the modified response.
1 parent 7fd224c commit 495b832

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

tests/mitmproxy_addons/callback.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import aiohttp
44
import json
55

6+
import mitmproxy
67
from mitmproxy import ctx, flowfilter
78
from mitmproxy.http import Response
89
from controller import MITM_DOMAIN_NAME
@@ -98,7 +99,7 @@ async def response(self, flow):
9899
}
99100
await self.send_callback(flow, self.config["callback_response_url"], callback_body)
100101

101-
async def send_callback(self, flow, url: str, body: dict):
102+
async def send_callback(self, flow: mitmproxy.http.HTTPFlow, url: str, body: dict):
102103
try:
103104
# use asyncio so we don't block other unrelated requests from being processed
104105
async with aiohttp.request(
@@ -122,12 +123,17 @@ async def send_callback(self, flow, url: str, body: dict):
122123
respond_status_code = test_response_body.get("respond_status_code", body.get("response_code"))
123124
respond_body = test_response_body.get("respond_body", body.get("response_body"))
124125
print(f'{datetime.now().strftime("%H:%M:%S.%f")} callback for {flow.request.url} returning custom response: HTTP {respond_status_code} {json.dumps(respond_body)}')
126+
125127
flow.response = Response.make(
126128
respond_status_code, json.dumps(respond_body),
127129
headers={
128130
"MITM-Proxy": "yes", # so we don't reprocess this
129131
"Content-Type": "application/json",
130-
})
132+
133+
# Copy the CORS headers from the original response
134+
**{k: v for k, v in flow.response.headers.items() if k.startswith("Access-Control")}
135+
},
136+
)
131137
except Exception as error:
132138
print(f"ERR: callback for {flow.request.url} returned {error}")
133139
print(f"ERR: callback, provided request body was {body}")

0 commit comments

Comments
 (0)