siyuan-note/siyuan · error
invalid template source
Error message
invalid template source
What it means
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.
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
Example fix
// before: content decoded as Latin-1 then sent as-is
const content = Buffer.from(raw, "latin1").toString();
// after
const content = Buffer.from(raw, "latin1").toString("utf8"); // validate UTF-8 before send Defensive patterns
Strategy: validation
Validate before calling
if !utf8.ValidString(content) { /* fix encoding before calling write APIs */ } Prevention
- Validate UTF-8 on the client before submitting template content
- Avoid decoding payloads with mismatched encodings
- Never concatenate binary data into template strings
When it happens
Trigger: DocSaveAsTemplateInDirectory or ManageTemplateFiles receiving template content containing invalid UTF-8 sequences (surrogates from broken JSON decoding, binary bytes in the payload).
Common situations: Clients sending content decoded with the wrong encoding, JSON payloads with lone surrogates, programmatic template generation concatenating binary data.
Related errors
- 344
- only UTF-8 text files can be edited
- skill resource is not valid UTF-8
- template source is not UTF-8
- conditional replacement of encrypted documents is not…
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/d745700067edd7f1.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/model/template_manage.go:171
return "", err
}
if !info.Mode().IsRegular() || info.Size() > maxTemplateSourceSize {
return "", errors.New("invalid template source file")
}
content, err := root.ReadFile(p)
if err != nil {
return "", err
}
if !utf8.Valid(content) {
return "", errors.New("template source is not UTF-8")
}
return string(content), nil
}
// 同目录临时文件写入完成后替换,写入失败时保留原模板。
func writeTemplateSource(root *os.Root, p, content string, create bool) error {
if !utf8.ValidString(content) {
return errors.New("invalid template source")
}
if create {
file, err := root.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return err
}
_, err = file.WriteString(content)
if err == nil {
err = file.Sync()
}
closeErr := file.Close()
if err != nil {
root.Remove(p)
return err
}
return closeErr
}
tmp := path.Join(path.Dir(p), ".template-"+ast.NewNodeID())View on GitHub (pinned to 9f775e8a12)