1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00

trezor.wire: unexpected message should interrupt workflow silently

This commit is contained in:
Jan Pochyla 2017-01-18 18:00:38 +01:00
parent 4ab469d02d
commit ec49c2d97a

View File

@ -61,12 +61,14 @@ async def call(session_id, pbuf_msg, *response_types):
class FailureError(Exception): class FailureError(Exception):
def __init__(self, code, message):
super(FailureError, self).__init__(code, message)
def to_protobuf(self): def to_protobuf(self):
from trezor.messages.Failure import Failure from trezor.messages.Failure import Failure
return Failure(code=self.args[0], message=self.args[1]) code, message = self.args
return Failure(code=code, message=message)
class CloseWorkflow(Exception):
pass
def protobuf_workflow(session_id, msg_type, data_len, callback, *args): def protobuf_workflow(session_id, msg_type, data_len, callback, *args):
@ -83,6 +85,9 @@ async def _wrap_protobuf_workflow(wf, session_id):
try: try:
result = await wf result = await wf
except CloseWorkflow:
return
except FailureError as e: except FailureError as e:
await write(session_id, e.to_protobuf()) await write(session_id, e.to_protobuf())
raise raise
@ -115,15 +120,14 @@ def _handle_response(session_id, msg_type, data_len, response_types, signal):
if msg_type in response_types: if msg_type in response_types:
return _build_protobuf(msg_type, signal.send) return _build_protobuf(msg_type, signal.send)
else: else:
from trezor.messages.FailureType import UnexpectedMessage signal.send(CloseWorkflow())
signal.send(FailureError(UnexpectedMessage, 'Unexpected message'))
return _handle_workflow(session_id, msg_type, data_len) return _handle_workflow(session_id, msg_type, data_len)
def _handle_workflow(session_id, msg_type, data_len): def _handle_workflow(session_id, msg_type, data_len):
if msg_type in _workflow_callbacks: if msg_type in _workflow_callbacks:
args = _workflow_args[msg_type]
callback = _workflow_callbacks[msg_type] callback = _workflow_callbacks[msg_type]
args = _workflow_args[msg_type]
return callback(session_id, msg_type, data_len, *args) return callback(session_id, msg_type, data_len, *args)
else: else:
return _handle_unexpected(session_id, msg_type, data_len) return _handle_unexpected(session_id, msg_type, data_len)
@ -133,12 +137,20 @@ def _handle_unexpected(session_id, msg_type, data_len):
log.warning( log.warning(
__name__, 'session %x: skip type %d, len %d', session_id, msg_type, data_len) __name__, 'session %x: skip type %d, len %d', session_id, msg_type, data_len)
# read the message in full
try: try:
while True: while True:
yield yield
except EOFError: except EOFError:
pass pass
# respond with an unknown message error
from trezor.messages.Failure import Failure
from trezor.messages.FailureType import UnexpectedMessage
failure = Failure(code=UnexpectedMessage, message='Unexpected message')
sessions.get_codec(session_id).encode(
session_id, failure.MESSAGE_WIRE_TYPE, failure.dumps(), _write_report)
def _write_report(report): def _write_report(report):
if __debug__: if __debug__: