Skip to content

Commit 86ceef0

Browse files
committed
updated
1 parent 22fb1f8 commit 86ceef0

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

app/main.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,11 @@ def _register_allowed_root(path: Path | str) -> None:
146146
"""Register a root directory that endpoints can safely access."""
147147
try:
148148
candidate = Path(path).expanduser().resolve()
149-
root = candidate if candidate.is_dir() else candidate.parent
149+
if candidate.exists() and candidate.is_file():
150+
root = candidate.parent
151+
else:
152+
# Treat missing paths as intended directory roots, not as parent fallback.
153+
root = candidate
150154
with ALLOWED_PATHS_LOCK:
151155
ALLOWED_PATH_ROOTS.add(root)
152156
except Exception as exc:

tests/test_api.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,29 @@ def test_open_location_blocks_non_allowed_path(client, project_root):
125125
assert response.json()["detail"] == "Acesso negado para este caminho"
126126

127127

128+
def test_register_allowed_root_missing_directory_does_not_expand_to_parent(tmp_path):
129+
import app.main as main_module
130+
131+
missing_dir = (tmp_path / "new-allowed-root").resolve()
132+
parent_dir = missing_dir.parent
133+
134+
with main_module.ALLOWED_PATHS_LOCK:
135+
original_roots = set(main_module.ALLOWED_PATH_ROOTS)
136+
main_module.ALLOWED_PATH_ROOTS.clear()
137+
138+
try:
139+
main_module._register_allowed_root(missing_dir)
140+
with main_module.ALLOWED_PATHS_LOCK:
141+
current_roots = set(main_module.ALLOWED_PATH_ROOTS)
142+
143+
assert missing_dir in current_roots
144+
assert parent_dir not in current_roots
145+
finally:
146+
with main_module.ALLOWED_PATHS_LOCK:
147+
main_module.ALLOWED_PATH_ROOTS.clear()
148+
main_module.ALLOWED_PATH_ROOTS.update(original_roots)
149+
150+
128151
def test_select_folder_returns_error_details_when_dialog_fails(client, monkeypatch):
129152
monkeypatch.setattr(
130153
"app.main._open_folder_dialog",

0 commit comments

Comments
 (0)