Skip to content

Commit 19a59dd

Browse files
codexBenWestgate
authored andcommitted
correct: classify malformed HRPs as usage
1 parent 002dbb0 commit 19a59dd

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

‎src/codex32/cli.py‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,9 @@ def _correct(
544544
separator = normalized.lower().rfind("1")
545545
if separator <= 0:
546546
raise _UsageError("Enter a complete application prefix followed by the separator 1.")
547-
hrp = normalized[:separator].lower()
547+
if len(raw_hrp := normalized[:separator]) > 83 or not all("!" <= c <= "~" for c in raw_hrp):
548+
raise _UsageError("The application prefix must be at most 83 printable ASCII characters.")
549+
hrp = raw_hrp.lower()
548550
if context.master_seed and hrp != Profile.MS.value:
549551
raise _UsageError("This command accepts only Bitcoin master-seed input beginning with ms1.")
550552
try:
@@ -721,10 +723,7 @@ def _main(context: _CliContext, argv: Sequence[str] | None = None) -> int:
721723
except _UsageError as error:
722724
_print(f"{scope}: {error}", err=True)
723725
return 2
724-
except (_CommandError, CodexError) as error:
725-
_print(f"{scope}: {error}", err=True)
726-
return correction_failed
727-
except BitcoinCoreError as error:
726+
except (_CommandError, CodexError, BitcoinCoreError) as error:
728727
_print(f"{scope}: {error}", err=True)
729728
return correction_failed
730729
except EOFError:

‎tests/test_cli.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,15 @@ def test_cli_rejects_sixteen_consecutive_erasures_as_outside_regular_bound() ->
17281728
assert "No valid correction found" in result.stderr
17291729

17301730

1731+
def test_correct_rejects_malformed_immutable_hrp_as_usage() -> None:
1732+
damaged = "é" + VECTOR_1["secret_s"][1:]
1733+
1734+
result = _invoke(["correct"], damaged)
1735+
1736+
assert result.exit_code == 2
1737+
assert "application prefix" in result.stderr
1738+
1739+
17311740
@pytest.mark.parametrize(
17321741
("byte_length", "options"),
17331742
((16, []), (64, []), (20, ["--bytes", "20"]), (24, ["--bytes", "?"])),

0 commit comments

Comments
 (0)