Skip to content

Instantly share code, notes, and snippets.

@inspector-ambitious
Created June 29, 2024 19:27
Show Gist options
  • Select an option

  • Save inspector-ambitious/86308c54dd4e84324bc00f7c00a4541f to your computer and use it in GitHub Desktop.

Select an option

Save inspector-ambitious/86308c54dd4e84324bc00f7c00a4541f to your computer and use it in GitHub Desktop.
Mitmproxy deminifyjs by claude
import mitmproxy.http
import requests
import json
import re
import base64
import sourcemap
class DeMinifyJS:
def response(self, flow: mitmproxy.http.HTTPFlow) -> None:
if flow.response.headers.get("Content-Type", "").startswith("application/javascript"):
content = flow.response.content.decode('utf-8')
# Look for sourcemap in comments
sourcemap_regex = r"//[#@]\s*sourceMappingURL=(.+)"
match = re.search(sourcemap_regex, content)
if match:
sourcemap_url = match.group(1)
# Handle base64 encoded sourcemaps
if sourcemap_url.startswith("data:application/json;base64,"):
base64_data = sourcemap_url.split(",", 1)[1]
sourcemap_content = json.loads(base64.b64decode(base64_data))
else:
# Resolve relative URLs
if not sourcemap_url.startswith(("http://", "https://")):
sourcemap_url = requests.compat.urljoin(flow.request.url, sourcemap_url)
sourcemap_response = requests.get(sourcemap_url)
if sourcemap_response.status_code == 200:
sourcemap_content = json.loads(sourcemap_response.text)
else:
return
# Extract original source
if 'sources' in sourcemap_content and sourcemap_content['sources']:
original_url = sourcemap_content['sources'][0]
if not original_url.startswith(("http://", "https://")):
original_url = requests.compat.urljoin(flow.request.url, original_url)
original_js_response = requests.get(original_url)
if original_js_response.status_code == 200:
flow.response.content = original_js_response.content
flow.response.headers["Content-Length"] = str(len(flow.response.content))
addons = [
DeMinifyJS()
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment