Skip to content

Commit 60fd487

Browse files
mnriemCopilot
andcommitted
Address review: reuse hardened downloader, refresh events, escape labels, fix bundler call
Responds to review feedback on #3914 and fixes CI (pytest bundler failure). - Extract shared `install_extension_from_url` helper in extensions/_commands.py that reuses the authenticated, redirect-guarded, bounded (50 MiB) download and TOCTOU-safe transient archive used by `extension add --from`. Both `extension add --from` and `specify init --extension <url>` now go through this single downloader instead of a second raw urlopen path. - Refresh native event configuration once after successful extension installs during init (mirrors `_refresh_events_and_warn` in the add path) so an extension declaring `events:` has its hooks activated. - Escape user-controlled extension specs and error text before interpolating them into StepTracker labels (Rich markup injection). - Pass `extensions=None` from bundler's `_run_init` so the init callback no longer receives the typer OptionInfo sentinel ('OptionInfo' object is not iterable), which broke `test_install_initializes_uninitialized_project`. - Add init URL coverage in TestExtensionFlag: non-HTTPS rejection and a successful HTTPS ZIP install with download-cache cleanup assertion. Assisted-by: GitHub Copilot (model: Claude Opus 4.8, autonomous) Co-authored-by: Copilot App <[email protected]>
1 parent 60ea204 commit 60fd487

4 files changed

Lines changed: 246 additions & 128 deletions

File tree

‎src/specify_cli/commands/bundle/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def _run_init(integration: str, *, script_type: str, offline: bool = False) -> N
119119
preset=None,
120120
integration=integration,
121121
integration_options=None,
122+
extensions=None,
122123
)
123124
except typer.Exit as exc:
124125
if exc.exit_code:

