Skip to content

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

:lock: Filter inaccessible database images in publish mode https://github.com/siyuan-note/siyuan/security/advisories/GHSA-72gf-6f75-p53f

Danielc6aa2cfc332c
diff --git a/kernel/api/av.go b/kernel/api/av.go
index a8bef0ca4f..a97f26f973 100644
--- a/kernel/api/av.go
+++ b/kernel/api/av.go
@@ -1193,7 +1193,7 @@ func getCurrentAttrViewImages(c *gin.Context) {
query = queryArg.(string)
}
- images, err := model.GetCurrentAttributeViewImages(id, viewID, query)
+ images, err := model.GetCurrentAttributeViewImages(c, id, viewID, query)
if err != nil {
ret.Code = -1
ret.Msg = err.Error()
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index cf3d740580..20fb0d2add 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -534,13 +534,19 @@ func multimodalValueOrDefault(value, fallback string) string {
return value
}
+type attributeViewItemFilter func(attrView *av.AttributeView, itemID string) bool
+
func DocAssets(rootID string, retainQueryStr bool) (ret []string, err error) {
+ return docAssets(rootID, retainQueryStr, nil)
+}
+
+func docAssets(rootID string, retainQueryStr bool, itemFilter attributeViewItemFilter) (ret []string, err error) {
tree, err := LoadTreeByBlockID(rootID)
if err != nil {
return
}
- ret = getAssetsLinkDests(tree.Root, false)
+ ret = getAssetsLinkDestsWithAttributeViewItemFilter(tree.Root, false, itemFilter)
if !retainQueryStr {
for i, asset := range ret {
if before, _, ok := strings.Cut(asset, "?"); ok {
@@ -2062,6 +2068,14 @@ func getQueryEmbedNodesAssetsLinkDests(node *ast.Node) (ret []string) {
}
func getAssetsLinkDests(node *ast.Node, includeServePath bool) (ret []string) {
+ return getAssetsLinkDestsWithAttributeViewItemFilter(node, includeServePath, nil)
+}
+
+func getAssetsLinkDestsWithAttributeViewItemFilter(
+ node *ast.Node,
+ includeServePath bool,
+ itemFilter attributeViewItemFilter,
+) (ret []string) {
ret = []string{}
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
if n.IsBlock() {
@@ -2118,33 +2132,7 @@ func getAssetsLinkDests(node *ast.Node, includeServePath bool) (ret []string) {
return ast.WalkContinue
}
- for _, keyValues := range attrView.KeyValues {
- if av.KeyTypeMAsset == keyValues.Key.Type {
- for _, value := range keyValues.Values {
- if 1 > len(value.MAsset) {
- continue
- }
-
- for _, asset := range value.MAsset {
- dest := asset.Content
- if !util.IsAssetLinkDest([]byte(dest), includeServePath) {
- continue
- }
- ret = append(ret, strings.TrimSpace(dest))
- }
- }
- } else if av.KeyTypeURL == keyValues.Key.Type {
- for _, value := range keyValues.Values {
- if nil != value.URL {
- dest := value.URL.Content
- if !util.IsAssetLinkDest([]byte(dest), includeServePath) {
- continue
- }
- ret = append(ret, strings.TrimSpace(dest))
- }
- }
- }
- }
+ ret = append(ret, getAttributeViewAssetsLinkDests(attrView, includeServePath, itemFilter)...)
} else {
if ast.NodeWidget == n.Type {
dataAssets := n.IALAttr("custom-data-assets")
@@ -2176,6 +2164,39 @@ func getAssetsLinkDests(node *ast.Node, includeServePath bool) (ret []string) {
return
}
+func getAttributeViewAssetsLinkDests(
+ attrView *av.AttributeView,
+ includeServePath bool,
+ itemFilter attributeViewItemFilter,
+) (ret []string) {
+ for _, keyValues := range attrView.KeyValues {
+ if av.KeyTypeMAsset != keyValues.Key.Type && av.KeyTypeURL != keyValues.Key.Type {
+ continue
+ }
+
+ for _, value := range keyValues.Values {
+ if nil != itemFilter && !itemFilter(attrView, value.BlockID) {
+ continue
+ }
+
+ if av.KeyTypeMAsset == keyValues.Key.Type {
+ for _, asset := range value.MAsset {
+ dest := asset.Content
+ if util.IsAssetLinkDest([]byte(dest), includeServePath) {
+ ret = append(ret, strings.TrimSpace(dest))
+ }
+ }
+ } else if nil != value.URL {
+ dest := value.URL.Content
+ if util.IsAssetLinkDest([]byte(dest), includeServePath) {
+ ret = append(ret, strings.TrimSpace(dest))
+ }
+ }
+ }
+ }
+ return
+}
+
func getAssetsLinkDestsInTree(tree *parse.Tree, includeServePath bool) (nodes []*ast.Node) {
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
diff --git a/kernel/model/assets_test.go b/kernel/model/assets_test.go
index dd61cb2f6b..2625d1ea68 100644
--- a/kernel/model/assets_test.go
+++ b/kernel/model/assets_test.go
@@ -33,6 +33,7 @@ import (
"time"
"github.com/88250/lute/ast"
+ "github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -451,3 +452,47 @@ func TestGetAssetLinkDestsByNode(t *testing.T) {
t.Fatalf("get asset link destination block ID: got %q, want %q", got, blockID)
}
}
+
+func TestGetAttributeViewAssetsLinkDestsFiltersItems(t *testing.T) {
+ attrView := &av.AttributeView{
+ KeyValues: []*av.KeyValues{
+ {
+ Key: &av.Key{Type: av.KeyTypeMAsset},
+ Values: []*av.Value{
+ {
+ BlockID: "public-item",
+ MAsset: []*av.ValueAsset{
+ {Type: av.AssetTypeImage, Content: "assets/public.png"},
+ },
+ },
+ {
+ BlockID: "private-item",
+ MAsset: []*av.ValueAsset{
+ {Type: av.AssetTypeImage, Content: "assets/private.png"},
+ },
+ },
+ },
+ },
+ {
+ Key: &av.Key{Type: av.KeyTypeURL},
+ Values: []*av.Value{
+ {BlockID: "public-item", URL: &av.ValueURL{Content: "assets/public-url.png"}},
+ {BlockID: "private-item", URL: &av.ValueURL{Content: "assets/private-url.png"}},
+ },
+ },
+ },
+ }
+
+ filter := func(_ *av.AttributeView, itemID string) bool {
+ return "public-item" == itemID
+ }
+ want := []string{"assets/public.png", "assets/public-url.png"}
+ if got := getAttributeViewAssetsLinkDests(attrView, false, filter); !reflect.DeepEqual(got, want) {
+ t.Fatalf("get filtered attribute view asset links: got %v, want %v", got, want)
+ }
+
+ want = []string{"assets/public.png", "assets/private.png", "assets/public-url.png", "assets/private-url.png"}
+ if got := getAttributeViewAssetsLinkDests(attrView, false, nil); !reflect.DeepEqual(got, want) {
+ t.Fatalf("get unfiltered attribute view asset links: got %v, want %v", got, want)
+ }
+}
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 9f213d2237..bcb711aa21 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -34,6 +34,7 @@ import (
"github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
+ "github.com/gin-gonic/gin"
"github.com/jinzhu/copier"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
@@ -2920,7 +2921,7 @@ func setAttrViewGroupStates(view *av.View, groupStates map[string]*GroupState) {
}
}
-func GetCurrentAttributeViewImages(avID, viewID, query string) (ret []string, err error) {
+func GetCurrentAttributeViewImages(c *gin.Context, avID, viewID, query string) (ret []string, err error) {
var attrView *av.AttributeView
attrView, err = av.ParseAttributeView(avID)
if err != nil {
@@ -2940,6 +2941,9 @@ func GetCurrentAttributeViewImages(avID, viewID, query string) (ret []string, er
table := getAttrViewTable(attrView, view, query)
av.Filter(table, attrView, rollupFurtherCollections, cachedAttrViews)
av.Sort(table, attrView)
+ if IsReadOnlyRoleContext(c) {
+ table = FilterViewByPublishAccess(c, GetPublishAccess(), table).(*av.Table)
+ }
ids := map[string]bool{}
for _, column := range table.Columns {
diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.go
index 2afe2f3081..a0cbbc74b7 100644
--- a/kernel/model/publish_access.go
+++ b/kernel/model/publish_access.go
@@ -274,23 +274,52 @@ func CheckAbsPathAccessableByPublishAccess(c *gin.Context, absPath string, publi
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" {
- publishIgnore := GetDisablePublishAccess(publishAccess)
- bts := treenode.GetBlockTreesByType("d")
- for _, bt := range bts {
- passwordID, password := GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess)
- if CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) && (password == "" || CheckPublishAuthCookie(c, passwordID, password)) {
- assets, _ := DocAssets(bt.ID, false)
- if slices.Contains(assets, relPath) {
- return true
- }
- }
- }
- return false
+ return checkAssetPathAccessableByPublishAccess(c, publishAccess, relPath, "")
+ }
+ }
+ return false
+}
+
+func checkAssetPathAccessableByPublishAccess(c *gin.Context, publishAccess PublishAccess, assetPath, boxID string) bool {
+ publishIgnore := GetDisablePublishAccess(publishAccess)
+ itemAccessCache := map[*av.AttributeView]map[string]bool{}
+ itemFilter := func(attrView *av.AttributeView, itemID string) bool {
+ itemAccess := itemAccessCache[attrView]
+ if nil == itemAccess {
+ itemAccess = map[string]bool{}
+ itemAccessCache[attrView] = itemAccess
+ }
+ if accessable, ok := itemAccess[itemID]; ok {
+ return accessable
+ }
+
+ accessable := checkAttributeViewItemIDAccessableByPublishAccess(c, publishAccess, attrView, itemID)
+ itemAccess[itemID] = accessable
+ return accessable
+ }
+ for _, bt := range treenode.GetBlockTreesByType("d") {
+ if "" != boxID && bt.BoxID != boxID {
+ continue
+ }
+
+ passwordID, password := GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess)
+ if !CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) ||
+ ("" != password && !CheckPublishAuthCookie(c, passwordID, password)) {
+ continue
+ }
+
+ assets, _ := docAssets(bt.ID, false, itemFilter)
+ if slices.Contains(assets, assetPath) {
+ return true
}
}
return false
@@ -359,6 +388,26 @@ func checkAttributeViewItemAccessableByPublishAccess(c *gin.Context, publishAcce
return CheckBlockIdAccessableByPublishAccess(c, publishAccess, blockValue.Block.ID)
}
+func checkAttributeViewItemIDAccessableByPublishAccess(
+ c *gin.Context,
+ publishAccess PublishAccess,
+ attrView *av.AttributeView,
+ itemID string,
+) bool {
+ if nil == attrView || "" == itemID {
+ return false
+ }
+
+ blockValue := attrView.GetBlockValue(itemID)
+ if nil == blockValue {
+ return false
+ }
+ if blockValue.IsDetached || nil == blockValue.Block || "" == blockValue.Block.ID {
+ return true
+ }
+ return CheckBlockIdAccessableByPublishAccess(c, publishAccess, blockValue.Block.ID)
+}
+
func FilterBlockAttributeViewKeysByPublishAccess(c *gin.Context, publishAccess PublishAccess, blockAttributeViewKeys []*BlockAttributeViewKeys) (ret []*BlockAttributeViewKeys) {
publishIgnore := GetDisablePublishAccess(publishAccess)
ret = []*BlockAttributeViewKeys{}
diff --git a/kernel/model/publish_access_test.go b/kernel/model/publish_access_test.go
index 65e2904683..7cdd5e07e7 100644
--- a/kernel/model/publish_access_test.go
+++ b/kernel/model/publish_access_test.go
@@ -22,6 +22,7 @@ import (
"testing"
"github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
@@ -62,6 +63,28 @@ func TestCheckBlockTreeAccessableByPublishAccess(t *testing.T) {
}
}
+func TestCheckAttributeViewItemIDAccessableByPublishAccess(t *testing.T) {
+ attrView := &av.AttributeView{
+ KeyValues: []*av.KeyValues{
+ {
+ Key: &av.Key{Type: av.KeyTypeBlock},
+ Values: []*av.Value{
+ {BlockID: "detached-item", Type: av.KeyTypeBlock, IsDetached: true},
+ },
+ },
+ },
+ }
+ c, _ := gin.CreateTestContext(httptest.NewRecorder())
+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
+
+ if !checkAttributeViewItemIDAccessableByPublishAccess(c, PublishAccess{}, attrView, "detached-item") {
+ t.Fatal("detached attribute view item should remain accessible")
+ }
+ if checkAttributeViewItemIDAccessableByPublishAccess(c, PublishAccess{}, attrView, "missing-item") {
+ t.Fatal("attribute view item without a primary value should not expose assets")
+ }
+}
+
func TestFilterSearchDocsByPublishAccess(t *testing.T) {
const (
boxID = "20260720000000-boxid01"