Add hex editor for non text files (#6768)

* add hex editor for non text files

* update changelog

* [autofix.ci] apply automated fixes

* fix failing text

* fix tests

* fix lint

* [autofix.ci] apply automated fixes

* Update test/mitmproxy/tools/console/test_master.py

Co-authored-by: Maximilian Hils <github@maximilianhils.com>

* Update test/mitmproxy/tools/console/test_master.py

Co-authored-by: Maximilian Hils <github@maximilianhils.com>

* a few changes

* fix tests

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Maximilian Hils <github@maximilianhils.com>
This commit is contained in:
winy 2024-04-02 08:19:01 -03:00 committed by GitHub
parent 174253b79b
commit ccf45a92e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 2 deletions

View File

@ -7,6 +7,8 @@
## Unreleased: mitmproxy next
* Add support for editing non text files in a hex editor
([#6768](https://github.com/mitmproxy/mitmproxy/pull/6768), @wnyyyy)
* Add section in mitmweb for rendering, adding and removing a comment
([#6709](https://github.com/mitmproxy/mitmproxy/pull/6709), @lups2000)
* Fix multipart form content view being unusable.

View File

@ -29,6 +29,7 @@ from mitmproxy.tools.console import keymap
from mitmproxy.tools.console import palettes
from mitmproxy.tools.console import signals
from mitmproxy.tools.console import window
from mitmproxy.utils import strutils
T = TypeVar("T", str, bytes)
@ -120,12 +121,23 @@ class ConsoleMaster(master.Master):
else:
return "vi"
def get_hex_editor(self) -> str:
editors = ["ghex", "bless", "hexedit", "hxd", "hexer", "hexcurse"]
for editor in editors:
if shutil.which(editor):
return editor
return self.get_editor()
def spawn_editor(self, data: T) -> T:
text = not isinstance(data, bytes)
text = isinstance(data, str)
fd, name = tempfile.mkstemp("", "mitmproxy", text=text)
with_hexeditor = isinstance(data, bytes) and strutils.is_mostly_bin(data)
with open(fd, "w" if text else "wb") as f:
f.write(data)
c = self.get_editor()
if with_hexeditor:
c = self.get_hex_editor()
else:
c = self.get_editor()
cmd = shlex.split(c)
cmd.append(name)
with self.uistopped():

View File

@ -0,0 +1,30 @@
from unittest.mock import Mock
def test_spawn_editor(monkeypatch, console):
text_data = "text"
binary_data = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09"
console.get_editor = Mock()
console.get_editor.return_value = "editor"
console.get_hex_editor = Mock()
console.get_hex_editor.return_value = "editor"
monkeypatch.setattr("subprocess.call", (lambda _: None))
console.loop = Mock()
console.loop.stop = Mock()
console.loop.start = Mock()
console.loop.draw_screen = Mock()
console.spawn_editor(text_data)
console.get_editor.assert_called_once()
console.spawn_editor(binary_data)
console.get_hex_editor.assert_called_once()
def test_get_hex_editor(monkeypatch, console):
test_editor = "hexedit"
monkeypatch.setattr("shutil.which", lambda x: x == test_editor)
editor = console.get_hex_editor()
assert editor == test_editor