{"record":{"id":"b359aac2fcc28f04","repo":"siyuan-note/siyuan","slug":"asset-path-resolves-outside-notebook-assets-direct","errorCode":null,"errorMessage":"asset path resolves outside notebook assets directory","messagePattern":"asset path resolves outside notebook assets directory","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"kernel/model/asset_download_read.go","lineNumber":112,"sourceCode":"\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\")\n\t\t\t}\n\t\t}\n\t\tif lookupPath == relativePath {\n\t\t\treturn absPath, nil\n\t\t}\n\t\tcandidates = append(candidates, absPath)\n\t}\n\tsort.Strings(candidates)\n\tif len(candidates) > 0 {\n\t\treturn candidates[0], nil\n\t}\n\treturn \"\", nil\n}\n\n// ensureReadableAssetLocal 先检查加密笔记本准入，再下载原始密文；实际读取仍须认证解密。\nfunc ensureReadableAssetLocal(absPath string) error {\n\tboxID := ExtractBoxIDFromAssetsPath(absPath)\n\tif boxID != \"\" && IsEncryptedBox(boxID) && !IsBoxUnlocked(boxID) {","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/asset_download_read.go#L94-L130","documentation":"deferredAssetPathFromFiles performs a second, real-path based containment check: it resolves the asset path, the notebook root, and the data dir via ResolveRealPath (following symlinks), and requires dataDir ⊇ notebookRoot ⊇ assetsRoot ⊇ resolved path. If any resolution fails or the chain of containment breaks — typically because symlinks point outside the notebook — it returns 'asset path resolves outside notebook assets directory'. This ensures deferred asset downloads only touch files actually inside the owning notebook's assets folder.","triggerScenarios":"deferredAssetPath on an asset whose resolved real path escapes the notebook's assets dir, e.g. assets is a symlink to an external directory, or a nested symlink re-points into another notebook/outside the data dir; exercised by TestDeferredAssetPathRejectsMissingLeafThroughEscapingSymlink.","commonSituations":"User symlinked a notebook's assets folder to a shared external directory; workspace assembled from linked subdirectories; snapshot restore created symlink layouts that ResolveRealPath normalizes differently across OSes (macOS /private vs /).","solutions":["Remove symlinks inside the notebook and keep real files/folders under <notebook>/assets","If sharing media, use SiYuan's own asset referencing or copy files into assets rather than linking","On macOS, avoid /tmp or /var symlinked locations whose real path (/private/var/...) breaks containment checks","Verify with ResolveRealPath that dataDir, notebookRoot and the asset path form a strict containment chain"],"exampleFix":"// before\nnotebook/assets -> /mnt/shared/media\n// after\ncp -r /mnt/shared/media/* notebook/assets/\nrm notebook/assets  # remove the symlink, keep the real directory","handlingStrategy":"validation","validationCode":"root, err := model.ResolveRealPath(filepath.Join(util.DataDir, boxID))\nresolved, rerr := model.ResolveRealPath(absPath)\nif err != nil || rerr != nil || !gulu.File.IsSubPath(root, resolved) {\n    return errors.New(\"asset resolves outside notebook\")\n}","typeGuard":null,"tryCatchPattern":"p, err := model.DeferredAssetPath(link)\nif err != nil && strings.Contains(err.Error(), \"outside notebook assets directory\") {\n    relocateAssetIntoNotebook(link) // copy file into assets/, drop symlink\n}","preventionTips":["Keep real directories (no symlinks) under notebook/assets","On macOS beware /tmp,/var symlinks to /private/* breaking real-path containment","Do not share one assets directory across notebooks via symlinks; copy instead","Run a containment audit (ResolveRealPath chain) after restructuring the workspace"],"tags":["symlink","path-resolution","security","filesystem"],"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"}