|
| 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