‎src/specify_cli/commands/init.py‎

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,23 @@ def _install_extension_during_init(project_path: Path, ext_spec: str, speckit_ve
4141
from urllib.parse import urlparse
4242

4343
from .._assets import _locate_bundled_extension
44-
from ..extensions import ExtensionCatalog, ExtensionManager
45-
from ..extensions._commands import _resolve_catalog_extension
44+
from ..extensions import ExtensionCatalog, ExtensionError, ExtensionManager
45+
from ..extensions._commands import (
46+
_resolve_catalog_extension,
47+
install_extension_from_url,
48+
)
4649

4750
manager = ExtensionManager(project_path)
4851

4952
# --- URL ---
5053
parsed = urlparse(ext_spec)
5154
if parsed.scheme in ("http", "https"):
52-
is_localhost = parsed.hostname in ("localhost", "127.0.0.1", "::1")
53-
if parsed.scheme != "https" and not (parsed.scheme == "http" and is_localhost):
54-
raise ValueError("URL must use HTTPS (HTTP is only allowed for localhost)")
55-
56-
import re as _re
57-
import urllib.error as _urllib_error
58-
import urllib.request
59-
60-
download_dir = project_path / ".specify" / "extensions" / ".cache" / "downloads"
61-
download_dir.mkdir(parents=True, exist_ok=True)
62-
safe_name = _re.sub(r"[^a-z0-9-]", "-", (parsed.path.split("/")[-1] or "download").lower())[:64]
63-
zip_path = download_dir / f"{safe_name}-init-download.zip"
6455
try:
65-
with urllib.request.urlopen(ext_spec, timeout=60) as _resp:
66-
zip_path.write_bytes(_resp.read())
67-
manifest = manager.install_from_zip(zip_path, speckit_version)
68-
except _urllib_error.URLError as exc:
69-
raise ValueError(f"Failed to download from {ext_spec}: {exc}") from exc
70-
finally:
71-
zip_path.unlink(missing_ok=True)
56+
manifest = install_extension_from_url(
57+
manager, project_path, ext_spec, speckit_version
58+
)
59+
except ExtensionError as exc:
60+
raise ValueError(str(exc)) from exc
7261
return f"{manifest.name} v{manifest.version} installed"
7362

7463
# --- Local path ---
@@ -524,8 +513,12 @@ def init(
524513
tracker.add(key, label)
525514

526515
if extensions:
516+
from rich.markup import escape as _escape_markup
517+
527518
for i, ext_spec in enumerate(extensions):
528-
tracker.add(f"extension-{i}", f"Install extension: {ext_spec}")
519+
tracker.add(
520+
f"extension-{i}", f"Install extension: {_escape_markup(ext_spec)}"
521+
)
529522

530523
tracker.add("final", "Finalize")
531524

@@ -740,17 +733,32 @@ def init(
740733

741734
# Install extensions specified via --extension
742735
if extensions:
736+
from rich.markup import escape as _escape_markup
737+
738+
from ..extensions._commands import _refresh_events_and_warn
739+
743740
speckit_ver = get_speckit_version()
741+
any_extension_installed = False
744742
for i, ext_spec in enumerate(extensions):
745743
tracker.start(f"extension-{i}")
746744
try:
747745
status_msg = _install_extension_during_init(
748746
project_path, ext_spec, speckit_ver
749747
)
750748
tracker.complete(f"extension-{i}", status_msg)
749+
any_extension_installed = True
751750
except Exception as ext_err:
752751
sanitized_ext = str(ext_err).replace("\n", " ").strip()
753-
tracker.error(f"extension-{i}", f"failed: {sanitized_ext[:120]}")
752+
tracker.error(
753+
f"extension-{i}",
754+
f"failed: {_escape_markup(sanitized_ext[:120])}",
755+
)
756+
757+
# Refresh native event configuration once after the batch so
758+
# that an extension declaring ``events:`` has its hooks
759+
# activated, mirroring the ``extension add`` path.
760+
if any_extension_installed:
761+
_refresh_events_and_warn(project_path)
754762

755763
# Seed the constitution AFTER preset installation so that a
756764
# preset-provided constitution-template (resolved via the

‎src/specify_cli/extensions/_commands.py‎

Lines changed: 123 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,118 @@ def _refresh_events_and_warn(project_root: Path) -> None:
9797
console.print(f" {key}: {_escape_markup(detail)}")
9898

9999

100+
def install_extension_from_url(
101+
manager,
102+
project_root: Path,
103+
url: str,
104+
speckit_version: str,
105+
*,
106+
priority: int = 10,
107+
force: bool = False,
108+
):
109+
"""Download a ZIP from *url* and install it, reusing the hardened path.
110+
111+
Shares the same download hardening as ``extension add --from``:
112+
HTTPS enforcement, the catalog's authenticated + redirect-guarded
113+
``_open_url`` fetch, a bounded (50 MiB) response read, and a TOCTOU-safe
114+
transient download file consumed directly by ``install_from_zip``.
115+
116+
Returns the installed manifest. Raises ``ExtensionError`` on any failure so
117+
callers can present a uniform message without a second downloader.
118+
"""
119+
import io
120+
import urllib.error
121+
122+
from . import ExtensionCatalog, ExtensionError
123+
124+
if not is_https_or_localhost_http(url):
125+
raise ExtensionError(
126+
"URL must use HTTPS (HTTP is only allowed for localhost)"
127+
)
128+
129+
download_dir = _validate_safe_cache_dir(project_root)
130+
zip_filename = f"extension-url-download-{uuid4().hex}.zip"
131+
# Only used for diagnostic messages: the real archive is a transient inode
132+
# (unlinked on POSIX, O_TEMPORARY on Windows) consumed via ``archive_file``
133+
# below, so this path is never opened again.
134+
zip_path = download_dir / zip_filename
135+
136+
try:
137+
dl_catalog = ExtensionCatalog(project_root)
138+
download_url = url
139+
extra_headers = None
140+
resolved_url = dl_catalog._resolve_github_release_asset_api_url(download_url)
141+
if resolved_url:
142+
download_url = resolved_url
143+
extra_headers = {"Accept": "application/octet-stream"}
144+
145+
with dl_catalog._open_url(
146+
download_url, timeout=60, extra_headers=extra_headers
147+
) as response:
148+
zip_data = read_response_limited(
149+
response,
150+
error_type=ExtensionError,
151+
label=f"extension {url}",
152+
)
153+
except urllib.error.URLError as exc:
154+
raise ExtensionError(f"Failed to download from {url}: {exc}") from exc
155+
156+
if not zipfile.is_zipfile(io.BytesIO(zip_data)):
157+
raise ExtensionError(
158+
f"{url} did not return a ZIP archive (got {len(zip_data)} bytes). "
159+
"This usually means the request was not authenticated and a login/HTML "
160+
"page was returned. Verify the URL is correct and that credentials for "
161+
"its host are configured in ~/.specify/auth.json."
162+
)
163+
164+
download_fd = -1
165+
download_file = None
166+
try:
167+
try:
168+
download_fd = _safe_open_download_zip(
169+
project_root, download_dir, zip_filename
170+
)
171+
except OSError as exc:
172+
raise ExtensionError(
173+
f"Could not safely create download file: {exc}"
174+
) from exc
175+
176+
try:
177+
download_file = os.fdopen(download_fd, "w+b")
178+
download_fd = -1
179+
download_file.write(zip_data)
180+
download_file.flush()
181+
download_file.seek(0)
182+
except OSError as exc:
183+
raise ExtensionError(
184+
f"Could not safely write download file: {exc}"
185+
) from exc
186+
187+
try:
188+
return manager.install_from_zip(
189+
zip_path,
190+
speckit_version,
191+
priority=priority,
192+
force=force,
193+
archive_file=download_file,
194+
)
195+
except OSError as exc:
196+
raise ExtensionError(
197+
f"Could not install extension from downloaded archive: {exc}"
198+
) from exc
199+
finally:
200+
if download_file is not None:
201+
try:
202+
download_file.close()
203+
except OSError:
204+
pass
205+
elif download_fd >= 0:
206+
try:
207+
os.close(download_fd)
208+
except OSError:
209+
pass
210+
211+
100212
def _load_catalog_command_config(project_root: Path, config_path: Path) -> dict:
101213
"""Load extension catalog CLI config with user-facing shape errors."""
102214
try:
@@ -812,112 +924,18 @@ def extension_add(
812924
)
813925

814926
elif from_url:
815-
# Install from URL (ZIP file)
816-
import io
817-
import urllib.error
818-
927+
# Install from URL (ZIP file) via the shared hardened
928+
# downloader (HTTPS enforcement, authenticated redirect-guarded
929+
# fetch, bounded read, TOCTOU-safe transient archive).
819930
console.print(f"Downloading from {safe_url}...")
820-
821-
download_dir = _validate_safe_cache_dir(project_root)
822-
zip_filename = f"extension-url-download-{uuid4().hex}.zip"
823-
# Only used for diagnostic messages: the real archive is a
824-
# transient inode (unlinked on POSIX, O_TEMPORARY on Windows)
825-
# consumed via ``archive_file`` below, so this path is never
826-
# opened again.
827-
zip_path = download_dir / zip_filename
828-
829-
try:
830-
# Use the catalog's authenticated fetch so configured
831-
# credentials (incl. GitHub Enterprise Server) are applied
832-
# and GHES release-asset URLs resolve via /api/v3 — keeping
833-
# --from consistent with catalog-based installs.
834-
dl_catalog = ExtensionCatalog(project_root)
835-
download_url = from_url
836-
extra_headers = None
837-
resolved_url = dl_catalog._resolve_github_release_asset_api_url(download_url)
838-
if resolved_url:
839-
download_url = resolved_url
840-
extra_headers = {"Accept": "application/octet-stream"}
841-
842-
with dl_catalog._open_url(
843-
download_url, timeout=60, extra_headers=extra_headers
844-
) as response:
845-
zip_data = read_response_limited(
846-
response,
847-
error_type=ExtensionError,
848-
label=f"extension {from_url}",
849-
)
850-
851-
if not zipfile.is_zipfile(io.BytesIO(zip_data)):
852-
console.print(
853-
f"[red]Error:[/red] {safe_url} did not return a ZIP archive "
854-
f"(got {len(zip_data)} bytes). This usually means the request "
855-
f"was not authenticated and a login/HTML page was returned. "
856-
f"Verify the URL is correct and that credentials for its host "
857-
f"are configured in ~/.specify/auth.json."
858-
)
859-
raise typer.Exit(1)
860-
861-
download_fd = -1
862-
download_file = None
863-
try:
864-
try:
865-
download_fd = _safe_open_download_zip(
866-
project_root, download_dir, zip_filename
867-
)
868-
except OSError as exc:
869-
console.print(
870-
"[red]Error:[/red] Could not safely create download file: "
871-
f"{_escape_markup(str(exc))}"
872-
)
873-
raise typer.Exit(1)
874-
875-
try:
876-
download_file = os.fdopen(download_fd, "w+b")
877-
download_fd = -1
878-
download_file.write(zip_data)
879-
download_file.flush()
880-
download_file.seek(0)
881-
except OSError as exc:
882-
console.print(
883-
"[red]Error:[/red] Could not safely write download file: "
884-
f"{_escape_markup(str(exc))}"
885-
)
886-
raise typer.Exit(1)
887-
888-
# Consume the transient inode reserved above rather
889-
# than reopening the cache pathname during extraction.
890-
try:
891-
manifest = manager.install_from_zip(
892-
zip_path,
893-
speckit_version,
894-
priority=priority,
895-
force=force,
896-
archive_file=download_file,
897-
)
898-
except OSError as exc:
899-
console.print(
900-
"[red]Error:[/red] Could not install extension from downloaded archive: "
901-
f"{_escape_markup(str(exc))}"
902-
)
903-
raise typer.Exit(1)
904-
finally:
905-
if download_file is not None:
906-
try:
907-
download_file.close()
908-
except OSError:
909-
pass
910-
elif download_fd >= 0:
911-
try:
912-
os.close(download_fd)
913-
except OSError:
914-
pass
915-
except urllib.error.URLError as e:
916-
console.print(
917-
f"[red]Error:[/red] Failed to download from {safe_url}: "
918-
f"{_escape_markup(str(e))}"
919-
)
920-
raise typer.Exit(1)
931+
manifest = install_extension_from_url(
932+
manager,
933+
project_root,
934+
from_url,
935+
speckit_version,
936+
priority=priority,
937+
force=force,
938+
)
921939

922940
else:
923941
# Try bundled extensions first (shipped with spec-kit)

0 commit comments

Comments
 (0)