Skip to content

Commit 528eed5

Browse files
committed
Refactor security model and improve checksum validation in parsing logic
1 parent 22990b9 commit 528eed5

4 files changed

Lines changed: 21 additions & 24 deletions

File tree

‎docs/security/model.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ repository root `SECURITY.md`.
88

99
- Unchecked text cannot enter sharing, generation, correction output, or wallet
1010
operations as a validated artifact.
11-
- An HRP selects only fixed application length rules before the codex32
12-
checksum is verified. Application payload semantics are applied afterward.
11+
- Parsing verifies the generic codex32 format and checksum before an HRP
12+
selects application length or payload rules. Header and application payload
13+
semantics are applied afterward.
1314
- Artifacts and headers are immutable. Shares have symbol semantics only and
1415
cannot be constructed from or converted to bytes.
1516
- Recovery requires exactly the declared threshold of compatible, distinct

‎src/codex32/bip93.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ def _checksum_for_encoded_length(hrp: str, encoded_length: int) -> _Checksum:
7474

7575
def _decode_codex32(text: str) -> tuple[_ProfileRules, tuple[int, ...], _Checksum]:
7676
hrp, encoded = bech32_decode(text)
77-
profile_rules = _profile_rules(hrp)
78-
profile_rules.validate_text_length(len(text))
7977
if len(hrp) + 1 + len(encoded) < 21:
8078
raise InvalidLength("codex32 string must contain at least 21 characters")
8179
checksum = _checksum_for_encoded_length(hrp, len(encoded))
80+
if not bech32_verify_checksum(hrp, encoded, checksum):
81+
raise InvalidChecksum(f"invalid {checksum.kind} checksum")
82+
profile_rules = _profile_rules(hrp)
83+
profile_rules.validate_text_length(len(text))
8284
body = tuple(encoded[: -checksum.length])
8385
profile_rules.validate_payload_length(len(body) - 6)
8486
Header._from_symbols(body[:6])
85-
if not bech32_verify_checksum(hrp, encoded, checksum):
86-
raise InvalidChecksum(f"invalid {checksum.kind} checksum")
8787
return profile_rules, tuple(encoded), checksum
8888

8989
@dataclass(frozen=True, slots=True, init=False)

‎tests/test_cli.py‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def answer(prompt: str) -> str:
313313
assert captured.err.endswith("\n\n")
314314

315315

316-
def test_tty_check_reports_truncated_ms_length_before_checksum(
316+
def test_tty_check_reports_checksum_before_truncated_ms_length(
317317
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
318318
) -> None:
319319
input_module = importlib.import_module("codex32._cli_input")
@@ -335,13 +335,8 @@ def isatty() -> bool:
335335

336336
assert main(["check"]) == 0
337337
captured = capsys.readouterr()
338-
for length in (45, 46, 47):
339-
message = (
340-
f"Rejected: This input has {length} characters. A Bitcoin master-seed backup must have "
341-
"exactly 48, 54, 61, 67, 74, or 127 characters."
342-
)
343-
assert captured.err.count(message) == 1
344-
assert "checksum" not in captured.err
338+
assert captured.err.count("Rejected: The checksum does not match.") == 3
339+
assert "Bitcoin master-seed backup must have" not in captured.err
345340

346341

347342
def test_tty_check_names_an_invalid_character_and_position(
@@ -396,8 +391,8 @@ def isatty() -> bool:
396391
"Rejected: The threshold must be 0 or a number from 2 through 9; found 'f'.",
397392
"Rejected: No separator (1) was found.",
398393
"Rejected: The application prefix before 1 is missing.",
399-
"Rejected: The application prefix 'm' is not supported.",
400-
"Rejected: The application prefix 's' is not supported.",
394+
"Rejected: The checksum does not match.",
395+
"Rejected: The checksum does not match.",
401396
]
402397

403398

‎tests/test_profiles.py‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def test_pr2258_supported_size_vectors(seed_hex: str, text: str) -> None:
121121

122122
@pytest.mark.parametrize("text", FORMERLY_VALID_UNSUPPORTED_MS)
123123
def test_pr2258_rejects_formerly_valid_unsupported_sizes(text: str) -> None:
124-
with pytest.raises(InvalidLength):
124+
expected = InvalidChecksum if text in FORMERLY_VALID_UNSUPPORTED_MS[-2:] else InvalidLength
125+
with pytest.raises(expected):
125126
parse_codex32(text)
126127

127128

@@ -163,28 +164,28 @@ def test_expanded_codeword_upper_bound() -> None:
163164
_checksum_for_encoded_length("ms", len(bech32_decode(oversized)[1]))
164165

165166

166-
def test_unknown_hrp_is_rejected_before_checksum_interpretation() -> None:
167+
def test_checksum_is_verified_before_unknown_hrp_dispatch() -> None:
167168
valid_generic = _oracle_encode("zz", "0tests" + "q" * 26)
168169
with pytest.raises(UnknownProfile):
169170
parse_codex32(valid_generic)
170-
with pytest.raises(UnknownProfile):
171+
with pytest.raises(InvalidChecksum):
171172
parse_codex32("zz10tests" + "q" * 39)
172173

173174

174175
@pytest.mark.parametrize(
175176
("hrp", "payload_length"),
176177
(("ms", 26), ("cl", 52), ("bip39_12w", 27), ("bip39_24w", 53)),
177178
)
178-
def test_profile_length_precedes_checksum(hrp: str, payload_length: int) -> None:
179+
def test_checksum_precedes_profile_length(hrp: str, payload_length: int) -> None:
179180
valid = _oracle_encode(hrp, "0tests" + "q" * payload_length)
180-
with pytest.raises(InvalidLength):
181+
with pytest.raises(InvalidChecksum):
181182
parse_codex32(valid[:-1])
182183

183184

184-
def test_ms_length_precedence_covers_extremes_and_alignment() -> None:
185-
with pytest.raises(InvalidLength, match="48, 54, 61, 67, 74, or 127"):
185+
def test_generic_length_and_checksum_precede_ms_length() -> None:
186+
with pytest.raises(InvalidLength, match="at least 21"):
186187
parse_codex32("ms1")
187-
with pytest.raises(InvalidLength, match="48, 54, 61, 67, 74, or 127"):
188+
with pytest.raises(InvalidChecksum):
188189
parse_codex32("ms10tests" + "q" * 40)
189190

190191

0 commit comments

Comments
 (0)