Skip to content

Commit a282da5

Browse files
committed
fix(extensions): resolve GHES release assets via /api/v3
Generalizes resolve_github_release_asset_api_url to GitHub Enterprise Server hosts (gated by auth.json github hosts), fixing private GHES extension/preset downloads. github#3147 Assisted-by: Claude Code (model: claude-sonnet-4-6, autonomous)
1 parent 521aec0 commit a282da5

2 files changed

Lines changed: 144 additions & 31 deletions

File tree

‎src/specify_cli/_github_http.py‎

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import os
1212
import urllib.request
13+
from fnmatch import fnmatch
1314
from typing import Callable, Dict, Optional
1415
from urllib.parse import quote, unquote, urlparse
1516

@@ -56,55 +57,74 @@ def build_github_request(url: str) -> urllib.request.Request:
5657
return urllib.request.Request(url, headers=headers)
5758

5859

60+
def _host_matches(hostname: str, patterns: tuple[str, ...]) -> bool:
61+
"""Return True when *hostname* matches a pattern (exact or ``*.suffix``)."""
62+
hostname = hostname.lower()
63+
return any(p == hostname or fnmatch(hostname, p) for p in patterns)
64+
65+
5966
def resolve_github_release_asset_api_url(
6067
download_url: str,
6168
open_url_fn: Callable,
6269
timeout: int = 60,
70+
github_hosts: tuple[str, ...] = (),
6371
) -> Optional[str]:
64-
"""Resolve a GitHub browser release URL to its REST API asset URL.
65-
66-
For private or SSO-protected repositories, browser release download
67-
URLs (``https://github.com/<owner>/<repo>/releases/download/<tag>/<asset>``)
68-
redirect to an HTML/SSO page instead of delivering the file. This
69-
helper resolves such a URL to the matching GitHub REST API asset URL
70-
(``https://api.github.com/repos/…/releases/assets/<id>``), which can
71-
then be downloaded with ``Accept: application/octet-stream`` and an
72-
auth token to retrieve the actual file payload.
73-
74-
If *download_url* is already a REST API asset URL, it is returned
75-
as-is. Non-GitHub URLs and GitHub URLs that are not release-download
76-
URLs return ``None``. If the API lookup fails (e.g. network error or
77-
asset not found), ``None`` is returned so callers can fall back to the
78-
original URL.
72+
"""Resolve a GitHub release browser-download URL to its REST API asset URL.
73+
74+
Works for public ``github.com`` and for GitHub Enterprise Server (GHES)
75+
hosts. A host is treated as GHES when it matches one of *github_hosts*
76+
(exact hostname or ``*.suffix``) — supply the hosts the user has trusted
77+
under a ``github`` provider in ``auth.json``. This allowlist is the
78+
security gate: unlisted hosts never receive GHES API treatment, so a
79+
malicious catalog cannot induce an API request to an arbitrary host.
80+
81+
For a public URL the API base is ``https://api.github.com``; for a GHES
82+
host it is ``{scheme}://{host[:port]}/api/v3``. Returns the API asset URL
83+
(downloadable with ``Accept: application/octet-stream`` + a token), the
84+
input unchanged if it is already an API asset URL, or ``None`` when the
85+
URL is not a resolvable GitHub release download or the lookup fails.
7986
8087
Args:
8188
download_url: The URL to resolve.
8289
open_url_fn: A callable compatible with
83-
``specify_cli.authentication.http.open_url`` used to make the
84-
authenticated API request.
90+
``specify_cli.authentication.http.open_url`` used for the
91+
authenticated release-metadata lookup.
8592
timeout: Per-request timeout in seconds.
86-
87-
Returns:
88-
The resolved REST API asset URL, or ``None`` if resolution is not
89-
applicable or fails.
93+
github_hosts: Host patterns to treat as GitHub Enterprise Server.
9094
"""
9195
import json
9296
import urllib.error
9397

9498
parsed = urlparse(download_url)
99+
hostname = (parsed.hostname or "").lower()
95100
parts = [unquote(part) for part in parsed.path.strip("/").split("/")]
96101

97-
# Already a REST API asset URL — use it directly
98-
if (
99-
parsed.hostname == "api.github.com"
100-
and len(parts) >= 6
101-
and parts[:1] == ["repos"]
102-
and parts[3:5] == ["releases", "assets"]
103-
):
102+
is_ghes = (
103+
bool(hostname)
104+
and hostname not in GITHUB_HOSTS
105+
and _host_matches(hostname, github_hosts)
106+
)
107+
108+
def _is_asset_path(segments: list[str]) -> bool:
109+
return (
110+
len(segments) >= 6
111+
and segments[:1] == ["repos"]
112+
and segments[3:5] == ["releases", "assets"]
113+
)
114+
115+
# Already a REST API asset URL — use it directly.
116+
if hostname == "api.github.com" and _is_asset_path(parts):
117+
return download_url
118+
if is_ghes and parts[:2] == ["api", "v3"] and _is_asset_path(parts[2:]):
104119
return download_url
105120

106-
# Only handle github.com browser release download URLs
107-
if parsed.hostname != "github.com":
121+
# Determine the REST API base for browser release-download URLs.
122+
if hostname == "github.com":
123+
api_base = "https://api.github.com"
124+
elif is_ghes:
125+
authority = hostname if parsed.port is None else f"{hostname}:{parsed.port}"
126+
api_base = f"{parsed.scheme}://{authority}/api/v3"
127+
else:
108128
return None
109129

110130
# Expecting /<owner>/<repo>/releases/download/<tag>/<asset>
@@ -114,7 +134,7 @@ def resolve_github_release_asset_api_url(
114134
owner, repo, tag = parts[0], parts[1], parts[4]
115135
asset_name = "/".join(parts[5:])
116136
encoded_tag = quote(tag, safe="")
117-
release_url = f"https://api.github.com/repos/{owner}/{repo}/releases/tags/{encoded_tag}"
137+
release_url = f"{api_base}/repos/{owner}/{repo}/releases/tags/{encoded_tag}"
118138

119139
try:
120140
with open_url_fn(release_url, timeout=timeout) as response:

‎tests/test_github_http.py‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,96 @@ def capturing_open(url, timeout=None, extra_headers=None):
188188
)
189189
assert len(captured_urls) == 1
190190
assert "releases/tags/v1%23beta" in captured_urls[0]
191+
192+
# --- GHES (GitHub Enterprise Server) ---
193+
194+
def test_resolves_ghes_browser_url_to_api_url(self):
195+
"""A GHES browser release URL resolves to the /api/v3 asset URL."""
196+
release_json = {
197+
"assets": [
198+
{"name": "ext.zip",
199+
"url": "https://ghes.example/api/v3/repos/o/r/releases/assets/7"}
200+
]
201+
}
202+
result = resolve_github_release_asset_api_url(
203+
"https://ghes.example/o/r/releases/download/v1/ext.zip",
204+
self._make_open_url_fn(release_json),
205+
github_hosts=("ghes.example",),
206+
)
207+
assert result == "https://ghes.example/api/v3/repos/o/r/releases/assets/7"
208+
209+
def test_passthrough_for_existing_ghes_api_asset_url(self):
210+
"""An already-resolved GHES /api/v3 asset URL is returned as-is."""
211+
url = "https://ghes.example/api/v3/repos/o/r/releases/assets/7"
212+
result = resolve_github_release_asset_api_url(
213+
url, lambda *a, **kw: None, github_hosts=("ghes.example",)
214+
)
215+
assert result == url
216+
217+
def test_returns_none_for_ghes_host_not_in_allowlist(self):
218+
"""Unlisted hosts get no GHES treatment and trigger no API call (anti-SSRF)."""
219+
called = []
220+
221+
@contextmanager
222+
def recording_open(url, timeout=None, extra_headers=None):
223+
called.append(url)
224+
resp = MagicMock()
225+
resp.read.return_value = b"{}"
226+
yield resp
227+
228+
result = resolve_github_release_asset_api_url(
229+
"https://ghes.example/o/r/releases/download/v1/ext.zip",
230+
recording_open,
231+
github_hosts=("other.example",),
232+
)
233+
assert result is None
234+
assert called == []
235+
236+
def test_ghes_api_base_preserves_scheme_and_port(self):
237+
"""The GHES API base mirrors the URL scheme and keeps a non-standard port."""
238+
captured = []
239+
240+
@contextmanager
241+
def capturing_open(url, timeout=None, extra_headers=None):
242+
captured.append(url)
243+
resp = MagicMock()
244+
resp.read.return_value = json.dumps({"assets": []}).encode()
245+
yield resp
246+
247+
resolve_github_release_asset_api_url(
248+
"http://localhost:8000/o/r/releases/download/v1/ext.zip",
249+
capturing_open,
250+
github_hosts=("localhost",),
251+
)
252+
assert captured == ["http://localhost:8000/api/v3/repos/o/r/releases/tags/v1"]
253+
254+
def test_ghes_wildcard_does_not_match_bare_host(self):
255+
"""A '*.suffix' pattern does not match the bare host (must list it explicitly)."""
256+
result = resolve_github_release_asset_api_url(
257+
"https://ghes.example/o/r/releases/download/v1/ext.zip",
258+
lambda *a, **kw: None,
259+
github_hosts=("*.ghes.example",),
260+
)
261+
assert result is None
262+
263+
def test_public_github_url_unaffected_by_github_hosts(self):
264+
"""Public github.com still resolves via api.github.com even with github_hosts set."""
265+
captured = []
266+
267+
@contextmanager
268+
def capturing_open(url, timeout=None, extra_headers=None):
269+
captured.append(url)
270+
resp = MagicMock()
271+
resp.read.return_value = json.dumps({
272+
"assets": [{"name": "pack.zip",
273+
"url": "https://api.github.com/repos/org/repo/releases/assets/99"}]
274+
}).encode()
275+
yield resp
276+
277+
result = resolve_github_release_asset_api_url(
278+
"https://github.com/org/repo/releases/download/v1.0/pack.zip",
279+
capturing_open,
280+
github_hosts=("ghes.example",),
281+
)
282+
assert result == "https://api.github.com/repos/org/repo/releases/assets/99"
283+
assert captured == ["https://api.github.com/repos/org/repo/releases/tags/v1.0"]

0 commit comments

Comments
 (0)