1010
1111import os
1212import urllib .request
13+ from fnmatch import fnmatch
1314from typing import Callable , Dict , Optional
1415from 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+
5966def 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 :
0 commit comments