{"record":{"id":"edb012001b42f7e4","repo":"Tencent/WeKnora","slug":"filename-cannot-be-empty","errorCode":null,"errorMessage":"fileName cannot be empty","messagePattern":"fileName cannot be empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/utils/security.go","lineNumber":132,"sourceCode":"\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"invalid base dir: %w\", err)\n\t}\n\tabsPath, err := filepath.Abs(filepath.Clean(filePath))\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"invalid file path: %w\", err)\n\t}\n\tsep := string(filepath.Separator)\n\tif absPath != absBase && !strings.HasPrefix(absPath, absBase+sep) {\n\t\treturn \"\", fmt.Errorf(\"path traversal denied: path is outside base directory\")\n\t}\n\treturn absPath, nil\n}\n\n// SafeFileName 校验并返回安全的“仅文件名”部分，防止路径遍历。\n// 仅保留最后一个路径成分，禁止 \"..\"、空名或仅含点，用于 SaveBytes 等场景。\nfunc SafeFileName(fileName string) (string, error) {\n\tif fileName == \"\" {\n\t\treturn \"\", fmt.Errorf(\"fileName cannot be empty\")\n\t}\n\tbase := filepath.Base(filepath.Clean(fileName))\n\tif base == \"\" || base == \".\" || base == \"..\" {\n\t\treturn \"\", fmt.Errorf(\"invalid fileName: path traversal or empty name\")\n\t}\n\tif strings.Contains(base, \"..\") {\n\t\treturn \"\", fmt.Errorf(\"invalid fileName: contains path traversal\")\n\t}\n\tif len(base) > 255 {\n\t\treturn \"\", fmt.Errorf(\"fileName too long\")\n\t}\n\treturn base, nil\n}\n\n// SafeObjectKey 校验对象存储的 key（如 COS/MinIO objectName），禁止包含 \"..\" 等路径遍历\nfunc SafeObjectKey(objectKey string) error {\n\tif objectKey == \"\" {\n\t\treturn fmt.Errorf(\"object key cannot be empty\")","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/utils/security.go#L114-L150","documentation":"Input validation at the top of SafeFileName: the fileName argument is an empty string, so there is no name to extract a safe final path component from. Raised before Base/Clean processing; used by SaveBytes to reject callers that pass blank filenames.","triggerScenarios":"Thrown at internal/utils/security.go:132 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Provide a non-empty file name","Default to a generated name when the client omits one"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}