Skip to content

Commit

Permalink
Validate preferred_dir relative to root_dir location, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-bates committed Nov 11, 2022
1 parent 85eba17 commit f803c17
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
8 changes: 8 additions & 0 deletions jupyter_server/services/contents/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 5 additions & 11 deletions tests/test_serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

0 comments on commit f803c17

Please sign in to comment.