Commit 5e02d18
authored
fix(webhook-proxy): surface webhook registration failures instead of silently loading (#1101)
* fix(webhook-proxy): surface webhook registration failures instead of silently loading
Defect 2 of #1020: when /config/.mcp_proxy_config.json contains a corrupted
target_url (e.g. the secret-path truncation triggered by Defect 1), the
mcp_proxy custom integration would proceed past its empty-string guard,
register with the truncated URL, and report state="loaded" while every
webhook request returned 404 from HA core.
Two changes in async_setup_entry:
- Validate target_url shape before registering. The path must match
/private_<token> with token >= 16 chars (real ha-mcp generates a
22-char base64url token; the truncation bug yielded ~7). On failure,
raise ConfigEntryError(reason) so HA surfaces the reason in the UI
and config-entries API instead of returning False.
- Wrap async_register() in try/except. On failure, close the aiohttp
session and re-raise as ConfigEntryError with the original error as
cause. Move hass.data[DOMAIN] assignment to after a successful
register so a failed setup doesn't leave half-state.
Tests cover the issue's exact reproducer (/private_ZZZZZZZ truncation),
all rejection paths, async_register raising, and the happy path. They
stub homeassistant.* and aiohttp via sys.modules so the integration can
be imported under our existing pytest harness without pulling HA Core
into dev deps.
Defect 1 (the parser truncation itself) is addressed separately via
PR #952, which persists the secret path to addon options so the regex
fallback isn't reached in practice.
* fix(webhook-proxy): close additional silent-failure gaps and tighten validator
Address PR-review findings on top of the original Defect 2 fix:
- _read_config now lets OSError/JSONDecodeError propagate instead of
swallowing them. async_setup_entry catches them and raises
ConfigEntryError so a corrupted /config/.mcp_proxy_config.json no
longer masquerades as a "no config yet, fresh install" state.
- _validate_target_url now also rejects URLs containing query strings,
fragments, or path parameters. urlparse strips these from parsed.path,
so previously something like /private_<token>?x=y validated clean but
the addon would 404 on the forwarded request.
- Drop the dead try/except ValueError around urlparse — it doesn't raise
on any string input.
- Validator failure reasons no longer echo parsed.path verbatim, which
was leaking the raw secret token into ERROR-level logs even when the
caller's masked_target was correct. Caught by the new caplog test.
- Reconcile the regex-vs-comment mismatch: comment said "22-char token",
regex enforced >=16. Reworded as "real tokens are 22 chars; we accept
>=16 as a sanity floor" to match what the code actually does.
Tests:
- TestUnloadEntry — async_unload_entry was untested. Two cases now pin
that unload after a failed setup is a no-op (no async_unregister, no
double-close) and that unload after a successful setup unregisters and
closes the session.
- test_truncated_url_does_not_log_full_token — caplog assertion that
catches the secret-leak in validator reasons.
- test_corrupted_json_raises_config_entry_error /
test_unreadable_config_raises_config_entry_error — pin the new behavior
that distinguishes "no file" from "broken file".
- test_register_failure_closes_session_and_raises is now parametrized
over RuntimeError/ValueError/KeyError (HA's documented failure modes
for async_register), documenting that the broad except catch is
intentional.
- TestTargetUrlValidation gains 15-char boundary, query, fragment, and
path-params rejection tests.
- All early-fail paths now also assert ClientSession is never constructed
so a future "early connect probing" refactor can't silently leak
resources.
Bump both the addon (config.yaml: 1.0.1 -> 1.0.2) and the bundled
mcp_proxy integration (manifest.json: 1.0.0 -> 1.0.2). The two had
diverged because an earlier addon-only bump didn't touch the integration;
aligning both to 1.0.2 going forward since they always ship together.
---------
Co-authored-by: kingpanther13 <kingpanther13@users.noreply.github.com>1 parent 22ef1c6 commit 5e02d18
4 files changed
Lines changed: 446 additions & 20 deletions
File tree
- homeassistant-addon-webhook-proxy
- mcp_proxy
- tests/addon
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
| 17 | + | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
| |||
22 | 24 | | |
23 | 25 | | |
24 | 26 | | |
| 27 | + | |
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
30 | 33 | | |
31 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
32 | 65 | | |
33 | 66 | | |
34 | 67 | | |
| |||
51 | 84 | | |
52 | 85 | | |
53 | 86 | | |
54 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
55 | 96 | | |
56 | 97 | | |
57 | 98 | | |
| |||
65 | 106 | | |
66 | 107 | | |
67 | 108 | | |
68 | | - | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
69 | 113 | | |
70 | 114 | | |
71 | 115 | | |
72 | 116 | | |
73 | 117 | | |
74 | 118 | | |
75 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
76 | 136 | | |
77 | 137 | | |
78 | 138 | | |
79 | 139 | | |
80 | 140 | | |
81 | 141 | | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
82 | 162 | | |
83 | 163 | | |
84 | 164 | | |
85 | 165 | | |
86 | 166 | | |
87 | 167 | | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | 168 | | |
98 | 169 | | |
99 | 170 | | |
100 | 171 | | |
101 | | - | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
102 | 178 | | |
103 | 179 | | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
| 180 | + | |
109 | 181 | | |
110 | 182 | | |
111 | 183 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
0 commit comments