Skip to content

Commit

Permalink
all: rename protobuf unsafe_prompts to safety_checks
Browse files Browse the repository at this point in the history
  • Loading branch information
matejcik committed Jul 24, 2020
1 parent 2c4661e commit 0ca9a54
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 25 deletions.
7 changes: 6 additions & 1 deletion common/protob/messages-management.proto
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ message ApplySettings {
optional uint32 auto_lock_delay_ms = 6;
optional uint32 display_rotation = 7; // in degrees from North
optional bool passphrase_always_on_device = 8; // do not prompt for passphrase, enforce device entry
optional bool unsafe_prompts = 9; // allow or disallow unsafe prompts
optional SafetyCheckLevel safety_checks = 9; // Safety check level, set to Prompt to limit path namespace enforcement

enum SafetyCheckLevel {
Strict = 0;
Prompt = 1;
}
}

/**
Expand Down
22 changes: 13 additions & 9 deletions core/src/apps/management/apply_settings.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import storage.device
from trezor import ui, wire, workflow
from trezor.messages import ButtonRequestType
from trezor.messages import ButtonRequestType, SafetyCheckLevel
from trezor.messages.Success import Success
from trezor.strings import format_duration_ms
from trezor.ui.text import Text
Expand All @@ -9,7 +9,7 @@
from apps.common.confirm import require_confirm, require_hold_to_confirm

if False:
from trezor.messages.ApplySettings import ApplySettings
from trezor.messages.ApplySettings import ApplySettings, EnumTypeSafetyCheckLevel


async def apply_settings(ctx: wire.Context, msg: ApplySettings):
Expand All @@ -22,7 +22,7 @@ async def apply_settings(ctx: wire.Context, msg: ApplySettings):
and msg.passphrase_always_on_device is None
and msg.display_rotation is None
and msg.auto_lock_delay_ms is None
and msg.unsafe_prompts is None
and msg.safety_checks is None
):
raise wire.ProcessError("No setting provided")

Expand Down Expand Up @@ -61,9 +61,11 @@ async def apply_settings(ctx: wire.Context, msg: ApplySettings):
# use the value that was stored, not the one that was supplied by the user
workflow.idle_timer.set(storage.device.get_autolock_delay_ms(), lock_device)

if msg.unsafe_prompts is not None:
await require_confirm_unsafe_prompts(ctx, msg.unsafe_prompts)
storage.device.set_unsafe_prompts_allowed(msg.unsafe_prompts)
if msg.safety_checks is not None:
await require_confirm_safety_checks(ctx, msg.safety_checks)
storage.device.set_unsafe_prompts_allowed(
msg.safety_checks == SafetyCheckLevel.Prompt
)

if msg.display_rotation is not None:
await require_confirm_change_display_rotation(ctx, msg.display_rotation)
Expand Down Expand Up @@ -132,16 +134,18 @@ async def require_confirm_change_autolock_delay(ctx, delay_ms):
await require_confirm(ctx, text, ButtonRequestType.ProtectCall)


async def require_confirm_unsafe_prompts(ctx, allow: bool) -> None:
if allow:
async def require_confirm_safety_checks(ctx, level: EnumTypeSafetyCheckLevel) -> None:
if level == SafetyCheckLevel.Prompt:
text = Text("Unsafe prompts", ui.ICON_WIPE)
text.normal(
"Trezor will allow you to", "confirm actions which", "might be dangerous."
)
text.br_half()
text.bold("Allow unsafe prompts?")
await require_hold_to_confirm(ctx, text, ButtonRequestType.ProtectCall)
else:
elif level == SafetyCheckLevel.Strict:
text = Text("Unsafe prompts", ui.ICON_CONFIG)
text.normal("Do you really want to", "disable unsafe prompts?")
await require_confirm(ctx, text, ButtonRequestType.ProtectCall)
else:
raise ValueError # enum value out of range
7 changes: 4 additions & 3 deletions core/src/trezor/messages/ApplySettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
try:
from typing import Dict, List # noqa: F401
from typing_extensions import Literal # noqa: F401
EnumTypeSafetyCheckLevel = Literal[0, 1]
except ImportError:
pass

Expand All @@ -22,7 +23,7 @@ def __init__(
auto_lock_delay_ms: int = None,
display_rotation: int = None,
passphrase_always_on_device: bool = None,
unsafe_prompts: bool = None,
safety_checks: EnumTypeSafetyCheckLevel = None,
) -> None:
self.language = language
self.label = label
Expand All @@ -31,7 +32,7 @@ def __init__(
self.auto_lock_delay_ms = auto_lock_delay_ms
self.display_rotation = display_rotation
self.passphrase_always_on_device = passphrase_always_on_device
self.unsafe_prompts = unsafe_prompts
self.safety_checks = safety_checks

@classmethod
def get_fields(cls) -> Dict:
Expand All @@ -43,5 +44,5 @@ def get_fields(cls) -> Dict:
6: ('auto_lock_delay_ms', p.UVarintType, 0),
7: ('display_rotation', p.UVarintType, 0),
8: ('passphrase_always_on_device', p.BoolType, 0),
9: ('unsafe_prompts', p.BoolType, 0),
9: ('safety_checks', p.EnumType("SafetyCheckLevel", (0, 1)), 0),
}
7 changes: 7 additions & 0 deletions core/src/trezor/messages/SafetyCheckLevel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Automatically generated by pb2py
# fmt: off
if False:
from typing_extensions import Literal

Strict = 0 # type: Literal[0]
Prompt = 1 # type: Literal[1]
11 changes: 8 additions & 3 deletions python/src/trezorlib/cli/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import click

from .. import device
from .. import device, messages
from . import ChoiceType, with_client

ROTATION = {"north": 0, "east": 90, "south": 180, "west": 270}
Expand Down Expand Up @@ -142,8 +142,13 @@ def unsafe_prompts(client, allow):
to confirm possibly dangerous actions instead of rejecting them outright.
Use with caution.
"""
allowed = allow == "on"
return device.apply_settings(client, unsafe_prompts=allowed)
# TODO change this to ChoiceType
if allow == "on":
level = messages.SafetyCheckLevel.Prompt
else:
level = messages.SafetyCheckLevel.Strict

return device.apply_settings(client, safety_checks=level)


#
Expand Down
4 changes: 2 additions & 2 deletions python/src/trezorlib/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def apply_settings(
passphrase_always_on_device=None,
auto_lock_delay_ms=None,
display_rotation=None,
unsafe_prompts=None,
safety_checks=None,
):
settings = messages.ApplySettings(
label=label,
Expand All @@ -44,7 +44,7 @@ def apply_settings(
passphrase_always_on_device=passphrase_always_on_device,
auto_lock_delay_ms=auto_lock_delay_ms,
display_rotation=display_rotation,
unsafe_prompts=unsafe_prompts,
safety_checks=safety_checks,
)

out = client.call(settings)
Expand Down
7 changes: 4 additions & 3 deletions python/src/trezorlib/messages/ApplySettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
try:
from typing import Dict, List # noqa: F401
from typing_extensions import Literal # noqa: F401
EnumTypeSafetyCheckLevel = Literal[0, 1]
except ImportError:
pass

Expand All @@ -22,7 +23,7 @@ def __init__(
auto_lock_delay_ms: int = None,
display_rotation: int = None,
passphrase_always_on_device: bool = None,
unsafe_prompts: bool = None,
safety_checks: EnumTypeSafetyCheckLevel = None,
) -> None:
self.language = language
self.label = label
Expand All @@ -31,7 +32,7 @@ def __init__(
self.auto_lock_delay_ms = auto_lock_delay_ms
self.display_rotation = display_rotation
self.passphrase_always_on_device = passphrase_always_on_device
self.unsafe_prompts = unsafe_prompts
self.safety_checks = safety_checks

@classmethod
def get_fields(cls) -> Dict:
Expand All @@ -43,5 +44,5 @@ def get_fields(cls) -> Dict:
6: ('auto_lock_delay_ms', p.UVarintType, 0),
7: ('display_rotation', p.UVarintType, 0),
8: ('passphrase_always_on_device', p.BoolType, 0),
9: ('unsafe_prompts', p.BoolType, 0),
9: ('safety_checks', p.EnumType("SafetyCheckLevel", (0, 1)), 0),
}
7 changes: 7 additions & 0 deletions python/src/trezorlib/messages/SafetyCheckLevel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Automatically generated by pb2py
# fmt: off
if False:
from typing_extensions import Literal

Strict = 0 # type: Literal[0]
Prompt = 1 # type: Literal[1]
1 change: 1 addition & 0 deletions python/src/trezorlib/messages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@
from . import PinMatrixRequestType
from . import RecoveryDeviceType
from . import RequestType
from . import SafetyCheckLevel
from . import SdProtectOperationType
from . import TezosBallotType
from . import TezosContractType
Expand Down
10 changes: 7 additions & 3 deletions tests/device_tests/test_msg_applysettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_apply_homescreen(self, client):

@pytest.mark.skip_t1
@pytest.mark.setup_client(pin=None)
def test_unsafe_prompts(self, client):
def test_safety_checks(self, client):
BAD_ADDRESS = parse_path("m/0")

with pytest.raises(
Expand All @@ -136,7 +136,9 @@ def test_unsafe_prompts(self, client):

with client:
client.set_expected_responses(EXPECTED_RESPONSES_NOPIN)
device.apply_settings(client, unsafe_prompts=True)
device.apply_settings(
client, safety_checks=messages.SafetyCheckLevel.Prompt
)

with client:
client.set_expected_responses(
Expand All @@ -146,7 +148,9 @@ def test_unsafe_prompts(self, client):

with client:
client.set_expected_responses(EXPECTED_RESPONSES_NOPIN)
device.apply_settings(client, unsafe_prompts=False)
device.apply_settings(
client, safety_checks=messages.SafetyCheckLevel.Strict
)

with pytest.raises(
exceptions.TrezorFailure, match="Forbidden key path"
Expand Down
2 changes: 1 addition & 1 deletion tests/ui_tests/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"test_msg_applysettings.py-test_apply_settings": "2cc8bf660f3be815d19a4bf1265936162a58386fbe632ca4be01541245b79134",
"test_msg_applysettings.py-test_apply_settings_passphrase": "5c1ed9a0be3d14475102d447da0b5d51bbb6dfaaeceff5ea9179064609db7870",
"test_msg_applysettings.py-test_apply_settings_passphrase_on_device": "3e6527e227bdde54f51bc9c417b176d0d87fdb6c40c4761368f50eb201b4beed",
"test_msg_applysettings.py-test_unsafe_prompts": "19bd500c3b791d51bbd1140085f306a838194593697529263f362acb0b1ab445",
"test_msg_applysettings.py-test_safety_checks": "19bd500c3b791d51bbd1140085f306a838194593697529263f362acb0b1ab445",
"test_msg_backup_device.py::test_backup_bip39": "2b63928444b8188eb2241fc03a3b9bc81191cfa9bbf3ef5431894c04ee0ed01f",
"test_msg_backup_device.py::test_backup_slip39_advanced": "31900e0e8ad694ce894eee1ce289b425558c1fcd7bcb6128a19c049af436d35f",
"test_msg_backup_device.py::test_backup_slip39_basic": "be4d88d882851ce1ddc45165c35952b23121ddca1a811c7fd7c7ef9d31989e8c",
Expand Down

0 comments on commit 0ca9a54

Please sign in to comment.