Skip to content

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

:lock: Verify marketplace package name consistency before install https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rpx2-p6hp-x5gj

Daniel26ccb40d390e
diff --git a/kernel/bazaar/install.go b/kernel/bazaar/install.go
index c01555b14b..d92bb07a3c 100644
--- a/kernel/bazaar/install.go
+++ b/kernel/bazaar/install.go
@@ -81,6 +81,16 @@ func incPackageDownloads(repoURL, systemID string) {
}).Post(u)
}
+// packageManifestNames 各类型集市包清单文件名
+var packageManifestNames = func() map[string]string {
+ // localPackageManifests 是清单文件名到包类型的映射,这里反转出包类型到清单文件名的映射
+ names := make(map[string]string, len(localPackageManifests))
+ for manifest, pkgType := range localPackageManifests {
+ names[pkgType] = manifest
+ }
+ return names
+}()
+
// InstallPackage 安装集市包
func InstallPackage(repoURL, repoHash, installPath, systemID, pkgType, packageName string, update bool) error {
var fallbackInstallTime time.Time
@@ -95,7 +105,7 @@ func InstallPackage(repoURL, repoHash, installPath, systemID, pkgType, packageNa
if err != nil {
return err
}
- if err = installPackage(data, installPath); err != nil {
+ if err = installPackage(data, installPath, pkgType, packageName, update); err != nil {
return err
}
RemoveInstalledPackageSizeCache(pkgType, packageName)
@@ -113,7 +123,19 @@ func InstallPackage(repoURL, repoHash, installPath, systemID, pkgType, packageNa
return nil
}
-func installPackage(data []byte, installPath string) (err error) {
+func installPackage(data []byte, installPath, pkgType, packageName string, update bool) (err error) {
+ // 非更新安装时目标目录已存在且非空则拒绝覆盖,防止把其他包的内容写入已有包目录
+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rpx2-p6hp-x5gj
+ if !update {
+ entries, statErr := os.ReadDir(installPath)
+ if statErr != nil && !os.IsNotExist(statErr) {
+ return statErr
+ }
+ if 0 < len(entries) {
+ return errors.New("marketplace package install path already exists")
+ }
+ }
+
tmpPackage := filepath.Join(util.TempDir, "bazaar", "package")
if err = os.MkdirAll(tmpPackage, 0755); err != nil {
return
@@ -142,6 +164,20 @@ func installPackage(data []byte, installPath string) (err error) {
srcPath = filepath.Join(unzipPath, dirs[0].Name())
}
+ // 校验下载包自身声明的名称与请求安装的包名一致,防止把其他包的内容写入指定目录
+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rpx2-p6hp-x5gj
+ jsonFileName, ok := packageManifestNames[pkgType]
+ if !ok {
+ return errors.New("invalid marketplace package type")
+ }
+ pkg, parseErr := ParsePackageJSON(filepath.Join(srcPath, jsonFileName))
+ if parseErr != nil || nil == pkg {
+ return errors.New("marketplace package manifest not found or invalid")
+ }
+ if packageName != pkg.Name {
+ return fmt.Errorf("marketplace package name mismatch: expected [%s], got [%s]", packageName, pkg.Name)
+ }
+
if err = filelock.Copy(srcPath, installPath); err != nil {
return
}
diff --git a/kernel/bazaar/install_test.go b/kernel/bazaar/install_test.go
new file mode 100644
index 0000000000..9308477c84
--- /dev/null
+++ b/kernel/bazaar/install_test.go
@@ -0,0 +1,161 @@
+// 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 bazaar
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+// TestInstallPackageNameMismatch 校验下载包声明的名称与请求安装的包名不一致时拒绝安装
+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rpx2-p6hp-x5gj
+func TestInstallPackageNameMismatch(t *testing.T) {
+ oldTempDir := util.TempDir
+ util.TempDir = t.TempDir()
+ t.Cleanup(func() { util.TempDir = oldTempDir })
+
+ installPath := filepath.Join(t.TempDir(), "plugins", "trusted-plugin")
+ if err := os.MkdirAll(installPath, 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(installPath, "index.js"), []byte("original"), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ // 请求安装 trusted-plugin,但下载内容是另一个包(attacker-plugin)
+ data := buildInstallPackageArchive(t, map[string]string{
+ "plugin.json": `{"name":"attacker-plugin","version":"1.0.0"}`,
+ "index.js": "malicious",
+ })
+ if err := installPackage(data, installPath, "plugins", "trusted-plugin", true); err == nil {
+ t.Fatal("expected name mismatch to be rejected")
+ }
+ content, err := os.ReadFile(filepath.Join(installPath, "index.js"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(content) != "original" {
+ t.Fatalf("existing package files were overwritten: %q", content)
+ }
+}
+
+// TestInstallPackageRefusesOverwriteWithoutUpdate 校验非更新安装时拒绝覆盖非空目标目录
+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rpx2-p6hp-x5gj
+func TestInstallPackageRefusesOverwriteWithoutUpdate(t *testing.T) {
+ oldTempDir := util.TempDir
+ util.TempDir = t.TempDir()
+ t.Cleanup(func() { util.TempDir = oldTempDir })
+
+ installPath := filepath.Join(t.TempDir(), "plugins", "sample")
+ if err := os.MkdirAll(installPath, 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(installPath, "index.js"), []byte("original"), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ data := buildInstallPackageArchive(t, map[string]string{
+ "plugin.json": `{"name":"sample","version":"2.0.0"}`,
+ "index.js": "new",
+ })
+ if err := installPackage(data, installPath, "plugins", "sample", false); err == nil {
+ t.Fatal("expected overwriting non-empty directory without update to be rejected")
+ }
+ content, err := os.ReadFile(filepath.Join(installPath, "index.js"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(content) != "original" {
+ t.Fatalf("existing package files were overwritten: %q", content)
+ }
+}
+
+// TestInstallPackageUpdateOverwrites 校验对同名已安装包的更新安装正常覆盖
+func TestInstallPackageUpdateOverwrites(t *testing.T) {
+ oldTempDir := util.TempDir
+ util.TempDir = t.TempDir()
+ t.Cleanup(func() { util.TempDir = oldTempDir })
+
+ installPath := filepath.Join(t.TempDir(), "plugins", "sample")
+ if err := os.MkdirAll(installPath, 0755); err != nil {
+ t.Fatal(err)
+ }
+
+ data := buildInstallPackageArchive(t, map[string]string{
+ "plugin.json": `{"name":"sample","version":"2.0.0"}`,
+ "index.js": "new",
+ })
+ if err := installPackage(data, installPath, "plugins", "sample", true); err != nil {
+ t.Fatalf("expected update install to succeed: %s", err)
+ }
+ content, err := os.ReadFile(filepath.Join(installPath, "index.js"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(content) != "new" {
+ t.Fatalf("expected updated content, got %q", content)
+ }
+}
+
+// TestInstallPackageFreshInstall 校验新安装写入目标目录
+func TestInstallPackageFreshInstall(t *testing.T) {
+ oldTempDir := util.TempDir
+ util.TempDir = t.TempDir()
+ t.Cleanup(func() { util.TempDir = oldTempDir })
+
+ installPath := filepath.Join(t.TempDir(), "plugins", "sample")
+ data := buildInstallPackageArchive(t, map[string]string{
+ "plugin.json": `{"name":"sample","version":"1.0.0"}`,
+ "index.js": "new",
+ })
+ if err := installPackage(data, installPath, "plugins", "sample", false); err != nil {
+ t.Fatalf("expected fresh install to succeed: %s", err)
+ }
+ if _, err := os.Stat(filepath.Join(installPath, "index.js")); err != nil {
+ t.Fatalf("installed file is missing: %s", err)
+ }
+}
+
+// TestInstallPackageMissingManifest 校验下载包缺少清单文件时拒绝安装
+func TestInstallPackageMissingManifest(t *testing.T) {
+ oldTempDir := util.TempDir
+ util.TempDir = t.TempDir()
+ t.Cleanup(func() { util.TempDir = oldTempDir })
+
+ installPath := filepath.Join(t.TempDir(), "plugins", "sample")
+ data := buildInstallPackageArchive(t, map[string]string{
+ "index.js": "new",
+ })
+ if err := installPackage(data, installPath, "plugins", "sample", false); err == nil {
+ t.Fatal("expected missing manifest to be rejected")
+ }
+}
+
+// buildInstallPackageArchive 构建内存中的集市包 zip 压缩数据
+func buildInstallPackageArchive(t *testing.T, files map[string]string) []byte {
+ t.Helper()
+ archivePath := filepath.Join(t.TempDir(), "package.zip")
+ writeLocalPackageArchive(t, archivePath, files)
+ data, err := os.ReadFile(archivePath)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return data
+}