{"record":{"id":"f71b798454a87c74","repo":"siyuan-note/siyuan","slug":"reserved-template-file-name","errorCode":null,"errorMessage":"reserved template file name","messagePattern":"reserved template file name","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_manage.go","lineNumber":76,"sourceCode":"\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\")\n\t}\n\treturn nil\n}\n\n// 清单与目录共同标识模板包，保留目录身份以维持搜索和集市更新。\nfunc isManagedTemplatePackage(root *os.Root, p string) bool {\n\t_, err := root.Lstat(path.Join(p, \"template.json\"))\n\treturn err == nil\n}\n\nfunc openTemplateRoot() (*os.Root, error) {\n\tif err := os.MkdirAll(filepath.Join(util.DataDir, \"templates\"), 0755); err != nil {\n\t\treturn nil, err\n\t}\n\treturn os.OpenRoot(filepath.Join(util.DataDir, \"templates\"))","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_manage.go#L58-L94","documentation":"validateNewTemplateName rejects new template file names whose base is a Windows reserved device name: CON, PRN, AUX, NUL, or COM1-9/LPT1-9. Creating such a file would be unreachable or dangerous on Windows, so the kernel blocks it up front for cross-platform safety.","triggerScenarios":"ManageTemplateFiles receiving a new template name like 'con.md', 'lpt1.txt', 'Com3.md' — the check upper-cases the text before the first dot, so any extension does not help.","commonSituations":"Users on macOS/Linux choosing device-like names that are legal there but break on Windows, batch imports of files named after devices, or scripts generating template names from unfiltered input.","solutions":["Choose a different file name that is not a Windows device name","Prefix or suffix the name (e.g. 'con-template.md') before calling ManageTemplateFiles","Validate user-supplied names against the reserved list in the client before submitting"],"exampleFix":"// before\nManageTemplateFiles(newName: \"con.md\", ...)\n// after\nManageTemplateFiles(newName: \"con-notes.md\", ...)","handlingStrategy":"validation","validationCode":"var reserved = map[string]bool{\"CON\":true,\"PRN\":true,\"AUX\":true,\"NUL\":true}\nfunc isReservedName(name string) bool {\n    d := strings.ToUpper(strings.SplitN(name, \".\", 2)[0])\n    if reserved[d] { return true }\n    if len(d) == 4 && (strings.HasPrefix(d,\"COM\")||strings.HasPrefix(d,\"LPT\")) && d[3]>='1' && d[3]<='9' { return true }\n    return false\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check names against the Windows reserved list even on Linux/macOS","Auto-suffix reserved names in UIs before submitting"],"tags":["naming","windows-compatibility","validation"],"backgroundTag":"invalid-identifier-format","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"}