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15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
"""
Path Traversal Security Tests (SECU-03.3).

Comprehensive tests for path traversal attack prevention:
- ../ sequences (Unix)
- ..\ sequences (Windows)
- Double-encoded variants
- URL-encoded variants
- Unicode-encoded variants

OWASP Category: A01:2021 - Broken Access Control
CWE: CWE-22 (Path Traversal)
"""
import pytest
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session


# ============================================================================
# Path Traversal Payloads
# ============================================================================

# Unix-style path traversal payloads
UNIX_PATH_TRAVERSAL = [
    "../../../etc/passwd",
    "..//..//..//etc/passwd",
    "....//....//....//etc/passwd",
    "../../../../etc/passwd",
    "..../../etc/passwd",
    "../etc/passwd",
    "../../etc/passwd",
    "../../../etc/shadow",
    "../../../../etc/shadow",
    "../.ssh/id_rsa",
    "../../.ssh/id_rsa",
    "../../../.ssh/id_rsa",
    "../root/.ssh/id_rsa",
    "../../root/.ssh/id_rsa",
]

# Windows-style path traversal payloads
WINDOWS_PATH_TRAVERSAL = [
    r"..\..\..\..\windows\system32\config\sam",
    r"..\\..\\..\\windows\\system32\\config\\sam",
    r"....\\....\\....\\windows\\system32\\config\\sam",
    r"..\..\..\windows\system32\drivers\etc\hosts",
    r"..\..\..\..\windows\system32\config\sam",
    r"..\..\boot.ini",
    r"..\..\..\boot.ini",
    r"..\..\..\..\boot.ini",
    r"..\..\..\..\..\windows\win.ini",
    r"..\..\..\..\windows\system32\config\sam",
]

# URL-encoded path traversal payloads
URL_ENCODED_TRAVERSAL = [
    "%2e%2e%2fetc%2fpasswd",  # ../etc/passwd
    "%2e%2e%5cetc%2fpasswd",  # ..\etc/passwd
    "%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd",  # ../../../etc/passwd
    "%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fshadow",
    "%2e%2e%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd",
    "..%252f..%252f..%252fetc%252fpasswd",  # Double-encoded
    "..%252f..%252f..%252fetc%252fshadow",  # Double-encoded
    "..%255c..%255c..%255cetc%255cshadow",  # Double-encoded backslash
    "%252e%252e%252f%252e%252e%252fetc%252fpasswd",  # Triple-encoded
]

# Unicode-encoded path traversal payloads
UNICODE_ENCODED_TRAVERSAL = [
    "..%c0%af..%c0%af..%c0%afetc/passwd",  # Unicode bypass
    "..%c0%af..%c0%af..%c0%afetc/shadow",
    "..%c0%af..%c0%afetc/passwd",
    "..%c0%afetc/passwd",
    "%c0%ae%c0%ae%c0%af%c0%ae%c0%ae%c0%afetc/passwd",  # Full Unicode encoding
]

# Mixed path traversal payloads
MIXED_TRAVERSAL = [
    "....//....//....//etc/passwd",
    "..\\..\\..\\..\\etc/passwd",  # Mixed separators
    "..%5c..%5c..%5cetc/passwd",  # URL-encoded backslash, forward slash
    "../..\\../etc/passwd",  # Alternating separators
    "..%2f..%2fetc%2fpasswd",
    "..\\..%5cetc%5cshadow",
]


class TestPathTraversalInFileUploads:
    """Test path traversal in file upload operations."""

    @pytest.mark.parametrize("payload", UNIX_PATH_TRAVERSAL[:5])
    def test_unix_path_traversal_in_filename(self, payload, client: TestClient, admin_token: str):
        """
        Test Unix-style path traversal in filename parameter.

        ATTACK: Attempt to access /etc/passwd using ../ sequences
        EXPECTED: Path validation rejects or sanitizes the input
        """
        # Try to upload a file with path traversal in filename
        response = client.post(
            "/api/files/upload",
            json={"filename": payload, "content": "test content"},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        # Should reject or sanitize
        # Should NOT return file contents like /etc/passwd
        assert response.status_code in [400, 403, 404, 422, 200]
        assert "root:" not in response.text
        assert "[extensions]" not in response.text

    @pytest.mark.parametrize("payload", WINDOWS_PATH_TRAVERSAL[:5])
    def test_windows_path_traversal_in_filename(self, payload, client: TestClient, admin_token: str):
        """
        Test Windows-style path traversal in filename parameter.

        ATTACK: Attempt to access Windows system files using ..\ sequences
        EXPECTED: Path validation rejects or sanitizes the input
        """
        response = client.post(
            "/api/files/upload",
            json={"filename": payload, "content": "test content"},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422, 200]
        # Should not contain Windows file contents
        assert "[boot loader]" not in response.text.lower()


class TestPathTraversalInCanvasTemplates:
    """Test path traversal in canvas template selection."""

    @pytest.mark.parametrize("payload", UNIX_PATH_TRAVERSAL[:5])
    def test_path_traversal_in_template_path(self, payload, client: TestClient, admin_token: str):
        """
        Test path traversal in canvas template parameter.

        ATTACK: Attempt to access sensitive files using template parameter
        EXPECTED: Template validation rejects path traversal attempts
        """
        response = client.post(
            "/api/canvas",
            json={
                "title": "Test Canvas",
                "template": payload,
                "canvas_type": "generic"
            },
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422, 200]
        assert "root:" not in response.text

    @pytest.mark.parametrize("payload", ["../../../sensitive/config.json", r"..\..\..\sensitive\api_keys.txt"])
    def test_path_traversal_to_sensitive_files(self, payload, client: TestClient, admin_token: str):
        """
        Test path traversal to sensitive configuration files.

        ATTACK: Attempt to access config files, API keys, secrets
        EXPECTED: Access blocked, files not exposed
        """
        response = client.post(
            "/api/canvas",
            json={
                "title": "Test Canvas",
                "template": payload,
                "canvas_type": "generic"
            },
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422, 200]
        # Should not leak sensitive data
        assert "api_key" not in response.text.lower()
        assert "secret" not in response.text.lower()
        assert "password" not in response.text.lower()


class TestPathTraversalInBrowserAutomation:
    """Test path traversal in browser automation."""

    @pytest.mark.parametrize("payload", UNIX_PATH_TRAVERSAL[:3])
    def test_path_traversal_in_screenshot_url(self, payload, client: TestClient, admin_token: str):
        """
        Test path traversal in browser screenshot URL parameter.

        ATTACK: Attempt to read local files via screenshot URL
        EXPECTED: URL validation rejects file:// protocol or path traversal
        """
        response = client.post(
            "/api/tools/browser/screenshot",
            json={"url": f"file:///{payload}"},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422, 401, 500]
        assert "root:" not in response.text

    @pytest.mark.parametrize("payload", ["file:///etc/passwd", "file:///etc/shadow", "file:///root/.ssh/id_rsa"])
    def test_file_protocol_blocked(self, payload, client: TestClient, admin_token: str):
        """
        Test file:// protocol is blocked.

        ATTACK: Attempt to use file:// protocol to read local files
        EXPECTED: Protocol validation rejects file:// URLs
        """
        response = client.post(
            "/api/tools/browser/screenshot",
            json={"url": payload},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422, 401, 500]
        assert "root:" not in response.text


class TestDoubleEncodedPathTraversal:
    """Test double-encoded path traversal attempts."""

    @pytest.mark.parametrize("payload", [
        "%252e%252e%252fetc%252fpasswd",  # Double-encoded ../
        "..%252f%252e%252e%252fetc%252fpasswd",
        "%252e%252e%255cetc%252fshadow",  # Double-encoded ..\
    ])
    def test_double_encoded_path_traversal(self, payload, client: TestClient, admin_token: str):
        """
        Test double-encoded path traversal payloads.

        ATTACK: Bypass input validation by double-encoding ../ sequences
        EXPECTED: URL decoding applied correctly, traversal detected
        """
        response = client.post(
            "/api/files/upload",
            json={"filename": payload, "content": "test"},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422]
        assert "root:" not in response.text


class TestUnicodeEncodedPathTraversal:
    """Test Unicode-encoded path traversal attempts."""

    @pytest.mark.parametrize("payload", UNICODE_ENCODED_TRAVERSAL[:3])
    def test_unicode_encoded_path_traversal(self, payload, client: TestClient, admin_token: str):
        """
        Test Unicode-encoded path traversal payloads.

        ATTACK: Bypass input validation using Unicode encoding
        EXPECTED: Unicode normalization applied, traversal detected
        """
        response = client.post(
            "/api/files/upload",
            json={"filename": payload, "content": "test"},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422]
        assert "root:" not in response.text


class TestMixedPathTraversal:
    """Test mixed path traversal attempts."""

    @pytest.mark.parametrize("payload", MIXED_TRAVERSAL[:3])
    def test_mixed_separator_traversal(self, payload, client: TestClient, admin_token: str):
        """
        Test mixed path separator traversal attempts.

        ATTACK: Mix Unix and Windows path separators to bypass validation
        EXPECTED: Normalization applied, traversal detected
        """
        response = client.post(
            "/api/files/upload",
            json={"filename": payload, "content": "test"},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422]
        assert "root:" not in response.text


class TestPathTraversalInQueryParameters:
    """Test path traversal in query parameters."""

    @pytest.mark.parametrize("payload", UNIX_PATH_TRAVERSAL[:3])
    def test_path_traversal_in_query_param(self, payload, client: TestClient, admin_token: str):
        """
        Test path traversal in query string parameters.

        ATTACK: Inject path traversal via query parameters
        EXPECTED: Query parameter validation blocks traversal
        """
        from urllib.parse import quote
        encoded_payload = quote(payload, safe='')

        response = client.get(
            f"/api/canvas?template={encoded_payload}",
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [200, 400, 403, 404, 422]
        if response.status_code == 200:
            # If successful, ensure no file contents leaked
            assert "root:" not in response.text


class TestPathTraversalInRequestBody:
    """Test path traversal in request body."""

    @pytest.mark.parametrize("payload", UNIX_PATH_TRAVERSAL[:3])
    def test_path_traversal_in_json_body(self, payload, client: TestClient, admin_token: str):
        """
        Test path traversal in JSON request body.

        ATTACK: Inject path traversal via JSON body fields
        EXPECTED: Request body validation blocks traversal
        """
        response = client.post(
            "/api/canvas",
            json={
                "title": "Test",
                "canvas_type": "generic",
                "custom_path": payload  # Malicious custom path
            },
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [200, 400, 403, 404, 422]
        if response.status_code == 200:
            assert "root:" not in response.text


class TestPathTraversalEdgeCases:
    """Test path traversal edge cases."""

    def test_null_byte_injection(self, client: TestClient, admin_token: str):
        """
        Test null byte injection in path traversal.

        ATTACK: Use null bytes to bypass string validation
        EXPECTED: Null byte stripping or rejection
        """
        payload = "../../../etc/passwd\x00.jpg"

        response = client.post(
            "/api/files/upload",
            json={"filename": payload, "content": "test"},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422]
        assert "root:" not in response.text

    def test_absolute_path_bypass(self, client: TestClient, admin_token: str):
        """
        Test absolute path bypass attempts.

        ATTACK: Use absolute paths instead of relative traversal
        EXPECTED: Absolute path validation blocks system files
        """
        for payload in ["/etc/passwd", "/etc/shadow", "C:\\Windows\\System32\\config\\SAM"]:
            response = client.post(
                "/api/files/upload",
                json={"filename": payload, "content": "test"},
                headers={"Authorization": f"Bearer {admin_token}"}
            )

            assert response.status_code in [400, 403, 404, 422]
            assert "root:" not in response.text

    def test_path_traversal_with_valid_extension(self, client: TestClient, admin_token: str):
        """
        Test path traversal with valid file extension.

        ATTACK: Use path traversal with .jpg/.png extension to bypass extension check
        EXPECTED: Full path validation, not just extension check
        """
        payload = "../../../etc/passwd.jpg"

        response = client.post(
            "/api/files/upload",
            json={"filename": payload, "content": "test"},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422]
        # Even with .jpg extension, should not access /etc/passwd
        assert "root:" not in response.text


class TestPathTraversalPreventionMechanisms:
    """Test that path traversal prevention mechanisms work."""

    def test_path_normalization(self, client: TestClient, admin_token: str):
        """
        Test that path normalization is applied.

        CHECK: ../ sequences are resolved and validated
        EXPECTED: Normalized path is checked against allowed directories
        """
        payload = "subdir/../../etc/passwd"

        response = client.post(
            "/api/files/upload",
            json={"filename": payload, "content": "test"},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422]
        assert "root:" not in response.text

    def test_canonical_path_check(self, client: TestClient, admin_token: str):
        """
        Test that canonical (real) paths are checked.

        CHECK: Symbolic links are resolved and checked
        EXPECTED: Real path is validated, not just the symlink path
        """
        # Even if symlinks are used, real path should be validated
        payload = "../../../var/log/syslog"

        response = client.post(
            "/api/files/upload",
            json={"filename": payload, "content": "test"},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422]
        # Should not leak system log contents

    def test_chroot_jail_boundary(self, client: TestClient, admin_token: str):
        """
        Test that chroot jail boundaries are enforced.

        CHECK: File access is restricted to allowed directory
        EXPECTED: Cannot escape chroot directory using ../
        """
        for payload in ["../../../etc/passwd", "..../etc/passwd", "../../etc/passwd"]:
            response = client.post(
                "/api/files/upload",
                json={"filename": payload, "content": "test"},
                headers={"Authorization": f"Bearer {admin_token}"}
            )

            assert response.status_code in [400, 403, 404, 422]
            assert "root:" not in response.text


class TestPathTraversalInSpecificEndpoints:
    """Test path traversal in specific API endpoints."""

    def test_path_traversal_in_episode_retrieval(self, client: TestClient, admin_token: str):
        """Test path traversal in episode file retrieval."""
        payload = "../../../etc/passwd"

        response = client.get(
            f"/api/episodes/file/{payload}",
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422]
        assert "root:" not in response.text

    def test_path_traversal_in_canvas_component_load(self, client: TestClient, admin_token: str):
        """Test path traversal in canvas component loading."""
        payload = "../../../etc/passwd"

        response = client.get(
            f"/api/canvas/components/{payload}",
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422]
        assert "root:" not in response.text

    def test_path_traversal_in_template_rendering(self, client: TestClient, admin_token: str):
        """Test path traversal in template rendering."""
        payload = "../../../etc/passwd"

        response = client.post(
            "/api/canvas/render",
            json={"template": payload, "data": {}},
            headers={"Authorization": f"Bearer {admin_token}"}
        )

        assert response.status_code in [400, 403, 404, 422]
        assert "root:" not in response.text