siyuan-note/siyuan · error

encrypted box asset must be written inside the box…

Error message

encrypted box asset must be written inside the box directory, got global path: %s

What it means

writeAssetFile refuses to write an asset for an encrypted notebook when the target path is outside any box directory (e.g. the global data/assets folder). Encrypted-notebook content must stay inside its box so it can be encrypted with the box's DEK; a global path would force plaintext on disk, so the write fails closed.

Solutions

  1. Ensure the target path is inside the encrypted notebook's own directory, e.g. <box>/assets/, before writing
  2. Create/resolve the box-level assets directory (getAssetsDir creates it for encrypted boxes) instead of falling back to data/assets
  3. If the target notebook need not be encrypted, use a non-encrypted box or a global path without an encrypted boxID

Example fix

// before: global path for encrypted box
writePath = "/data/assets/photo.png"; boxID = "20240101-enc-box"
// after: path inside the encrypted box
writePath = "/data/20240101-enc-box/assets/photo.png"; boxID = "20240101-enc-box"
Defensive patterns

Strategy: validation

Validate before calling

func ensureInsideEncryptedBox(writePath, boxID string) error {
    if boxID != "" && IsEncryptedBox(boxID) && ExtractBoxIDFromAssetsPath(writePath) == "" {
        return fmt.Errorf("path %s is outside box %s", writePath, boxID)
    }
    return nil
}

Try / catch

if err := writeAssetFile(writePath, src, boxID, name); err != nil {
    if strings.Contains(err.Error(), "must be written inside the box directory") {
        // recompute writePath under the box's assets dir and retry
    }
}

Prevention

When it happens

Trigger: Calling one of writeAssetFile's callers (UploadAssets, InsertAssetBytes, insertLocalAssets, netAssets2LocalAssets0, importSYAssets) with boxID set to an encrypted notebook while the resolved writePath falls back to the global data/assets directory — typically because the box-level assets dir could not be resolved.

Common situations: An encrypted notebook whose doc directory lacks an assets folder combined with code relying on the legacy global-assets fallback; plugins constructing a global assets path for an encrypted notebook; migrations porting non-encrypted asset logic to encrypted boxes.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/76c05a56402cac00. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/upload.go:604

		}
	}
	return
}

// writeAssetFile 把 src 的内容写入 writePath。从 writePath 反查真实 boxID 决定是否加密——
// 不轻信传入的 boxID(调用方可能未传,或 assetsDirPath 指向加密笔记本但 id 为空)。
// 加密笔记本必须已解锁(DEK 在内存)才写入;加密但未解锁返回错误(fail-closed,避免明文落盘)。
// 非加密笔记本按 reader 直接写(走 filelock.WriteFileByReader 原路径,保留锁语义)。
func writeAssetFile(writePath string, src io.Reader, boxID, originalName string) (err error) {
	// 从 writePath 反查真实 boxID,与传入 boxID 交叉校验
	pathBoxID := ExtractBoxIDFromAssetsPath(writePath)
	// 传入 boxID 与路径 box 都非空但不一致:路径指向另一个 box,拒绝(防跨 box 写入)
	if boxID != "" && pathBoxID != "" && boxID != pathBoxID {
		return fmt.Errorf("boxID mismatch: param=%s, path=%s", boxID, pathBoxID)
	}
	// 路径不在 box 下但传入的是加密 box:加密内容只能写 box 内,拒绝写全局 assets
	if pathBoxID == "" && boxID != "" && IsEncryptedBox(boxID) {
		return fmt.Errorf("encrypted box asset must be written inside the box directory, got global path: %s", writePath)
	}
	actualBoxID := pathBoxID
	if actualBoxID == "" {
		actualBoxID = boxID // 路径不在 box 下(如全局 assets),回退传入值
	}
	if actualBoxID != "" && IsEncryptedBox(actualBoxID) {
		HoldBoxReadLock(actualBoxID)
		defer ReleaseBoxReadLock(actualBoxID)
		dek, dekErr := GetDEKIfUnlocked(actualBoxID)
		if dekErr != nil {
			// 加密笔记本未解锁:拒绝写入,避免明文落盘(深度防御,见 issue #18034)
			return dekErr
		}
		// 已解锁的加密 box:全读 → 加密 → 落盘
		raw, readErr := io.ReadAll(src)
		if readErr != nil {
			return readErr
		}

View on GitHub (pinned to 9f775e8a12)