From 825bc9aa47d63876d81b6cf25817d1669d33cfe8 Mon Sep 17 00:00:00 2001 From: Itai Sadan Date: Fri, 17 Jan 2020 16:22:38 +0200 Subject: Fix command quotes and error logging Fixes error when trying to manually set "multipart form" view. Also fixes "(more in eventlog)" prompt where nothing is written to the event log. --- mitmproxy/tools/console/consoleaddons.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mitmproxy/tools/console/consoleaddons.py b/mitmproxy/tools/console/consoleaddons.py index 7fcd9b48..ac92c96f 100644 --- a/mitmproxy/tools/console/consoleaddons.py +++ b/mitmproxy/tools/console/consoleaddons.py @@ -276,11 +276,11 @@ class ConsoleAddon: def callback(opt): # We're now outside of the call context... - repl = " ".join(command_lexer.quote(x) for x in args) - repl = repl.replace("{choice}", opt) + repl = " ".join(command_lexer.quote(x.replace("{choice}", opt)) for x in args) try: self.master.commands.execute(subcmd + " " + repl) except exceptions.CommandError as e: + ctx.log.error(str(e)) signals.status_message.send(message=str(e)) self.master.overlay( @@ -539,9 +539,10 @@ class ConsoleAddon: raise exceptions.CommandError("Invalid flowview mode.") try: - cmd = 'view.settings.setval @focus flowview_mode_%s %s' % (idx, mode) + cmd = 'view.settings.setval @focus flowview_mode_%s %s' % (idx, command_lexer.quote(mode)) self.master.commands.execute(cmd) except exceptions.CommandError as e: + ctx.log.error(e) signals.status_message.send(message=str(e)) @command.command("console.flowview.mode.options") -- cgit v1.2.3 From f2c0f598b5224f8e3fbe94dc5817ab861b0a8438 Mon Sep 17 00:00:00 2001 From: Itai Sadan Date: Fri, 17 Jan 2020 16:26:59 +0200 Subject: Replace tabs with spaces in error because the event log replaces them with question marks --- mitmproxy/command.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mitmproxy/command.py b/mitmproxy/command.py index 48f9051e..341a1401 100644 --- a/mitmproxy/command.py +++ b/mitmproxy/command.py @@ -103,7 +103,7 @@ class Command: except TypeError: expected = f'Expected: {str(self.signature.parameters)}' received = f'Received: {str(args)}' - raise exceptions.CommandError(f"Command argument mismatch: \n\t{expected}\n\t{received}") + raise exceptions.CommandError(f"Command argument mismatch: \n {expected}\n {received}") for name, value in bound_arguments.arguments.items(): convert_to = self.signature.parameters[name].annotation -- cgit v1.2.3 From 0afd0c933b7b5a3654cb6a9e27e8e24dbedcd789 Mon Sep 17 00:00:00 2001 From: Itai Sadan Date: Sat, 18 Jan 2020 10:44:00 +0200 Subject: consoleaddons.py - add command_lexer.quote where it seems relevant --- mitmproxy/tools/console/consoleaddons.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/mitmproxy/tools/console/consoleaddons.py b/mitmproxy/tools/console/consoleaddons.py index ac92c96f..8e99566d 100644 --- a/mitmproxy/tools/console/consoleaddons.py +++ b/mitmproxy/tools/console/consoleaddons.py @@ -160,7 +160,7 @@ class ConsoleAddon: fv = self.master.window.current("options") if not fv: raise exceptions.CommandError("Not viewing options.") - self.master.commands.execute("options.reset.one %s" % fv.current_name()) + self.master.commands.execute("options.reset.one %s" % command_lexer.quote(fv.current_name())) @command.command("console.nav.start") def nav_start(self) -> None: @@ -280,8 +280,9 @@ class ConsoleAddon: try: self.master.commands.execute(subcmd + " " + repl) except exceptions.CommandError as e: - ctx.log.error(str(e)) - signals.status_message.send(message=str(e)) + msg = str(e) + ctx.log.error(msg) + signals.status_message.send(message=msg) self.master.overlay( overlay.Chooser(self.master, prompt, choices, "", callback) @@ -455,7 +456,7 @@ class ConsoleAddon: flow.request.url = url.decode() elif flow_part in ["method", "status_code", "reason"]: self.master.commands.execute( - "console.command flow.set @focus %s " % flow_part + "console.command flow.set @focus %s " % command_lexer.quote(flow_part) ) def _grideditor(self): @@ -542,8 +543,9 @@ class ConsoleAddon: cmd = 'view.settings.setval @focus flowview_mode_%s %s' % (idx, command_lexer.quote(mode)) self.master.commands.execute(cmd) except exceptions.CommandError as e: - ctx.log.error(e) - signals.status_message.send(message=str(e)) + msg = str(e) + ctx.log.error(msg) + signals.status_message.send(message=msg) @command.command("console.flowview.mode.options") def flowview_mode_options(self) -> typing.Sequence[str]: @@ -562,7 +564,7 @@ class ConsoleAddon: raise exceptions.CommandError("Not viewing a flow.") idx = fv.body.tab_offset - cmd = 'view.settings.getval @focus flowview_mode_%s %s' % (idx, self.master.options.console_default_contentview) + cmd = 'view.settings.getval @focus flowview_mode_%s %s' % (idx, command_lexer.quote(self.master.options.console_default_contentview)) return self.master.commands.execute(cmd) @command.command("console.key.contexts") -- cgit v1.2.3 From 046de4a39b40c7805136926abe349da760a9753f Mon Sep 17 00:00:00 2001 From: Itai Sadan Date: Sat, 18 Jan 2020 11:22:54 +0200 Subject: Expose CommandManager.call_strings and use it in consoleaddons.py This avoids the whole quote/unquote issue. --- mitmproxy/command.py | 4 ++-- mitmproxy/tools/console/consoleaddons.py | 32 +++++++++++++++++++------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/mitmproxy/command.py b/mitmproxy/command.py index 341a1401..8822bb9b 100644 --- a/mitmproxy/command.py +++ b/mitmproxy/command.py @@ -241,7 +241,7 @@ class CommandManager: raise exceptions.CommandError("Unknown command: %s" % command_name) return self.commands[command_name].func(*args) - def _call_strings(self, command_name: str, args: typing.Sequence[str]) -> typing.Any: + def call_strings(self, command_name: str, args: typing.Sequence[str]) -> typing.Any: """ Call a command using a list of string arguments. May raise CommandError. """ @@ -262,7 +262,7 @@ class CommandManager: for part in parts if part.type != mitmproxy.types.Space ] - return self._call_strings(command_name, args) + return self.call_strings(command_name, args) def dump(self, out=sys.stdout) -> None: cmds = list(self.commands.values()) diff --git a/mitmproxy/tools/console/consoleaddons.py b/mitmproxy/tools/console/consoleaddons.py index 8e99566d..617ac5e5 100644 --- a/mitmproxy/tools/console/consoleaddons.py +++ b/mitmproxy/tools/console/consoleaddons.py @@ -160,7 +160,7 @@ class ConsoleAddon: fv = self.master.window.current("options") if not fv: raise exceptions.CommandError("Not viewing options.") - self.master.commands.execute("options.reset.one %s" % command_lexer.quote(fv.current_name())) + self.master.commands.call_strings("options.reset.one", [fv.current_name()]) @command.command("console.nav.start") def nav_start(self) -> None: @@ -248,12 +248,13 @@ class ConsoleAddon: def callback(opt): # We're now outside of the call context... - repl = cmd + " " + " ".join(args) - repl = repl.replace("{choice}", opt) + repl = [arg.replace("{choice}", opt) for arg in args] try: - self.master.commands.execute(repl) + self.master.commands.call_strings(cmd, repl) except exceptions.CommandError as e: - signals.status_message.send(message=str(e)) + msg = str(e) + ctx.log.error(msg) + signals.status_message.send(message=msg) self.master.overlay( overlay.Chooser(self.master, prompt, choices, "", callback) @@ -276,9 +277,9 @@ class ConsoleAddon: def callback(opt): # We're now outside of the call context... - repl = " ".join(command_lexer.quote(x.replace("{choice}", opt)) for x in args) + repl = [arg.replace("{choice}", opt) for arg in args] try: - self.master.commands.execute(subcmd + " " + repl) + self.master.commands.call_strings(subcmd, repl) except exceptions.CommandError as e: msg = str(e) ctx.log.error(msg) @@ -455,8 +456,9 @@ class ConsoleAddon: url = edited_url.rstrip(b"\n") flow.request.url = url.decode() elif flow_part in ["method", "status_code", "reason"]: - self.master.commands.execute( - "console.command flow.set @focus %s " % command_lexer.quote(flow_part) + self.master.commands.call_strings( + "console.command", + ["flow.set", "@focus", flow_part] ) def _grideditor(self): @@ -540,8 +542,10 @@ class ConsoleAddon: raise exceptions.CommandError("Invalid flowview mode.") try: - cmd = 'view.settings.setval @focus flowview_mode_%s %s' % (idx, command_lexer.quote(mode)) - self.master.commands.execute(cmd) + self.master.commands.call_strings( + "view.settings.setval", + ["@focus", "flowview_mode_%s" % (idx,), mode] + ) except exceptions.CommandError as e: msg = str(e) ctx.log.error(msg) @@ -564,8 +568,10 @@ class ConsoleAddon: raise exceptions.CommandError("Not viewing a flow.") idx = fv.body.tab_offset - cmd = 'view.settings.getval @focus flowview_mode_%s %s' % (idx, command_lexer.quote(self.master.options.console_default_contentview)) - return self.master.commands.execute(cmd) + return self.master.commands.call_strings( + "view.settings.getval", + ["@focus", "flowview_mode_%s" % (idx,), self.master.options.console_default_contentview] + ) @command.command("console.key.contexts") def key_contexts(self) -> typing.Sequence[str]: -- cgit v1.2.3 From eaab77960b90b1fbb2d4282550ffa084e004051d Mon Sep 17 00:00:00 2001 From: Itai Sadan Date: Sat, 18 Jan 2020 11:44:12 +0200 Subject: fix missing error to event log in commandexecutor.py --- mitmproxy/tools/console/commandexecutor.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mitmproxy/tools/console/commandexecutor.py b/mitmproxy/tools/console/commandexecutor.py index c738e349..697c616c 100644 --- a/mitmproxy/tools/console/commandexecutor.py +++ b/mitmproxy/tools/console/commandexecutor.py @@ -2,6 +2,7 @@ import typing from mitmproxy import exceptions from mitmproxy import flow +from mitmproxy import ctx from mitmproxy.tools.console import overlay from mitmproxy.tools.console import signals @@ -15,8 +16,10 @@ class CommandExecutor: if cmd.strip(): try: ret = self.master.commands.execute(cmd) - except exceptions.CommandError as v: - signals.status_message.send(message=str(v)) + except exceptions.CommandError as e: + msg = str(e) + ctx.log.error(msg) + signals.status_message.send(message=msg) else: if ret: if type(ret) == typing.Sequence[flow.Flow]: -- cgit v1.2.3 From 04c88b49c8df71bd3198bad10f15a58ca449fbce Mon Sep 17 00:00:00 2001 From: Itai Sadan Date: Sat, 18 Jan 2020 12:00:23 +0200 Subject: remove status_message.send where log.error has the same effect --- mitmproxy/tools/console/commandexecutor.py | 4 +--- mitmproxy/tools/console/consoleaddons.py | 12 +++--------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/mitmproxy/tools/console/commandexecutor.py b/mitmproxy/tools/console/commandexecutor.py index 697c616c..1c6d5aa6 100644 --- a/mitmproxy/tools/console/commandexecutor.py +++ b/mitmproxy/tools/console/commandexecutor.py @@ -17,9 +17,7 @@ class CommandExecutor: try: ret = self.master.commands.execute(cmd) except exceptions.CommandError as e: - msg = str(e) - ctx.log.error(msg) - signals.status_message.send(message=msg) + ctx.log.error(str(e)) else: if ret: if type(ret) == typing.Sequence[flow.Flow]: diff --git a/mitmproxy/tools/console/consoleaddons.py b/mitmproxy/tools/console/consoleaddons.py index 617ac5e5..905653e7 100644 --- a/mitmproxy/tools/console/consoleaddons.py +++ b/mitmproxy/tools/console/consoleaddons.py @@ -252,9 +252,7 @@ class ConsoleAddon: try: self.master.commands.call_strings(cmd, repl) except exceptions.CommandError as e: - msg = str(e) - ctx.log.error(msg) - signals.status_message.send(message=msg) + ctx.log.error(str(e)) self.master.overlay( overlay.Chooser(self.master, prompt, choices, "", callback) @@ -281,9 +279,7 @@ class ConsoleAddon: try: self.master.commands.call_strings(subcmd, repl) except exceptions.CommandError as e: - msg = str(e) - ctx.log.error(msg) - signals.status_message.send(message=msg) + ctx.log.error(str(e)) self.master.overlay( overlay.Chooser(self.master, prompt, choices, "", callback) @@ -547,9 +543,7 @@ class ConsoleAddon: ["@focus", "flowview_mode_%s" % (idx,), mode] ) except exceptions.CommandError as e: - msg = str(e) - ctx.log.error(msg) - signals.status_message.send(message=msg) + ctx.log.error(str(e)) @command.command("console.flowview.mode.options") def flowview_mode_options(self) -> typing.Sequence[str]: -- cgit v1.2.3 From 78dc2094b0d92abcfac97427d88e56568ed9f487 Mon Sep 17 00:00:00 2001 From: Itai Sadan Date: Sat, 18 Jan 2020 12:59:59 +0200 Subject: fix re-raised exceptions not having a message --- mitmproxy/command.py | 2 +- mitmproxy/types.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mitmproxy/command.py b/mitmproxy/command.py index 8822bb9b..bfec47e1 100644 --- a/mitmproxy/command.py +++ b/mitmproxy/command.py @@ -284,7 +284,7 @@ def parsearg(manager: CommandManager, spec: str, argtype: type) -> typing.Any: try: return t.parse(manager, argtype, spec) except exceptions.TypeError as e: - raise exceptions.CommandError from e + raise exceptions.CommandError(str(e)) from e def command(name: typing.Optional[str] = None): diff --git a/mitmproxy/types.py b/mitmproxy/types.py index ac992217..0d4ffd69 100644 --- a/mitmproxy/types.py +++ b/mitmproxy/types.py @@ -134,7 +134,7 @@ class _IntType(_BaseType): try: return int(s) except ValueError as e: - raise exceptions.TypeError from e + raise exceptions.TypeError(str(e)) from e def is_valid(self, manager: "CommandManager", typ: typing.Any, val: typing.Any) -> bool: return isinstance(val, int) @@ -328,7 +328,7 @@ class _FlowType(_BaseFlowType): try: flows = manager.execute("view.flows.resolve %s" % (s)) except exceptions.CommandError as e: - raise exceptions.TypeError from e + raise exceptions.TypeError(str(e)) from e if len(flows) != 1: raise exceptions.TypeError( "Command requires one flow, specification matched %s." % len(flows) @@ -347,7 +347,7 @@ class _FlowsType(_BaseFlowType): try: return manager.execute("view.flows.resolve %s" % (s)) except exceptions.CommandError as e: - raise exceptions.TypeError from e + raise exceptions.TypeError(str(e)) from e def is_valid(self, manager: "CommandManager", typ: typing.Any, val: typing.Any) -> bool: try: -- cgit v1.2.3