Skip to content

Reject malformed SettingsQR and sign-message QRs instead of crashing - #1080

Open
knorrium wants to merge 1 commit into
SeedSigner:devfrom
knorrium:harden-settingsqr-signmessage-parsing
Open

knorrium wants to merge 1 commit into
SeedSigner:devfrom
knorrium:harden-settingsqr-signmessage-parsing

Conversation

@knorrium

Copy link
Copy Markdown

A SettingsQR or sign-message QR that is cut short reached unguarded indexing and ended on the System Error screen with an IndexError or ValueError:

settings::v1 settings.py data.split()[1]
settings::v1 name= settings.py ...split()[0] on ""
settings::v1 not-a-key-value settings.py entry.split("=")
signmessage decode_qr.py parts[1]
signmessage m decode_qr.py parts[2]

parse_settingsqr now raises InvalidSettingsQRData for each, which is what it already raises for every other malformed SettingsQR. SignMessageQrDecoder.add returns DecodeQRStatus.INVALID.

A decoder returning INVALID did not mark the scan itself invalid, so ScanView saw a scan that was neither complete nor invalid and returned to the Main Menu without a word. DecodeQR.add_data now records INVALID from any decoder, and ScanView shows ScanInvalidQRTypeView.

Found by mutation testing of the QR parsers.

Description

Problem or Issue being addressed

A QR code is untrusted input, and two of the parsers behind the scanner index into it without checking that the pieces are there. A truncated or malformed code ends on the System Error screen with a Python exception rather than being rejected as an invalid QR:

Scanned payload On dev
settings::v1 IndexError in parse_settingsqr (data.split()[1])
settings::v1 name= IndexError in parse_settingsqr (.split()[0] on an empty value)
settings::v1 not-a-key-value ValueError in parse_settingsqr (entry.split("="))
signmessage IndexError in SignMessageQrDecoder.add (parts[1])
signmessage m / signmessage m/84h/0h/0h IndexError in SignMessageQrDecoder.add (parts[2])

None of these put funds or keys at risk. The device fails closed, and the user is sent back to the Main Menu. It is a robustness fix: a hostile or damaged QR should get the same "invalid QR" treatment as any other unreadable code, not a stack trace.

I found these by mutation-testing the QR parsers (coverage-guided, starting from valid payloads).

Solution

  • Settings.parse_settingsqr splits the data once and raises InvalidSettingsQRData, the exception it already raises for every other malformed SettingsQR, when there are no settings after the header, when name= has no value, or when an entry has no =. entry.split("=", 1) also stops a value that contains = from raising.
  • SignMessageQrDecoder.add returns DecodeQRStatus.INVALID unless the payload has the three parts signmessage {derivation_path} {fmt}:{message}.
  • DecodeQR.add_data now records an INVALID result from a decoder on the scan itself (_record_decoder_status). Before this, a decoder could return INVALID while DecodeQR.is_invalid stayed false; ScanView then saw a scan that was neither complete nor invalid and returned to the Main Menu with no message at all. With the status recorded, ScanView routes to ScanInvalidQRTypeView.

Additional Information

  • Behavior change beyond the two parsers: the third bullet applies to every single-frame decoder that returns INVALID (an unsupported sign-message format, an address that fails the address regex, a descriptor embit rejects). Those scans used to drop silently to the Main Menu and now show "Unknown QR Type". I think that is the intended behavior, but it is a visible change, so I am calling it out.
  • A malformed SettingsQR still ends on the System Error screen, now with InvalidSettingsQRData and a message saying what is wrong. That is how SettingsIngestSettingsQRView already treats that exception ("May raise an Exception which will bubble up to the Controller to display to the user"); routing it to a friendlier screen would be a separate change.
  • This does not validate the derivation path inside a sign-message QR. signmessage m ascii:x is well-formed as far as the decoder is concerned and still fails later in embit_utils.parse_derivation_path. I have left that for a follow-up to keep this PR small.

Screenshots

Before/after for each payload (rendered through the real Views with the screenshot renderer):

