Skip to content

Commit 3d5e60d

Browse files
committed
correct: classify malformed HRPs as usage
1 parent 2a82d70 commit 3d5e60d

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
@@ -546,7 +546,9 @@ def _correct(
546546
separator = normalized.lower().rfind("1")
547547
if separator <= 0:
548548
raise _UsageError("Enter a complete application prefix followed by the separator 1.")
549-
hrp = normalized[:separator].lower()
549+
if len(raw_hrp := normalized[:separator]) > 83 or not all("!" <= c <= "~" for c in raw_hrp):
550+
raise _UsageError("The application prefix must be at most 83 printable ASCII characters.")
551+
hrp = raw_hrp.lower()
550552
if context.master_seed and hrp != Profile.MS.value:
551553
raise _UsageError("This command accepts only Bitcoin master-seed input beginning with ms1.")
552554
try:
@@ -737,10 +739,7 @@ def _main(context: _CliContext, argv: Sequence[str] | None = None) -> int:
737739
except _UsageError as error:
738740
_print(f"{scope}: {error}", err=True)
739741
return 2
740-
except (_CommandError, CodexError) as error:
741-
_print(f"{scope}: {error}", err=True)
742-
return correction_failed
743-
except BitcoinCoreError as error:
742+
except (_CommandError, CodexError, BitcoinCoreError) as error:
744743
_print(f"{scope}: {error}", err=True)
745744
return correction_failed
746745
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)