Skip to content

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

:bug: Fix loading assets stored beside documents

Danielb071fc658753
diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.go
index 33f9b12204..afb8bd12de 100644
--- a/kernel/model/publish_access.go
+++ b/kernel/model/publish_access.go
@@ -455,6 +455,22 @@ func CheckPublishAuthCookie(c *gin.Context, ID string, password string) bool {
return err == nil && authCookie.Value == util.SHA256Hash([]byte(ID+password))
}
+func assetPathFromDataRelativePath(relPath string) (assetPath, boxID string, ok bool) {
+ pathParts := strings.Split(relPath, "/")
+ if 1 < len(pathParts) && pathParts[0] == "assets" {
+ return strings.Join(pathParts, "/"), "", true
+ }
+ if 2 >= len(pathParts) || !ast.IsNodeIDPattern(pathParts[0]) {
+ return
+ }
+ for i := 1; i < len(pathParts)-1; i++ {
+ if pathParts[i] == "assets" {
+ return strings.Join(pathParts[i:], "/"), pathParts[0], true
+ }
+ }
+ return
+}
+
func CheckAbsPathAccessableByPublishAccess(c *gin.Context, absPath string, publishAccess PublishAccess) bool {
absPath = filepath.Clean(absPath)
@@ -475,18 +491,16 @@ func CheckAbsPathAccessableByPublishAccess(c *gin.Context, absPath string, publi
return true
}
+ if assetPath, box, ok := assetPathFromDataRelativePath(relPath); ok {
+ return checkAssetPathAccessableByPublishAccess(c, publishAccess, assetPath, box)
+ }
+
if ast.IsNodeIDPattern(pathParts[0]) {
box := pathParts[0]
- if 2 < len(pathParts) && "assets" == pathParts[1] {
- assetPath := strings.Join(pathParts[1:], "/")
- return checkAssetPathAccessableByPublishAccess(c, publishAccess, assetPath, box)
- }
blockPath := "/" + strings.Join(pathParts[1:], "/")
passwordID, password := GetPathPasswordByPublishAccess(box, blockPath, publishAccess)
publishIgnore := GetDisablePublishAccess(publishAccess)
return CheckPathAccessableByPublishIgnore(box, blockPath, publishIgnore) && (password == "" || CheckPublishAuthCookie(c, passwordID, password))
- } else if pathParts[0] == "assets" {
- return checkAssetPathAccessableByPublishAccess(c, publishAccess, relPath, "")
}
}
return false
diff --git a/kernel/model/publish_access_test.go b/kernel/model/publish_access_test.go
index 0dc40c12e2..35c0cad845 100644
--- a/kernel/model/publish_access_test.go
+++ b/kernel/model/publish_access_test.go
@@ -63,6 +63,36 @@ func TestFilterLocalStorageByPublishAccess(t *testing.T) {
}
}
+func TestAssetPathFromDataRelativePath(t *testing.T) {
+ const boxID = "20260806000000-box0001"
+ tests := []struct {
+ name string
+ relativePath string
+ wantAssetPath string
+ wantBoxID string
+ wantOK bool
+ }{
+ {name: "global asset", relativePath: "assets/image.png", wantAssetPath: "assets/image.png", wantOK: true},
+ {name: "global nested asset", relativePath: "assets/images/image.png", wantAssetPath: "assets/images/image.png", wantOK: true},
+ {name: "notebook asset", relativePath: boxID + "/assets/image.png", wantAssetPath: "assets/image.png", wantBoxID: boxID, wantOK: true},
+ {name: "document asset", relativePath: boxID + "/20260806000001-doc0001/assets/image.png", wantAssetPath: "assets/image.png", wantBoxID: boxID, wantOK: true},
+ {name: "nested document asset", relativePath: boxID + "/20260806000001-doc0001/20260806000002-doc0002/assets/images/image.png", wantAssetPath: "assets/images/image.png", wantBoxID: boxID, wantOK: true},
+ {name: "nested assets directory", relativePath: boxID + "/20260806000001-doc0001/assets/images/assets/image.png", wantAssetPath: "assets/images/assets/image.png", wantBoxID: boxID, wantOK: true},
+ {name: "notebook directory", relativePath: boxID + "/assets"},
+ {name: "document", relativePath: boxID + "/20260806000001-doc0001.sy"},
+ {name: "non-notebook asset", relativePath: "storage/assets/image.png"},
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ assetPath, actualBoxID, ok := assetPathFromDataRelativePath(test.relativePath)
+ if assetPath != test.wantAssetPath || actualBoxID != test.wantBoxID || ok != test.wantOK {
+ t.Fatalf("assetPathFromDataRelativePath(%q) = [%q, %q, %v], want [%q, %q, %v]",
+ test.relativePath, assetPath, actualBoxID, ok, test.wantAssetPath, test.wantBoxID, test.wantOK)
+ }
+ })
+ }
+}
+
func TestCheckBlockTreeAccessableByPublishAccess(t *testing.T) {
const (
boxID = "20260721000000-boxid01"
diff --git a/kernel/server/serve.go b/kernel/server/serve.go
index 1b3d3d9a07..f4b3cf1f3b 100644
--- a/kernel/server/serve.go
+++ b/kernel/server/serve.go
@@ -886,6 +886,26 @@ func isValidAssetRequestPath(requestPath string) bool {
return err == nil
}
+func isValidResolvedAssetPath(assetAbsPath, requestBoxID string) bool {
+ pathBoxID := model.ExtractBoxIDFromAssetsPath(assetAbsPath)
+ if requestBoxID != "" {
+ if requestBoxID != pathBoxID {
+ return false
+ }
+ return gulu.File.IsSubPath(filepath.Join(util.DataDir, requestBoxID, "assets"), assetAbsPath)
+ }
+
+ if model.IsEncryptedAssetPath(assetAbsPath) {
+ return false
+ }
+ dataRelativePath, err := filepath.Rel(util.DataDir, assetAbsPath)
+ if err != nil {
+ return false
+ }
+ _, validatedAbsPath, err := model.ResolveDataAssetPath(filepath.ToSlash(dataRelativePath))
+ return err == nil && filepath.Clean(validatedAbsPath) == filepath.Clean(assetAbsPath)
+}
+
func serveAssets(ginServer *gin.Engine) {
ginServer.POST("/upload", model.CheckAuth, model.CheckAdminRole, model.CheckReadonly, model.Upload)
@@ -924,21 +944,7 @@ func serveAssets(ginServer *gin.Engine) {
return
}
- // 验证最终绝对路径必须在 data/assets 或 <boxID>/assets 下
- boxIDFromPath := model.ExtractBoxIDFromAssetsPath(p)
- assetsRoot := filepath.Join(util.DataDir, "assets")
- if boxIDFromPath != "" {
- assetsRoot = filepath.Join(util.DataDir, boxIDFromPath, "assets")
- }
- if boxID != "" && boxID != boxIDFromPath {
- context.Status(http.StatusForbidden)
- return
- }
- if boxID == "" && model.IsEncryptedAssetPath(p) {
- context.Status(http.StatusForbidden)
- return
- }
- if !gulu.File.IsSubPath(assetsRoot, p) {
+ if !isValidResolvedAssetPath(p, boxID) {
context.Status(http.StatusForbidden)
return
}
diff --git a/kernel/server/serve_assets_test.go b/kernel/server/serve_assets_test.go
index 809b26cc3f..66b94d3d89 100644
--- a/kernel/server/serve_assets_test.go
+++ b/kernel/server/serve_assets_test.go
@@ -19,6 +19,7 @@ import (
"testing"
"github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/util"
)
type assetRequestPathTest struct {
@@ -90,6 +91,65 @@ func TestAssetRequestPathURLDecoding(t *testing.T) {
}
}
+func TestIsValidResolvedAssetPath(t *testing.T) {
+ originalDataDir := util.DataDir
+ util.DataDir = t.TempDir()
+ t.Cleanup(func() {
+ util.DataDir = originalDataDir
+ })
+
+ const (
+ boxID = "20260806000000-box0001"
+ otherBoxID = "20260806000001-box0002"
+ encryptedBoxID = "20260806000002-box0003"
+ docID = "20260806000003-doc0001"
+ )
+ writeFile := func(filePath, content string) {
+ t.Helper()
+ if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ writeFile(filepath.Join(util.DataDir, boxID, ".siyuan", "conf.json"), `{}`)
+ writeFile(filepath.Join(util.DataDir, encryptedBoxID, ".siyuan", "conf.json"), `{"encrypted":true}`)
+ globalAssetPath := filepath.Join(util.DataDir, "assets", "global.png")
+ boxAssetPath := filepath.Join(util.DataDir, boxID, "assets", "box.png")
+ documentAssetPath := filepath.Join(util.DataDir, boxID, docID, "assets", "document.png")
+ encryptedAssetPath := filepath.Join(util.DataDir, encryptedBoxID, "assets", "encrypted.png")
+ nonAssetPath := filepath.Join(util.DataDir, boxID, docID, "document.png")
+ for _, assetPath := range []string{globalAssetPath, boxAssetPath, documentAssetPath, encryptedAssetPath, nonAssetPath} {
+ writeFile(assetPath, "image")
+ }
+
+ tests := []struct {
+ name string
+ assetPath string
+ requestBoxID string
+ want bool
+ }{
+ {name: "global asset", assetPath: globalAssetPath, want: true},
+ {name: "notebook asset", assetPath: boxAssetPath, want: true},
+ {name: "document asset", assetPath: documentAssetPath, want: true},
+ {name: "explicit notebook", assetPath: boxAssetPath, requestBoxID: boxID, want: true},
+ {name: "explicit notebook rejects document asset", assetPath: documentAssetPath, requestBoxID: boxID},
+ {name: "mismatched notebook", assetPath: boxAssetPath, requestBoxID: otherBoxID},
+ {name: "encrypted asset requires notebook", assetPath: encryptedAssetPath},
+ {name: "encrypted asset with notebook", assetPath: encryptedAssetPath, requestBoxID: encryptedBoxID, want: true},
+ {name: "non-asset file", assetPath: nonAssetPath},
+ }
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ if got := isValidResolvedAssetPath(test.assetPath, test.requestBoxID); got != test.want {
+ t.Fatalf("isValidResolvedAssetPath(%q, %q) = %v, want %v", test.assetPath, test.requestBoxID, got, test.want)
+ }
+ })
+ }
+}
+
func TestSecureAssetContentHeadersForcesAttachmentOnScriptCapableAssets(t *testing.T) {
// 可执行脚本的资产必须强制附件下载,禁止浏览器同源内联渲染
// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-mjf3-jwmf-r6wf