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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: dont allow renaming to '.' or '..', show dotfiles (#1047)
Myles Scolnick1b74abe3c677
diff --git a/marimo/_server/files/os_file_system.py b/marimo/_server/files/os_file_system.pyindex aac76190c0..3bc5bf0652 100644--- a/marimo/_server/files/os_file_system.py+++ b/marimo/_server/files/os_file_system.py@@ -11,12 +11,16 @@ from marimo._server.files.file_system import FileSystemfrom marimo._server.models.files import FileDetailsResponse, FileInfoIGNORE_LIST = [+ ".",+ "..",+ ".DS_Store","__pycache__","node_modules",]-IGNORE_PREFIXES = [+DISALLOWED_NAMES = [".",+ "..",]@@ -30,11 +34,6 @@ class OSFileSystem(FileSystem):for entry in it:if entry.name in IGNORE_LIST:continue- if any(- entry.name.startswith(prefix) for prefix in IGNORE_PREFIXES- ):- continue-try:is_directory = entry.is_dir()entry_stat = entry.stat()@@ -108,6 +107,13 @@ class OSFileSystem(FileSystem):name: str,contents: Optional[bytes],) -> FileInfo:+ if name in DISALLOWED_NAMES:+ raise ValueError(+ f"Cannot create file or directory with name {name}"+ )+ if name.strip() == "":+ raise ValueError("Cannot create file or directory with empty name")+full_path = os.path.join(path, name)# If the file already exists, generate a new nameif os.path.exists(full_path):@@ -139,6 +145,11 @@ class OSFileSystem(FileSystem):return Truedef move_file_or_directory(self, path: str, new_path: str) -> FileInfo:+ file_name = os.path.basename(new_path)+ # Disallow renaming to . or ..+ if file_name in DISALLOWED_NAMES:+ raise ValueError(f"Cannot rename to {new_path}")+shutil.move(path, new_path)return self.get_details(new_path).filediff --git a/tests/_server/files/test_os_file_system.py b/tests/_server/files/test_os_file_system.pyindex f0fff67f8b..283ccf61df 100644--- a/tests/_server/files/test_os_file_system.py+++ b/tests/_server/files/test_os_file_system.py@@ -46,6 +46,14 @@ class TestOSFileSystem(unittest.TestCase):expected_path = os.path.join(self.test_dir, test_dir_name)assert os.path.isdir(expected_path)+ def test_create_with_empty_name(self):+ with self.assertRaises(ValueError):+ self.fs.create_file_or_directory(self.test_dir, "file", "", None)++ def test_create_with_disallowed_name(self):+ with self.assertRaises(ValueError):+ self.fs.create_file_or_directory(self.test_dir, "file", ".", None)+def test_list_files(self):# Create a test file and directoryself.test_create_file()@@ -53,6 +61,13 @@ class TestOSFileSystem(unittest.TestCase):files = self.fs.list_files(self.test_dir)assert len(files) == 2 # Expecting 1 file and 1 directory+ def test_list_files_with_broken_directory_symlink(self):+ # Create a broken symlink+ broken_symlink = os.path.join(self.test_dir, "broken_symlink")+ os.symlink("non_existent_file", broken_symlink)+ files = self.fs.list_files(self.test_dir)+ assert len(files) == 0+def test_get_details(self):test_file_name = "test_file.txt"self.fs.create_file_or_directory(@@ -120,6 +135,16 @@ class TestOSFileSystem(unittest.TestCase):assert os.path.exists(new_path)assert not os.path.exists(original_path)+ def test_move_with_disallowed_name(self):+ original_file_name = "original.txt"+ new_file_name = "."+ original_path = os.path.join(self.test_dir, original_file_name)+ new_path = os.path.join(self.test_dir, new_file_name)+ with open(original_path, "w"):+ pass+ with self.assertRaises(ValueError):+ self.fs.move_file_or_directory(original_path, new_path)+def test_update_file(self):test_file_name = "test_file.txt"file_path = os.path.join(self.test_dir, test_file_name)