An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:lock: Preserve encrypted asset access restrictions https://github.com/siyuan-note/siyuan/issues/18034
Danielfcf1ab0693c4
diff --git a/kernel/api/asset.go b/kernel/api/asset.goindex 3b55c76c74..ce89b8c2a5 100644--- a/kernel/api/asset.go+++ b/kernel/api/asset.go@@ -478,13 +478,19 @@ func resolveAssetPath(c *gin.Context) {}path := arg["path"].(string)- p, err := model.PrepareAssetForOpen(path)+ p, err := model.GetAssetAbsPathInBox(path, "")if err != nil {ret.Code = -1ret.Msg = err.Error()ret.Data = map[string]any{"closeTimeout": 3000}return}+ if model.IsEncryptedAssetPath(p) {+ ret.Code = -1+ ret.Msg = model.Conf.Language(314)+ ret.Data = map[string]any{"closeTimeout": 3000}+ return+ }ret.Data = preturn}diff --git a/kernel/api/clipboard.go b/kernel/api/clipboard.goindex c40c07a4cf..58598c74f7 100644--- a/kernel/api/clipboard.go+++ b/kernel/api/clipboard.go@@ -72,7 +72,7 @@ func writeFilePath(c *gin.Context) {return}- absPath, err := model.PrepareAssetForOpen(pathArg)+ absPath, err := model.GetAssetAbsPathInBox(pathArg, "")if err != nil {logging.LogErrorf("get asset [%s] abs path failed: %s", pathArg, err)ret.Code = -1@@ -80,6 +80,12 @@ func writeFilePath(c *gin.Context) {ret.Data = map[string]any{"closeTimeout": 5000}return}+ if model.IsEncryptedAssetPath(absPath) {+ ret.Code = -1+ ret.Msg = model.Conf.Language(314)+ ret.Data = map[string]any{"closeTimeout": 5000}+ return+ }if err = util.WriteFilePaths([]string{absPath}); err != nil {logging.LogErrorf("write file path to clipboard failed: %s", err)diff --git a/kernel/model/asset_open.go b/kernel/model/asset_open.godeleted file mode 100644index 9c076d0f34..0000000000--- a/kernel/model/asset_open.go+++ /dev/null@@ -1,117 +0,0 @@-// SiYuan - Refactor your thinking-// Copyright (c) 2020-present, b3log.org-//-// This program is free software: you can redistribute it and/or modify-// it under the terms of the GNU Affero General Public License as published by-// the Free Software Foundation, either version 3 of the License, or-// (at your option) any later version.-//-// This program is distributed in the hope that it will be useful,-// but WITHOUT ANY WARRANTY; without even the implied warranty of-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-// GNU Affero General Public License for more details.-//-// You should have received a copy of the GNU Affero General Public License-// along with this program. If not, see <https://www.gnu.org/licenses/>.--package model--import (- "errors"- "os"- "path/filepath"- "strings"-- "github.com/siyuan-note/filelock"- "github.com/siyuan-note/siyuan/kernel/util"-)--// PrepareAssetForOpen 返回可交给系统应用打开的资源绝对路径。-// 加密资源会流式解密到按 box 隔离的临时目录,该目录在笔记本锁定或内核下次启动时清理。-func PrepareAssetForOpen(assetPath string) (ret string, err error) {- assetAbsPath, err := GetAssetAbsPathInBox(assetPath, "")- if err != nil {- return "", err- }- if !IsEncryptedAssetPath(assetAbsPath) {- return assetAbsPath, nil- }-- boxID := ExtractBoxIDFromAssetsPath(assetAbsPath)- if err = AcquireEncryptedBoxOperation(boxID); err != nil {- return "", errors.New(Conf.Language(314))- }- defer ReleaseEncryptedBoxOperation(boxID)- HoldBoxReadLock(boxID)- defer ReleaseBoxReadLock(boxID)-- dek, err := GetDEKIfUnlocked(boxID)- if err != nil {- return "", errors.New(Conf.Language(314))- }- defer zeroAndClear(dek)- if strings.TrimSpace(util.TempDir) == "" {- return "", errors.New("temporary directory is not initialized")- }-- openID, err := newManagedEncryptedExportID()- if err != nil {- return "", err- }- tempDir := filepath.Join(util.TempDir, "export", boxID, "asset-open", openID)- if err = os.MkdirAll(tempDir, 0700); err != nil {- return "", err- }- cleanup := true- defer func() {- if cleanup {- _ = os.RemoveAll(tempDir)- }- }()-- source, err := filelock.OpenFile(assetAbsPath, os.O_RDONLY, 0)- if err != nil {- return "", err- }- sourceOpen := true- defer func() {- if sourceOpen {- _ = filelock.CloseFile(source)- }- }()-- payloadPath := filepath.Join(tempDir, "payload")- destination, err := os.OpenFile(payloadPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600)- if err != nil {- return "", err- }- originalName, decryptErr := DecryptAssetToWriter(boxID, filepath.Base(assetAbsPath), dek, source, destination)- sourceCloseErr := filelock.CloseFile(source)- sourceOpen = false- destinationCloseErr := destination.Close()- if decryptErr != nil {- return "", decryptErr- }- if sourceCloseErr != nil {- return "", sourceCloseErr- }- if destinationCloseErr != nil {- return "", destinationCloseErr- }-- originalName = util.FilterFileName(filepath.Base(originalName))- if originalName == "" || originalName == "." {- originalName = filepath.Base(assetAbsPath)- }- ret = filepath.Join(tempDir, originalName)- if ret != payloadPath {- if err = os.Rename(payloadPath, ret); err != nil {- return "", err- }- }- if err = os.Chmod(ret, 0600); err != nil {- return "", err- }- cleanup = false- return ret, nil-}diff --git a/kernel/model/asset_open_test.go b/kernel/model/asset_open_test.godeleted file mode 100644index b3665017f9..0000000000--- a/kernel/model/asset_open_test.go+++ /dev/null@@ -1,112 +0,0 @@-package model--import (- "os"- "path/filepath"- "strings"- "testing"-- "github.com/88250/gulu"- "github.com/siyuan-note/siyuan/kernel/conf"- "github.com/siyuan-note/siyuan/kernel/util"-)--func TestPrepareAssetForOpen(t *testing.T) {- originalWorkspaceDir := util.WorkspaceDir- originalDataDir := util.DataDir- originalTempDir := util.TempDir- originalConf := Conf- workspaceDir := t.TempDir()- util.WorkspaceDir = workspaceDir- util.DataDir = filepath.Join(workspaceDir, "data")- util.TempDir = filepath.Join(workspaceDir, "temp")- Conf = NewAppConf()- Conf.Editor = conf.NewEditor()- Conf.Editor.VirtualBlockRef = false- boxID := "20260801120000-assetop"- defer func() {- LockBox(boxID)- util.WorkspaceDir = originalWorkspaceDir- util.DataDir = originalDataDir- util.TempDir = originalTempDir- Conf = originalConf- }()-- plainAssetPath := filepath.Join(util.DataDir, "assets", "plain.png")- if err := os.MkdirAll(filepath.Dir(plainAssetPath), 0755); err != nil {- t.Fatal(err)- }- if err := os.WriteFile(plainAssetPath, []byte("plain image"), 0600); err != nil {- t.Fatal(err)- }- preparedPlainPath, err := PrepareAssetForOpen("assets/plain.png")- if err != nil {- t.Fatal(err)- }- if preparedPlainPath != plainAssetPath {- t.Fatalf("plain asset path changed: got %q, want %q", preparedPlainPath, plainAssetPath)- }-- boxConf := conf.NewBoxConf()- boxConf.Encrypted = true- confData, err := gulu.JSON.MarshalIndentJSON(boxConf, "", " ")- if err != nil {- t.Fatal(err)- }- confPath := filepath.Join(util.DataDir, boxID, ".siyuan", "conf.json")- if err = os.MkdirAll(filepath.Dir(confPath), 0755); err != nil {- t.Fatal(err)- }- if err = os.WriteFile(confPath, confData, 0600); err != nil {- t.Fatal(err)- }-- dek, err := util.GenerateDEK()- if err != nil {- t.Fatal(err)- }- setDEKForTest(boxID, dek)- setEncryptedBoxState(boxID, EncryptedBoxStateUnlocked)- diskName := "asset-20260801120001-abcdefg.png"- originalName := "机密图片.png"- plaintext := []byte("encrypted image")- ciphertext, err := EncryptAsset(boxID, diskName, originalName, dek, plaintext)- if err != nil {- t.Fatal(err)- }- encryptedAssetPath := filepath.Join(util.DataDir, boxID, "assets", diskName)- if err = os.MkdirAll(filepath.Dir(encryptedAssetPath), 0755); err != nil {- t.Fatal(err)- }- if err = os.WriteFile(encryptedAssetPath, ciphertext, 0600); err != nil {- t.Fatal(err)- }-- preparedEncryptedPath, err := PrepareAssetForOpen("assets/" + diskName + "?box=" + boxID)- if err != nil {- t.Fatal(err)- }- wantTempRoot := filepath.Join(util.TempDir, "export", boxID, "asset-open")- if rel, relErr := filepath.Rel(wantTempRoot, preparedEncryptedPath); relErr != nil || rel == ".." ||- strings.HasPrefix(rel, ".."+string(filepath.Separator)) {- t.Fatalf("prepared encrypted asset escaped its temporary root: %q", preparedEncryptedPath)- }- if filepath.Base(preparedEncryptedPath) != originalName {- t.Fatalf("prepared encrypted asset name: got %q, want %q", filepath.Base(preparedEncryptedPath), originalName)- }- preparedContent, err := os.ReadFile(preparedEncryptedPath)- if err != nil {- t.Fatal(err)- }- if string(preparedContent) != string(plaintext) {- t.Fatalf("prepared encrypted asset content: got %q, want %q", preparedContent, plaintext)- }-- LockBox(boxID)- if _, err = os.Stat(preparedEncryptedPath); !os.IsNotExist(err) {- t.Fatalf("locking the notebook should remove the prepared asset: %v", err)- }- if _, err = PrepareAssetForOpen("assets/" + diskName + "?box=" + boxID); err == nil {- t.Fatal("locked encrypted asset was prepared")- }-}