Skip to content

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

:lock: https://github.com/siyuan-note/siyuan/security/advisories/GHSA-qvvf-q994-x79v

Daniel5ee00907f0b0
diff --git a/kernel/api/import.go b/kernel/api/import.go
index bced56a829..6aa4596070 100644
--- a/kernel/api/import.go
+++ b/kernel/api/import.go
@@ -68,8 +68,17 @@ func importSY(c *gin.Context) {
ret.Msg = err.Error()
return
}
- writePath := filepath.Join(util.TempDir, "import", file.Filename)
+
+ writePath := filepath.Join(importDir, file.Filename)
+ if !util.IsSubPath(importDir, writePath) {
+ logging.LogErrorf("import path [%s] is not sub path of import dir [%s]", writePath, importDir)
+ ret.Code = -1
+ ret.Msg = "import path is not sub path of import dir"
+ return
+ }
+
defer os.RemoveAll(writePath)
+
writer, err := os.OpenFile(writePath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
logging.LogErrorf("open import .sy.zip [%s] failed: %s", writePath, err)
@@ -119,14 +128,14 @@ func importData(c *gin.Context) {
return
}
- tmpImport := filepath.Join(util.TempDir, "import")
- err = os.MkdirAll(tmpImport, 0755)
+ importDir := filepath.Join(util.TempDir, "import")
+ err = os.MkdirAll(importDir, 0755)
if err != nil {
ret.Code = -1
ret.Msg = "create temp import dir failed"
return
}
- dataZipPath := filepath.Join(tmpImport, util.CurrentTimeSecondsStr()+".zip")
+ dataZipPath := filepath.Join(importDir, util.CurrentTimeSecondsStr()+".zip")
defer os.RemoveAll(dataZipPath)
dataZipFile, err := os.Create(dataZipPath)
if err != nil {
@@ -225,8 +234,17 @@ func importZipMd(c *gin.Context) {
ret.Msg = err.Error()
return
}
+
writePath := filepath.Join(util.TempDir, "import", file.Filename)
+ if !util.IsSubPath(importDir, writePath) {
+ logging.LogErrorf("import path [%s] is not sub path of import dir [%s]", writePath, importDir)
+ ret.Code = -1
+ ret.Msg = "import path is not sub path of import dir"
+ return
+ }
+
defer os.RemoveAll(writePath)
+
writer, err := os.OpenFile(writePath, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
logging.LogErrorf("open import .zip [%s] failed: %s", writePath, err)
diff --git a/kernel/api/sync.go b/kernel/api/sync.go
index 1fc70adb7c..30711127aa 100644
--- a/kernel/api/sync.go
+++ b/kernel/api/sync.go
@@ -79,8 +79,15 @@ func importSyncProviderWebDAV(c *gin.Context) {
return
}
- tmp := filepath.Join(importDir, f.Filename)
- if err = os.WriteFile(tmp, data, 0644); err != nil {
+ writePath := filepath.Join(importDir, f.Filename)
+ if !util.IsSubPath(importDir, writePath) {
+ logging.LogErrorf("import path [%s] is not sub path of import dir [%s]", writePath, importDir)
+ ret.Code = -1
+ ret.Msg = "import path is not sub path of import dir"
+ return
+ }
+
+ if err = os.WriteFile(writePath, data, 0644); err != nil {
logging.LogErrorf("import WebDAV provider failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
@@ -89,15 +96,15 @@ func importSyncProviderWebDAV(c *gin.Context) {
tmpDir := filepath.Join(importDir, "webdav")
os.RemoveAll(tmpDir)
- if strings.HasSuffix(strings.ToLower(tmp), ".zip") {
- if err = gulu.Zip.Unzip(tmp, tmpDir); err != nil {
+ if strings.HasSuffix(strings.ToLower(writePath), ".zip") {
+ if err = gulu.Zip.Unzip(writePath, tmpDir); err != nil {
logging.LogErrorf("import WebDAV provider failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}
- } else if strings.HasSuffix(strings.ToLower(tmp), ".json") {
- if err = gulu.File.CopyFile(tmp, filepath.Join(tmpDir, f.Filename)); err != nil {
+ } else if strings.HasSuffix(strings.ToLower(writePath), ".json") {
+ if err = gulu.File.CopyFile(writePath, filepath.Join(tmpDir, f.Filename)); err != nil {
logging.LogErrorf("import WebDAV provider failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
@@ -124,8 +131,8 @@ func importSyncProviderWebDAV(c *gin.Context) {
return
}
- tmp = filepath.Join(tmpDir, entries[0].Name())
- data, err = os.ReadFile(tmp)
+ writePath = filepath.Join(tmpDir, entries[0].Name())
+ data, err = os.ReadFile(writePath)
if err != nil {
logging.LogErrorf("import WebDAV provider failed: %s", err)
ret.Code = -1
@@ -265,8 +272,15 @@ func importSyncProviderS3(c *gin.Context) {
return
}
- tmp := filepath.Join(importDir, f.Filename)
- if err = os.WriteFile(tmp, data, 0644); err != nil {
+ writePath := filepath.Join(importDir, f.Filename)
+ if !util.IsSubPath(importDir, writePath) {
+ logging.LogErrorf("import path [%s] is not sub path of import dir [%s]", writePath, importDir)
+ ret.Code = -1
+ ret.Msg = "import path is not sub path of import dir"
+ return
+ }
+
+ if err = os.WriteFile(writePath, data, 0644); err != nil {
logging.LogErrorf("import S3 provider failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
@@ -275,15 +289,15 @@ func importSyncProviderS3(c *gin.Context) {
tmpDir := filepath.Join(importDir, "s3")
os.RemoveAll(tmpDir)
- if strings.HasSuffix(strings.ToLower(tmp), ".zip") {
- if err = gulu.Zip.Unzip(tmp, tmpDir); err != nil {
+ if strings.HasSuffix(strings.ToLower(writePath), ".zip") {
+ if err = gulu.Zip.Unzip(writePath, tmpDir); err != nil {
logging.LogErrorf("import S3 provider failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}
- } else if strings.HasSuffix(strings.ToLower(tmp), ".json") {
- if err = gulu.File.CopyFile(tmp, filepath.Join(tmpDir, f.Filename)); err != nil {
+ } else if strings.HasSuffix(strings.ToLower(writePath), ".json") {
+ if err = gulu.File.CopyFile(writePath, filepath.Join(tmpDir, f.Filename)); err != nil {
logging.LogErrorf("import S3 provider failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
@@ -310,8 +324,8 @@ func importSyncProviderS3(c *gin.Context) {
return
}
- tmp = filepath.Join(tmpDir, entries[0].Name())
- data, err = os.ReadFile(tmp)
+ writePath = filepath.Join(tmpDir, entries[0].Name())
+ data, err = os.ReadFile(writePath)
if err != nil {
logging.LogErrorf("import S3 provider failed: %s", err)
ret.Code = -1
diff --git a/kernel/api/system.go b/kernel/api/system.go
index a0cfa003a5..71ed6dd65d 100644
--- a/kernel/api/system.go
+++ b/kernel/api/system.go
@@ -441,8 +441,15 @@ func importConf(c *gin.Context) {
return
}
- tmp := filepath.Join(importDir, f.Filename)
- if err = os.WriteFile(tmp, data, 0644); err != nil {
+ writePath := filepath.Join(importDir, f.Filename)
+ if !util.IsSubPath(importDir, writePath) {
+ logging.LogErrorf("import path [%s] is not sub path of import dir [%s]", writePath, importDir)
+ ret.Code = -1
+ ret.Msg = "import path is not sub path of import dir"
+ return
+ }
+
+ if err = os.WriteFile(writePath, data, 0644); err != nil {
logging.LogErrorf("import conf failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
@@ -451,15 +458,15 @@ func importConf(c *gin.Context) {
tmpDir := filepath.Join(importDir, "conf")
os.RemoveAll(tmpDir)
- if strings.HasSuffix(strings.ToLower(tmp), ".zip") {
- if err = gulu.Zip.Unzip(tmp, tmpDir); err != nil {
+ if strings.HasSuffix(strings.ToLower(writePath), ".zip") {
+ if err = gulu.Zip.Unzip(writePath, tmpDir); err != nil {
logging.LogErrorf("import conf failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
return
}
- } else if strings.HasSuffix(strings.ToLower(tmp), ".json") {
- if err = gulu.File.CopyFile(tmp, filepath.Join(tmpDir, f.Filename)); err != nil {
+ } else if strings.HasSuffix(strings.ToLower(writePath), ".json") {
+ if err = gulu.File.CopyFile(writePath, filepath.Join(tmpDir, f.Filename)); err != nil {
logging.LogErrorf("import conf failed: %s", err)
ret.Code = -1
ret.Msg = err.Error()
@@ -486,8 +493,8 @@ func importConf(c *gin.Context) {
return
}
- tmp = filepath.Join(tmpDir, entries[0].Name())
- data, err = os.ReadFile(tmp)
+ writePath = filepath.Join(tmpDir, entries[0].Name())
+ data, err = os.ReadFile(writePath)
if err != nil {
logging.LogErrorf("import conf failed: %s", err)
ret.Code = -1