Skip to content

Commit ad4c871

Browse files
jantmanclaude
andcommitted
ESB Support: address PR review feedback on the status webhook
Adversarial review (claude bot) and Copilot review findings: - (High) Webhook payload last_update was stale for RFID/oops events: MachineState.update() assigned self.last_update = time() *after* the handler ran, but the handler fires the webhook (reading status_dict) before returning, so the payload's last_update lagged one event behind and did not match its own timestamp (contradicting the documented example). Move the last_update assignment before the handler call in the oops and RFID-change branches. Add a regression test asserting payload["last_update"] == payload["timestamp"] for login/logout/oops fired through update(). - (Medium) Fire-and-forget delivery tasks had no strong reference and could be garbage-collected mid-flight (create_task returns a weakly-held task, and _deliver can run ~27s). Track in-flight tasks in a set and discard via a done-callback (the asyncio-docs pattern). - (Low) _deliver opened a new aiohttp.ClientSession per retry attempt; reuse a single session across attempts so retries benefit from connection pooling. - (Copilot) from_env logged the full STATUS_WEBHOOK_URL, which could leak secrets embedded in userinfo/query. Log only scheme+host via a new _safe_url helper; add tests proving credentials are not logged. - (Copilot) test _machine() helper did not return a fresh status_dict per access like the real property; replace the Mock with a small _FakeMachine whose status_dict property returns a fresh dict. All nox sessions pass; webhook.py at 100% coverage. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 9d2ddc2 commit ad4c871

4 files changed

Lines changed: 127 additions & 20 deletions

File tree