settingsqr_empty_name settingsqr_entry_without_equals settingsqr_header_only signmessage_no_message signmessage_no_path_or_message signmessage_truncated
Payload Before / after
settings::v1 images/settingsqr_header_only.png
settings::v1 name= images/settingsqr_empty_name.png
settings::v1 not-a-key-value images/settingsqr_entry_without_equals.png
signmessage images/signmessage_no_path_or_message.png
signmessage m images/signmessage_truncated.png
signmessage m/84h/0h/0h images/signmessage_no_message.png

No existing screen changes appearance.

Testing

  • New: tests/test_parser_hardening.py (sign-message decoder), TestSettingsQRParser.test_settingsqr_rejects_truncated_payload, and a FlowTest, test_scan_truncated_signmessage_shows_invalid_qr_error, for the route to ScanInvalidQRTypeView.
  • To try it by hand, QR codes for each payload (plus two well-formed controls) are attached:

Test data: malformed SettingsQR and sign-message QRs

Scan each QR from the Main Menu's Scan. qr/<name>.png holds the payload in the second column.

QR Payload On dev With this PR
settingsqr_header_only.png settings::v1 System Error: IndexError, settings.py 76 System Error: InvalidSettingsQRData, "SettingsQR has no settings"
settingsqr_empty_name.png settings::v1 name= System Error: IndexError, settings.py 77 System Error: InvalidSettingsQRData, "name cannot be empty"
settingsqr_entry_without_equals.png settings::v1 not-a-key-value System Error: ValueError, settings.py 82 System Error: InvalidSettingsQRData, "Invalid settings entry"
signmessage_no_path_or_message.png signmessage System Error: IndexError, decode_qr.py 974 Error: Unknown QR Type
signmessage_truncated.png signmessage m System Error: IndexError, decode_qr.py 975 Error: Unknown QR Type
signmessage_no_message.png signmessage m/84h/0h/0h System Error: IndexError, decode_qr.py 975 Error: Unknown QR Type
settingsqr_empty_name settingsqr_entry_without_equals settingsqr_header_only signmessage_no_message signmessage_no_path_or_message signmessage_truncated

A well-formed QR of each kind, to confirm nothing regressed:

QR Payload Expected
control_settingsqr.png settings::v1 name=Test lang=en persistent=D Settings QR screen, config name "Test"
control_signmessage.png signmessage m/84h/0h/0h/0/0 ascii:hello world Message signing flow starts (with message signing enabled in Settings; otherwise the "option disabled" screen)
control_settingsqr control_signmessage

This pull request is categorized as a:

  • New feature
  • Bug fix
  • Code refactor
  • Documentation
  • Other

Checklist

I ran pytest locally

  • All tests passed before submitting the PR
  • I couldn't run the tests
  • N/A

I included screenshots of any new or modified screens

Should be part of the PR description above.

  • Yes
  • No
  • N/A

I added or updated tests

Any new or altered functionality should be covered in a unit test. Any new or updated sequences require FlowTests.

  • Yes
  • No, I’m a fool
  • N/A

I tested this PR hands-on on the following platform(s):


I have reviewed these notes:

  • Keep your changes limited in scope.
  • If you uncover other issues or improvements along the way, ideally submit those as a separate PR.
  • The more complicated the PR, the harder it is to review, test, and merge.
  • We appreciate your efforts, but we're a small team of volunteers so PR review can be a very slow process.
  • Please only "@" mention a contributor if their input is truly needed to enable further progress.
  • I understand

A SettingsQR or sign-message QR that is cut short reached unguarded
indexing and ended on the System Error screen with an IndexError or
ValueError:

  settings::v1                    settings.py   data.split()[1]
  settings::v1 name=              settings.py   ...split()[0] on ""
  settings::v1 not-a-key-value    settings.py   entry.split("=")
  signmessage                     decode_qr.py  parts[1]
  signmessage m                   decode_qr.py  parts[2]

parse_settingsqr now raises InvalidSettingsQRData for each, which is
what it already raises for every other malformed SettingsQR.
SignMessageQrDecoder.add returns DecodeQRStatus.INVALID.

A decoder returning INVALID did not mark the scan itself invalid, so
ScanView saw a scan that was neither complete nor invalid and returned
to the Main Menu without a word. DecodeQR.add_data now records INVALID
from any decoder, and ScanView shows ScanInvalidQRTypeView.

Found by mutation testing of the QR parsers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant