Repository navigation
Commit 1bbe69c
fix(client): percent-encode ids in REST transport paths (a2aproject#1436)
# Description
A REST client that names its push notification configs, or talks to a
server whose task ids are not plain UUIDs, can read or delete the wrong
resource. `RestTransport` puts task ids and push config ids into the URL
path as raw text, so an id with `#`, `?` or `%` changes the URL:
`delete_task_push_notification_config(id='alerts#ops')` sends `DELETE
.../pushNotificationConfigs/alerts` and deletes the config `alerts`. The
same ids work over JSON-RPC and gRPC, which send them in the request
body.
This PR encodes each id with `urllib.parse.quote(value, safe='')` at the
7 path sites (`get_task`, `cancel_task`, `subscribe` and the four push
config methods). The server's router decodes the segment back, so ids
with `#`, `?`, `%`, spaces, unicode or `:` now reach their own resource.
Ids without reserved characters produce the same URL as before.
Reproducer: a real `DefaultRequestHandler` behind `create_rest_routes`,
served through `httpx.ASGITransport` to a real `RestTransport`. The
client creates the configs `alerts` and `alerts#ops` on one task,
deletes `alerts#ops`, then calls `get_task('t1?x')`.
```
# main (28f8a62)
configs before: ['alerts', 'alerts#ops']
delete('alerts#ops') -> configs after: ['alerts#ops']
get_task('t1?x') -> returned task 't1'
# this branch
configs before: ['alerts', 'alerts#ops']
delete('alerts#ops') -> configs after: ['alerts']
get_task('t1?x') -> TaskNotFoundError
```
```mermaid
flowchart LR
I["id = 'alerts#ops'"] --> P{RestTransport path}
P -->|main| R1["f'.../pushNotificationConfigs/{id}'"]
R1 --> H1["httpx: '#ops' is a fragment"]
H1 --> S1["DELETE .../alerts: wrong config deleted"]
P -->|this PR| R2["_quote_id(id) = 'alerts%23ops'"]
R2 --> H2["DELETE .../alerts%23ops"]
H2 --> S2["router decodes push_id = 'alerts#ops'"]
```
The tests in `tests/client/transports/test_rest_client.py`
(`TestRestTransportPathEncoding`) run the reproducer above: a real
`DefaultRequestHandler` behind `create_rest_routes`, called through
`httpx.ASGITransport`. One deletes `alerts#ops` and asserts that
`alerts` is left. The other asserts that `get_task('t1?x')` raises
`TaskNotFoundError`.
```
$ pytest tests/client/transports/test_rest_client.py -k PathEncoding # rest.py from main
2 failed, 41 deselected
$ pytest tests/client/transports/test_rest_client.py -k PathEncoding # this branch
2 passed, 41 deselected
```
Run on Windows 11, Python 3.10, on b2a6aa3: `ruff check` and `ruff
format --check` pass on the changed files; `pytest
--ignore=tests/integration/cross_version
--ignore=tests/integration/test_scenarios.py` gives 2431 passed, 207
skipped, 4 xfailed. The 4 push notification e2e tests error at setup on
Windows (spawn pickling) on `main` as well.
Out of scope, noted for review:
- An id containing `/` is encoded as `%2F`, which Starlette decodes
before routing, so it now gets a 404 instead of reaching another route.
Supporting `/` in ids would need a server change.
- Ids that are exactly `.` or `..` are still collapsed by httpx
dot-segment handling.
- The tenant prefix in `_get_path` is left as is: it is configured by
the operator and is not an id.
- `src/a2a/compat/v0_3/rest_transport.py` builds paths the same way;
happy to follow up there.
- [x] Follow the [`CONTRIBUTING`
Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md).
- [x] Make your Pull Request title in the
<https://www.conventionalcommits.org/> specification.
- [x] Ensure the tests and linter pass (Run `bash scripts/format.sh`
from the repository root to format)
- [ ] Appropriate docs were updated (if necessary)
---------
Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>1 parent 01483cb commit 1bbe69c
2 files changed
Lines changed: 106 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
89 | 90 | | |
90 | 91 | | |
91 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
92 | 98 | | |
93 | 99 | | |
94 | 100 | | |
| |||
158 | 164 | | |
159 | 165 | | |
160 | 166 | | |
161 | | - | |
| 167 | + | |
162 | 168 | | |
163 | 169 | | |
164 | 170 | | |
| |||
198 | 204 | | |
199 | 205 | | |
200 | 206 | | |
201 | | - | |
| 207 | + | |
202 | 208 | | |
203 | 209 | | |
204 | 210 | | |
| |||
215 | 221 | | |
216 | 222 | | |
217 | 223 | | |
218 | | - | |
| 224 | + | |
219 | 225 | | |
220 | 226 | | |
221 | 227 | | |
| |||
242 | 248 | | |
243 | 249 | | |
244 | 250 | | |
245 | | - | |
| 251 | + | |
| 252 | + | |
246 | 253 | | |
247 | 254 | | |
248 | 255 | | |
| |||
267 | 274 | | |
268 | 275 | | |
269 | 276 | | |
270 | | - | |
| 277 | + | |
271 | 278 | | |
272 | 279 | | |
273 | 280 | | |
| |||
294 | 301 | | |
295 | 302 | | |
296 | 303 | | |
297 | | - | |
| 304 | + | |
| 305 | + | |
298 | 306 | | |
299 | 307 | | |
300 | 308 | | |
| |||
309 | 317 | | |
310 | 318 | | |
311 | 319 | | |
312 | | - | |
| 320 | + | |
313 | 321 | | |
314 | 322 | | |
315 | 323 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
14 | 22 | | |
15 | 23 | | |
16 | 24 | | |
| |||
24 | 32 | | |
25 | 33 | | |
26 | 34 | | |
| 35 | + | |
27 | 36 | | |
28 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
29 | 49 | | |
30 | | - | |
31 | | - | |
32 | 50 | | |
33 | 51 | | |
| 52 | + | |
34 | 53 | | |
35 | 54 | | |
36 | 55 | | |
| |||
752 | 771 | | |
753 | 772 | | |
754 | 773 | | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
| 778 | + | |
| 779 | + | |
| 780 | + | |
| 781 | + | |
| 782 | + | |
| 783 | + | |
| 784 | + | |
| 785 | + | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
755 | 844 | | |
756 | 845 | | |
757 | 846 | | |
| |||
0 commit comments