Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Protect marketplace install snapshots and concurrent edits https://github.com/siyuan-note/siyuan/pull/20816

Danielebb9a83d15ea
diff --git a/kernel/api/contract_plugin_development_guard_test.go b/kernel/api/contract_plugin_development_guard_test.go
index 07ccdbb7b3..951077c529 100644
--- a/kernel/api/contract_plugin_development_guard_test.go
+++ b/kernel/api/contract_plugin_development_guard_test.go
@@ -15,12 +15,14 @@ import (
)
func TestAPIContractPluginDevelopmentRawGuards(t *testing.T) {
- origWorkspace, origData, origConfDir, origConf := util.WorkspaceDir, util.DataDir, util.ConfDir, model.Conf
+ origWorkspace, origData, origConfDir, origTemp, origConf := util.WorkspaceDir, util.DataDir, util.ConfDir, util.TempDir, model.Conf
util.WorkspaceDir = t.TempDir()
util.DataDir, util.ConfDir = filepath.Join(util.WorkspaceDir, "data"), filepath.Join(util.WorkspaceDir, "conf")
+ util.TempDir = filepath.Join(util.WorkspaceDir, "temp")
model.Conf = model.NewAppConf()
t.Cleanup(func() {
util.WorkspaceDir, util.DataDir, util.ConfDir, model.Conf = origWorkspace, origData, origConfDir, origConf
+ util.TempDir = origTemp
})
engine := gin.New()
engine.Use(func(c *gin.Context) { c.Set(model.RoleContextKey, model.RoleAdministrator) })
@@ -33,6 +35,8 @@ func TestAPIContractPluginDevelopmentRawGuards(t *testing.T) {
"data/storage/ai/agent/sessions/task/runtime.json",
"data/storage/ai/agent/sessions/task/runtime.jsona1b2c3d.tmp",
"data/plugins/.siyuan-package-install-abcdef/staging/index.js",
+ "temp/bazaar/local/extract-abcdef/index.js",
+ "temp/bazaar/package/online-abcdef/index.js",
}
for _, path := range protected {
abs := filepath.Join(util.WorkspaceDir, filepath.FromSlash(path))
@@ -68,7 +72,14 @@ func TestAPIContractPluginDevelopmentRawGuards(t *testing.T) {
})
}
}
- for _, path := range []string{"data/storage/ai/agent", "data/storage/ai/agent/sessions/task", "conf"} {
+ for _, path := range []string{"temp/bazaar/local", "temp/bazaar/package"} {
+ recorder := httptest.NewRecorder()
+ engine.ServeHTTP(recorder, httptest.NewRequest("POST", "/api/file/readDir", strings.NewReader(`{"path":"`+path+`"}`)))
+ if !strings.Contains(recorder.Body.String(), "managed plugin development") {
+ t.Fatalf("extracted packages were listed: %s", recorder.Body.String())
+ }
+ }
+ for _, path := range []string{"data/storage/ai/agent", "data/storage/ai/agent/sessions/task", "conf", "temp/bazaar", "temp/bazaar/local", "temp/bazaar/package"} {
recorder := httptest.NewRecorder()
engine.ServeHTTP(recorder, httptest.NewRequest("POST", "/api/file/removeFile", strings.NewReader(`{"path":"`+path+`"}`)))
if !strings.Contains(recorder.Body.String(), "managed plugin development") {
diff --git a/kernel/bazaar/install.go b/kernel/bazaar/install.go
index 2ab47d5098..e4fc5102d7 100644
--- a/kernel/bazaar/install.go
+++ b/kernel/bazaar/install.go
@@ -169,12 +169,8 @@ func installPackageWithOptions(data []byte, installPath, pkgType, packageName st
if err != nil {
return
}
- packageTempPath := filepath.Join(util.TempDir, "bazaar", "package")
- if err = os.MkdirAll(packageTempPath, 0755); err != nil {
- return
- }
// 先在 temp 中解压并完成归档、路径和包名校验,data 下只接收已通过校验的内容
- unpackPath, err := os.MkdirTemp(packageTempPath, "online-")
+ unpackPath, err := makePackageExtractionDir("package", "online-")
if err != nil {
return
}
@@ -226,9 +222,13 @@ func replacePackageDirectoryWithOptions(sourcePath, installPath string, update b
if err = validateInstalledRevision(options.ExpectedInstalledRevision); err != nil {
return
}
- // 逐层拒绝链接后再创建操作目录,避免把内容写入连接目录
- if err = rejectInstallPathLinks(installPath); err != nil {
- return
+ // 复制前固定目标版本;即使调用方未显式绑定版本,也不能覆盖复制期间发生的编辑或安装。
+ previousRevision, err := lockedInstalledPackageRevision(options.context(), installPath)
+ if err != nil {
+ return nil, err
+ }
+ if options.ExpectedInstalledRevision != "" && previousRevision != options.ExpectedInstalledRevision {
+ return nil, ErrInstalledRevisionConflict
}
if err = os.MkdirAll(filepath.Dir(installPath), 0755); err != nil {
return
@@ -271,11 +271,14 @@ func replacePackageDirectoryWithOptions(sourcePath, installPath string, update b
if err = options.check(); err != nil {
return nil, err
}
- previousRevision, err := installedPackageRevision(installPath)
+ if err = rejectInstallPathLinks(installPath); err != nil {
+ return nil, err
+ }
+ currentRevision, err := installedPackageRevision(installPath)
if err != nil {
return nil, err
}
- if options.ExpectedInstalledRevision != "" && previousRevision != options.ExpectedInstalledRevision {
+ if currentRevision != previousRevision {
return nil, ErrInstalledRevisionConflict
}
containsFile, statErr := PackageDirContainsFile(installPath)
diff --git a/kernel/bazaar/install_paths.go b/kernel/bazaar/install_paths.go
index 6d68281e93..38891cb7a1 100644
--- a/kernel/bazaar/install_paths.go
+++ b/kernel/bazaar/install_paths.go
@@ -45,6 +45,24 @@ func GetPackageLayout(pkgType string) (layout PackageLayout, ok bool) {
return PackageLayout{}, false
}
+// makePackageExtractionDir 在受文件访问守卫保护的临时根下创建独占目录,并拒绝空间内部的链接。
+func makePackageExtractionDir(kind, pattern string) (string, error) {
+ if !filepath.IsAbs(util.TempDir) {
+ return "", errors.New("package extraction root is unavailable")
+ }
+ root := filepath.Join(util.TempDir, "bazaar", kind)
+ if err := rejectInstallPathLinks(root); err != nil {
+ return "", err
+ }
+ if err := os.MkdirAll(root, 0755); err != nil {
+ return "", err
+ }
+ if err := rejectInstallPathLinks(root); err != nil {
+ return "", err
+ }
+ return os.MkdirTemp(root, pattern)
+}
+
func rejectInstallPathLinks(target string) error {
target, err := filepath.Abs(target)
if err != nil {
diff --git a/kernel/bazaar/install_paths_windows_test.go b/kernel/bazaar/install_paths_windows_test.go
index ea11904aa0..46ec26cf0c 100644
--- a/kernel/bazaar/install_paths_windows_test.go
+++ b/kernel/bazaar/install_paths_windows_test.go
@@ -19,8 +19,10 @@ func makeInstallJunction(t *testing.T, link, target string) {
func TestInstallPathsAllowWorkspaceJunctions(t *testing.T) {
for _, workspaceIsJunction := range []bool{false, true} {
t.Run(map[bool]string{false: "ancestor", true: "workspace"}[workspaceIsJunction], func(t *testing.T) {
- oldWorkspace, oldConf, oldData := util.WorkspaceDir, util.ConfDir, util.DataDir
- t.Cleanup(func() { util.WorkspaceDir, util.ConfDir, util.DataDir = oldWorkspace, oldConf, oldData })
+ oldWorkspace, oldConf, oldData, oldTemp := util.WorkspaceDir, util.ConfDir, util.DataDir, util.TempDir
+ t.Cleanup(func() {
+ util.WorkspaceDir, util.ConfDir, util.DataDir, util.TempDir = oldWorkspace, oldConf, oldData, oldTemp
+ })
root := t.TempDir()
realRoot, alias := filepath.Join(root, "real"), filepath.Join(root, "alias")
if err := os.Mkdir(realRoot, 0755); err != nil {
@@ -36,6 +38,12 @@ func TestInstallPathsAllowWorkspaceJunctions(t *testing.T) {
}
util.ConfDir = filepath.Join(util.WorkspaceDir, "conf")
util.DataDir = filepath.Join(util.WorkspaceDir, "data")
+ util.TempDir = filepath.Join(util.WorkspaceDir, "temp")
+ for _, kind := range []string{"local", "package"} {
+ if _, err := makePackageExtractionDir(kind, "extract-"); err != nil {
+ t.Fatalf("extract %s under workspace junction: %v", kind, err)
+ }
+ }
source := filepath.Join(realRoot, "source")
if err := os.Mkdir(source, 0755); err != nil {
t.Fatal(err)
@@ -59,6 +67,33 @@ func TestInstallPathsAllowWorkspaceJunctions(t *testing.T) {
}
}
+func TestPackageExtractionRejectsJunctionsInsideWorkspace(t *testing.T) {
+ for _, relative := range []string{"temp", "temp/bazaar", "temp/bazaar/local", "temp/bazaar/package"} {
+ t.Run(relative, func(t *testing.T) {
+ oldWorkspace, oldTemp := util.WorkspaceDir, util.TempDir
+ t.Cleanup(func() { util.WorkspaceDir, util.TempDir = oldWorkspace, oldTemp })
+ util.WorkspaceDir = t.TempDir()
+ util.TempDir = filepath.Join(util.WorkspaceDir, "temp")
+ outside := t.TempDir()
+ link := filepath.Join(util.WorkspaceDir, filepath.FromSlash(relative))
+ if err := os.MkdirAll(filepath.Dir(link), 0755); err != nil {
+ t.Fatal(err)
+ }
+ makeInstallJunction(t, link, outside)
+ kind := "local"
+ if filepath.Base(link) == "package" {
+ kind = "package"
+ }
+ if _, err := makePackageExtractionDir(kind, "extract-"); err == nil {
+ t.Fatal("extraction followed an internal junction")
+ }
+ if entries, err := os.ReadDir(outside); err != nil || len(entries) != 0 {
+ t.Fatalf("extraction wrote through a junction: %v, %v", entries, err)
+ }
+ })
+ }
+}
+
func TestInstallPathsRejectJunctionsInsideWorkspace(t *testing.T) {
oldWorkspace := util.WorkspaceDir
t.Cleanup(func() { util.WorkspaceDir = oldWorkspace })
diff --git a/kernel/bazaar/install_revision.go b/kernel/bazaar/install_revision.go
index 54bdaa2e4e..6d41d5574d 100644
--- a/kernel/bazaar/install_revision.go
+++ b/kernel/bazaar/install_revision.go
@@ -73,7 +73,11 @@ func validateInstalledRevision(revision string) error {
// InstalledPackageRevision 在与安装、卸载相同的锁内读取完整代码目录摘要。
func InstalledPackageRevision(installPath string) (string, error) {
- if err := packageInstallLock.Acquire(context.Background(), 1); err != nil {
+ return lockedInstalledPackageRevision(context.Background(), installPath)
+}
+
+func lockedInstalledPackageRevision(ctx context.Context, installPath string) (string, error) {
+ if err := packageInstallLock.Acquire(ctx, 1); err != nil {
return "", err
}
defer packageInstallLock.Release(1)
diff --git a/kernel/bazaar/install_revision_test.go b/kernel/bazaar/install_revision_test.go
index 8d26b545b1..a4dbbf1ffb 100644
--- a/kernel/bazaar/install_revision_test.go
+++ b/kernel/bazaar/install_revision_test.go
@@ -57,6 +57,55 @@ func TestInstallRevisionConflictHasNoReplacement(t *testing.T) {
}
}
+func TestInstallRevisionRejectsTargetChangeDuringStaging(t *testing.T) {
+ for _, change := range []string{"edit", "create", "delete", "empty directory"} {
+ t.Run(change, func(t *testing.T) {
+ source, target := useInstallRevisionFixture(t)
+ update := change == "edit" || change == "delete"
+ if !update {
+ if err := os.RemoveAll(target); err != nil {
+ t.Fatal(err)
+ }
+ }
+ changedRevision := ""
+ // 在线安装未显式绑定版本;在 staging 完成后模拟另一个写入方修改目标。
+ options := PackageInstallOptions{Recheck: func() error {
+ staged, err := filepath.Glob(filepath.Join(filepath.Dir(target), ".siyuan-package-install-*", "staging", "index.js"))
+ if err != nil || len(staged) == 0 || changedRevision != "" {
+ return err
+ }
+ switch change {
+ case "delete":
+ err = os.RemoveAll(target)
+ case "empty directory":
+ err = os.MkdirAll(target, 0755)
+ default:
+ if err = os.MkdirAll(target, 0755); err == nil {
+ err = os.WriteFile(filepath.Join(target, "index.js"), []byte("concurrent edit"), 0644)
+ }
+ }
+ if err != nil {
+ return err
+ }
+ changedRevision, err = installedPackageRevision(target)
+ return err
+ }}
+ if _, err := replacePackageDirectoryWithOptions(source, target, update, options); !errors.Is(err, ErrInstalledRevisionConflict) {
+ t.Fatalf("target change was not rejected: %v", err)
+ }
+ if changedRevision == "" {
+ t.Fatal("target was not changed during staging")
+ }
+ if revision, err := InstalledPackageRevision(target); err != nil || revision != changedRevision {
+ t.Fatalf("concurrent change was lost: %s, %v", revision, err)
+ }
+ if paths, err := filepath.Glob(filepath.Join(filepath.Dir(target), ".siyuan-package-install-*")); err != nil || len(paths) != 0 {
+ t.Fatalf("conflicting installation left staging: %v, %v", paths, err)
+ }
+ })
+ }
+}
+
func TestInstallRevisionMissingBinding(t *testing.T) {
source, target := useInstallRevisionFixture(t)
options := PackageInstallOptions{ExpectedInstalledRevision: MissingInstalledRevision}
@@ -166,6 +215,10 @@ func TestInstallArchiveHashConsumesVerifiedSnapshot(t *testing.T) {
t.Fatal(err)
}
defer cleanup()
+ if !util.IsForbiddenAbsPath(filepath.Join(source, "index.js")) ||
+ !util.IsPluginDevelopmentRawPathForbidden(source, true) {
+ t.Fatal("hash-bound extraction is accessible through raw file APIs")
+ }
if gotHash != hash {
t.Fatal("incorrect archive hash")
}
diff --git a/kernel/bazaar/install_staging_test.go b/kernel/bazaar/install_staging_test.go
index ba3acdbe67..7d30b99434 100644
--- a/kernel/bazaar/install_staging_test.go
+++ b/kernel/bazaar/install_staging_test.go
@@ -5,6 +5,7 @@
package bazaar
import (
+ "fmt"
"os"
"path/filepath"
"testing"
@@ -12,6 +13,60 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
+func TestInstallPackageExtractionIsProtected(t *testing.T) {
+ useInstallTempDir(t)
+ data := buildInstallPackageArchive(t, map[string]string{
+ "plugin.json": `{"name":"sample","version":"1.0.0"}`,
+ "index.js": "approved",
+ })
+ checked := false
+ options := PackageInstallOptions{Recheck: func() error {
+ paths, err := filepath.Glob(filepath.Join(util.TempDir, "bazaar", "package", "online-*", "index.js"))
+ if err != nil || len(paths) == 0 {
+ return err
+ }
+ checked = true
+ if !util.IsForbiddenAbsPath(paths[0]) || !util.IsPluginDevelopmentRawPathForbidden(filepath.Dir(paths[0]), true) {
+ return fmt.Errorf("online extraction is accessible through raw file APIs")
+ }
+ return nil
+ }}
+ if _, err := installPackageWithOptions(data, filepath.Join(t.TempDir(), "sample"), "plugins", "sample", false, options); err != nil {
+ t.Fatal(err)
+ }
+ if !checked {
+ t.Fatal("online extraction was not inspected")
+ }
+}
+
+func TestInstallPackageStagingReleasesInstallLock(t *testing.T) {
+ source, target := useInstallRevisionFixture(t)
+ checked := false
+ options := PackageInstallOptions{Recheck: func() error {
+ paths, err := filepath.Glob(filepath.Join(filepath.Dir(target), ".siyuan-package-install-*", "staging", "index.js"))
+ if err != nil || len(paths) == 0 || checked {
+ return err
+ }
+ checked = true
+ if !packageInstallLock.TryAcquire(1) {
+ return fmt.Errorf("staging held the install lock")
+ }
+ packageInstallLock.Release(1)
+ return nil
+ }}
+ if _, err := replacePackageDirectoryWithOptions(source, target, true, options); err != nil {
+ t.Fatal(err)
+ }
+ if !checked {
+ t.Fatal("staging was not inspected")
+ }
+ // 提交后的锁仍可获取,避免初始版本读取与提交两段加锁泄漏许可。
+ if !packageInstallLock.TryAcquire(1) {
+ t.Fatal("install lock was retained after commit")
+ }
+ packageInstallLock.Release(1)
+}
+
// useInstallTempDir 把解压目录指向独立临时目录,避免写入工作空间 temp
func useInstallTempDir(t *testing.T) {
t.Helper()
diff --git a/kernel/bazaar/local.go b/kernel/bazaar/local.go
index 73c0029435..2cd1bd902c 100644
--- a/kernel/bazaar/local.go
+++ b/kernel/bazaar/local.go
@@ -29,7 +29,6 @@ import (
"strings"
"github.com/88250/gulu"
- "github.com/siyuan-note/siyuan/kernel/util"
)
const (
@@ -96,7 +95,10 @@ func extractLocalPackageWithHash(ctx context.Context, archivePath, expectedHash
if err = ctx.Err(); err != nil {
return
}
- tempPath := filepath.Join(util.TempDir, "bazaar", "local", gulu.Rand.String(7))
+ tempPath, err := makePackageExtractionDir("local", "extract-")
+ if err != nil {
+ return
+ }
cleanup = func() { _ = os.RemoveAll(tempPath) }
reader, readerErr := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if readerErr != nil {
diff --git a/kernel/util/plugin_development.go b/kernel/util/plugin_development.go
index 0aaa36f685..44966f4c57 100644
--- a/kernel/util/plugin_development.go
+++ b/kernel/util/plugin_development.go
@@ -157,7 +157,9 @@ func IsPluginDevelopmentRawPathForbidden(abs string, ancestors bool) bool {
return true
}
}
- for _, root := range []string{PluginProjectsDir(), filepath.Join(ConfDir, "plugin-development")} {
+ // 解压后的包仍属于已核验归档的私有快照,禁止原始文件入口读写及移动其祖先目录。
+ for _, root := range []string{PluginProjectsDir(), filepath.Join(ConfDir, "plugin-development"),
+ filepath.Join(TempDir, "bazaar", "local"), filepath.Join(TempDir, "bazaar", "package")} {
if pluginDevelopmentContains(root, candidate) || ancestors && pluginDevelopmentContains(candidate, root) {
return true
}
diff --git a/kernel/util/plugin_development_test.go b/kernel/util/plugin_development_test.go
index ea682ca8c8..4c091fceed 100644
--- a/kernel/util/plugin_development_test.go
+++ b/kernel/util/plugin_development_test.go
@@ -14,10 +14,13 @@ import (
)
func TestPluginDevelopmentRawPathGuard(t *testing.T) {
- originalWorkspace, originalData, originalConf := WorkspaceDir, DataDir, ConfDir
+ originalWorkspace, originalData, originalConf, originalTemp := WorkspaceDir, DataDir, ConfDir, TempDir
WorkspaceDir = t.TempDir()
DataDir, ConfDir = filepath.Join(WorkspaceDir, "data"), filepath.Join(WorkspaceDir, "conf")
- t.Cleanup(func() { WorkspaceDir, DataDir, ConfDir = originalWorkspace, originalData, originalConf })
+ TempDir = filepath.Join(WorkspaceDir, "temp")
+ t.Cleanup(func() {
+ WorkspaceDir, DataDir, ConfDir, TempDir = originalWorkspace, originalData, originalConf, originalTemp
+ })
for _, rel := range []string{
"data/storage/ai/agent/plugin-projects/task/source/index.js",
"data/storage/ai/agent/plugin-projects/task/artifacts/package.zip",
@@ -30,6 +33,10 @@ func TestPluginDevelopmentRawPathGuard(t *testing.T) {
"data/storage/ai/agent/plugin-projects. /task/source/index.js",
"conf/plugin-development::$INDEX_ALLOCATION/task/control.json",
"data/plugins/.siyuan-package-install-abcdef/staging/index.js",
+ "temp/bazaar/local/extract-abcdef/index.js",
+ "temp/bazaar/package/online-abcdef/index.js",
+ "temp/bazaar/local. /extract-abcdef/index.js",
+ "temp/bazaar/package::$INDEX_ALLOCATION/online-abcdef/index.js",
} {
path := filepath.Join(WorkspaceDir, filepath.FromSlash(rel))
if !IsPluginDevelopmentRawPathForbidden(path, false) || !IsForbiddenAbsPath(path) {
@@ -46,12 +53,12 @@ func TestPluginDevelopmentRawPathGuard(t *testing.T) {
t.Fatal(err)
}
}
- for _, rel := range []string{".", "data", "data/plugins", "data/storage", "data/storage/ai/agent", "data/storage/ai/agent/sessions/task", "conf"} {
+ for _, rel := range []string{".", "data", "data/plugins", "data/storage", "data/storage/ai/agent", "data/storage/ai/agent/sessions/task", "conf", "temp", "temp/bazaar", "temp/bazaar/local", "temp/bazaar/package"} {
if !IsPluginDevelopmentRawPathForbidden(filepath.Join(WorkspaceDir, rel), true) {
t.Errorf("protected ancestor mutation accepted: %s", rel)
}
}
- for _, rel := range []string{"data/storage/ai/agent/sessions/task/output/full.txt", "data/storage/ai/agent/sessions/task/session.json", "data/plugins/example/index.js", "temp/scratch.js"} {
+ for _, rel := range []string{"data/storage/ai/agent/sessions/task/output/full.txt", "data/storage/ai/agent/sessions/task/session.json", "data/plugins/example/index.js", "temp/scratch.js", "temp/bazaar/local-package.zip"} {
if IsPluginDevelopmentRawPathForbidden(filepath.Join(WorkspaceDir, rel), false) {
t.Errorf("ordinary read rejected: %s", rel)
}
@@ -62,6 +69,14 @@ func TestPluginDevelopmentRawPathGuard(t *testing.T) {
t.Fatal("missing-leaf symlink alias bypassed protection")
}
}
+ for _, kind := range []string{"local", "package"} {
+ alias := filepath.Join(WorkspaceDir, "alias-"+kind)
+ if err := os.Symlink(filepath.Join(TempDir, "bazaar", kind), alias); err == nil {
+ if !IsPluginDevelopmentRawPathForbidden(filepath.Join(alias, "new", "index.js"), true) {
+ t.Fatalf("extraction symlink alias bypassed protection: %s", kind)
+ }
+ }
+ }
for _, rel := range []string{"storage/ai/agent/plugin-projects/task/source/index.js", "storage/ai/agent/sessions/task/runtime.json", "storage/ai/agent/sessions/task/runtime.jsona1b2c3d.tmp", "plugins/.siyuan-package-install-abc/staging/index.js"} {
if !IsForbiddenDataRelPath(rel) {
t.Errorf("history path accepted: %s", rel)