From f803c17400852ae52b5946b82a45baf5ccea2758 Mon Sep 17 00:00:00 2001 From: Kevin Bates Date: Fri, 11 Nov 2022 15:00:29 -0800 Subject: [PATCH] Validate preferred_dir relative to root_dir location, update tests --- jupyter_server/services/contents/manager.py | 8 ++++++++ tests/test_serverapp.py | 16 +++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/jupyter_server/services/contents/manager.py b/jupyter_server/services/contents/manager.py index cb2d9a464b..a94946b8c1 100644 --- a/jupyter_server/services/contents/manager.py +++ b/jupyter_server/services/contents/manager.py @@ -83,6 +83,14 @@ def emit(self, data): ), ) + @validate("preferred_dir") + def _validate_preferred_dir(self, proposal): + value = proposal["value"] + + if not value.startswith(self.root_dir): + raise TraitError(_i18n(f"{value} is outside root contents directory")) + return value + allow_hidden = Bool(False, config=True, help="Allow access to hidden files") notary = Instance(sign.NotebookNotary) diff --git a/tests/test_serverapp.py b/tests/test_serverapp.py index 7825e00b6a..5be17f5624 100644 --- a/tests/test_serverapp.py +++ b/tests/test_serverapp.py @@ -386,9 +386,8 @@ async def test_invalid_preferred_dir_not_root_subdir_set(tmp_path, jp_configurab not_subdir_path = os.path.relpath(tmp_path, path) app = jp_configurable_serverapp(root_dir=path) - app.contents_manager.preferred_dir = not_subdir_path - with pytest.raises(HTTPError) as error: - await app.contents_manager.dir_exists(app.contents_manager.preferred_dir) + with pytest.raises(TraitError) as error: + app.contents_manager.preferred_dir = not_subdir_path assert "is outside root contents directory" in str(error.value) @@ -399,12 +398,7 @@ async def test_absolute_preferred_dir_not_root_subdir_set(tmp_path, jp_configura not_subdir_path = str(tmp_path) app = jp_configurable_serverapp(root_dir=path) - app.contents_manager.preferred_dir = not_subdir_path - with pytest.raises(HTTPError) as error: - print(app.contents_manager.preferred_dir) - await app.contents_manager.dir_exists(app.contents_manager.preferred_dir) + with pytest.raises(TraitError) as error: + app.contents_manager.preferred_dir = not_subdir_path - if os.name == "nt": - assert "is not a relative API path" in str(error.value) - else: - assert "Preferred directory not found" in str(error.value) + assert "is outside root contents directory" in str(error.value)