‎src/dm_mac/models/machine.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,12 @@ async def update(
966966
self.internal_temperature_c = internal_temperature_c
967967
self.last_checkin = time()
968968
if oops:
969-
await self._handle_oops(users)
969+
# Set last_update *before* the handler runs: the handler fires
970+
# the status-change webhook synchronously (from status_dict), so
971+
# last_update must already reflect this event for the webhook
972+
# payload's last_update to match its own timestamp.
970973
self.last_update = time()
974+
await self._handle_oops(users)
971975
# Handle always-enabled machines - track RFID but maintain always-on state
972976
if (
973977
self.machine.always_enabled
@@ -983,11 +987,14 @@ async def update(
983987
if rfid_value != self.rfid_value:
984988
await self._handle_rfid_tracking_always_enabled(users, rfid_value)
985989
elif rfid_value != self.rfid_value:
990+
# Set last_update *before* the handler for the same reason as
991+
# the oops branch above: the handler fires the webhook and the
992+
# payload reads last_update from status_dict.
993+
self.last_update = time()
986994
if rfid_value is None:
987995
await self._handle_rfid_remove()
988996
else:
989997
await self._handle_rfid_insert(users, rfid_value)
990-
self.last_update = time()
991998
else:
992999
# No RFID change - check for stale always-enabled state.
9931000
# This handles the case where always_enabled config was changed

‎src/dm_mac/webhook.py‎

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import logging
44
import os
55
from asyncio import CancelledError
6+
from asyncio import Task
67
from asyncio import create_task
78
from asyncio import sleep
89
from time import time
910
from typing import TYPE_CHECKING
1011
from typing import Any
1112
from typing import Dict
1213
from typing import Optional
14+
from typing import Set
15+
from urllib.parse import urlsplit
1316

1417
import aiohttp
1518

@@ -49,14 +52,35 @@ class WebhookNotifier:
4952
def __init__(self, url: str):
5053
"""Initialize a WebhookNotifier posting to ``url``."""
5154
self.url: str = url
55+
#: Strong references to in-flight delivery tasks. ``asyncio`` only
56+
#: holds a *weak* reference to tasks created via
57+
#: :func:`asyncio.create_task`, so without this a long-running
58+
#: :meth:`_deliver` (up to ~27 s of backoff + timeouts) could be
59+
#: garbage-collected mid-flight. Each task removes itself via a
60+
#: done-callback (the pattern from the asyncio docs).
61+
self._tasks: Set["Task[None]"] = set()
62+
63+
@staticmethod
64+
def _safe_url(url: str) -> str:
65+
"""Return ``scheme://host[:port]`` of ``url`` for safe logging.
66+
67+
Strips any userinfo, path, and query string so credentials embedded
68+
in the URL (userinfo or query params) are never written to logs.
69+
"""
70+
parts = urlsplit(url)
71+
host: str = parts.hostname or ""
72+
if parts.port is not None:
73+
host = f"{host}:{parts.port}"
74+
return f"{parts.scheme}://{host}" if parts.scheme else host
5275

5376
@classmethod
5477
def from_env(cls) -> Optional["WebhookNotifier"]:
5578
"""Build a notifier from ``STATUS_WEBHOOK_URL``, or None if unset."""
5679
url: str = os.environ.get("STATUS_WEBHOOK_URL", "").strip()
5780
if not url:
5881
return None
59-
logger.info("Status-change webhook enabled; posting to %s", url)
82+
# Log only scheme+host: the URL may contain secrets (userinfo/query).
83+
logger.info("Status-change webhook enabled; posting to %s", cls._safe_url(url))
6084
return cls(url)
6185

6286
def build_payload(
@@ -90,33 +114,39 @@ def notify(
90114
) -> None:
91115
"""Build the payload and spawn a fire-and-forget delivery task."""
92116
payload: Dict[str, Any] = self.build_payload(machine, event, user=user)
93-
create_task(self._deliver(payload))
117+
task: "Task[None]" = create_task(self._deliver(payload))
118+
# Hold a strong reference until the task completes so the event loop's
119+
# weak reference cannot let it be garbage-collected mid-delivery.
120+
self._tasks.add(task)
121+
task.add_done_callback(self._tasks.discard)
94122

95123
async def _deliver(self, payload: Dict[str, Any]) -> None:
96124
"""Deliver one webhook with retries and exponential backoff.
97125
98126
Returns as soon as an attempt gets a non-error (< 400) response. On
99127
network errors or 4xx/5xx responses it retries up to
100128
:data:`MAX_ATTEMPTS` times, backing off exponentially between attempts,
101-
then logs a single error and gives up.
129+
then logs a single error and gives up. A single
130+
:class:`aiohttp.ClientSession` is reused across attempts so retries can
131+
benefit from connection pooling.
102132
"""
103133
timeout: aiohttp.ClientTimeout = aiohttp.ClientTimeout(
104134
total=self.REQUEST_TIMEOUT_SEC
105135
)
106136
last_err: Optional[str] = None
107-
for attempt in range(1, self.MAX_ATTEMPTS + 1):
108-
if attempt > 1:
109-
await sleep(self.BACKOFF_BASE_SEC * (2 ** (attempt - 2)))
110-
try:
111-
async with aiohttp.ClientSession(timeout=timeout) as session:
137+
async with aiohttp.ClientSession(timeout=timeout) as session:
138+
for attempt in range(1, self.MAX_ATTEMPTS + 1):
139+
if attempt > 1:
140+
await sleep(self.BACKOFF_BASE_SEC * (2 ** (attempt - 2)))
141+
try:
112142
async with session.post(self.url, json=payload) as resp:
113143
if resp.status < 400:
114144
return
115145
last_err = f"HTTP {resp.status}"
116-
except CancelledError: # pragma: no cover
117-
raise
118-
except Exception as ex:
119-
last_err = repr(ex)
146+
except CancelledError: # pragma: no cover
147+
raise
148+
except Exception as ex:
149+
last_err = repr(ex)
120150
logger.error(
121151
"Failed to deliver status webhook for machine %s event %s after "
122152
"%d attempts: %s",

‎tests/test_webhook.py‎

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,21 @@
1414
pbm: str = "dm_mac.webhook"
1515

1616

17-
def _machine(status_dict: Optional[Dict[str, Any]] = None) -> Mock:
18-
"""Build a fake Machine whose status_dict returns a fresh dict each call."""
17+
class _FakeMachine:
18+
"""Stand-in for Machine whose ``status_dict`` returns a fresh dict each
19+
access, exactly like the real property, so ``build_payload``'s in-place
20+
mutation cannot leak between accesses."""
21+
22+
def __init__(self, status_dict: Dict[str, Any]):
23+
self._status_dict = status_dict
24+
25+
@property
26+
def status_dict(self) -> Dict[str, Any]:
27+
return dict(self._status_dict)
28+
29+
30+
def _machine(status_dict: Optional[Dict[str, Any]] = None) -> _FakeMachine:
31+
"""Build a fake Machine whose status_dict returns a fresh dict each access."""
1932
if status_dict is None:
2033
status_dict = {
2134
"name": "planer",
@@ -28,10 +41,7 @@ def _machine(status_dict: Optional[Dict[str, Any]] = None) -> Mock:
2841
"last_checkin": 100.0,
2942
"last_update": 200.0,
3043
}
31-
mach = Mock()
32-
# Return a copy each access so build_payload's mutation doesn't leak.
33-
mach.status_dict = dict(status_dict)
34-
return mach
44+
return _FakeMachine(status_dict)
3545

3646

3747
class _FakePost:
@@ -98,6 +108,37 @@ def test_set_returns_instance(self) -> None:
98108
assert notifier is not None
99109
assert notifier.url == "https://esb/hook"
100110

111+
def test_does_not_log_secrets(self) -> None:
112+
"""from_env logs only scheme+host, never userinfo or query secrets."""
113+
secret_url = "https://user:pass@esb.example.com/hook?token=SECRET"
114+
with patch.dict("os.environ", {"STATUS_WEBHOOK_URL": secret_url}), patch(
115+
f"{pbm}.logger"
116+
) as mock_logger:
117+
notifier = WebhookNotifier.from_env()
118+
assert notifier is not None
119+
# The full URL (with credentials) is still used for delivery...
120+
assert notifier.url == secret_url
121+
# ...but the logged message contains only scheme+host.
122+
logged = " ".join(str(a) for a in mock_logger.info.call_args.args)
123+
assert "esb.example.com" in logged
124+
assert "pass" not in logged
125+
assert "SECRET" not in logged
126+
127+
128+
class TestSafeUrl:
129+
"""Tests for WebhookNotifier._safe_url redaction."""
130+
131+
def test_strips_userinfo_and_query(self) -> None:
132+
"""Userinfo, path, and query are stripped; scheme+host kept."""
133+
assert (
134+
WebhookNotifier._safe_url("https://u:p@host.example:8443/hook?k=v")
135+
== "https://host.example:8443"
136+
)
137+
138+
def test_plain_url(self) -> None:
139+
"""A plain URL reduces to scheme://host."""
140+
assert WebhookNotifier._safe_url("https://esb/hook") == "https://esb"
141+
101142

102143
class TestBuildPayload:
103144
"""Tests for WebhookNotifier.build_payload."""

‎tests/views/test_webhook_wiring.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
from typing import List
77
from unittest.mock import AsyncMock
88
from unittest.mock import Mock
9+
from unittest.mock import patch
910

11+
from freezegun import freeze_time
1012
from quart import Quart
1113
from quart.typing import TestClientProtocol
1214

15+
from dm_mac.webhook import WebhookNotifier
16+
1317
from .quart_test_helpers import app_and_client
1418

1519

@@ -156,3 +160,28 @@ async def test_disabled_notifier_is_noop(self, tmp_path: Path) -> None:
156160
json=_heartbeat("metal-mill", rfid_value="8114346998"),
157161
)
158162
assert resp.status_code == 200
163+
164+
@freeze_time("2024-01-01 00:00:00")
165+
async def test_payload_last_update_matches_timestamp(self, tmp_path: Path) -> None:
166+
"""Regression: the webhook payload's last_update reflects the current
167+
event, so it matches the payload's own timestamp for login/logout/oops
168+
fired through update() (previously it lagged one event behind)."""
169+
app, client = app_and_client(tmp_path)
170+
with patch.object(
171+
WebhookNotifier, "_deliver", new_callable=AsyncMock
172+
) as mock_deliver:
173+
app.config["WEBHOOK_NOTIFIER"] = WebhookNotifier("http://x")
174+
await client.post(
175+
"/api/machine/update",
176+
json=_heartbeat("metal-mill", rfid_value="8114346998"),
177+
) # login
178+
await client.post(
179+
"/api/machine/update", json=_heartbeat("metal-mill")
180+
) # logout
181+
await client.post(
182+
"/api/machine/update", json=_heartbeat("metal-mill", oops=True)
183+
) # oops
184+
payloads = [call.args[0] for call in mock_deliver.call_args_list]
185+
assert [p["event"] for p in payloads] == ["login", "logout", "oops"]
186+
for p in payloads:
187+
assert p["last_update"] == p["timestamp"], p["event"]

0 commit comments

Comments
 (0)