Skip to content

Commit f803c17

Browse files
committed
Validate preferred_dir relative to root_dir location, update tests
1 parent 85eba17 commit f803c17

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

jupyter_server/services/contents/manager.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ def emit(self, data):
8383
),
8484
)
8585

86+
@validate("preferred_dir")
87+
def _validate_preferred_dir(self, proposal):
88+
value = proposal["value"]
89+
90+
if not value.startswith(self.root_dir):
91+
raise TraitError(_i18n(f"{value} is outside root contents directory"))
92+
return value
93+
8694
allow_hidden = Bool(False, config=True, help="Allow access to hidden files")
8795

8896
notary = Instance(sign.NotebookNotary)

tests/test_serverapp.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,8 @@ async def test_invalid_preferred_dir_not_root_subdir_set(tmp_path, jp_configurab
386386
not_subdir_path = os.path.relpath(tmp_path, path)
387387

388388
app = jp_configurable_serverapp(root_dir=path)
389-
app.contents_manager.preferred_dir = not_subdir_path
390-
with pytest.raises(HTTPError) as error:
391-
await app.contents_manager.dir_exists(app.contents_manager.preferred_dir)
389+
with pytest.raises(TraitError) as error:
390+
app.contents_manager.preferred_dir = not_subdir_path
392391

393392
assert "is outside root contents directory" in str(error.value)
394393

@@ -399,12 +398,7 @@ async def test_absolute_preferred_dir_not_root_subdir_set(tmp_path, jp_configura
399398
not_subdir_path = str(tmp_path)
400399

401400
app = jp_configurable_serverapp(root_dir=path)
402-
app.contents_manager.preferred_dir = not_subdir_path
403-
with pytest.raises(HTTPError) as error:
404-
print(app.contents_manager.preferred_dir)
405-
await app.contents_manager.dir_exists(app.contents_manager.preferred_dir)
401+
with pytest.raises(TraitError) as error:
402+
app.contents_manager.preferred_dir = not_subdir_path
406403

407-
if os.name == "nt":
408-
assert "is not a relative API path" in str(error.value)
409-
else:
410-
assert "Preferred directory not found" in str(error.value)
404+
assert "is outside root contents directory" in str(error.value)

0 commit comments

Comments
 (0)