Prevent using name with leading dot when create/rename/duplicate scene/folder/script/resource Fixes #62497

This commit is contained in:
James 2022-07-12 15:51:18 +08:00 committed by Relintai
parent 15cccb331f
commit 4d93f4434d
6 changed files with 42 additions and 6 deletions

View File

@ -52,15 +52,22 @@ String DirectoryCreateDialog::_validate_path(const String &p_path) const {
return TTR("Folder name cannot be empty.");
}
if (p_path.find("\\") != -1 || p_path.find(":") != -1 || p_path.find("*") != -1 ||
p_path.find("|") != -1 || p_path.find(">") != -1) {
return TTR("Folder name contains invalid characters.");
}
const Vector<String> parts = p_path.split("/");
for (int i = 0; i < parts.size(); i++) {
const String part = parts[i];
if (part.empty()) {
return TTR("Folder name cannot be empty.");
}
if (p_path.find("\\") != -1 || p_path.find(":") != -1 || p_path.find("*") != -1 ||
p_path.find("|") != -1 || p_path.find(">") != -1 || 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.");
}
}

View File

@ -164,7 +164,15 @@ void EditorDirDialog::_make_dir_confirm() {
DirAccessRef d = DirAccess::open(dir);
ERR_FAIL_COND_MSG(!d, "Cannot open directory '" + dir + "'.");
Error err = d->make_dir(makedirname->get_text());
String dir_name = makedirname->get_text();
if (dir_name.begins_with(".")) {
mkdirerr->set_text(TTR("Could not use a name with a leading dot."));
mkdirerr->popup_centered_minsize(Size2(250, 80) * EDSCALE);
return;
}
Error err = d->make_dir(dir_name);
if (err != OK) {
mkdirerr->popup_centered_minsize(Size2(250, 80) * EDSCALE);

View File

@ -1113,9 +1113,16 @@ EditorFileDialog::Access EditorFileDialog::get_access() const {
}
void EditorFileDialog::_make_dir_confirm() {
Error err = dir_access->make_dir(makedirname->get_text().strip_edges());
String dir_name = makedirname->get_text().strip_edges();
if (dir_name.begins_with(".")) {
mkdirerr->set_text(TTR("Could not use a name with a leading dot."));
mkdirerr->popup_centered_minsize(Size2(250, 50) * EDSCALE);
return;
}
Error err = dir_access->make_dir(dir_name);
if (err == OK) {
dir_access->change_dir(makedirname->get_text().strip_edges());
dir_access->change_dir(dir_name);
invalidate();
update_filters();
update_dir();

View File

@ -1840,6 +1840,11 @@ void EditorNode::_dialog_action(String p_file) {
case RESOURCE_SAVE:
case RESOURCE_SAVE_AS: {
ERR_FAIL_COND(saving_resource.is_null());
if (p_file.get_file().begins_with(".")) {
show_accept(TTR("Could not use a name with a leading dot."), TTR("OK"));
return;
}
save_resource_in_path(saving_resource, p_file);
saving_resource = Ref<Resource>();
ObjectID current = editor_history.get_current();

View File

@ -1446,6 +1446,9 @@ void FileSystemDock::_rename_operation_confirm() {
} else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
EditorNode::get_singleton()->show_warning(TTR("Name contains invalid characters."));
return;
} 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."));
return;
} else if (to_rename.is_file && old_name.get_extension() != new_name.get_extension()) {
if (!EditorFileSystem::get_singleton()->get_valid_extensions().find(new_name.get_extension())) {
EditorNode::get_singleton()->show_warning(TTR("This file extension is not recognized by the editor.\nIf you want to rename it anyway, use your operating system's file manager.\nAfter renaming to an unknown extension, the file won't be shown in the editor anymore."));
@ -1508,6 +1511,9 @@ void FileSystemDock::_duplicate_operation_confirm() {
} else if (new_name.find("/") != -1 || new_name.find("\\") != -1 || new_name.find(":") != -1) {
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();

View File

@ -193,6 +193,9 @@ String ScriptCreateDialog::_validate_path(const String &p_path, bool p_file_must
if (p.get_file().get_basename() == "") {
return TTR("Filename is empty.");
}
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://")) {