fix: guard remaining adv_parsers against short payloads#496
Merged
Conversation
Sweep of `switchbot/adv_parsers/` to extend the length-guard hardening started in sblibs#495 (leak/presence_sensor/contact) and sblibs#492 (relay_switch) to the rest of the parser surface. The dispatcher in `adv_parser.py` does not pre-validate length before invoking the matched parser, so a malformed BLE advertisement with `manufacturer_id == 2409` could index past the end of `mfr_data` / `data` and raise `IndexError`/`ValueError` inside the parser. The outer `try/except` in `parse_advertisement_data` catches it but logs a noisy `_LOGGER.exception` and drops the whole advertisement (including valid service_data). Each parser now returns `{}` (or the documented "unknown" stub for `bot`/`keypad`/`humidifier`/`remote`) on short input. Guards mirror the highest index actually accessed by each function. Includes `tests/test_short_payload_guards.py` — 119 parametrized cases exercising `None`, empty, and undersized payloads against every guarded parser. Full suite: 1197 passed.
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
bdraco
approved these changes
May 15, 2026
This was referenced May 17, 2026
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.
What
Extend the length-guard sweep started in #495 / #492 to the remaining 24 advertisement parsers in
switchbot/adv_parsers/.Why
The dispatcher in
adv_parser.pydoes not validatemfr_data/datalength before invoking the matched parser. A malformed BLE advertisement withmanufacturer_id == 2409(untrusted but range-limited input) can index past the end of the buffer and raiseIndexError/ValueError. The outertry/exceptinparse_advertisement_datacatches it, but logs a noisy_LOGGER.exceptionand drops the whole advertisement — same class of bug as #285 / #369 / #494.Audit walked every parser, tabulated the max index accessed against the existing guard, and tightened each guard to
max_index + 1bytes. Mirrors thelen(mfr_data) < Npattern from #495.How
Hardened parsers (guard size in brackets):
air_purifier[≥14],art_frame[≥10],bulb[≥11],ceiling_light[≥11],climate_panel[≥16],fan[≥10],hub3[≥17],evaporative_humidifier[≥17],plug[≥12],smart_thermostat_radiator[≥13],vacuum[≥14],vacuum_k[≥9]blind_tilt[≥10 + data≥3],roller_shade[≥10 + data≥3],curtain[data≥6 fallback, data≥3 for battery, data≥2 for calibration]bot[data≥3],wohumidifier[data≥5],remote[data≥3]keypad[data≥3, mfr_data≥7]keypad_visioncommon [≥13], vision/pro suffix [≥14]light_strip:process_wostrip/process_candle_warmer_lamp[≥9],process_light[≥cw_offset+2]lock:process_locklite/process_wolock[≥9],parse_common_data/process_wolock_pro/process_lock2[≥12];data[2]battery now guarded bylen(data) ≥ 3meter(process_wosensorth): mfr_data ≥11 for temp slice, data ≥3 for battery, data ≥6 for fallback temp slicemotion: data ≥6 (previously unguarded); also fixes anUnboundLocalErroronmotion_detectedwhen both inputs are too shorthub2,hubmini_matter: ≥16 (temp_data slice needs 3 bytes starting at byte 13)Testing
tests/test_short_payload_guards.py: 119 parametrized cases coveringNone/ empty / undersized payloads for every guarded parser.ruff check/ruff format --check: clean.Companion to #495 (leak/presence_sensor/contact) and #492 (relay_switch) — same shape of fix applied uniformly across the parser surface.
Quality Report
Changes: 25 files changed, 242 insertions(+), 39 deletions(-)
Code scan: clean
Tests: failed (FAILED)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline