{"record":{"id":"b36ecbaecabc9ba6","repo":"FoundationAgents/MetaGPT","slug":"invalid-file-name","errorCode":null,"errorMessage":"Invalid file name.","messagePattern":"Invalid file name\\.","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"metagpt/tools/libs/editor.py","lineNumber":537,"sourceCode":"\n        Args:\n            file_name: Path: The name of the file to edit or append to.\n            start: int | None = None: The start line number for editing. Ignored if is_append is True.\n            end: int | None = None: The end line number for editing. Ignored if is_append is True.\n            content: str: The content to replace the lines with or to append.\n            is_insert: bool = False: Whether to insert content at the given line number instead of editing.\n            is_append: bool = False: Whether to append content to the file instead of editing.\n        \"\"\"\n\n        ERROR_MSG = f\"[Error editing file {file_name}. Please confirm the file is correct.]\"\n        ERROR_MSG_SUFFIX = (\n            \"Your changes have NOT been applied. Please fix your edit command and try again.\\n\"\n            \"You either need to 1) Open the correct file and try again or 2) Specify the correct line number arguments.\\n\"\n            \"DO NOT re-run the same failed edit command. Running it again will lead to the same error.\"\n        )\n\n        if not self._is_valid_filename(file_name.name):\n            raise FileNotFoundError(\"Invalid file name.\")\n\n        if not self._is_valid_path(file_name):\n            raise FileNotFoundError(\"Invalid path or file name.\")\n\n        if not self._create_paths(file_name):\n            raise PermissionError(\"Could not access or create directories.\")\n\n        if not file_name.is_file():\n            raise FileNotFoundError(f\"File {file_name} not found.\")\n\n        if is_insert and is_append:\n            raise ValueError(\"Cannot insert and append at the same time.\")\n\n        # Use a temporary file to write changes\n        content = str(content or \"\")\n        temp_file_path = \"\"\n        src_abs_path = file_name.resolve()\n        first_error_line = None","sourceCodeStart":519,"sourceCodeEnd":555,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/tools/libs/editor.py#L519-L555","documentation":"Raised at the top of Editor._edit_file_impl when _is_valid_filename(file_name.name) fails, i.e. the basename of the file being edited does not match the editor's allowed filename pattern. This is a pre-flight validation before any filesystem access; the message is a static string and does not echo the bad name.","triggerScenarios":"Editing a file whose basename contains disallowed characters, is empty, or otherwise fails the validity regex (e.g. names with wildcards, control chars, or hidden/dot-only names depending on the pattern); passing a directory-like path so .name is empty.","commonSituations":"LLM agents passing shell-quoted or glob-style names; paths ending in '/' causing an empty basename; unusual filenames with spaces or special characters rejected by the pattern.","solutions":["Inspect Editor._is_valid_filename and normalize the basename to a plain, sane filename (letters, digits, dot, underscore, hyphen)","Strip trailing slashes, quotes, and glob characters from the path before calling","Rename the target file if its name genuinely violates the pattern"],"exampleFix":"// before\neditor._edit_file_impl(Path(\"src/'weird name?.py\"))  # FileNotFoundError: Invalid file name.\n\n// after\neditor._edit_file_impl(Path('src/weird_name.py'))","handlingStrategy":"validation","validationCode":"import re\nname = Path(file_name).name\nif not re.fullmatch(r'[A-Za-z0-9._\\- ]+', name) or name in ('', '.', '..'):\n    raise ValueError(f'unusable filename: {name!r}')\n# then call the editor","typeGuard":"def is_safe_filename(name: str) -> bool:\n    return bool(name) and name not in ('.', '..') and '/' not in name and '\\x00' not in name","tryCatchPattern":"try:\n    editor._edit_file_impl(Path(p), ...)\nexcept FileNotFoundError as e:\n    if str(e) == 'Invalid file name.':\n        p = sanitize_basename(p)  # normalize, then retry\n    raise","preventionTips":["Build paths with pathlib instead of string concatenation","Strip quotes, globs, and trailing slashes from LLM-supplied filenames","Centralize filename sanitization where untrusted input enters"],"tags":["editor","filename","validation","metagpt"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}