Skip to content

Commit 2607487

Browse files
Move mitmweb to Vite (mitmproxy#7971)
* Partially Revert "Web: harden `xsrf_token` usage (mitmproxy#7491)" This reverts commit b761cb4. The reason for this revert is that it's incompatible with Vite's server (mitmproxy#7969). We keep the parts that are compatible, and add an additional `Sec-Fetch-Site` check for for all requests. * use type imports for Vite compatibility * make flow columns work with function name minification * make modals work with function name minification * vite: move assets * web: switch builds to vite * move to vite * vite: move css and js * update CHANGELOG * [autofix.ci] apply automated fixes * fix test failures * fix static viewer * obtain xsrf cookie lazily * split js/css bundles into app/vendor * [autofix.ci] apply automated fixes * update compiled assets * fix nits --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 7093e50 commit 2607487

57 files changed

Lines changed: 3801 additions & 7692 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 2 additions & 0 deletions

mitmproxy/tools/web/app.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
from collections.abc import Sequence
1414
from io import BytesIO
1515
from typing import Any
16+
from typing import Awaitable
1617
from typing import ClassVar
1718
from typing import Concatenate
1819
from typing import Literal
20+
from typing import Optional
1921

2022
import tornado.escape
2123
import tornado.web
@@ -35,7 +37,6 @@
3537
from mitmproxy import version
3638
from mitmproxy.dns import DNSFlow
3739
from mitmproxy.http import HTTPFlow
38-
from mitmproxy.net.http import status_codes
3940
from mitmproxy.tcp import TCPFlow
4041
from mitmproxy.tcp import TCPMessage
4142
from mitmproxy.tools.web.webaddons import WebAuth
@@ -274,6 +275,14 @@ def get_current_user(self) -> bool:
274275
class RequestHandler(AuthRequestHandler):
275276
application: Application
276277

278+
def prepare(self):
279+
if (
280+
self.request.method not in ("GET", "HEAD", "OPTIONS")
281+
and "Sec-Fetch-Site" in self.request.headers
282+
and self.request.headers["Sec-Fetch-Site"] not in ("same-origin", "none")
283+
):
284+
raise tornado.httpclient.HTTPError(403)
285+
277286
def write(self, chunk: str | bytes | dict | list):
278287
# Writing arrays on the top level is ok nowadays.
279288
# http://flask.pocoo.org/docs/0.11/security/#json-security
@@ -292,6 +301,7 @@ def set_default_headers(self):
292301
"Content-Security-Policy",
293302
"default-src 'self'; "
294303
"connect-src 'self' ws:; "
304+
"img-src 'self' data:; "
295305
"style-src 'self' 'unsafe-inline'",
296306
)
297307

@@ -343,25 +353,11 @@ def write_error(self, status_code: int, **kwargs):
343353

344354

345355
class IndexHandler(RequestHandler):
346-
def _is_fetch_mode_navigate(self) -> bool:
347-
# Forbid access for non-navigate fetch modes so that they can't obtain xsrf_token.
348-
return self.request.headers.get("Sec-Fetch-Mode", "navigate") == "navigate"
349-
350356
def auth_fail(self, invalid_password: bool) -> None:
351-
# For mitmweb, we only write a login form for IndexHandler,
352-
# which has additional Sec-Fetch-Mode protections.
353-
if self._is_fetch_mode_navigate():
354-
self.render("login.html", invalid_password=invalid_password)
357+
self.render("login.html", invalid_password=invalid_password)
355358

356359
def get(self):
357-
# Forbid access for non-navigate fetch modes so that they can't obtain xsrf_token.
358-
if self._is_fetch_mode_navigate():
359-
self.render("index.html", xsrf_token=self.xsrf_token)
360-
else:
361-
raise APIError(
362-
status_codes.PRECONDITION_FAILED,
363-
f"Unexpected Sec-Fetch-Mode header: {self.request.headers.get('Sec-Fetch-Mode')}",
364-
)
360+
self.render("../index.html")
365361

366362
post = get # login form
367363

@@ -378,6 +374,11 @@ class WebSocketEventBroadcaster(tornado.websocket.WebSocketHandler, AuthRequestH
378374
_send_queue: asyncio.Queue[bytes]
379375
_send_task: asyncio.Task[None]
380376

377+
def prepare(self) -> Optional[Awaitable[None]]:
378+
token = self.xsrf_token # https://github.com/tornadoweb/tornado/issues/645
379+
assert token
380+
return None
381+
381382
def open(self, *args, **kwargs):
382383
self.connections.add(self)
383384
self._send_queue = asyncio.Queue()
@@ -918,11 +919,12 @@ def __init__(
918919
template_path=os.path.join(os.path.dirname(__file__), "templates"),
919920
static_path=os.path.join(os.path.dirname(__file__), "static"),
920921
xsrf_cookies=True,
921-
xsrf_cookie_kwargs=dict(httponly=True, samesite="Strict"),
922+
xsrf_cookie_kwargs=dict(samesite="Strict"),
922923
cookie_secret=secrets.token_bytes(32),
923924
debug=debug,
924925
autoreload=False,
925926
transforms=[GZipContentAndFlowFiles],
926927
is_valid_password=auth_addon.is_valid_password,
927928
auth_cookie_name=auth_addon.auth_cookie_name,
929+
compiled_template_cache=False, # Vite
928930
)

mitmproxy/tools/web/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>mitmproxy</title>
6+
<link rel="icon" href="static/favicon.ico" type="image/x-icon" />
7+
<meta name="viewport" content="width=device-width, initial-scale=1" />
8+
<script type="module" crossorigin src="./static/index-B67aJAE2.js"></script>
9+
<link rel="modulepreload" crossorigin href="./static/vendor-BS4xPthR.js">
10+
<link rel="stylesheet" crossorigin href="./static/vendor-Cg3S-P9H.css">
11+
<link rel="stylesheet" crossorigin href="./static/index-DhPPoJ7G.css">
12+
</head>
13+
<body>
14+
<div id="mitmproxy"></div>
15+
</body>
16+
</html>

mitmproxy/tools/web/static/app.css

Lines changed: 0 additions & 2 deletions
This file was deleted.

mitmproxy/tools/web/static/app.js

Lines changed: 0 additions & 159 deletions
This file was deleted.
File renamed without changes.

mitmproxy/tools/web/static/fonts/fontawesome-webfont.woff2 renamed to mitmproxy/tools/web/static/fontawesome-webfont-B-jkhYfk.woff2

File renamed without changes.

mitmproxy/tools/web/static/fonts/fontawesome-webfont.woff renamed to mitmproxy/tools/web/static/fontawesome-webfont-CDK5bt4p.woff

File renamed without changes.

mitmproxy/tools/web/static/fonts/fontawesome-webfont.ttf renamed to mitmproxy/tools/web/static/fontawesome-webfont-CQDK8MU3.ttf

File renamed without changes.

mitmproxy/tools/web/static/fonts/fontawesome-webfont.svg renamed to mitmproxy/tools/web/static/fontawesome-webfont-D13rzr4g.svg

File renamed without changes.

0 commit comments

Comments
 (0)