Skip to content

Commit 3d04ad1

Browse files
fix: sanitize endpoint path params
1 parent ec65511 commit 3d04ad1

23 files changed

+279
-52
lines changed

src/onebusaway/_utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ._path import path_template as path_template
12
from ._sync import asyncify as asyncify
23
from ._proxy import LazyProxy as LazyProxy
34
from ._utils import (

src/onebusaway/_utils/_path.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
from __future__ import annotations
2+
3+
import re
4+
from typing import (
5+
Any,
6+
Mapping,
7+
Callable,
8+
)
9+
from urllib.parse import quote
10+
11+
# Matches '.' or '..' where each dot is either literal or percent-encoded (%2e / %2E).
12+
_DOT_SEGMENT_RE = re.compile(r"^(?:\.|%2[eE]){1,2}$")
13+
14+
_PLACEHOLDER_RE = re.compile(r"\{(\w+)\}")
15+
16+
17+
def _quote_path_segment_part(value: str) -> str:
18+
"""Percent-encode `value` for use in a URI path segment.
19+
20+
Considers characters not in `pchar` set from RFC 3986 §3.3 to be unsafe.
21+
https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
22+
"""
23+
# quote() already treats unreserved characters (letters, digits, and -._~)
24+
# as safe, so we only need to add sub-delims, ':', and '@'.
25+
# Notably, unlike the default `safe` for quote(), / is unsafe and must be quoted.
26+
return quote(value, safe="!$&'()*+,;=:@")
27+
28+
29+
def _quote_query_part(value: str) -> str:
30+
"""Percent-encode `value` for use in a URI query string.
31+
32+
Considers &, = and characters not in `query` set from RFC 3986 §3.4 to be unsafe.
33+
https://datatracker.ietf.org/doc/html/rfc3986#section-3.4
34+
"""
35+
return quote(value, safe="!$'()*+,;:@/?")
36+
37+
38+
def _quote_fragment_part(value: str) -> str:
39+
"""Percent-encode `value` for use in a URI fragment.
40+
41+
Considers characters not in `fragment` set from RFC 3986 §3.5 to be unsafe.
42+
https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
43+
"""
44+
return quote(value, safe="!$&'()*+,;=:@/?")
45+
46+
47+
def _interpolate(
48+
template: str,
49+
values: Mapping[str, Any],
50+
quoter: Callable[[str], str],
51+
) -> str:
52+
"""Replace {name} placeholders in `template`, quoting each value with `quoter`.
53+
54+
Placeholder names are looked up in `values`.
55+
56+
Raises:
57+
KeyError: If a placeholder is not found in `values`.
58+
"""
59+
# re.split with a capturing group returns alternating
60+
# [text, name, text, name, ..., text] elements.
61+
parts = _PLACEHOLDER_RE.split(template)
62+
63+
for i in range(1, len(parts), 2):
64+
name = parts[i]
65+
if name not in values:
66+
raise KeyError(f"a value for placeholder {{{name}}} was not provided")
67+
val = values[name]
68+
if val is None:
69+
parts[i] = "null"
70+
elif isinstance(val, bool):
71+
parts[i] = "true" if val else "false"
72+
else:
73+
parts[i] = quoter(str(values[name]))
74+
75+
return "".join(parts)
76+
77+
78+
def path_template(template: str, /, **kwargs: Any) -> str:
79+
"""Interpolate {name} placeholders in `template` from keyword arguments.
80+
81+
Args:
82+
template: The template string containing {name} placeholders.
83+
**kwargs: Keyword arguments to interpolate into the template.
84+
85+
Returns:
86+
The template with placeholders interpolated and percent-encoded.
87+
88+
Safe characters for percent-encoding are dependent on the URI component.
89+
Placeholders in path and fragment portions are percent-encoded where the `segment`
90+
and `fragment` sets from RFC 3986 respectively are considered safe.
91+
Placeholders in the query portion are percent-encoded where the `query` set from
92+
RFC 3986 §3.3 is considered safe except for = and & characters.
93+
94+
Raises:
95+
KeyError: If a placeholder is not found in `kwargs`.
96+
ValueError: If resulting path contains /./ or /../ segments (including percent-encoded dot-segments).
97+
"""
98+
# Split the template into path, query, and fragment portions.
99+
fragment_template: str | None = None
100+
query_template: str | None = None
101+
102+
rest = template
103+
if "#" in rest:
104+
rest, fragment_template = rest.split("#", 1)
105+
if "?" in rest:
106+
rest, query_template = rest.split("?", 1)
107+
path_template = rest
108+
109+
# Interpolate each portion with the appropriate quoting rules.
110+
path_result = _interpolate(path_template, kwargs, _quote_path_segment_part)
111+
112+
# Reject dot-segments (. and ..) in the final assembled path. The check
113+
# runs after interpolation so that adjacent placeholders or a mix of static
114+
# text and placeholders that together form a dot-segment are caught.
115+
# Also reject percent-encoded dot-segments to protect against incorrectly
116+
# implemented normalization in servers/proxies.
117+
for segment in path_result.split("/"):
118+
if _DOT_SEGMENT_RE.match(segment):
119+
raise ValueError(f"Constructed path {path_result!r} contains dot-segment {segment!r} which is not allowed")
120+
121+
result = path_result
122+
if query_template is not None:
123+
result += "?" + _interpolate(query_template, kwargs, _quote_query_part)
124+
if fragment_template is not None:
125+
result += "#" + _interpolate(fragment_template, kwargs, _quote_fragment_part)
126+
127+
return result

src/onebusaway/resources/agency.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import httpx
66

77
from .._types import Body, Query, Headers, NotGiven, not_given
8+
from .._utils import path_template
89
from .._compat import cached_property
910
from .._resource import SyncAPIResource, AsyncAPIResource
1011
from .._response import (
@@ -65,7 +66,7 @@ def retrieve(
6566
if not agency_id:
6667
raise ValueError(f"Expected a non-empty value for `agency_id` but received {agency_id!r}")
6768
return self._get(
68-
f"/api/where/agency/{agency_id}.json",
69+
path_template("/api/where/agency/{agency_id}.json", agency_id=agency_id),
6970
options=make_request_options(
7071
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
7172
),
@@ -119,7 +120,7 @@ async def retrieve(
119120
if not agency_id:
120121
raise ValueError(f"Expected a non-empty value for `agency_id` but received {agency_id!r}")
121122
return await self._get(
122-
f"/api/where/agency/{agency_id}.json",
123+
path_template("/api/where/agency/{agency_id}.json", agency_id=agency_id),
123124
options=make_request_options(
124125
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
125126
),

src/onebusaway/resources/arrival_and_departure.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from ..types import arrival_and_departure_list_params, arrival_and_departure_retrieve_params
1111
from .._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
12-
from .._utils import maybe_transform, async_maybe_transform
12+
from .._utils import path_template, maybe_transform, async_maybe_transform
1313
from .._compat import cached_property
1414
from .._resource import SyncAPIResource, AsyncAPIResource
1515
from .._response import (
@@ -76,7 +76,7 @@ def retrieve(
7676
if not stop_id:
7777
raise ValueError(f"Expected a non-empty value for `stop_id` but received {stop_id!r}")
7878
return self._get(
79-
f"/api/where/arrival-and-departure-for-stop/{stop_id}.json",
79+
path_template("/api/where/arrival-and-departure-for-stop/{stop_id}.json", stop_id=stop_id),
8080
options=make_request_options(
8181
extra_headers=extra_headers,
8282
extra_query=extra_query,
@@ -131,7 +131,7 @@ def list(
131131
if not stop_id:
132132
raise ValueError(f"Expected a non-empty value for `stop_id` but received {stop_id!r}")
133133
return self._get(
134-
f"/api/where/arrivals-and-departures-for-stop/{stop_id}.json",
134+
path_template("/api/where/arrivals-and-departures-for-stop/{stop_id}.json", stop_id=stop_id),
135135
options=make_request_options(
136136
extra_headers=extra_headers,
137137
extra_query=extra_query,
@@ -201,7 +201,7 @@ async def retrieve(
201201
if not stop_id:
202202
raise ValueError(f"Expected a non-empty value for `stop_id` but received {stop_id!r}")
203203
return await self._get(
204-
f"/api/where/arrival-and-departure-for-stop/{stop_id}.json",
204+
path_template("/api/where/arrival-and-departure-for-stop/{stop_id}.json", stop_id=stop_id),
205205
options=make_request_options(
206206
extra_headers=extra_headers,
207207
extra_query=extra_query,
@@ -256,7 +256,7 @@ async def list(
256256
if not stop_id:
257257
raise ValueError(f"Expected a non-empty value for `stop_id` but received {stop_id!r}")
258258
return await self._get(
259-
f"/api/where/arrivals-and-departures-for-stop/{stop_id}.json",
259+
path_template("/api/where/arrivals-and-departures-for-stop/{stop_id}.json", stop_id=stop_id),
260260
options=make_request_options(
261261
extra_headers=extra_headers,
262262
extra_query=extra_query,

src/onebusaway/resources/block.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import httpx
66

77
from .._types import Body, Query, Headers, NotGiven, not_given
8+
from .._utils import path_template
89
from .._compat import cached_property
910
from .._resource import SyncAPIResource, AsyncAPIResource
1011
from .._response import (
@@ -65,7 +66,7 @@ def retrieve(
6566
if not block_id:
6667
raise ValueError(f"Expected a non-empty value for `block_id` but received {block_id!r}")
6768
return self._get(
68-
f"/api/where/block/{block_id}.json",
69+
path_template("/api/where/block/{block_id}.json", block_id=block_id),
6970
options=make_request_options(
7071
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
7172
),
@@ -119,7 +120,7 @@ async def retrieve(
119120
if not block_id:
120121
raise ValueError(f"Expected a non-empty value for `block_id` but received {block_id!r}")
121122
return await self._get(
122-
f"/api/where/block/{block_id}.json",
123+
path_template("/api/where/block/{block_id}.json", block_id=block_id),
123124
options=make_request_options(
124125
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
125126
),

src/onebusaway/resources/report_problem_with_stop.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from ..types import report_problem_with_stop_retrieve_params
1010
from .._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
11-
from .._utils import maybe_transform, async_maybe_transform
11+
from .._utils import path_template, maybe_transform, async_maybe_transform
1212
from .._compat import cached_property
1313
from .._resource import SyncAPIResource, AsyncAPIResource
1414
from .._response import (
@@ -85,7 +85,7 @@ def retrieve(
8585
if not stop_id:
8686
raise ValueError(f"Expected a non-empty value for `stop_id` but received {stop_id!r}")
8787
return self._get(
88-
f"/api/where/report-problem-with-stop/{stop_id}.json",
88+
path_template("/api/where/report-problem-with-stop/{stop_id}.json", stop_id=stop_id),
8989
options=make_request_options(
9090
extra_headers=extra_headers,
9191
extra_query=extra_query,
@@ -168,7 +168,7 @@ async def retrieve(
168168
if not stop_id:
169169
raise ValueError(f"Expected a non-empty value for `stop_id` but received {stop_id!r}")
170170
return await self._get(
171-
f"/api/where/report-problem-with-stop/{stop_id}.json",
171+
path_template("/api/where/report-problem-with-stop/{stop_id}.json", stop_id=stop_id),
172172
options=make_request_options(
173173
extra_headers=extra_headers,
174174
extra_query=extra_query,

src/onebusaway/resources/report_problem_with_trip.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from ..types import report_problem_with_trip_retrieve_params
1010
from .._types import Body, Omit, Query, Headers, NotGiven, omit, not_given
11-
from .._utils import maybe_transform, async_maybe_transform
11+
from .._utils import path_template, maybe_transform, async_maybe_transform
1212
from .._compat import cached_property
1313
from .._resource import SyncAPIResource, AsyncAPIResource
1414
from .._response import (
@@ -107,7 +107,7 @@ def retrieve(
107107
if not trip_id:
108108
raise ValueError(f"Expected a non-empty value for `trip_id` but received {trip_id!r}")
109109
return self._get(
110-
f"/api/where/report-problem-with-trip/{trip_id}.json",
110+
path_template("/api/where/report-problem-with-trip/{trip_id}.json", trip_id=trip_id),
111111
options=make_request_options(
112112
extra_headers=extra_headers,
113113
extra_query=extra_query,
@@ -217,7 +217,7 @@ async def retrieve(
217217
if not trip_id:
218218
raise ValueError(f"Expected a non-empty value for `trip_id` but received {trip_id!r}")
219219
return await self._get(
220-
f"/api/where/report-problem-with-trip/{trip_id}.json",
220+
path_template("/api/where/report-problem-with-trip/{trip_id}.json", trip_id=trip_id),
221221
options=make_request_options(
222222
extra_headers=extra_headers,
223223
extra_query=extra_query,

src/onebusaway/resources/route.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import httpx
66

77
from .._types import Body, Query, Headers, NotGiven, not_given
8+
from .._utils import path_template
89
from .._compat import cached_property
910
from .._resource import SyncAPIResource, AsyncAPIResource
1011
from .._response import (
@@ -65,7 +66,7 @@ def retrieve(
6566
if not route_id:
6667
raise ValueError(f"Expected a non-empty value for `route_id` but received {route_id!r}")
6768
return self._get(
68-
f"/api/where/route/{route_id}.json",
69+
path_template("/api/where/route/{route_id}.json", route_id=route_id),
6970
options=make_request_options(
7071
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
7172
),
@@ -119,7 +120,7 @@ async def retrieve(
119120
if not route_id:
120121
raise ValueError(f"Expected a non-empty value for `route_id` but received {route_id!r}")
121122
return await self._get(
122-
f"/api/where/route/{route_id}.json",
123+
path_template("/api/where/route/{route_id}.json", route_id=route_id),
123124
options=make_request_options(
124125
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
125126
),

src/onebusaway/resources/route_ids_for_agency.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import httpx
66

77
from .._types import Body, Query, Headers, NotGiven, not_given
8+
from .._utils import path_template
89
from .._compat import cached_property
910
from .._resource import SyncAPIResource, AsyncAPIResource
1011
from .._response import (
@@ -65,7 +66,7 @@ def list(
6566
if not agency_id:
6667
raise ValueError(f"Expected a non-empty value for `agency_id` but received {agency_id!r}")
6768
return self._get(
68-
f"/api/where/route-ids-for-agency/{agency_id}.json",
69+
path_template("/api/where/route-ids-for-agency/{agency_id}.json", agency_id=agency_id),
6970
options=make_request_options(
7071
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
7172
),
@@ -119,7 +120,7 @@ async def list(
119120
if not agency_id:
120121
raise ValueError(f"Expected a non-empty value for `agency_id` but received {agency_id!r}")
121122
return await self._get(
122-
f"/api/where/route-ids-for-agency/{agency_id}.json",
123+
path_template("/api/where/route-ids-for-agency/{agency_id}.json", agency_id=agency_id),
123124
options=make_request_options(
124125
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
125126
),

src/onebusaway/resources/routes_for_agency.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import httpx
66

77
from .._types import Body, Query, Headers, NotGiven, not_given
8+
from .._utils import path_template
89
from .._compat import cached_property
910
from .._resource import SyncAPIResource, AsyncAPIResource
1011
from .._response import (
@@ -65,7 +66,7 @@ def list(
6566
if not agency_id:
6667
raise ValueError(f"Expected a non-empty value for `agency_id` but received {agency_id!r}")
6768
return self._get(
68-
f"/api/where/routes-for-agency/{agency_id}.json",
69+
path_template("/api/where/routes-for-agency/{agency_id}.json", agency_id=agency_id),
6970
options=make_request_options(
7071
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
7172
),
@@ -119,7 +120,7 @@ async def list(
119120
if not agency_id:
120121
raise ValueError(f"Expected a non-empty value for `agency_id` but received {agency_id!r}")
121122
return await self._get(
122-
f"/api/where/routes-for-agency/{agency_id}.json",
123+
path_template("/api/where/routes-for-agency/{agency_id}.json", agency_id=agency_id),
123124
options=make_request_options(
124125
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
125126
),

0 commit comments

Comments
 (0)