|
17 | 17 | from ofrak import Analyzer, Unpacker, Modifier, Packer |
18 | 18 | from ofrak.core import File |
19 | 19 | from ofrak.core.entropy import DataSummaryAnalyzer |
20 | | -from ofrak.gui.server import AiohttpOFRAKServer, start_server |
| 20 | +from ofrak.gui.server import ( |
| 21 | + AiohttpOFRAKServer, |
| 22 | + start_server, |
| 23 | + validate_safe_directory_path, |
| 24 | + sanitize_repo_name, |
| 25 | +) |
21 | 26 | from ofrak.model.component_filters import ComponentOrMetaFilter, ComponentTypeFilter |
22 | 27 | from ofrak.service.serialization.pjson import ( |
23 | 28 | PJSONSerializationService, |
@@ -2169,3 +2174,192 @@ async def test_get_project_by_resource_id(ofrak_client: TestClient, test_project |
2169 | 2174 | project_resp = await ofrak_client.get(f"/{resource_id}/get_project_by_resource_id") |
2170 | 2175 | project = await project_resp.json() |
2171 | 2176 | assert project["session_id"] == id |
| 2177 | + |
| 2178 | + |
| 2179 | +def test_validate_safe_directory_path_valid_relative(tmpdir): |
| 2180 | + """ |
| 2181 | + Test that valid relative paths are properly resolved within the base directory |
| 2182 | +
|
| 2183 | + This test verifies that: |
| 2184 | + - Relative paths are converted to absolute paths |
| 2185 | + - The resolved path stays within the base directory |
| 2186 | + - Path normalization works correctly |
| 2187 | + """ |
| 2188 | + base_dir = str(tmpdir) |
| 2189 | + result = validate_safe_directory_path(base_dir, "myproject") |
| 2190 | + expected = os.path.join(os.path.realpath(base_dir), "myproject") |
| 2191 | + assert result == expected |
| 2192 | + |
| 2193 | + |
| 2194 | +def test_validate_safe_directory_path_valid_nested(tmpdir): |
| 2195 | + """ |
| 2196 | + Test that nested relative paths are allowed within the base directory |
| 2197 | +
|
| 2198 | + This test verifies that: |
| 2199 | + - Multi-level relative paths are properly resolved |
| 2200 | + - Nested directories remain within the base directory boundary |
| 2201 | + """ |
| 2202 | + base_dir = str(tmpdir) |
| 2203 | + result = validate_safe_directory_path(base_dir, "foo/bar/baz") |
| 2204 | + expected = os.path.join(os.path.realpath(base_dir), "foo/bar/baz") |
| 2205 | + assert result == expected |
| 2206 | + |
| 2207 | + |
| 2208 | +def test_validate_safe_directory_path_traversal_attack(tmpdir): |
| 2209 | + """ |
| 2210 | + Test that directory traversal attacks are blocked |
| 2211 | +
|
| 2212 | + This test verifies that: |
| 2213 | + - Path traversal attempts using ../ are detected |
| 2214 | + - A ValueError is raised with appropriate error message |
| 2215 | + - Attackers cannot escape the base directory |
| 2216 | + """ |
| 2217 | + base_dir = str(tmpdir) |
| 2218 | + with pytest.raises(ValueError, match="Path traversal attempt detected"): |
| 2219 | + validate_safe_directory_path(base_dir, "../../../etc/passwd") |
| 2220 | + |
| 2221 | + |
| 2222 | +def test_validate_safe_directory_path_absolute_escape(tmpdir): |
| 2223 | + """ |
| 2224 | + Test that absolute paths outside the base directory are rejected |
| 2225 | +
|
| 2226 | + This test verifies that: |
| 2227 | + - Absolute paths pointing outside base directory are blocked |
| 2228 | + - A ValueError is raised for such attempts |
| 2229 | + """ |
| 2230 | + base_dir = str(tmpdir) |
| 2231 | + with pytest.raises(ValueError, match="Path traversal attempt detected"): |
| 2232 | + validate_safe_directory_path(base_dir, "/etc/passwd") |
| 2233 | + |
| 2234 | + |
| 2235 | +def test_validate_safe_directory_path_symlink_escape(tmpdir): |
| 2236 | + """ |
| 2237 | + Test that symlink-based directory escapes are caught |
| 2238 | +
|
| 2239 | + This test verifies that: |
| 2240 | + - Symlinks pointing outside the base directory are detected |
| 2241 | + - Path resolution follows symlinks to detect escapes |
| 2242 | + """ |
| 2243 | + base_dir = str(tmpdir) |
| 2244 | + symlink_path = tmpdir.join("evil_link") |
| 2245 | + os.symlink("/etc", str(symlink_path)) |
| 2246 | + |
| 2247 | + with pytest.raises(ValueError, match="Path traversal attempt detected"): |
| 2248 | + validate_safe_directory_path(base_dir, "evil_link") |
| 2249 | + |
| 2250 | + |
| 2251 | +def test_validate_safe_directory_path_same_as_base(tmpdir): |
| 2252 | + """ |
| 2253 | + Test that current directory reference returns the base directory |
| 2254 | +
|
| 2255 | + This test verifies that: |
| 2256 | + - Using "." as the path returns the base directory itself |
| 2257 | + - This is treated as a valid case (staying within bounds) |
| 2258 | + """ |
| 2259 | + base_dir = str(tmpdir) |
| 2260 | + result = validate_safe_directory_path(base_dir, ".") |
| 2261 | + expected = os.path.realpath(base_dir) |
| 2262 | + assert result == expected |
| 2263 | + |
| 2264 | + |
| 2265 | +def test_validate_safe_directory_path_double_dots_in_middle(tmpdir): |
| 2266 | + """ |
| 2267 | + Test that complex path traversal attempts are handled correctly |
| 2268 | +
|
| 2269 | + This test verifies that: |
| 2270 | + - Paths with .. in the middle are normalized |
| 2271 | + - The final normalized path must still be within base directory |
| 2272 | + """ |
| 2273 | + base_dir = str(tmpdir) |
| 2274 | + # This should fail because it attempts to escape |
| 2275 | + with pytest.raises(ValueError, match="Path traversal attempt detected"): |
| 2276 | + validate_safe_directory_path(base_dir, "foo/../../bar") |
| 2277 | + |
| 2278 | + |
| 2279 | +def test_sanitize_repo_name_https_url(): |
| 2280 | + """ |
| 2281 | + Test that HTTPS git URLs are properly parsed to extract repo name |
| 2282 | +
|
| 2283 | + This test verifies that: |
| 2284 | + - HTTPS URLs are parsed correctly |
| 2285 | + - The .git extension is removed |
| 2286 | + - Only the repository name is returned |
| 2287 | + """ |
| 2288 | + result = sanitize_repo_name("https://github.com/redballoonsecurity/ofrak.git") |
| 2289 | + assert result == "ofrak" |
| 2290 | + |
| 2291 | + |
| 2292 | +def test_sanitize_repo_name_ssh_url(): |
| 2293 | + """ |
| 2294 | + Test that SSH git URLs are properly parsed to extract repo name |
| 2295 | +
|
| 2296 | + This test verifies that: |
| 2297 | + - SSH-style URLs (git@host:path) are parsed correctly |
| 2298 | + - The repository name is extracted from the path |
| 2299 | + """ |
| 2300 | + result = sanitize_repo_name("git@github.com:redballoonsecurity/ofrak.git") |
| 2301 | + assert result == "ofrak" |
| 2302 | + |
| 2303 | + |
| 2304 | +def test_sanitize_repo_name_no_git_extension(): |
| 2305 | + """ |
| 2306 | + Test that URLs without .git extension work correctly |
| 2307 | +
|
| 2308 | + This test verifies that: |
| 2309 | + - URLs without the .git suffix are handled |
| 2310 | + - The repository name is still extracted correctly |
| 2311 | + """ |
| 2312 | + result = sanitize_repo_name("https://github.com/redballoonsecurity/ofrak") |
| 2313 | + assert result == "ofrak" |
| 2314 | + |
| 2315 | + |
| 2316 | +def test_sanitize_repo_name_special_characters(): |
| 2317 | + """ |
| 2318 | + Test that special characters are removed by secure_filename |
| 2319 | +
|
| 2320 | + This test verifies that: |
| 2321 | + - Path traversal characters in repo names are sanitized |
| 2322 | + - The result is safe for use as a directory name |
| 2323 | + """ |
| 2324 | + result = sanitize_repo_name("https://github.com/user/../malicious/repo.git") |
| 2325 | + # secure_filename should remove or replace dangerous characters |
| 2326 | + assert ".." not in result |
| 2327 | + assert "/" not in result |
| 2328 | + |
| 2329 | + |
| 2330 | +def test_sanitize_repo_name_empty_result(): |
| 2331 | + """ |
| 2332 | + Test that invalid URLs that produce empty names are rejected |
| 2333 | +
|
| 2334 | + This test verifies that: |
| 2335 | + - URLs that result in empty repo names raise ValueError |
| 2336 | + - The error message indicates an invalid repository URL |
| 2337 | + """ |
| 2338 | + with pytest.raises(ValueError, match="Invalid repository URL"): |
| 2339 | + sanitize_repo_name("https://github.com/user/") |
| 2340 | + |
| 2341 | + |
| 2342 | +def test_sanitize_repo_name_only_special_chars(): |
| 2343 | + """ |
| 2344 | + Test that URLs that sanitize to empty strings are rejected |
| 2345 | +
|
| 2346 | + This test verifies that: |
| 2347 | + - URLs containing only special characters are detected |
| 2348 | + - A ValueError is raised with appropriate message |
| 2349 | + """ |
| 2350 | + with pytest.raises(ValueError, match="Invalid repository URL"): |
| 2351 | + sanitize_repo_name("https://github.com/user/../../../.git") |
| 2352 | + |
| 2353 | + |
| 2354 | +def test_sanitize_repo_name_unicode(): |
| 2355 | + """ |
| 2356 | + Test that unicode characters are handled by secure_filename |
| 2357 | +
|
| 2358 | + This test verifies that: |
| 2359 | + - Unicode characters in repository names are converted to ASCII |
| 2360 | + - The result is safe for filesystem use |
| 2361 | + """ |
| 2362 | + result = sanitize_repo_name("https://github.com/user/repö.git") |
| 2363 | + # secure_filename should convert to ASCII-safe characters |
| 2364 | + assert result.isascii() |
| 2365 | + assert len(result) > 0 |
0 commit comments