{"record":{"id":"ae76dc3273e0c6b1","repo":"siyuan-note/siyuan","slug":"invalid-child-template-path","errorCode":null,"errorMessage":"invalid child template path","messagePattern":"invalid child template path","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":402,"sourceCode":"\t\treturn true\n\t})\n\tif maxTemplateDocTreePlans <= count && nil != oldest {\n\t\ttemplateDocTreePlans.Delete(oldest.id)\n\t}\n\ttemplateDocTreePlans.Store(id, plan)\n\ttemplateDocTreePlansLock.Unlock()\n\ttime.AfterFunc(templateDocTreePlanTTL, func() {\n\t\ttemplateDocTreePlans.Delete(id)\n\t})\n\treturn collector.summary(id)\n}\n\nfunc resolveTemplatePackageFile(rootTemplatePath, relativePath string) (string, error) {\n\trelativePath = strings.TrimPrefix(filepath.ToSlash(strings.TrimSpace(relativePath)), \"/\")\n\tcleanPath := filepath.Clean(filepath.FromSlash(relativePath))\n\tif \"\" == cleanPath || \".\" == cleanPath || filepath.IsAbs(cleanPath) || \"..\" == cleanPath ||\n\t\tstrings.HasPrefix(cleanPath, \"..\"+string(os.PathSeparator)) {\n\t\treturn \"\", errors.New(\"invalid child template path\")\n\t}\n\ttemplatesRoot := filepath.Clean(filepath.Join(util.DataDir, \"templates\"))\n\trelRootTemplate, err := filepath.Rel(templatesRoot, filepath.Clean(rootTemplatePath))\n\tif nil != err || strings.HasPrefix(relRootTemplate, \"..\"+string(os.PathSeparator)) {\n\t\treturn \"\", errors.New(\"template path is outside templates directory\")\n\t}\n\tparts := strings.Split(filepath.ToSlash(relRootTemplate), \"/\")\n\tpackageRoot := templatesRoot\n\tif 1 < len(parts) {\n\t\tpackageRoot = filepath.Join(templatesRoot, parts[0])\n\t}\n\tabsPath := filepath.Join(packageRoot, cleanPath)\n\tif !gulu.File.IsSubPath(packageRoot, absPath) || !filelock.IsExist(absPath) {\n\t\treturn \"\", fmt.Errorf(\"child template [%s] not found in the current template package\", relativePath)\n\t}\n\trealRoot, err := filepath.EvalSymlinks(packageRoot)\n\tif nil != err {\n\t\treturn \"\", err","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L384-L420","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use a package-relative path without '..' or leading '/', e.g. \"sub/page.md\"","Move the shared template inside the same template package directory","Verify the path is non-empty after trimming"],"exampleFix":"// before\n{{child \"../../other-package/tpl.md\"}}\n// after\n{{child \"other-package/tpl.md\"}} // stays within the templates root package rules","handlingStrategy":"validation","validationCode":"clean := path.Clean(strings.TrimSpace(rel))\nif rel == \"\" || path.IsAbs(rel) || clean == \".\" || clean == \"..\" || strings.HasPrefix(clean, \"../\") { return false }","typeGuard":"func safeRelTemplatePath(rel string) bool {\n  c := path.Clean(strings.TrimSpace(rel))\n  return c != \"\" && c != \".\" && c != \"..\" && !strings.HasPrefix(c, \"../\") && !strings.HasPrefix(c, \"/\")\n}","tryCatchPattern":"p, err := resolveTemplatePackageFile(root, rel)\nif err != nil && err.Error() == \"invalid child template path\" {\n  // fix the relative path in the template\n}","preventionTips":["Never use '..' or absolute paths in child template references","Keep all children inside the template package directory","Trim and validate paths before embedding them in templates"],"tags":["go","security","path-traversal","template"],"backgroundTag":"path-traversal-blocked","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"}