{"record":{"id":"d745700067edd7f1","repo":"siyuan-note/siyuan","slug":"invalid-template-source","errorCode":null,"errorMessage":"invalid template source","messagePattern":"invalid template source","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_manage.go","lineNumber":171,"sourceCode":"\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)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\t_, err = file.WriteString(content)\n\t\tif err == nil {\n\t\t\terr = file.Sync()\n\t\t}\n\t\tcloseErr := file.Close()\n\t\tif err != nil {\n\t\t\troot.Remove(p)\n\t\t\treturn err\n\t\t}\n\t\treturn closeErr\n\t}\n\ttmp := path.Join(path.Dir(p), \".template-\"+ast.NewNodeID())","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_manage.go#L153-L189","documentation":"writeTemplateSource validates the incoming content string with utf8.ValidString before any file is created or replaced, throwing this error for invalid UTF-8. It guards the write path so corrupt or wrongly-encoded content never lands in the templates directory.","triggerScenarios":"DocSaveAsTemplateInDirectory or ManageTemplateFiles receiving template content containing invalid UTF-8 sequences (surrogates from broken JSON decoding, binary bytes in the payload).","commonSituations":"Clients sending content decoded with the wrong encoding, JSON payloads with lone surrogates, programmatic template generation concatenating binary data.","solutions":["Ensure the request body/content is valid UTF-8 before calling the API","Re-decode the source with the correct original encoding (e.g. GBK -> UTF-8)","Strip or repair invalid byte sequences client-side, then retry"],"exampleFix":"// before: content decoded as Latin-1 then sent as-is\nconst content = Buffer.from(raw, \"latin1\").toString();\n// after\nconst content = Buffer.from(raw, \"latin1\").toString(\"utf8\"); // validate UTF-8 before send","handlingStrategy":"validation","validationCode":"if !utf8.ValidString(content) { /* fix encoding before calling write APIs */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate UTF-8 on the client before submitting template content","Avoid decoding payloads with mismatched encodings","Never concatenate binary data into template strings"],"tags":["encoding","utf-8","write"],"backgroundTag":"invalid-encoding","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"}