{"record":{"id":"3cf6837602f87bc2","repo":"siyuan-note/siyuan","slug":"asset-path-s-is-outside-assets-directory-s","errorCode":null,"errorMessage":"asset path [%s] is outside assets directory [%s]","messagePattern":"asset path \\[(.+?)\\] is outside assets directory \\[(.+?)\\]","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/assets.go","lineNumber":2828,"sourceCode":"\t\treturn nil\n\t})\n\tif walkErr != nil {\n\t\tif os.IsNotExist(walkErr) {\n\t\t\treturn\n\t\t}\n\t\treturn nil, fmt.Errorf(\"walk global assets [%s] failed: %w\", dataAssetsAbsPath, walkErr)\n\t}\n\treturn\n}\n\nfunc assetPathMapKey(assetsDirPath, assetPath string, isDir bool) (ret string, err error) {\n\trelPath, err := filepath.Rel(assetsDirPath, assetPath)\n\tif err != nil {\n\t\treturn\n\t}\n\trelPath = filepath.ToSlash(relPath)\n\tif relPath == \".\" || relPath == \"..\" || strings.HasPrefix(relPath, \"../\") || path.IsAbs(relPath) {\n\t\terr = fmt.Errorf(\"asset path [%s] is outside assets directory [%s]\", assetPath, assetsDirPath)\n\t\treturn\n\t}\n\n\tret = path.Join(\"assets\", relPath)\n\tif isDir {\n\t\tret += \"/\"\n\t}\n\treturn\n}\n\n// copyBoxAssetsToDataAssets 将笔记本路径下所有（包括子文档）的 assets 复制一份到 data/assets 中。\nfunc copyBoxAssetsToDataAssets(boxID string) error {\n\tboxLocalPath := filepath.Join(util.DataDir, boxID)\n\treturn copyAssetsToDataAssets(boxLocalPath)\n}\n\n// copyDocAssetsToDataAssets 将文档路径下所有（包括子文档）的 assets 复制一份到 data/assets 中。\nfunc copyDocAssetsToDataAssets(boxID, parentDocPath string) error {","sourceCodeStart":2810,"sourceCodeEnd":2846,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/assets.go#L2810-L2846","documentation":"assetPathMapKey computes a stable web-style asset key from a filesystem path; if the computed relative path escapes the assets directory ('..', absolute, or '../' prefix), the kernel rejects it as a safety measure. This prevents assets outside the designated assets root from being indexed or addressed via the assets namespace (path traversal).","triggerScenarios":"Calling assetPathMapKey (directly or via the asset listing walks) with an assetPath whose relative path to assetsDirPath is '.', '..', starts with '../', or is absolute — e.g. mismatched assetsDirPath/assetPath roots, symlinks pointing outside the assets tree, or a caller passing an unnormalized path.","commonSituations":"Custom tooling passing wrong base directory; symlinked asset folders pointing outside the workspace; workspaces moved/migrated with stale absolute paths; plugins or scripts constructing asset paths manually.","solutions":["Ensure assetPath is genuinely inside assetsDirPath (compute filepath.Rel yourself to pre-check)","Normalize both paths (filepath.Abs / filepath.Clean) before calling; resolve symlinks that escape the assets tree","Move external assets into the workspace's assets folder so they are addressable","Fix callers that hard-code absolute paths instead of deriving them from the data assets root"],"exampleFix":"// before: path escapes assets dir\nkey, err := assetPathMapKey('/data/assets', '/data/other/pic.png', false)\n// after: keep asset inside assets root\nkey, err := assetPathMapKey('/data/assets', '/data/assets/pic.png', false)","handlingStrategy":"validation","validationCode":"import path from 'path';\nfunction isInsideAssets(assetsDir, assetPath) {\n  const rel = path.relative(assetsDir, assetPath);\n  return rel !== '' && rel !== '..' && !rel.startsWith('..' + path.sep) && !path.isAbsolute(rel);\n}","typeGuard":"const isSafeRel = (rel) => typeof rel === 'string' && rel !== '.' && rel !== '..' && !rel.startsWith('../') && !path.isAbsolute(rel);","tryCatchPattern":"if (!isInsideAssets(assetsDir, p)) { rejectAsset(p); return; }\ntry { await buildAssetMap(p) } catch (e) {\n  if (e.message.includes('is outside assets directory')) logTraversalAttempt(p);\n}","preventionTips":["Resolve symlinks before passing paths into asset APIs","Always derive asset paths from the data assets root, never from user input","Normalize (clean/abs) both directory and file paths before comparison"],"tags":["path-traversal","validation","assets"],"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"}