{"record":{"id":"de0a29d0ab7cec1d","repo":"siyuan-note/siyuan","slug":"invalid-template-path","errorCode":null,"errorMessage":"invalid template path","messagePattern":"invalid template path","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_manage.go","lineNumber":61,"sourceCode":"\tPath     string `json:\"path\"`\n\tTarget   string `json:\"target\"`\n\tContent  string `json:\"content\"`\n\tRevision string `json:\"revision\"`\n}\n\ntype TemplateFileEntry struct {\n\tPath      string `json:\"path\"`\n\tIsDir     bool   `json:\"isDir\"`\n\tIsPackage bool   `json:\"isPackage,omitempty\"`\n}\n\n// 访问已有模板只校验目录边界，不对文件名进行清理或改写。\nfunc validateTemplateRelativePath(p string, allowRoot bool) error {\n\tif p == \"\" && allowRoot {\n\t\treturn nil\n\t}\n\tif p == \"\" || !fs.ValidPath(p) || strings.ContainsAny(p, \"\\\\:\\x00\") {\n\t\treturn errors.New(\"invalid template path\")\n\t}\n\tfor _, part := range strings.Split(p, \"/\") {\n\t\tif strings.HasPrefix(part, \".\") {\n\t\t\treturn errors.New(\"hidden template paths are reserved\")\n\t\t}\n\t}\n\treturn nil\n}\n\n// 新名称保持跨平台可用，已有父目录沿用原名。\nfunc validateNewTemplateName(p string) error {\n\tpart := path.Base(p)\n\tdevice := strings.ToUpper(strings.SplitN(part, \".\", 2)[0])\n\tif device == \"CON\" || device == \"PRN\" || device == \"AUX\" || device == \"NUL\" || (len(device) == 4 && (strings.HasPrefix(device, \"COM\") || strings.HasPrefix(device, \"LPT\")) && device[3] >= '1' && device[3] <= '9') {\n\t\treturn errors.New(\"reserved template file name\")\n\t}\n\tif strings.HasPrefix(part, \".\") || strings.TrimSpace(part) != part || strings.HasSuffix(part, \".\") || strings.ContainsAny(part, \"\\\\:<>\\\"|?*\") || strings.ContainsFunc(part, unicode.IsControl) {\n\t\treturn errors.New(\"invalid template path component\")","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_manage.go#L43-L79","documentation":"validateTemplateRelativePath rejects any relative template path that is empty when a root entry is not allowed, is not a valid fs path per fs.ValidPath, or contains backslash, colon, or NUL characters. The kernel enforces this so template storage stays inside the templates directory and filenames remain usable on every platform.","triggerScenarios":"Calling DocSaveAsTemplateInDirectory or checkTemplateFilePath with a path containing '\\\\' or ':' separators, an absolute path like '/abs/name.tpl', '..' segments, or an empty string when allowRoot is false.","commonSituations":"Windows-style paths passed from a client (C:\\templates\\x.md), user-supplied directory input not normalized before the API call, or URL-encoded traversal attempts like '../x.md' that survive normalization as invalid paths.","solutions":["Use forward slashes and a clean relative path such as 'subdir/name.md'","Strip any leading root separator and collapse '.'/'..' segments before calling","Reject or convert backslashes and colons (drive letters) to '/' before invoking the API","Pass an empty path only when the API allows operating on the templates root"],"exampleFix":"// before\nDocSaveAsTemplateInDirectory(\"C:\\\\templates\\\\note.md\", ...)\n// after\nDocSaveAsTemplateInDirectory(\"templates/note.md\", ...)","handlingStrategy":"validation","validationCode":"func validTemplateRelPath(p string) bool {\n    return p != \"\" && fs.ValidPath(p) && !strings.ContainsAny(p, \"\\\\:\\x00\")\n}","typeGuard":null,"tryCatchPattern":"if err := validateTemplateRelativePath(p, false); err != nil { /* surface to user as invalid path input */ }","preventionTips":["Always pass slash-separated relative paths","Normalize client paths before calling the API","Never accept absolute paths or drive letters for template locations"],"tags":["path-validation","filesystem","templates"],"backgroundTag":"path-traversal-blocked","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}