Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: address mo-7713 (#10939)

Dylan Madisettif60d46f87a78
diff --git a/marimo/_plugins/ui/_impl/file_browser.py b/marimo/_plugins/ui/_impl/file_browser.py
index 5a61d101f9..89300b97b4 100644
--- a/marimo/_plugins/ui/_impl/file_browser.py
+++ b/marimo/_plugins/ui/_impl/file_browser.py
@@ -621,15 +621,29 @@ class file_browser(
def _convert_value(
self, value: list[TypedFileBrowserFileInfo]
) -> Sequence[FileBrowserFileInfo]:
- return tuple(
- FileBrowserFileInfo(
- id=file["id"],
- name=file["name"],
- path=self._create_path(file["path"]),
- is_directory=file["is_directory"],
+ converted: list[FileBrowserFileInfo] = []
+ for file in value:
+ path = self._create_path(file["path"])
+ # The value can be set directly through the set_ui_element_value
+ # API, bypassing _list_directory. Enforce the navigation
+ # restriction here too so a client cannot select a path outside
+ # the initial path.
+ if self._restrict_navigation and not _is_path_within(
+ path, self._initial_path
+ ):
+ raise RuntimeError(
+ "Navigation is restricted; selecting a path outside the "
+ "initial path is not allowed."
+ )
+ converted.append(
+ FileBrowserFileInfo(
+ id=file["id"],
+ name=file["name"],
+ path=path,
+ is_directory=file["is_directory"],
+ )
)
- for file in value
- )
+ return tuple(converted)
def name(self, index: int = 0) -> str | None:
"""Get file name at index.
diff --git a/tests/_plugins/ui/_impl/test_file_browser.py b/tests/_plugins/ui/_impl/test_file_browser.py
index 331dc9d0d1..71e0e034cb 100644
--- a/tests/_plugins/ui/_impl/test_file_browser.py
+++ b/tests/_plugins/ui/_impl/test_file_browser.py
@@ -684,6 +684,81 @@ def test_missing_path_without_restriction_reports_not_found(
fb._list_directory(ListDirectoryArgs(path=str(tmp_path / "missing")))
+def test_restricted_update_rejects_value_outside_root(tmp_path: Path) -> None:
+ """A frontend value pointing outside the root must be rejected.
+
+ `_list_directory` blocks browsing outside `initial_path`, but the value can
+ also be set directly through the `set_ui_element_value` API, which calls
+ `_update` -> `_convert_value`. That path must enforce the same restriction,
+ otherwise an app viewer can read any file the server can (MO-7713).
+ """
+ restricted = tmp_path / "restricted"
+ restricted.mkdir()
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ secret = outside / "secret.txt"
+ secret.write_text("secret data here")
+
+ fb = file_browser(initial_path=restricted, restrict_navigation=True)
+ with pytest.raises(RuntimeError, match="Navigation is restricted"):
+ fb._update(
+ [
+ {
+ "id": str(secret),
+ "path": str(secret),
+ "name": secret.name,
+ "is_directory": False,
+ }
+ ]
+ )
+
+
+def test_restricted_update_accepts_value_within_root(tmp_path: Path) -> None:
+ """A frontend value inside the root is still accepted after the fix."""
+ restricted = tmp_path / "restricted"
+ restricted.mkdir()
+ reachable = restricted / "data.txt"
+ reachable.write_text("ok")
+
+ fb = file_browser(initial_path=restricted, restrict_navigation=True)
+ fb._update(
+ [
+ {
+ "id": str(reachable),
+ "path": str(reachable),
+ "name": reachable.name,
+ "is_directory": False,
+ }
+ ]
+ )
+ assert fb.path() == reachable
+
+
+def test_unrestricted_update_accepts_value_outside_root(
+ tmp_path: Path,
+) -> None:
+ """Without restriction, values outside the root remain allowed."""
+ restricted = tmp_path / "restricted"
+ restricted.mkdir()
+ outside = tmp_path / "outside"
+ outside.mkdir()
+ target = outside / "data.txt"
+ target.write_text("ok")
+
+ fb = file_browser(initial_path=restricted, restrict_navigation=False)
+ fb._update(
+ [
+ {
+ "id": str(target),
+ "path": str(target),
+ "name": target.name,
+ "is_directory": False,
+ }
+ ]
+ )
+ assert fb.path() == target
+
+
def test_name_method() -> None:
fb = file_browser(initial_path=Path.cwd())
fb._value = [