Skip to content

Commit c4aab55

Browse files
committed
fix(input-handler): validate git/download URLs against SSRF and add zip-slip protection
Three security hardening changes to the input handler: 1. SSRF protection: Both git clone and file download now validate the target hostname against an allowlist of known-safe hosts (GitHub, GitLab, Bitbucket, HuggingFace). URLs resolving to private/loopback IPs are blocked regardless of hostname. 2. Zip-slip prevention: Archive extraction now validates every member path against the extraction directory to prevent path traversal attacks (e.g., ../../etc/passwd entries). 3. Configurable allowlists: ALLOWED_GIT_HOSTS and ALLOWED_DOWNLOAD_HOSTS are module-level constants that can be extended for enterprise deployments with internal forges. Fixes NVIDIA#147
1 parent b1586b7 commit c4aab55

2 files changed

Lines changed: 253 additions & 3 deletions

File tree

‎src/skillspector/input_handler.py‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828

2929
from __future__ import annotations
3030

31+
import ipaddress
3132
import shutil
33+
import socket
3234
import subprocess
3335
import tempfile
3436
import zipfile
@@ -41,6 +43,38 @@
4143

4244
logger = get_logger(__name__)
4345

46+
ALLOWED_GIT_HOSTS = frozenset({
47+
"github.com",
48+
"gitlab.com",
49+
"bitbucket.org",
50+
})
51+
52+
ALLOWED_DOWNLOAD_HOSTS = frozenset({
53+
"github.com",
54+
"raw.githubusercontent.com",
55+
"gitlab.com",
56+
"bitbucket.org",
57+
"huggingface.co",
58+
})
59+
60+
61+
def _is_private_ip(host: str) -> bool:
62+
"""Return True if host resolves to a private/reserved IP address."""
63+
try:
64+
addr = ipaddress.ip_address(host)
65+
return addr.is_private or addr.is_loopback or addr.is_link_local or addr.is_reserved
66+
except ValueError:
67+
pass
68+
try:
69+
resolved = socket.getaddrinfo(host, None, socket.AF_UNSPEC, socket.SOCK_STREAM)
70+
for _, _, _, _, sockaddr in resolved:
71+
addr = ipaddress.ip_address(sockaddr[0])
72+
if addr.is_private or addr.is_loopback or addr.is_link_local or addr.is_reserved:
73+
return True
74+
except (socket.gaierror, OSError):
75+
return True
76+
return False
77+
4478

