{"record":{"id":"4bfcb084c67d8e25","repo":"siyuan-note/siyuan","slug":"asset-path-escapes-data-directory","errorCode":null,"errorMessage":"asset path escapes data directory","messagePattern":"asset path escapes data directory","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"kernel/model/asset_download_read.go","lineNumber":94,"sourceCode":"\t\t\t}\n\t\t\tlookupPath = target\n\t\t} else {\n\t\t\tswitch {\n\t\t\tcase candidate == relativePath, strings.HasSuffix(candidate, \"/\"+relativePath):\n\t\t\tcase strings.HasPrefix(candidate, relativePath+\"/\"):\n\t\t\t\tlookupPath = relativePath\n\t\t\tcase strings.Contains(candidate, \"/\"+relativePath+\"/\"):\n\t\t\t\tlookupPath = candidate[:strings.Index(candidate, \"/\"+relativePath+\"/\")+len(relativePath)+1]\n\t\t\tdefault:\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t\tabsPath := filepath.Join(util.DataDir, filepath.FromSlash(lookupPath))\n\t\tif !includeEncrypted && IsEncryptedAssetPath(absPath) {\n\t\t\tcontinue\n\t\t}\n\t\tif !gulu.File.IsSubPath(util.DataDir, absPath) {\n\t\t\treturn \"\", errors.New(\"asset path escapes data directory\")\n\t\t}\n\t\tif boxID == \"\" && !IsEncryptedAssetPath(absPath) {\n\t\t\tif _, _, resolveErr := ResolveDataAssetPath(lookupPath); resolveErr != nil {\n\t\t\t\treturn \"\", resolveErr\n\t\t\t}\n\t\t} else {\n\t\t\tresolvedBoxID := boxID\n\t\t\tif resolvedBoxID == \"\" {\n\t\t\t\tresolvedBoxID = ExtractBoxIDFromAssetsPath(absPath)\n\t\t\t}\n\t\t\troot, rootErr := ResolveAssetPathWithMissingLeaf(filepath.Join(util.DataDir, resolvedBoxID, \"assets\"))\n\t\t\tresolved, resolveErr := ResolveAssetPathWithMissingLeaf(absPath)\n\t\t\tnotebookRoot, notebookErr := ResolveRealPath(filepath.Join(util.DataDir, resolvedBoxID))\n\t\t\tdataRoot, dataErr := ResolveRealPath(util.DataDir)\n\t\t\tif rootErr != nil || resolveErr != nil || notebookErr != nil || dataErr != nil ||\n\t\t\t\t!gulu.File.IsSubPath(dataRoot, notebookRoot) ||\n\t\t\t\t!gulu.File.IsSubPath(notebookRoot, root) || !gulu.File.IsSubPath(root, resolved) {\n\t\t\t\treturn \"\", errors.New(\"asset path resolves outside notebook assets directory\")","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/asset_download_read.go#L76-L112","documentation":"deferredAssetPathFromFiles builds a candidate absolute path by joining util.DataDir with a lookup path taken from asset link files; if the resulting path is not a sub-path of the data directory (gulu.File.IsSubPath check), it rejects it with 'asset path escapes data directory'. This is a security guard against path traversal via crafted asset links (../ sequences or absolute-ish link targets).","triggerScenarios":"deferredAssetPath / deferredAssetPathFromFiles processing an asset link whose resolved lookupPath contains .. segments or otherwise normalizes outside util.DataDir; tests TestDeferredAssetPathWithoutLocalDirectories and TestDeferredAssetPathRejectsMissingLeafThroughEscapingSymlink exercise this.","commonSituations":"Documents imported from untrusted sources containing crafted asset URLs like assets/../../etc/passwd; notebooks copied between OSes where slash/backslash normalization changes the resolved path; user hand-edited .sy content or asset links.","solutions":["Locate the document containing the offending asset link and fix the URL to a normal 'assets/...' relative path","Never use ../ or absolute paths in asset links; move the file into the notebook's assets folder and relink","Sanitize/normalize imported content (pasted HTML, markdown imports) before saving","If the path is legitimate but rejected, check for case/separator mismatches after cross-OS copy and correct the link"],"exampleFix":"// before: crafted link in document\n<img src=\"assets/../../secrets/key.pem\">\n// after: place file under assets and use a relative link\n<img src=\"assets/key.pem\">","handlingStrategy":"validation","validationCode":"abs := filepath.Join(util.DataDir, filepath.FromSlash(lookupPath))\nif !gulu.File.IsSubPath(util.DataDir, abs) {\n    return errors.New(\"asset path escapes data directory\")\n}","typeGuard":null,"tryCatchPattern":"p, err := model.DeferredAssetPath(assetLink)\nif err != nil && strings.Contains(err.Error(), \"escapes data directory\") {\n    sanitizeAssetLink(assetLink) // strip ../ and absolute components\n}","preventionTips":["Sanitize pasted/imported HTML and markdown asset URLs before saving documents","Reject or rewrite asset links containing .. or absolute path components at ingestion","Never hand-edit .sy files to reference files outside the data dir","Keep asset files inside the notebook's assets directory and relink through the UI"],"tags":["path-traversal","security","asset-links","validation"],"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"}