siyuan-note/siyuan · error

invalid child template path

Error message

invalid child template path

What it means

resolveTemplatePackageFile validates the relative child-template path before joining it under the templates root. Empty, '.', absolute, '..', or parent-escaping (../) paths are rejected as 'invalid child template path' to prevent path traversal outside the template package.

Solutions

  1. Use a package-relative path without '..' or leading '/', e.g. "sub/page.md"
  2. Move the shared template inside the same template package directory
  3. Verify the path is non-empty after trimming

Example fix

// before
{{child "../../other-package/tpl.md"}}
// after
{{child "other-package/tpl.md"}} // stays within the templates root package rules
Defensive patterns

Strategy: validation

Validate before calling

clean := path.Clean(strings.TrimSpace(rel))
if rel == "" || path.IsAbs(rel) || clean == "." || clean == ".." || strings.HasPrefix(clean, "../") { return false }

Type guard

func safeRelTemplatePath(rel string) bool {
  c := path.Clean(strings.TrimSpace(rel))
  return c != "" && c != "." && c != ".." && !strings.HasPrefix(c, "../") && !strings.HasPrefix(c, "/")
}

Try / catch

p, err := resolveTemplatePackageFile(root, rel)
if err != nil && err.Error() == "invalid child template path" {
  // fix the relative path in the template
}

Prevention

When it happens

Trigger: A doc-tree template action references a child template with a relative path that is empty, absolute (e.g. /etc/passwd or C:\...), or contains '..' components escaping the package after cleaning.

Common situations: Template authors writing {{child ..\shared\tpl}} to reach a sibling package; copy-pasted absolute paths from the filesystem; trailing slashes or whitespace-only path values.

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/ae76dc3273e0c6b1. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/template_doc_tree.go:402

		return true
	})
	if maxTemplateDocTreePlans <= count && nil != oldest {
		templateDocTreePlans.Delete(oldest.id)
	}
	templateDocTreePlans.Store(id, plan)
	templateDocTreePlansLock.Unlock()
	time.AfterFunc(templateDocTreePlanTTL, func() {
		templateDocTreePlans.Delete(id)
	})
	return collector.summary(id)
}

func resolveTemplatePackageFile(rootTemplatePath, relativePath string) (string, error) {
	relativePath = strings.TrimPrefix(filepath.ToSlash(strings.TrimSpace(relativePath)), "/")
	cleanPath := filepath.Clean(filepath.FromSlash(relativePath))
	if "" == cleanPath || "." == cleanPath || filepath.IsAbs(cleanPath) || ".." == cleanPath ||
		strings.HasPrefix(cleanPath, ".."+string(os.PathSeparator)) {
		return "", errors.New("invalid child template path")
	}
	templatesRoot := filepath.Clean(filepath.Join(util.DataDir, "templates"))
	relRootTemplate, err := filepath.Rel(templatesRoot, filepath.Clean(rootTemplatePath))
	if nil != err || strings.HasPrefix(relRootTemplate, ".."+string(os.PathSeparator)) {
		return "", errors.New("template path is outside templates directory")
	}
	parts := strings.Split(filepath.ToSlash(relRootTemplate), "/")
	packageRoot := templatesRoot
	if 1 < len(parts) {
		packageRoot = filepath.Join(templatesRoot, parts[0])
	}
	absPath := filepath.Join(packageRoot, cleanPath)
	if !gulu.File.IsSubPath(packageRoot, absPath) || !filelock.IsExist(absPath) {
		return "", fmt.Errorf("child template [%s] not found in the current template package", relativePath)
	}
	realRoot, err := filepath.EvalSymlinks(packageRoot)
	if nil != err {
		return "", err

View on GitHub (pinned to 9f775e8a12)