{"record":{"id":"48a29623e9966623","repo":"Tencent/WeKnora","slug":"invalid-file-path-w-48a296","errorCode":null,"errorMessage":"invalid file path: %w","messagePattern":"invalid file path: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/utils/security.go","lineNumber":119,"sourceCode":"\t\t}\n\t}\n\n\treturn strings.TrimSpace(input), true\n}\n\n// SafePathUnderBase 校验 filePath 是否落在 baseDir 下，防止路径遍历（如 ../../）。\n// 返回规范化的绝对路径；若路径逃逸出 baseDir 则返回错误。\nfunc SafePathUnderBase(baseDir, filePath string) (string, error) {\n\tif baseDir == \"\" || filePath == \"\" {\n\t\treturn \"\", fmt.Errorf(\"baseDir and filePath cannot be empty\")\n\t}\n\tabsBase, err := filepath.Abs(filepath.Clean(baseDir))\n\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}","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/utils/security.go#L101-L137","documentation":"Path validation in SafePathUnderBase: filepath.Abs/Clean could not normalize the user-supplied filePath (e.g., it is not a usable path on this OS). The %w wraps the filepath error; it fires during sanitization before the escape check, catching malformed paths rather than traversal attempts (which get the explicit 'path traversal denied' error).","triggerScenarios":"Thrown at internal/utils/security.go:119 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Inspect the wrapped filepath.Abs error","Correct the malformed filePath before retrying"],"exampleFix":null,"handlingStrategy":"try-catch","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"}