siyuan-note/siyuan · warning

asset path escapes data directory

Error message

asset path escapes data directory

What it means

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).

Solutions

  1. Locate the document containing the offending asset link and fix the URL to a normal 'assets/...' relative path
  2. Never use ../ or absolute paths in asset links; move the file into the notebook's assets folder and relink
  3. Sanitize/normalize imported content (pasted HTML, markdown imports) before saving
  4. If the path is legitimate but rejected, check for case/separator mismatches after cross-OS copy and correct the link

Example fix

// before: crafted link in document
<img src="assets/../../secrets/key.pem">
// after: place file under assets and use a relative link
<img src="assets/key.pem">
Defensive patterns

Strategy: validation

Validate before calling

abs := filepath.Join(util.DataDir, filepath.FromSlash(lookupPath))
if !gulu.File.IsSubPath(util.DataDir, abs) {
    return errors.New("asset path escapes data directory")
}

Try / catch

p, err := model.DeferredAssetPath(assetLink)
if err != nil && strings.Contains(err.Error(), "escapes data directory") {
    sanitizeAssetLink(assetLink) // strip ../ and absolute components
}

Prevention

When it happens

Trigger: deferredAssetPath / deferredAssetPathFromFiles processing an asset link whose resolved lookupPath contains .. segments or otherwise normalizes outside util.DataDir; tests TestDeferredAssetPathWithoutLocalDirectories and TestDeferredAssetPathRejectsMissingLeafThroughEscapingSymlink exercise this.

Common situations: 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.

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

Appendix: source

Thrown at kernel/model/asset_download_read.go:94

			}
			lookupPath = target
		} else {
			switch {
			case candidate == relativePath, strings.HasSuffix(candidate, "/"+relativePath):
			case strings.HasPrefix(candidate, relativePath+"/"):
				lookupPath = relativePath
			case strings.Contains(candidate, "/"+relativePath+"/"):
				lookupPath = candidate[:strings.Index(candidate, "/"+relativePath+"/")+len(relativePath)+1]
			default:
				continue
			}
		}
		absPath := filepath.Join(util.DataDir, filepath.FromSlash(lookupPath))
		if !includeEncrypted && IsEncryptedAssetPath(absPath) {
			continue
		}
		if !gulu.File.IsSubPath(util.DataDir, absPath) {
			return "", errors.New("asset path escapes data directory")
		}
		if boxID == "" && !IsEncryptedAssetPath(absPath) {
			if _, _, resolveErr := ResolveDataAssetPath(lookupPath); resolveErr != nil {
				return "", resolveErr
			}
		} else {
			resolvedBoxID := boxID
			if resolvedBoxID == "" {
				resolvedBoxID = ExtractBoxIDFromAssetsPath(absPath)
			}
			root, rootErr := ResolveAssetPathWithMissingLeaf(filepath.Join(util.DataDir, resolvedBoxID, "assets"))
			resolved, resolveErr := ResolveAssetPathWithMissingLeaf(absPath)
			notebookRoot, notebookErr := ResolveRealPath(filepath.Join(util.DataDir, resolvedBoxID))
			dataRoot, dataErr := ResolveRealPath(util.DataDir)
			if rootErr != nil || resolveErr != nil || notebookErr != nil || dataErr != nil ||
				!gulu.File.IsSubPath(dataRoot, notebookRoot) ||
				!gulu.File.IsSubPath(notebookRoot, root) || !gulu.File.IsSubPath(root, resolved) {
				return "", errors.New("asset path resolves outside notebook assets directory")

View on GitHub (pinned to 9f775e8a12)