SFT-7112: share one validation path between both message formats
What changed, and why it matters
This commit fixes a bug where a specially crafted message file could crash the Passport firmware with an IndexError. If a file loaded from microSD or scanned via QR began with a blank line, the firmware would try to read the first character of an empty message and crash. The fix consolidates the message validation so the same empty-message check applies to both the newer Envoy format and the older 2- or 3-line format. It also adds tests to prevent the bug from returning.
Treat this as a low-to-moderate reliability/security fix. The crash is reachable from user-supplied microSD and QR inputs and could be used to deny service or disrupt signing workflows. Apply the patch and run the extended unit tests. Review other flows for duplicated validation logic that may have similar inconsistent guards.
Security signals we found
Uncaught IndexError from empty string indexing in message validation
Input reachable from untrusted sources: microSD card and QR code scans
Duplicated validation logic led to inconsistent guards between code branches
Fix includes regression tests for the missed edge case
Evidence from the diff
In health_check_common_flow.py, validate_lines() had two separate validation branches for Envoy-style and legacy 2/3-line message formats. The empty-message guard was only present in the Envoy branch. In the legacy branch, self.text was set to self.lines[0] and then passed to validate_sign_text(), which under default check_whitespace behavior checks text[0] == ’ ‘. If lines[0] was empty (e.g., input split by splitlines() on a file starting with a newline), this raised an uncaught IndexError. The patch moves the empty check and the validate_sign_text() call into a single shared path executed for both formats. Address-type derivation remains branch-specific because Envoy derives it from the derivation path while the legacy format may specify it on a third line. Unit tests are added to cover empty first lines in both 2- and 3-line legacy formats, both signing modes, plus positive and negative controls.
Changed components
ports/stm32/boards/Passport/modules/flows/health_check_common_flow.pyports/stm32/boards/Passport/modules/tests/unit/single_line_message.pyInspect captured patch +64 / −29
### ports/stm32/boards/Passport/modules/flows/health_check_common_flow.py
@@ -39,31 +39,8 @@ async def validate_lines(self):
self.subpath = parts[1]
self.text = parts[2][len('ascii:'):]
- if not self.text:
- await ErrorPage(text='Message is empty.').show()
- self.set_result(None)
- return
-
- (subpath, error) = validate_sign_text(self.text, self.subpath)
-
- if error is not None:
- await ErrorPage(text=error).show()
- self.set_result(None)
- return
-
- self.subpath = subpath
-
- # A root or missing path has no purpose component for address-type
- # detection, even though the general path validator accepts it.
- if not subpath or subpath == 'm':
- await ErrorPage(text='Message derivation path is invalid.').show()
- self.set_result(None)
- return
-
- derived = get_addr_type_from_deriv(self.subpath)
-
- if derived is not None:
- self.addr_type = derived
+ # Envoy does not send an address type, so it comes from the path.
+ derive_addr_type = True
else:
if len(self.lines) not in [2, 3]:
await ErrorPage('{} format is invalid.'.format(err_label)).show()
@@ -76,14 +53,38 @@ async def validate_lines(self):
if len(self.lines) == 3:
self.addr_type = get_addr_type_from_string(self.lines[2])
- (subpath, error) = validate_sign_text(self.text, self.subpath)
+ derive_addr_type = False
+
+ # One validation path for both formats. These checks used to be
+ # duplicated per branch, and the empty-message guard was only ever added
+ # to one of them, so an empty first line in the legacy format still
+ # reached validate_sign_text() and indexed text[0] of an empty string.
+ if not self.text:
+ await ErrorPage(text='Message is empty.').show()
+ self.set_result(None)
+ return
+
+ (subpath, error) = validate_sign_text(self.text, self.subpath)
+
+ if error is not None:
+ await ErrorPage(text=error).show()
+ self.set_result(None)
+ return
+
+ self.subpath = subpath
- if error is not None:
- await ErrorPage(text=error).show()
+ if derive_addr_type:
+ # A root or missing path has no purpose component for address-type
+ # detection, even though the general path validator accepts it.
+ if not self.subpath or self.subpath == 'm':
+ await ErrorPage(text='Message derivation path is invalid.').show()
self.set_result(None)
return
- self.subpath = subpath
+ derived = get_addr_type_from_deriv(self.subpath)
+
+ if derived is not None:
+ self.addr_type = derived
# User Interaction for non-health check signing
if self.normal_signing:
### ports/stm32/boards/Passport/modules/tests/unit/single_line_message.py
@@ -95,6 +95,40 @@ async def run_tests():
assert MockErrorPage.errors == ['Message format is invalid.']
assert flow.result is None
assert flow.next_state is None
+
+ # The legacy 2- and 3-line format takes the same empty-message check.
+ # A file beginning with a newline splits to an empty first line, which
+ # used to reach validate_sign_text() and raise IndexError on text[0].
+ for normal_signing in (True, False):
+ for lines in (['', path], ['', path, 'p2wpkh']):
+ MockErrorPage.errors = []
+ flow = MockFlow(lines, normal_signing)
+ await HealthCheckCommonFlow.validate_lines(flow)
+ assert MockErrorPage.errors == ['Message is empty.']
+ assert flow.result is None
+ assert flow.next_state is None
+
+ # The legacy format still works, and still takes its address type from
+ # the third line rather than from the path.
+ for lines in (['hello', path], ['hello', path, 'p2pkh']):
+ MockErrorPage.errors = []
+ flow = MockFlow(lines, True)
+ await HealthCheckCommonFlow.validate_lines(flow)
+ assert not MockErrorPage.errors
+ assert flow.text == 'hello'
+ assert flow.subpath == path
+ assert flow.next_state == flow.show_message
+ assert flow.result == 'unset'
+
+ # A legacy file whose path is unusable is still refused by the shared
+ # validator, not by the path check that only the Envoy format runs.
+ MockErrorPage.errors = []
+ flow = MockFlow(['hello', 'not-a-path'], True)
+ await HealthCheckCommonFlow.validate_lines(flow)
+ assert MockErrorPage.errors
+ assert flow.result is None
+ assert flow.next_state is None
+
return_value.write(b'OK')
finally:
pages.ErrorPage = original_error_pageWhy this scored 57/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.