Skip to content

Commit

Permalink
commands: verify command function signatures before call (mitmproxy#2659
Browse files Browse the repository at this point in the history
)

Fixes mitmproxy#2652, and many other possible crashes on user input.
  • Loading branch information
cortesi authored and mhils committed Dec 11, 2017
1 parent 472a740 commit b8cbb4d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mitmproxy/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,19 @@ def parsearg(manager: CommandManager, spec: str, argtype: type) -> typing.Any:
raise exceptions.CommandError("Unsupported argument type: %s" % argtype)


def verify_arg_signature(f: typing.Callable, args: list, kwargs: dict) -> None:
sig = inspect.signature(f)
try:
sig.bind(*args, **kwargs)
except TypeError as v:
raise exceptions.CommandError("Argument mismatch: %s" % v.args[0])


def command(path):
def decorator(function):
@functools.wraps(function)
def wrapper(*args, **kwargs):
verify_arg_signature(function, args, kwargs)
return function(*args, **kwargs)
wrapper.__dict__["command_path"] = path
return wrapper
Expand Down
7 changes: 7 additions & 0 deletions test/mitmproxy/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,10 @@ def test_decorator():
with taddons.context() as tctx:
tctx.master.addons.add(a)
assert tctx.master.commands.call("cmd1 bar") == "ret bar"


def test_verify_arg_signature():
with pytest.raises(exceptions.CommandError):
command.verify_arg_signature(lambda: None, [1, 2], {})
print('hello there')
command.verify_arg_signature(lambda a, b: None, [1, 2], {})

0 comments on commit b8cbb4d

Please sign in to comment.