4579
class InputHandler:
4680
"""
@@ -107,8 +141,8 @@ def _is_git_url(self, path: str) -> bool:
107141
if not path.startswith(("http://", "https://", "git@")):
108142
return False
109143
parsed = urlparse(path)
110-
git_hosts = ["github.com", "gitlab.com", "bitbucket.org"]
111-
if any(host in parsed.netloc for host in git_hosts):
144+
host = parsed.hostname or ""
145+
if any(allowed in host for allowed in ALLOWED_GIT_HOSTS):
112146
if "/raw/" in path or "/blob/" in path or path.endswith((".md", ".py", ".sh")):
113147
return False
114148
return True
@@ -122,8 +156,30 @@ def _is_file_url(self, path: str) -> bool:
122156
return False
123157
return not self._is_git_url(path)
124158

159+
def _validate_url_host(self, url: str, allowed_hosts: frozenset[str]) -> str:
160+
"""Validate URL host against allowlist and SSRF protections.
161+
162+
Returns the hostname on success, raises ValueError on blocked URLs.
163+
"""
164+
parsed = urlparse(url)
165+
host = parsed.hostname or ""
166+
if not host:
167+
raise ValueError(f"URL has no valid hostname: {url}")
168+
if not any(host == allowed or host.endswith("." + allowed) for allowed in allowed_hosts):
169+
raise ValueError(
170+
f"Host '{host}' is not in the allowed hosts list. "
171+
f"Allowed: {sorted(allowed_hosts)}"
172+
)
173+
if _is_private_ip(host):
174+
raise ValueError(
175+
f"URL resolves to a private/internal IP address: {url}. "
176+
"This is blocked to prevent SSRF attacks."
177+
)
178+
return host
179+
125180
def _clone_git(self, url: str) -> Path:
126181
"""Clone a Git repository to a temporary directory."""
182+
self._validate_url_host(url, ALLOWED_GIT_HOSTS)
127183
temp_dir = self._get_temp_dir()
128184
clone_dir = temp_dir / "repo"
129185
try:
@@ -149,6 +205,7 @@ def _clone_git(self, url: str) -> Path:
149205

150206
def _download_file(self, url: str) -> Path:
151207
"""Download a file from URL to a temporary directory."""
208+
self._validate_url_host(url, ALLOWED_DOWNLOAD_HOSTS)
152209
temp_dir = self._get_temp_dir()
153210
parsed = urlparse(url)
154211
filename = Path(parsed.path).name or "SKILL.md"
@@ -171,14 +228,21 @@ def _download_file(self, url: str) -> Path:
171228
return temp_dir
172229

173230
def _extract_zip(self, zip_path: Path) -> Path:
174-
"""Extract a zip file to a temporary directory."""
231+
"""Extract a zip file to a temporary directory with path traversal protection."""
175232
if not zip_path.exists():
176233
raise FileNotFoundError(f"Zip file not found: {zip_path}") from None
177234
temp_dir = self._get_temp_dir()
178235
extract_dir = temp_dir / "extracted"
179236
extract_dir.mkdir(exist_ok=True)
180237
try:
181238
with zipfile.ZipFile(zip_path, "r") as zf:
239+
for member in zf.namelist():
240+
member_path = (extract_dir / member).resolve()
241+
if not str(member_path).startswith(str(extract_dir.resolve())):
242+
raise ValueError(
243+
f"Zip entry '{member}' would escape extraction directory (zip-slip). "
244+
"Archive is potentially malicious."
245+
)
182246
zf.extractall(extract_dir)
183247
except zipfile.BadZipFile:
184248
logger.warning("Invalid zip or extract failed: %s", zip_path)
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""Tests for SSRF protection and zip-slip prevention in input_handler."""
17+
18+
from __future__ import annotations
19+
20+
import zipfile
21+
from pathlib import Path
22+
from unittest.mock import patch
23+
24+
import pytest
25+
26+
from skillspector.input_handler import (
27+
ALLOWED_DOWNLOAD_HOSTS,
28+
ALLOWED_GIT_HOSTS,
29+
InputHandler,
30+
_is_private_ip,
31+
)
32+
33+
34+
class TestPrivateIPDetection:
35+
"""_is_private_ip blocks internal network addresses."""
36+
37+
def test_localhost_blocked(self) -> None:
38+
assert _is_private_ip("127.0.0.1") is True
39+
40+
def test_ipv6_loopback_blocked(self) -> None:
41+
assert _is_private_ip("::1") is True
42+
43+
def test_private_10_range_blocked(self) -> None:
44+
assert _is_private_ip("10.0.0.1") is True
45+
46+
def test_private_172_range_blocked(self) -> None:
47+
assert _is_private_ip("172.16.0.1") is True
48+
49+
def test_private_192_range_blocked(self) -> None:
50+
assert _is_private_ip("192.168.1.1") is True
51+
52+
def test_link_local_169_blocked(self) -> None:
53+
assert _is_private_ip("169.254.169.254") is True
54+
55+
def test_public_ip_allowed(self) -> None:
56+
assert _is_private_ip("140.82.121.3") is False
57+
58+
def test_unresolvable_host_blocked(self) -> None:
59+
assert _is_private_ip("definitely-not-a-real-host-xyz123.invalid") is True
60+
61+
62+
class TestGitCloneSSRF:
63+
"""Git clone validates URLs against allowlist and SSRF."""
64+
65+
def test_internal_git_url_blocked(self) -> None:
66+
handler = InputHandler()
67+
with pytest.raises(ValueError, match="not in the allowed hosts"):
68+
handler._clone_git("https://internal-gitlab.corp.local/repo.git")
69+
handler.cleanup()
70+
71+
def test_localhost_git_url_blocked(self) -> None:
72+
handler = InputHandler()
73+
with pytest.raises(ValueError, match="not in the allowed hosts"):
74+
handler._clone_git("http://127.0.0.1:8080/repo.git")
75+
handler.cleanup()
76+
77+
def test_metadata_endpoint_blocked(self) -> None:
78+
handler = InputHandler()
79+
with pytest.raises(ValueError, match="not in the allowed hosts"):
80+
handler._clone_git("http://169.254.169.254/latest/meta-data/")
81+
handler.cleanup()
82+
83+
@patch("skillspector.input_handler.subprocess.run")
84+
def test_github_url_allowed(self, mock_run) -> None:
85+
mock_run.return_value = None
86+
handler = InputHandler()
87+
handler._clone_git("https://github.com/NVIDIA/SkillSpector.git")
88+
mock_run.assert_called_once()
89+
handler.cleanup()
90+
91+
@patch("skillspector.input_handler.subprocess.run")
92+
def test_gitlab_url_allowed(self, mock_run) -> None:
93+
mock_run.return_value = None
94+
handler = InputHandler()
95+
handler._clone_git("https://gitlab.com/user/repo.git")
96+
mock_run.assert_called_once()
97+
handler.cleanup()
98+
99+
100+
class TestDownloadSSRF:
101+
"""File download validates URLs against allowlist and SSRF."""
102+
103+
def test_internal_url_blocked(self) -> None:
104+
handler = InputHandler()
105+
with pytest.raises(ValueError, match="not in the allowed hosts"):
106+
handler._download_file("http://192.168.1.100/secrets.txt")
107+
handler.cleanup()
108+
109+
def test_cloud_metadata_blocked(self) -> None:
110+
handler = InputHandler()
111+
with pytest.raises(ValueError, match="not in the allowed hosts"):
112+
handler._download_file("http://169.254.169.254/latest/meta-data/iam/")
113+
handler.cleanup()
114+
115+
def test_arbitrary_host_blocked(self) -> None:
116+
handler = InputHandler()
117+
with pytest.raises(ValueError, match="not in the allowed hosts"):
118+
handler._download_file("https://evil-attacker.com/payload.md")
119+
handler.cleanup()
120+
121+
@patch("skillspector.input_handler.httpx.Client")
122+
def test_raw_githubusercontent_allowed(self, mock_client_cls) -> None:
123+
mock_client = mock_client_cls.return_value.__enter__.return_value
124+
mock_response = mock_client.get.return_value
125+
mock_response.content = b"# SKILL.md content"
126+
mock_response.headers = {}
127+
handler = InputHandler()
128+
result = handler._download_file(
129+
"https://raw.githubusercontent.com/NVIDIA/SkillSpector/main/SKILL.md"
130+
)
131+
assert result.is_dir()
132+
handler.cleanup()
133+
134+
135+
class TestZipSlipPrevention:
136+
"""Zip extraction blocks path traversal attacks."""
137+
138+
def test_zip_slip_blocked(self, tmp_path: Path) -> None:
139+
zip_path = tmp_path / "malicious.zip"
140+
with zipfile.ZipFile(zip_path, "w") as zf:
141+
zf.writestr("../../etc/passwd", "root:x:0:0:")
142+
handler = InputHandler()
143+
handler._temp_dir = tmp_path / "work"
144+
handler._temp_dir.mkdir()
145+
with pytest.raises(ValueError, match="zip-slip"):
146+
handler._extract_zip(zip_path)
147+
handler.cleanup()
148+
149+
def test_normal_zip_extracts_fine(self, tmp_path: Path) -> None:
150+
zip_path = tmp_path / "normal.zip"
151+
with zipfile.ZipFile(zip_path, "w") as zf:
152+
zf.writestr("skill/SKILL.md", "# Normal skill")
153+
zf.writestr("skill/tool.py", "print('hello')")
154+
handler = InputHandler()
155+
handler._temp_dir = tmp_path / "work"
156+
handler._temp_dir.mkdir()
157+
result = handler._extract_zip(zip_path)
158+
assert result.is_dir()
159+
assert (result / "SKILL.md").exists()
160+
handler.cleanup()
161+
162+
def test_deeply_nested_path_allowed(self, tmp_path: Path) -> None:
163+
zip_path = tmp_path / "deep.zip"
164+
with zipfile.ZipFile(zip_path, "w") as zf:
165+
zf.writestr("a/b/c/d/file.txt", "deep content")
166+
handler = InputHandler()
167+
handler._temp_dir = tmp_path / "work"
168+
handler._temp_dir.mkdir()
169+
result = handler._extract_zip(zip_path)
170+
assert result.is_dir()
171+
handler.cleanup()
172+
173+
174+
class TestAllowlistConfiguration:
175+
"""Allowlists contain expected hosts."""
176+
177+
def test_git_hosts_include_major_forges(self) -> None:
178+
assert "github.com" in ALLOWED_GIT_HOSTS
179+
assert "gitlab.com" in ALLOWED_GIT_HOSTS
180+
assert "bitbucket.org" in ALLOWED_GIT_HOSTS
181+
182+
def test_download_hosts_include_raw_github(self) -> None:
183+
assert "raw.githubusercontent.com" in ALLOWED_DOWNLOAD_HOSTS
184+
185+
def test_download_hosts_include_huggingface(self) -> None:
186+
assert "huggingface.co" in ALLOWED_DOWNLOAD_HOSTS

0 commit comments

Comments
 (0)