{"record":{"id":"2c2f02993db84ab2","repo":"siyuan-note/siyuan","slug":"invalid-template-source-file","errorCode":null,"errorMessage":"invalid template source file","messagePattern":"invalid template source file","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_manage.go","lineNumber":156,"sourceCode":"\t\t})\n\t} else {\n\t\tif info.Size() > maxTemplateSourceSize {\n\t\t\treturn \"\", errors.New(\"template source is too large\")\n\t\t}\n\t\tvar content []byte\n\t\tcontent, err = root.ReadFile(p)\n\t\th.Write(content)\n\t}\n\treturn fmt.Sprintf(\"%x\", h.Sum(nil)), err\n}\n\nfunc readTemplateSource(root *os.Root, p string) (string, error) {\n\tinfo, err := root.Stat(p)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif !info.Mode().IsRegular() || info.Size() > maxTemplateSourceSize {\n\t\treturn \"\", errors.New(\"invalid template source file\")\n\t}\n\tcontent, err := root.ReadFile(p)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif !utf8.Valid(content) {\n\t\treturn \"\", errors.New(\"template source is not UTF-8\")\n\t}\n\treturn string(content), nil\n}\n\n// 同目录临时文件写入完成后替换，写入失败时保留原模板。\nfunc writeTemplateSource(root *os.Root, p, content string, create bool) error {\n\tif !utf8.ValidString(content) {\n\t\treturn errors.New(\"invalid template source\")\n\t}\n\tif create {\n\t\tfile, err := root.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_manage.go#L138-L174","documentation":"readTemplateSource rejects files that are not regular files or that exceed maxTemplateSourceSize, returning this single combined error. It guarantees the kernel only reads bounded, plain-file template content.","triggerScenarios":"ManageTemplateFiles reading a template that is a symlink or special file, or whose Stat-reported size is over maxTemplateSourceSize.","commonSituations":"Symlinked template files, oversized templates from bulk imports, device/FIFO files present in the templates directory.","solutions":["Replace the symlink/special file with a real regular file","Reduce the file size below maxTemplateSourceSize","Remove the offending entry from the templates directory"],"exampleFix":"# before: templates/x.md -> elsewhere.md (symlink)\n# after\ncp -L elsewhere.md templates/x.md  # regular file under the size cap","handlingStrategy":"validation","validationCode":"info, err := os.Lstat(p)\nif err != nil || !info.Mode().IsRegular() || info.Size() > maxTemplateSourceSize { /* refuse before calling */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify templates are regular files under the size cap before import","Remove special files from the templates directory"],"tags":["file-validation","file-size","templates"],"backgroundTag":"file-read-failed","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"}