siyuan-note/siyuan · error

invalid template path

Error message

invalid template path

What it means

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.

Solutions

  1. Use forward slashes and a clean relative path such as 'subdir/name.md'
  2. Strip any leading root separator and collapse '.'/'..' segments before calling
  3. Reject or convert backslashes and colons (drive letters) to '/' before invoking the API
  4. Pass an empty path only when the API allows operating on the templates root

Example fix

// before
DocSaveAsTemplateInDirectory("C:\\templates\\note.md", ...)
// after
DocSaveAsTemplateInDirectory("templates/note.md", ...)
Defensive patterns

Strategy: validation

Validate before calling

func validTemplateRelPath(p string) bool {
    return p != "" && fs.ValidPath(p) && !strings.ContainsAny(p, "\\:\x00")
}

Try / catch

if err := validateTemplateRelativePath(p, false); err != nil { /* surface to user as invalid path input */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Path traversal blocked: "path escapes the workspace" and "outside site root" errors when a path will not stay inside its allowed directory — this error's family across 26 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/de0a29d0ab7cec1d. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/template_manage.go:61

	Path     string `json:"path"`
	Target   string `json:"target"`
	Content  string `json:"content"`
	Revision string `json:"revision"`
}

type TemplateFileEntry struct {
	Path      string `json:"path"`
	IsDir     bool   `json:"isDir"`
	IsPackage bool   `json:"isPackage,omitempty"`
}

// 访问已有模板只校验目录边界,不对文件名进行清理或改写。
func validateTemplateRelativePath(p string, allowRoot bool) error {
	if p == "" && allowRoot {
		return nil
	}
	if p == "" || !fs.ValidPath(p) || strings.ContainsAny(p, "\\:\x00") {
		return errors.New("invalid template path")
	}
	for _, part := range strings.Split(p, "/") {
		if strings.HasPrefix(part, ".") {
			return errors.New("hidden template paths are reserved")
		}
	}
	return nil
}

// 新名称保持跨平台可用,已有父目录沿用原名。
func validateNewTemplateName(p string) error {
	part := path.Base(p)
	device := strings.ToUpper(strings.SplitN(part, ".", 2)[0])
	if 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') {
		return errors.New("reserved template file name")
	}
	if strings.HasPrefix(part, ".") || strings.TrimSpace(part) != part || strings.HasSuffix(part, ".") || strings.ContainsAny(part, "\\:<>\"|?*") || strings.ContainsFunc(part, unicode.IsControl) {
		return errors.New("invalid template path component")

View on GitHub (pinned to 9f775e8a12)