fix(browser): bound mitmproxy replies by the inherited file limit

A tighter RLIMIT_FSIZE was installed, but the reply check still used the
requested budget. A reply under that budget could exceed the inherited
limit and truncate. The effective cap now bounds the write, and a reply
that does not fit returns a fixed file-size limit. Tests also keep a
higher hard limit distinct from the new soft limit.
This commit is contained in:
Adolanium authored and Ysopwnable committed 2026-11-21 20:08:53 +01:00
1 parent 919fbfb983
commit 465ae4d1d5
2 files changed
+131 -8

No files matched your search

+37 -4
View File
@@ -5,6 +5,7 @@ The containing mitmdump process and its configuration directory are REA-owned.
"""
import base64
import errno
import hashlib
import ipaddress
import json
@@ -360,7 +361,32 @@ def _clamp_resource_limit(kind, requested):
"""Lower one soft limit. Keep the inherited hard limit, including infinity."""
soft, hard = resource.getrlimit(kind)
finite = [requested] + [value for value in (soft, hard) if value != resource.RLIM_INFINITY]
resource.setrlimit(kind, (min(finite), hard))
effective = min(finite)
resource.setrlimit(kind, (effective, hard))
return effective
def _file_limit_reply(effective):
"""A reply that fits a tighter inherited file-size limit and names that limit."""
return json.dumps(
{
"ok": False,
"reason": "fixed-limit",
"message": "The effective RLIMIT_FSIZE of " + str(effective) + " bytes cannot hold the serialized reply. The requested output budget does not raise an inherited limit.",
"pointer": "",
"boundary": "file-size",
"ceiling": {
"name": "The inherited file-size limit of " + str(effective) + " bytes",
"kind": "bytes",
"limit": effective,
"unit": "bytes",
"narrowing": "Raise the process RLIMIT_FSIZE above the complete reply, or select a capture whose reply fits in the inherited limit.",
},
},
ensure_ascii=True,
allow_nan=False,
separators=(",", ":"),
).encode("ascii")
class OfflineCapture:
@@ -378,7 +404,7 @@ class OfflineCapture:
if any(type(budgets[key]) is not int or budgets[key] < 1 for key in ("inputBytes", "outputBytes", "depth")):
raise ValueError("Invalid admitted capture budgets")
output_bytes = budgets["outputBytes"]
_clamp_resource_limit(resource.RLIMIT_FSIZE, output_bytes)
file_limit = _clamp_resource_limit(resource.RLIMIT_FSIZE, output_bytes)
try:
response = {"ok": True, "value": decode(request)}
except Exception as error:
@@ -387,8 +413,15 @@ class OfflineCapture:
output = json.dumps(response, ensure_ascii=True, allow_nan=False, separators=(",", ":")).encode("ascii")
except Exception as error:
output = json.dumps(failure_reply(error), separators=(",", ":")).encode("ascii")
if len(output) > output_bytes:
output = b'{"ok":false,"reason":"limit","message":"Native reply exceeds the complete-evidence output budget.","pointer":""}'
if len(output) > file_limit:
# file_limit is never above the requested budget. A lower value is the inherited cap.
output = (
_file_limit_reply(file_limit)
if file_limit < output_bytes
else b'{"ok":false,"reason":"limit","message":"Native reply exceeds the complete-evidence output budget.","pointer":""}'
)
if len(output) > file_limit:
raise OSError(errno.EFBIG, "file-size limit cannot hold a limit refusal")
Path(request["reply_path"]).write_bytes(output)
finally:
ctx.master.shutdown()
+94 -4
View File
@@ -284,11 +284,12 @@ class OfflineLimitTests(unittest.TestCase):
def test_tighter_hard_limits_are_kept_and_shutdown_still_runs(self):
infinity = 2 ** 62
# Linux never reports a soft limit above its hard limit. Start at that ceiling.
limits = {
1: (infinity, 1024),
2: (infinity, 5),
1: (1024, 1024),
2: (5, 5),
3: (infinity, infinity),
4: (infinity, 32),
4: (512, 512),
}
calls = []
@@ -319,11 +320,100 @@ class OfflineLimitTests(unittest.TestCase):
reply = json.loads(handle.read())
self.assertEqual(
calls,
[(1, (1024, 1024)), (2, (5, 5)), (3, (0, 0)), (4, (32, 32))],
[(1, (1024, 1024)), (2, (5, 5)), (3, (0, 0)), (4, (512, 512))],
)
self.assertEqual(shutdowns, ["down"])
self.assertFalse(reply["ok"])
def test_inherited_hard_limit_stays_above_the_new_soft_limit(self):
infinity = 2 ** 62
address_limit = 768 * 1024 * 1024
output_bytes = 1000
# Unlimited CPU, and finite soft limits strictly below their hard limits.
# Writing the new soft value into the hard limit fails these pairs.
limits = {
1: (address_limit * 2, address_limit * 4),
2: (infinity, infinity),
3: (infinity, infinity),
4: (5000, 9000),
}
calls = []
def setrlimit(kind, pair):
_soft, hard = limits[kind]
if hard != infinity and pair[1] > hard:
raise ValueError("not allowed to raise maximum limit")
calls.append((kind, pair))
limits[kind] = pair
module = self.load_limited(limits, setrlimit)
shutdowns = []
module.ctx.master = types.SimpleNamespace(shutdown=lambda: shutdowns.append("down"))
with tempfile.TemporaryDirectory() as parent:
request_path = os.path.join(parent, "request.json")
reply_path = os.path.join(parent, "reply.json")
with open(request_path, "w", encoding="utf-8") as handle:
json.dump(
{
"budgets": {"inputBytes": 8, "outputBytes": output_bytes, "depth": 2},
"reply_path": reply_path,
},
handle,
)
module.ctx.options = types.SimpleNamespace(rea_request_path=request_path)
module.addons[0].running()
self.assertEqual(
calls,
[
(1, (address_limit, address_limit * 4)),
(2, (30, infinity)),
(3, (0, 0)),
(4, (output_bytes, 9000)),
],
)
self.assertEqual(shutdowns, ["down"])
def test_tighter_inherited_file_limit_bounds_the_reply(self):
infinity = 2 ** 62
file_limit = 4096
limits = {kind: (infinity, infinity) for kind in (1, 2, 3)}
limits[4] = (file_limit, 8192)
calls = []
def setrlimit(kind, pair):
_soft, hard = limits[kind]
if hard != infinity and pair[1] > hard:
raise ValueError("not allowed to raise maximum limit")
calls.append((kind, pair))
limits[kind] = pair
module = self.load_limited(limits, setrlimit)
module.decode = lambda _request: {"pad": "x" * 5000}
shutdowns = []
module.ctx.master = types.SimpleNamespace(shutdown=lambda: shutdowns.append("down"))
with tempfile.TemporaryDirectory() as parent:
request_path = os.path.join(parent, "request.json")
reply_path = os.path.join(parent, "reply.json")
with open(request_path, "w", encoding="utf-8") as handle:
json.dump(
{
"budgets": {"inputBytes": 8, "outputBytes": 10000, "depth": 2},
"reply_path": reply_path,
},
handle,
)
module.ctx.options = types.SimpleNamespace(rea_request_path=request_path)
module.addons[0].running()
with open(reply_path, "rb") as handle:
reply_bytes = handle.read()
reply = json.loads(reply_bytes.decode("ascii"))
self.assertLessEqual(len(reply_bytes), file_limit)
self.assertEqual(reply["reason"], "fixed-limit")
self.assertEqual(reply["boundary"], "file-size")
self.assertEqual(reply["ceiling"]["limit"], file_limit)
self.assertEqual(calls[-1], (4, (file_limit, 8192)))
self.assertEqual(shutdowns, ["down"])
def test_limit_setup_failure_still_shuts_down(self):
infinity = 2 ** 62
limits = {kind: (infinity, infinity) for kind in (1, 2, 3, 4)}