Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
devsettings::v1IndexErrorinparse_settingsqr(data.split()[1])settings::v1 name=IndexErrorinparse_settingsqr(.split()[0]on an empty value)settings::v1 not-a-key-valueValueErrorinparse_settingsqr(entry.split("="))signmessageIndexErrorinSignMessageQrDecoder.add(parts[1])signmessage m/signmessage m/84h/0h/0hIndexErrorinSignMessageQrDecoder.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_settingsqrsplits the data once and raisesInvalidSettingsQRData, the exception it already raises for every other malformed SettingsQR, when there are no settings after the header, whenname=has no value, or when an entry has no=.entry.split("=", 1)also stops a value that contains=from raising.SignMessageQrDecoder.addreturnsDecodeQRStatus.INVALIDunless the payload has the three partssignmessage {derivation_path} {fmt}:{message}.DecodeQR.add_datanow records anINVALIDresult from a decoder on the scan itself (_record_decoder_status). Before this, a decoder could returnINVALIDwhileDecodeQR.is_invalidstayed false;ScanViewthen saw a scan that was neither complete nor invalid and returned to the Main Menu with no message at all. With the status recorded,ScanViewroutes toScanInvalidQRTypeView.Additional Information
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.InvalidSettingsQRDataand a message saying what is wrong. That is howSettingsIngestSettingsQRViewalready 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.signmessage m ascii:xis well-formed as far as the decoder is concerned and still fails later inembit_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):
settings::v1images/settingsqr_header_only.pngsettings::v1 name=images/settingsqr_empty_name.pngsettings::v1 not-a-key-valueimages/settingsqr_entry_without_equals.pngsignmessageimages/signmessage_no_path_or_message.pngsignmessage mimages/signmessage_truncated.pngsignmessage m/84h/0h/0himages/signmessage_no_message.pngNo existing screen changes appearance.
Testing
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 toScanInvalidQRTypeView.Test data: malformed SettingsQR and sign-message QRs
Scan each QR from the Main Menu's Scan.
qr/<name>.pngholds the payload in the second column.settingsqr_header_only.pngsettings::v1settingsqr_empty_name.pngsettings::v1 name=settingsqr_entry_without_equals.pngsettings::v1 not-a-key-valuesignmessage_no_path_or_message.pngsignmessagesignmessage_truncated.pngsignmessage msignmessage_no_message.pngsignmessage m/84h/0h/0hA well-formed QR of each kind, to confirm nothing regressed:
control_settingsqr.pngsettings::v1 name=Test lang=en persistent=Dcontrol_signmessage.pngsignmessage m/84h/0h/0h/0/0 ascii:hello worldThis pull request is categorized as a:
Checklist
I ran
pytestlocallyI included screenshots of any new or modified screens
Should be part of the PR description above.
I added or updated tests
Any new or altered functionality should be covered in a unit test. Any new or updated sequences require FlowTests.
I tested this PR hands-on on the following platform(s):
I have reviewed these notes: