-
Notifications
You must be signed in to change notification settings - Fork 67
Fix case-run path traversal #431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,11 @@ | |||||||||
|
|
||||||||||
| datafile_api = Blueprint('DataFileRoute', __name__) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def _safe_child_path(base_dir, *parts): | ||||||||||
| relative_path = os.path.join(*[part for part in parts if part not in (None, "")]) | ||||||||||
| return Path(Config.validate_path(base_dir, relative_path)) | ||||||||||
|
|
||||||||||
| @datafile_api.route("/generateDataFile", methods=['POST']) | ||||||||||
| def generateDataFile(): | ||||||||||
| try: | ||||||||||
|
|
@@ -77,8 +82,8 @@ def deleteCaseRun(): | |||||||||
| if not casename: | ||||||||||
| return jsonify({'message': 'No model selected.', 'status_code': 'error'}), 400 | ||||||||||
|
||||||||||
| return jsonify({'message': 'No model selected.', 'status_code': 'error'}), 400 | |
| return jsonify({'message': 'No model selected.', 'status_code': 'error'}), 400 | |
| if not caserunname: | |
| return jsonify({'message': 'No case run selected.', 'status_code': 'error'}), 400 |
Copilot
AI
Apr 13, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching PermissionError and returning “Invalid path.” will also convert real filesystem permission failures (e.g., inability to delete due to OS ACLs/locks) into a 400 that looks like a traversal attempt. If you want to preserve debuggability, consider only mapping PermissionError originating from Config.validate_path/_safe_child_path to 400 and letting deletion-time permission errors surface as 5xx/appropriate error codes.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| from pathlib import Path | ||
| import shutil | ||
| import uuid | ||
|
|
||
| from Classes.Base import Config | ||
| from Classes.Base.FileClass import File | ||
|
|
||
|
|
||
| def _create_case(case_name, case_runs=None): | ||
| case_dir = Path(Config.DATA_STORAGE, case_name) | ||
| (case_dir / "view").mkdir(parents=True) | ||
| (case_dir / "res").mkdir(parents=True) | ||
|
|
||
| File.writeFile({"osy-casename": case_name}, case_dir / "genData.json") | ||
| File.writeFile({"osy-cases": case_runs or []}, case_dir / "view" / "resData.json") | ||
|
|
||
| for run in case_runs or []: | ||
| (case_dir / "res" / run["Case"]).mkdir(parents=True) | ||
|
|
||
| return case_dir | ||
|
|
||
|
|
||
| def test_delete_case_run_deletes_requested_case_run(client): | ||
| case_name = f"delete_run_case_{uuid.uuid4().hex}" | ||
| case_dir = _create_case(case_name, case_runs=[{"Case": "run1"}]) | ||
|
|
||
| try: | ||
| response = client.post( | ||
| "/deleteCaseRun", | ||
| json={"casename": case_name, "caserunname": "run1", "resultsOnly": False}, | ||
| ) | ||
|
|
||
| assert response.status_code == 200 | ||
| assert response.get_json() == { | ||
| "message": "You have deleted a case run!", | ||
| "status_code": "success", | ||
| } | ||
| assert not (case_dir / "res" / "run1").exists() | ||
| assert File.readFile(case_dir / "view" / "resData.json") == {"osy-cases": []} | ||
| finally: | ||
| shutil.rmtree(case_dir, ignore_errors=True) | ||
|
|
||
|
|
||
| def test_delete_case_run_blocks_path_traversal_to_sibling_case(client): | ||
| source_case = f"source_case_{uuid.uuid4().hex}" | ||
| victim_case = f"victim_case_{uuid.uuid4().hex}" | ||
| source_dir = _create_case(source_case) | ||
| victim_dir = _create_case(victim_case) | ||
|
|
||
| try: | ||
| response = client.post( | ||
| "/deleteCaseRun", | ||
| json={ | ||
| "casename": source_case, | ||
| "caserunname": f"../../{victim_case}", | ||
| "resultsOnly": False, | ||
| }, | ||
| ) | ||
|
|
||
| assert response.status_code == 400 | ||
| assert response.get_json() == { | ||
| "message": "Invalid path.", | ||
| "status_code": "error", | ||
| } | ||
| assert victim_dir.exists() | ||
| finally: | ||
| shutil.rmtree(source_dir, ignore_errors=True) | ||
| shutil.rmtree(victim_dir, ignore_errors=True) | ||
|
|
||
|
|
||
| def test_download_file_blocks_path_traversal_to_sibling_case(client): | ||
| source_case = f"download_source_{uuid.uuid4().hex}" | ||
| victim_case = f"download_victim_{uuid.uuid4().hex}" | ||
| source_dir = _create_case(source_case) | ||
| victim_dir = _create_case(victim_case) | ||
| (source_dir / "res" / "csv").mkdir(parents=True, exist_ok=True) | ||
|
|
||
| try: | ||
| with client.session_transaction() as session_data: | ||
| session_data["osycase"] = source_case | ||
|
|
||
| response = client.get( | ||
| "/downloadFile", | ||
| query_string={"file": f"../../../{victim_case}/genData.json"}, | ||
| ) | ||
|
|
||
| assert response.status_code == 400 | ||
| assert response.get_json() == { | ||
| "message": "Invalid path.", | ||
| "status_code": "error", | ||
| } | ||
| assert victim_dir.exists() | ||
| finally: | ||
| shutil.rmtree(source_dir, ignore_errors=True) | ||
| shutil.rmtree(victim_dir, ignore_errors=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_safe_child_path can raise a TypeError when all provided parts are None/"" because os.path.join() is called with an empty argument list. This can be triggered by an empty string input (e.g., caserunname == ""), resulting in a 500 instead of a controlled 400. Consider explicitly handling the “no effective parts” case (e.g., raise PermissionError / return a 400) or ensure callers validate non-empty segments before calling this helper.