Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /promptdriven_pdd /tests /server /test_security.py
| import os | |
| import pytest | |
| import fnmatch | |
| from pathlib import Path | |
| from unittest.mock import MagicMock, AsyncMock, patch | |
| from fastapi import FastAPI, HTTPException, Request | |
| from fastapi.security import HTTPAuthorizationCredentials | |
| import z3 | |
| from pdd.server.security import ( | |
| PathValidator, | |
| SecurityError, | |
| configure_cors, | |
| create_token_dependency, | |
| SecurityLoggingMiddleware, | |
| DEFAULT_BLACKLIST | |
| ) | |
| # --- Fixtures --- | |
| def mock_project_root(tmp_path): | |
| """Creates a temporary directory to serve as the project root.""" | |
| root = tmp_path / "project_root" | |
| root.mkdir() | |
| # Create some valid structure | |
| (root / "src").mkdir() | |
| (root / "src" / "main.py").touch() | |
| (root / "public").mkdir() | |
| (root / "public" / "index.html").touch() | |
| # Create some sensitive files for blacklist testing | |
| (root / ".env").touch() | |
| (root / "node_modules").mkdir() | |
| (root / "node_modules" / "pkg").mkdir() | |
| return root | |
| def validator(mock_project_root): | |
| """Returns a PathValidator instance initialized with the mock root.""" | |
| return PathValidator(project_root=mock_project_root) | |
| # --- Unit Tests: PathValidator --- | |
| def test_validator_init_defaults(mock_project_root): | |
| """Test initialization with default blacklist.""" | |
| validator = PathValidator(mock_project_root) | |
| assert validator.project_root == mock_project_root.resolve() | |
| assert validator.blacklist == DEFAULT_BLACKLIST | |
| def test_validator_valid_relative_path(validator, mock_project_root): | |
| """Test validating a safe relative path.""" | |
| path = "src/main.py" | |
| resolved = validator.validate(path) | |
| expected = (mock_project_root / "src" / "main.py").resolve() | |
| assert resolved == expected | |
| def test_validator_valid_absolute_path(validator, mock_project_root): | |
| """Test validating a safe absolute path inside the root.""" | |
| path = (mock_project_root / "public" / "index.html").resolve() | |
| resolved = validator.validate(path) | |
| assert resolved == path | |
| def test_validator_path_traversal_dots(validator, mock_project_root): | |
| """Test that '..' attempts to escape root are caught.""" | |
| # Attempt to go up from root | |
| path = "../outside_file.txt" | |
| with pytest.raises(SecurityError) as excinfo: | |
| validator.validate(path) | |
| assert excinfo.value.code == "PATH_TRAVERSAL" | |
| assert "outside the project root" in excinfo.value.message | |
| def test_validator_path_traversal_absolute(validator, mock_project_root): | |
| """Test that absolute paths outside root are caught.""" | |
| # Use a system path that is definitely outside tmp_path | |
| # On linux/mac /etc/passwd, on windows C:\\Windows | |
| if os.name == 'nt': | |
| path = "C:\\Windows\\System32\\drivers\\etc\\hosts" | |
| else: | |
| path = "/etc/passwd" | |
| with pytest.raises(SecurityError) as excinfo: | |
| validator.validate(path) | |
| assert excinfo.value.code == "PATH_TRAVERSAL" | |
| def test_validator_blacklist_exact_match(validator): | |
| """Test blocking a file that exactly matches a blacklist pattern.""" | |
| path = ".env" | |
| with pytest.raises(SecurityError) as excinfo: | |
| validator.validate(path) | |
| assert excinfo.value.code == "BLACKLISTED_PATH" | |
| assert "blacklisted" in excinfo.value.message | |
| def test_validator_blacklist_wildcard(validator): | |
| """Test blocking a file matching a wildcard pattern.""" | |
| path = "secrets.key" # Matches *.key | |
| with pytest.raises(SecurityError) as excinfo: | |
| validator.validate(path) | |
| assert excinfo.value.code == "BLACKLISTED_PATH" | |
| def test_validator_blacklist_nested_directory(validator): | |
| """Test blocking a file inside a blacklisted directory.""" | |
| # 'node_modules' is in default blacklist | |
| path = "node_modules/pkg/index.js" | |
| with pytest.raises(SecurityError) as excinfo: | |
| validator.validate(path) | |
| assert excinfo.value.code == "BLACKLISTED_PATH" | |
| def test_validator_blacklist_nested_component(validator): | |
| """Test blocking when a blacklisted folder is deep in the path.""" | |
| # 'node_modules' is blacklisted. | |
| # Path: src/node_modules/foo should be blocked because one component matches. | |
| path = "src/node_modules/foo.js" | |
| with pytest.raises(SecurityError) as excinfo: | |
| validator.validate(path) | |
| assert excinfo.value.code == "BLACKLISTED_PATH" | |
| def test_validator_custom_blacklist(mock_project_root): | |
| """Test using a custom blacklist.""" | |
| custom_list = ["*.unsafe"] | |
| validator = PathValidator(mock_project_root, blacklist_patterns=custom_list) | |
| # Should block .unsafe | |
| with pytest.raises(SecurityError): | |
| validator.validate("file.unsafe") | |
| # Should NOT block default .env anymore | |
| # We need to create it or resolve might fail if strict checking was enforced (it isn't in the code) | |
| # But validate returns resolved path. | |
| res = validator.validate(".env") | |
| assert res.name == ".env" | |
| def test_validator_invalid_path_type(validator): | |
| """Test handling of invalid inputs (though type hinting says str/Path).""" | |
| # If we pass something that Path() constructor hates, or causes other errors | |
| # e.g. null bytes in older python versions, or just general exceptions | |
| # We must patch the Path class imported in the module under test. Use the | |
| # bound method globals so this remains stable when other server tests reload | |
| # pdd.server.security during collection. | |
| validate_globals = validator.validate.__func__.__globals__ | |
| with patch.dict(validate_globals, {"Path": MagicMock(side_effect=Exception("Boom"))}): | |
| with pytest.raises(validate_globals["SecurityError"]) as excinfo: | |
| validator.validate("anything") | |
| assert excinfo.value.code == "INVALID_PATH" | |
| # --- Unit Tests: CORS Configuration --- | |
| def test_configure_cors(): | |
| """Test that CORS middleware is added to the app.""" | |
| app = MagicMock(spec=FastAPI) | |
| configure_cors(app) | |
| # Verify add_middleware was called | |
| assert app.add_middleware.called | |
| # Check arguments | |
| call_args = app.add_middleware.call_args | |
| middleware_class = call_args[0][0] | |
| kwargs = call_args[1] | |
| # We can't easily import CORSMiddleware class identity to compare directly if it's mocked differently, | |
| # but we can check the kwargs. | |
| assert "allow_origins" in kwargs | |
| assert "http://localhost:3000" in kwargs["allow_origins"] | |
| assert kwargs["allow_credentials"] is True | |
| assert kwargs["expose_headers"] == ["X-Job-Id", "X-Total-Chunks"] | |
| def test_configure_cors_custom_origins(): | |
| """Test CORS with custom origins.""" | |
| app = MagicMock(spec=FastAPI) | |
| custom_origins = ["https://example.com"] | |
| configure_cors(app, allowed_origins=custom_origins) | |
| call_args = app.add_middleware.call_args | |
| kwargs = call_args[1] | |
| assert kwargs["allow_origins"] == custom_origins | |
| # --- Unit Tests: Token Authentication --- | |
| async def test_token_dependency_no_auth(): | |
| """Test that dependency returns None when no token is configured.""" | |
| dependency = create_token_dependency(token=None) | |
| # When token is None, it shouldn't check credentials | |
| result = await dependency(credentials=None) | |
| assert result is None | |
| async def test_token_dependency_valid(): | |
| """Test valid token authentication.""" | |
| token = "secret-token" | |
| dependency = create_token_dependency(token=token) | |
| creds = HTTPAuthorizationCredentials(scheme="Bearer", credentials="secret-token") | |
| result = await dependency(credentials=creds) | |
| assert result == creds | |
| async def test_token_dependency_invalid(): | |
| """Test invalid token raises 401.""" | |
| token = "secret-token" | |
| dependency = create_token_dependency(token=token) | |
| creds = HTTPAuthorizationCredentials(scheme="Bearer", credentials="wrong-token") | |
| with pytest.raises(HTTPException) as excinfo: | |
| await dependency(credentials=creds) | |
| assert excinfo.value.status_code == 401 | |
| assert excinfo.value.detail == "Invalid authentication token" | |
| async def test_token_dependency_missing_creds(): | |
| """Test missing credentials raises 401.""" | |
| token = "secret-token" | |
| dependency = create_token_dependency(token=token) | |
| with pytest.raises(HTTPException) as excinfo: | |
| await dependency(credentials=None) | |
| assert excinfo.value.status_code == 401 | |
| assert excinfo.value.detail == "Missing authentication credentials" | |
| # --- Unit Tests: Middleware --- | |
| async def test_security_logging_middleware(): | |
| """Test that middleware logs request and calls next.""" | |
| middleware = SecurityLoggingMiddleware(app=MagicMock()) | |
| # Mock Request | |
| request = MagicMock(spec=Request) | |
| request.method = "GET" | |
| request.url.path = "/test" | |
| request.client.host = "127.0.0.1" | |
| # Mock call_next | |
| async def call_next(req): | |
| response = MagicMock() | |
| response.status_code = 200 | |
| return response | |
| # Patch the dispatch function's console so this remains stable when other | |
| # server tests reload pdd.server.security during collection. | |
| with patch.object( | |
| SecurityLoggingMiddleware.dispatch.__globals__["console"], "print" | |
| ) as mock_print: | |
| response = await middleware.dispatch(request, call_next) | |
| assert response.status_code == 200 | |
| assert mock_print.call_count >= 2 # Request log and Response log | |
| # --- Z3 Formal Verification --- | |
| def test_z3_path_containment_logic(): | |
| """ | |
| Formal verification of path containment logic. | |
| We model the concept of "directory depth" relative to the project root. | |
| Root is at depth 0. | |
| 'folder' adds +1 to depth. | |
| '..' adds -1 to depth. | |
| We verify that if the final depth is < 0, the path is outside the root. | |
| This proves that simply checking the string prefix or resolved path containment | |
| is mathematically equivalent to ensuring depth >= 0. | |
| """ | |
| s = z3.Solver() | |
| # Let 'n' be the number of path components | |
| # We model a path as a sequence of moves: +1 (down) or -1 (up) | |
| # For simplicity, let's assume a path of length 3: [m1, m2, m3] | |
| m1, m2, m3 = z3.Ints('m1 m2 m3') | |
| # Each move is either 1 (dir) or -1 (..) | |
| # We restrict moves to these values | |
| s.add(z3.Or(m1 == 1, m1 == -1)) | |
| s.add(z3.Or(m2 == 1, m2 == -1)) | |
| s.add(z3.Or(m3 == 1, m3 == -1)) | |
| # Calculate cumulative depth at each step | |
| d0 = 0 # Root | |
| d1 = d0 + m1 | |
| d2 = d1 + m2 | |
| d3 = d2 + m3 | |
| # Definition of "Traversal Attack": | |
| # A traversal attack occurs if at ANY point the depth becomes negative. | |
| # (e.g., root/../ -> depth -1). | |
| # Even if it comes back down later (root/../root/file), it effectively left the jail. | |
| traversal_occurred = z3.Or(d1 < 0, d2 < 0, d3 < 0) | |
| # We want to verify: Is it possible for a traversal to occur WITHOUT the final depth being affected? | |
| # Actually, the security requirement is usually: "The final resolved path must be inside root". | |
| # But intermediate traversal (escaping and re-entering) is also dangerous if symlinks are involved. | |
| # However, `resolve()` handles '..'. | |
| # So, let's model `resolve()`. | |
| # `resolve()` effectively sums the moves. | |
| # If we sum the moves, does `sum < 0` imply outside? Yes. | |
| final_depth = m1 + m2 + m3 | |
| # Theorem: If final_depth < 0, then the path is outside. | |
| # We negate: Is it possible that final_depth < 0 AND path is inside (depth >= 0)? | |
| # This is trivially UNSAT, but let's ask Z3. | |
| s.reset() | |
| s.add(final_depth < 0) | |
| s.add(final_depth >= 0) # Contradiction | |
| assert s.check() == z3.unsat | |
| # A more interesting property: | |
| # If we have a path that attempts to go up 2 levels and down 1 level. | |
| # m1=-1, m2=-1, m3=1. Sum = -1. | |
| # The code checks `resolved_path.relative_to(root)`. | |
| # This check fails if resolved path is not a child. | |
| # In our integer model, this corresponds to `final_depth >= 0`. | |
| # Let's verify that `relative_to` logic (prefix matching) is consistent with depth logic. | |
| # If Path A is relative to Path B, then len(A) >= len(B). | |
| len_base = z3.Int('len_base') | |
| len_target = z3.Int('len_target') | |
| s.reset() | |
| s.add(len_base > 0) | |
| # Condition: Target is relative to Base | |
| # This implies len_target >= len_base | |
| is_relative = (len_target >= len_base) | |
| # We want to prove that if len_target < len_base, it CANNOT be relative. | |
| s.add(len_target < len_base) | |
| s.add(is_relative) | |
| assert s.check() == z3.unsat | |
| def test_z3_blacklist_matching(): | |
| """ | |
| Formal verification of blacklist logic. | |
| We verify that if a path component matches a blacklist pattern, it is rejected. | |
| """ | |
| s = z3.Solver() | |
| # Model: | |
| # A path is a set of components. | |
| # A blacklist is a set of forbidden values. | |
| # If intersection is not empty -> Rejected. | |
| # Let's use integers to represent "file types" or "names". | |
| # 1 = ".env", 2 = "safe.txt", 3 = "node_modules" | |
| # Blacklist = {1, 3} | |
| path_component = z3.Int('path_component') | |
| # Blacklist definition | |
| is_blacklisted = z3.Or(path_component == 1, path_component == 3) | |
| # Code logic: | |
| # for part in parts: if fnmatch(part, pattern): raise | |
| # This implies: if is_blacklisted(part) -> Rejected. | |
| rejected = z3.Bool('rejected') | |
| # We assert the logic of the code: | |
| s.add(z3.Implies(is_blacklisted, rejected)) | |
| # We want to verify: Is it possible to have a blacklisted component that is NOT rejected? | |
| # Negate theorem: exists component s.t. is_blacklisted AND Not(rejected) | |
| s.add(is_blacklisted) | |
| s.add(z3.Not(rejected)) | |
| assert s.check() == z3.unsat | |
Xet Storage Details
- Size:
- 13.8 kB
- Xet hash:
- 9d3d9e690718299c3fbb022f17580125d6b45a6dc1b607bcae246b3c195091da
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.