Skip to content

fix(teslemetry): decode dotted-quad IPv4 gateway addresses with strict validation - #127

Merged
Bre77 merged 4 commits into
mainfrom
fm/pyfleet-decode-ipv4-string
Aug 22, 2026
Merged

fix(teslemetry): decode dotted-quad IPv4 gateway addresses with strict validation#127
Bre77 merged 4 commits into
mainfrom
fm/pyfleet-decode-ipv4-string

Conversation

@Bre77

@Bre77 Bre77 commented Aug 22, 2026

Copy link
Copy Markdown
Member

Intent

Fix a Codex PR-review-flagged P2 on PR #127 (fm/pyfleet-decode-ipv4-string): _DOTTED_QUAD_RE.match() used a $ anchor, which Python regex matches before a trailing newline, so a malformed string like '0.0.0.0\n' passed the regex and then bypassed the exact '0.0.0.0'/'255.255.255.255' sentinel equality check - it could shadow a valid address in interface selection instead of being treated as undecodable. Fixed by switching match() to fullmatch(), which requires the entire string to match with no unmatched trailing characters. Added regression tests for trailing-newline and surrounding-whitespace forms of both a normal address and both sentinel values, including one exercising the actual fallback-to-wifi selection behavior when eth's address is a trailing-newline sentinel variant. All prior task requirements (dual uint32/string decoding, active_route preference, 0.0.0.0/255.255.255.255/bool rejection, None-on-malformed) remain unchanged and covered. Verified: ruff check/format, pyright strict, and full pytest suite (646 tests) all pass locally.

What Changed

  • find_gateway_address() in tesla_fleet_api/teslemetry/energysite.py now decodes networking_status.ipv4_config.address as either a raw big-endian uint32 or a dotted-quad string (the API has been observed serving both forms), in addition to the existing uint32-only path.
  • Dotted-quad string validation uses a strict regex matched with fullmatch() (not match()), so malformed input such as a trailing newline can no longer slip past the format check and falsely satisfy the 0.0.0.0/255.255.255.255 sentinel-rejection logic.
  • Updated docs/teslemetry.md to describe the dual uint32/string decoding instead of the stale uint32-only description.
  • Added regression tests in tests/test_teslemetry_gateway_address.py covering dotted-quad string decoding, trailing-newline and surrounding-whitespace malformed variants for both normal and sentinel addresses, and the fallback-to-wifi selection path when eth's address is an invalid sentinel variant.

Risk Assessment

✅ Low: Single-line regex-method fix (match→fullmatch) verified by hand to correctly reject the trailing-newline/whitespace bypass described in the intent, backed by thorough new regression tests; no other functional code changed.

Testing

Ran the targeted gateway-address test file (26 tests) against the fix commit — all pass. To validate the tests aren't vacuous, I temporarily reverted the one-line fullmatch->match change and reran the new trailing-newline/whitespace tests: they failed exactly as the P2 review described ('0.0.0.0\n' and '255.255.255.255\n' slipping past the sentinel check, with the zero-sentinel case wrongly returning the malformed value instead of falling back to wifi's real address), confirming the fix and its regression coverage are effective. Working tree was restored to clean afterward.

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • uv run pytest tests/test_teslemetry_gateway_address.py -v (26 passed) on target commit 3003ebf
  • Manually reverted .fullmatch() back to .match() in tesla_fleet_api/teslemetry/energysite.py and reran the new regression tests (-k "trailing_newline or surrounding_whitespace") to confirm they fail against the pre-fix behavior — 3 failed as expected, proving the tests actually exercise the fixed bug
  • Restored the source file via git checkout -- and reran the full targeted test file to confirm 26/26 pass on the real fix
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Bre77 added 3 commits August 22, 2026 09:54
…decode

networking_status now serves ipv4_config address/subnet_mask/gateway as
dotted-quad strings on some sites, alongside the legacy big-endian uint32
int; _decode_ipv4 only handled the int form, so find_gateway_address()
silently returned None on affected sites.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 96b39c0e61

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

return None
address = socket.inet_ntoa(struct.pack(">I", value))
elif isinstance(value, str):
if not _DOTTED_QUAD_RE.match(value):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject trailing newlines in dotted-quad addresses

When an address string ends in \n, Python's $ anchor matches immediately before that final newline, so _DOTTED_QUAD_RE.match() succeeds and the unvalidated original string is returned. In particular, "0.0.0.0\n" also bypasses the sentinel equality check and can shadow a valid Wi-Fi address during interface selection. Use fullmatch() or a strict end-of-string anchor so any surrounding whitespace remains undecodable.

AGENTS.md reference: AGENTS.md:L149-L149

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: switched to fullmatch() in 3003ebf, closing the trailing-newline/whitespace gap for both a valid address and the two sentinels. Added regression tests for both cases.

@Bre77 Bre77 added the fm Opened by a Firstmate crewmate label Aug 22, 2026
re.match with a $ anchor accepts a trailing newline, so a string like
"0.0.0.0\n" passed the regex and then failed the exact sentinel
equality check, letting a malformed/undecodable address slip through
as if it were a validated one. fullmatch requires the whole string to
match, closing that gap.
@Bre77 Bre77 changed the title fix(teslemetry): accept dotted-quad string IPv4 addresses in gateway decode fix(teslemetry): decode dotted-quad IPv4 gateway addresses with strict validation Aug 22, 2026
@Bre77
Bre77 merged commit cac65a5 into main Aug 22, 2026
6 checks passed
@Bre77 Bre77 mentioned this pull request Aug 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fm Opened by a Firstmate crewmate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant