From bbeb2f98f553812e50645f35faf6cad157fbaa5a Mon Sep 17 00:00:00 2001 From: sekoia Date: Mon, 28 Aug 2023 15:27:00 +0200 Subject: [PATCH] Prevent creating any type of file with a leading dot Co-authored-by: A Thousand Ships <96648715+AThousandShips@users.noreply.github.com> Co-authored-by: gotnospirit --- editor/directory_create_dialog.cpp | 13 ++++++++++--- editor/filesystem_dock.cpp | 5 ++++- editor/scene_create_dialog.cpp | 2 ++ editor/script_create_dialog.cpp | 3 +++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/editor/directory_create_dialog.cpp b/editor/directory_create_dialog.cpp index fed7cb82c91..0efd11a6c12 100644 --- a/editor/directory_create_dialog.cpp +++ b/editor/directory_create_dialog.cpp @@ -51,13 +51,20 @@ String DirectoryCreateDialog::_validate_path(const String &p_path) const { return TTR("Folder name cannot be empty."); } + if (p_path.contains("\\") || p_path.contains(":") || p_path.contains("*") || + p_path.contains("|") || p_path.contains(">")) { + return TTR("Folder name contains invalid characters."); + } + for (const String &part : p_path.split("/")) { if (part.is_empty()) { return TTR("Folder name cannot be empty."); } - if (p_path.contains("\\") || p_path.contains(":") || p_path.contains("*") || - p_path.contains("|") || p_path.contains(">") || p_path.ends_with(".") || p_path.ends_with(" ")) { - return TTR("Folder name contains invalid characters."); + if (part.ends_with(" ") || part[0] == ' ') { + return TTR("Folder name cannot begin or end with a space."); + } + if (part[0] == '.') { + return TTR("Folder name cannot begin with a dot."); } } diff --git a/editor/filesystem_dock.cpp b/editor/filesystem_dock.cpp index 74345a6bccb..40a107d6c38 100644 --- a/editor/filesystem_dock.cpp +++ b/editor/filesystem_dock.cpp @@ -1688,7 +1688,7 @@ void FileSystemDock::_rename_operation_confirm() { } else if (new_name.contains("/") || new_name.contains("\\") || new_name.contains(":")) { EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters.")); rename_error = true; - } else if (new_name.begins_with(".")) { + } else if (new_name[0] == '.') { EditorNode::get_singleton()->show_warning(TTR("This filename begins with a dot rendering the file invisible to the editor.\nIf you want to rename it anyway, use your operating system's file manager.")); rename_error = true; } else if (to_rename.is_file && to_rename.path.get_extension() != new_name.get_extension()) { @@ -1760,6 +1760,9 @@ void FileSystemDock::_duplicate_operation_confirm() { } else if (new_name.contains("/") || new_name.contains("\\") || new_name.contains(":")) { EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters.")); return; + } else if (new_name[0] == '.') { + EditorNode::get_singleton()->show_warning(TTR("Name begins with a dot.")); + return; } String base_dir = to_duplicate.path.get_base_dir(); diff --git a/editor/scene_create_dialog.cpp b/editor/scene_create_dialog.cpp index aac9ca3739e..fef78e51540 100644 --- a/editor/scene_create_dialog.cpp +++ b/editor/scene_create_dialog.cpp @@ -103,6 +103,8 @@ void SceneCreateDialog::update_dialog() { if (validation_panel->is_valid() && !scene_name.is_valid_filename()) { validation_panel->set_message(MSG_ID_PATH, TTR("File name invalid."), EditorValidationPanel::MSG_ERROR); + } else if (validation_panel->is_valid() && scene_name[0] == '.') { + validation_panel->set_message(MSG_ID_PATH, TTR("File name begins with a dot."), EditorValidationPanel::MSG_ERROR); } if (validation_panel->is_valid()) { diff --git a/editor/script_create_dialog.cpp b/editor/script_create_dialog.cpp index daac7555295..e701d2ae7dd 100644 --- a/editor/script_create_dialog.cpp +++ b/editor/script_create_dialog.cpp @@ -243,6 +243,9 @@ String ScriptCreateDialog::_validate_path(const String &p_path, bool p_file_must if (!p.get_file().get_basename().is_valid_filename()) { return TTR("Filename is invalid."); } + if (p.get_file().begins_with(".")) { + return TTR("Name begins with a dot."); + } p = ProjectSettings::get_singleton()->localize_path(p); if (!p.begins_with("res://")) {