fix: guard device get_basic_info parsers against short responses#500
fix: guard device get_basic_info parsers against short responses#500bluetoothbot wants to merge 6 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR hardens device-level get_basic_info() (and a few related response parsers) so truncated/short BLE replies return None instead of raising IndexError, aligning robustness with the recent advertisement parser guard sweep.
Changes:
- Added minimum-length guards across multiple device
get_basic_info()implementations (plus a few related parsers) to avoid indexing past short responses. - Tightened several “unsuccessful reply” short-circuits from specific-byte checks to general length-based checks.
- Added a new regression test suite covering short-payload behavior across all patched devices, and updated Art Frame command tests to avoid relying on parsing side effects.
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/test_device_basic_info_guards.py | New regression tests ensuring short responses return None instead of raising. |
| tests/test_art_frame.py | Adjusts Art Frame command tests to mock _get_current_image_index directly. |
| switchbot/devices/vacuum.py | Adds a minimum-length guard before indexing firmware byte. |
| switchbot/devices/smart_thermostat_radiator.py | Adds a minimum-length guard before parsing thermostat payload fields. |
| switchbot/devices/roller_shade.py | Adds a minimum-length guard before parsing roller shade fields. |
| switchbot/devices/light_strip.py | Adds guards for both _data and _version_info in light strip + candle warmer lamp basic info parsing. |
| switchbot/devices/keypad_vision.py | Adds minimum-length guards for basic info and password count parsing (Vision vs Pro). |
| switchbot/devices/fan.py | Adds length guards in get_basic_info and broadens _get_basic_info failure detection to len <= 1. |
| switchbot/devices/evaporative_humidifier.py | Adds a minimum-length guard before parsing humidifier settings. |
| switchbot/devices/curtain.py | Adds a minimum-length guard for get_basic_info and tightens short-response handling in get_extended_info_summary. |
| switchbot/devices/ceiling_light.py | Adds guards for _data and _version_info before parsing. |
| switchbot/devices/bulb.py | Adds guards for _data and _version_info before parsing. |
| switchbot/devices/bot.py | Adds a minimum-length guard before parsing bot basic info offsets. |
| switchbot/devices/blind_tilt.py | Adds a minimum-length guard for get_basic_info and tightens short-response handling in get_extended_info_summary. |
| switchbot/devices/base_cover.py | Tightens short-response handling and avoids indexing into grouped-device data unless length permits. |
| switchbot/devices/art_frame.py | Adds a minimum-length guard plus a dynamic guard for all_images_index based on claimed image count. |
| switchbot/devices/air_purifier.py | Adds guards for _data, led_settings, and led_status before parsing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| # If grouped curtain device present. | ||
| if _data[4]: | ||
| if len(_data) >= 7 and _data[4]: |
PR Review — fix: guard device get_basic_info parsers against short responsesCorrect, well-tested hardening sweep — but the branch is stale and fan.py conflicts with merged work. Strengths:
Needs attention:
🟡 Important
1. Branch is stale — fan.py conflicts with merged #522 refactor
|
Each device's get_basic_info() reads fixed byte offsets in the command response. The base _get_basic_info() only filters single-byte error replies (b"\x07", b"\x00"); a truncated payload from a flaky BLE proxy or device firmware error slips through and raises IndexError when the device-level parser indexes past the buffer end. Mirrors the adv_parsers audit pattern (sblibs#494/sblibs#495/sblibs#496) at the command-response layer: bot _data[10] → len >= 11 bulb _data[10] → len >= 11, version_info >= 3 ceiling_light _data[3:5] → len >= 5, version_info >= 3 fan _data[9] → len >= 10, version_info >= 3 + fan._get_basic_info: tighten filter from {b"\x07", b"\x00"} to len <= 1 keypad_vision _data[14] → len >= 15 _data[5/7] → password_count >= 6 (Vision) / >= 8 (Pro) air_purifier _data[15] → len >= 16, led_settings >= 6, led_status >= 2 blind_tilt _data[7] → len >= 8 + get_extended_info_summary: tighten filter curtain _data[7] → len >= 8 + get_extended_info_summary: tighten filter evap_humid. _data[10] → len >= 11 light_strip _data[10] → len >= 11, version_info >= 3 candle_lamp _data[2] → len >= 3, version_info >= 3 roller_shade _data[6] → len >= 7 smart_therm. _data[14] → len >= 15 vacuum _data[2] → len >= 3 art_frame _data[6] → len >= 7 + dynamic guard against total_num_of_images overrunning buffer Adds 46 regression tests in test_device_basic_info_guards.py. Also patches test_art_frame next/prev/set_image tests to mock _get_current_image_index directly — they previously relied on a bare AsyncMock slipping past the old length-blind parsers (per the same trap noted in PR sblibs#492). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Same pattern as the rest of this PR. SwitchbotBaseCover.get_extended_info_adv indexes _data[3] for device0 and _data[6] for the chained-device block, but only filters single-byte error replies (b"\x07", b"\x00"). A 2-3 byte truncated reply (BLE proxy strip, firmware error) IndexErrors on _data[3]; a 5-6 byte reply with _data[4] set IndexErrors on _data[5]/_data[6]. Tightens the filter to len < 4 (covers any unparseable device0 payload) and gates the device1 block on len >= 7 so a truncated chain reply parses device0 and skips device1 instead of crashing. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Rebase with requested adjustmentsBranch Changes applied
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
53db9ff to
1fd8a46
Compare
What
Adds length guards to every device-class
get_basic_info()(and a few related response parsers) so a truncated BLE reply returnsNoneinstead of raisingIndexError.Why
The base
_get_basic_info()only filters single-byte error replies (b\"\x07\",b\"\x00\"). A 2-byte non-matching reply (e.g.b\"\x02\x00\"from firmware error states, or a payload truncated by a flaky BLE proxy) slips through and crashes the device-level parser when it indexes past the buffer end (e.g.bot.get_basic_inforeads_data[10]).This is the command-response counterpart to the adv_parsers audit (#494 / #495 / #496) — same class of bug, different layer.
How
One-liner length guards in each device's
get_basic_info, sized tomax_index + 1:_data[10]_data[10]_data[3:5]_data[9]_data1≥ 3)_data[14]_data[5/7]_data[15]_data[7]_data[7]_data[10]_data[10]_data[2]_data[6]_data[14]_data[2]_data[6]+ dynamic image-count guardAlso tightens three short-circuits that used the same brittle
(b\"\x07\", b\"\x00\")membership test:fan._get_basic_info→len <= 1blind_tilt.get_extended_info_summary→len < 2curtain.get_extended_info_summary→len < 3Testing
tests/test_device_basic_info_guards.pycovering every patched function with short payloads.tests/test_art_frame.pynext/prev/set_image tests updated to mock_get_current_image_indexdirectly — they previously relied on a bareAsyncMockslipping past the length-blind parser (same trap as PR fix: guard relay_switch parsers against short payloads (#369) #492).🤖 Generated with Claude Code
Quality Report
Changes: 17 files changed, 383 insertions(+), 5 deletions(-)
Code scan: clean
Tests: passed (1264 passed)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline