fix: guard leak, presence_sensor, contact parsers against short mfr_data#495
Conversation
PR Review — fix: guard leak, presence_sensor, contact parsers against short mfr_dataThe parser guards are correct and the new regression tests cover the right edge cases. However, the pre-commit 🟡 Important1. Ruff PT006: parametrize argnames should be a tuple (`tests/test_short_mfr_guards.py`, L36)The lint failure flagged by @bdraco originates here. Ruff rule 🟢 Suggestions1. Minor: `len(data) >= 2` check is redundant after the early return (`switchbot/adv_parsers/contact.py`, L13-21)
Checklist
SummaryThe parser guards are correct and the new regression tests cover the right edge cases. However, the pre-commit Automated review by Kōandf20154 |
…ata (sblibs#494) The leak, presence_sensor and contact parsers indexed `mfr_data` beyond their length guards, raising `IndexError` on truncated/malformed BLE advertisements with manufacturer_id 2409. Bump the guards so each parser returns `{}` when the payload is too short to decode. - leak: guard < 9 (was < 2; indexes mfr_data[7] and mfr_data[8]) - presence_sensor: guard < 12 (was None-only; indexes up to mfr_data[11]) - contact: require either mfr_data >= 13 or data >= 9 before the fallback path Closes sblibs#494
Rebase with requested adjustmentsBranch StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
0449416 to
df20154
Compare
|
code is fine but lint is failing |
Confirmed — this is Ruff's |
Rebase with requested adjustmentsBranch Changes applied
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
What
Bumps length guards on three advertisement parsers so truncated BLE payloads return
{}instead of raisingIndexError.Why
Audit in #494 found that
leak.py,presence_sensor.py, andcontact.pyindexmfr_databeyond their guards. Withmanufacturer_id == 2409the dispatcher inadv_parser.pydoes not length-validate before calling the parsers, so a malformed advertisement (untrusted but range-limited input) crashes the caller (e.g. Home Assistant) instead of degrading gracefully to an empty result.How
leak: guard< 9(was< 2) — function indexesmfr_data[7]andmfr_data[8].presence_sensor: guard< 12(was None-only) — function indexes up tomfr_data[11].contact: require eithermfr_data >= 13ordata >= 9before the fallback path; also tighten the inlinedata[2]/data[1]accesses with explicit length checks. Preserves the existing behavior when either source is long enough.Testing
Added
tests/test_short_mfr_guards.py(16 parametrized cases). Full suite green: 1094 passed.Closes #494
Quality Report
Changes: 4 files changed, 86 insertions(+), 5 deletions(-)
Code scan: clean
Tests: failed (FAILED)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline