Skip to content

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

:lock: Filter publish-inaccessible attribute view rows and cells when exporting https://github.com/siyuan-note/siyuan/security/advisories/GHSA-778g-gq49-fxxj

Daniel29cc1906042e
diff --git a/kernel/api/export.go b/kernel/api/export.go
index 27f581a6ec..1796b891a7 100644
--- a/kernel/api/export.go
+++ b/kernel/api/export.go
@@ -752,6 +752,7 @@ var exportPreview = contractHandler(apicontract.ExportPreview, func(c *gin.Conte
}
isReadOnlyRole := model.IsReadOnlyRoleContext(c)
+ avPublishFilter := model.NewAVExportPublishFilter(c)
var publishAccess model.PublishAccess
var accessChecker model.EmbedBlockAccessChecker
if isReadOnlyRole {
@@ -760,7 +761,7 @@ var exportPreview = contractHandler(apicontract.ExportPreview, func(c *gin.Conte
return model.CheckBlockIdAccessableByPublishAccess(c, publishAccess, blockID)
}
}
- stdHTML := model.ExportPreview(id, fillCSSVar, accessChecker)
+ stdHTML := model.ExportPreview(id, fillCSSVar, avPublishFilter, accessChecker)
if isReadOnlyRole {
bt := treenode.GetBlockTree(id)
if bt != nil {
diff --git a/kernel/api/lute.go b/kernel/api/lute.go
index 4862ddc0a4..871d2830eb 100644
--- a/kernel/api/lute.go
+++ b/kernel/api/lute.go
@@ -49,6 +49,7 @@ var copyStdMarkdown = contractHandler(apicontract.CopyStdMarkdown, func(c *gin.C
id := request.ID
assetsDestSpace2Underscore, fillCSSVar, adjustHeadingLevel, imgTag := request.AssetsDestSpace2Underscore, request.FillCSSVar, request.AdjustHeadingLevel, request.ImgTag
isReadOnlyRole := model.IsReadOnlyRoleContext(c)
+ avPublishFilter := model.NewAVExportPublishFilter(c)
var publishAccess model.PublishAccess
var accessChecker model.EmbedBlockAccessChecker
if isReadOnlyRole {
@@ -57,7 +58,7 @@ var copyStdMarkdown = contractHandler(apicontract.CopyStdMarkdown, func(c *gin.C
return model.CheckBlockIdAccessableByPublishAccess(c, publishAccess, blockID)
}
}
- markdownContent := model.ExportStdMarkdown(id, assetsDestSpace2Underscore, fillCSSVar, adjustHeadingLevel, imgTag, accessChecker)
+ markdownContent := model.ExportStdMarkdown(id, assetsDestSpace2Underscore, fillCSSVar, adjustHeadingLevel, imgTag, avPublishFilter, accessChecker)
if isReadOnlyRole {
bt := treenode.GetBlockTree(id)
if bt != nil {
diff --git a/kernel/cli/cmd/export.go b/kernel/cli/cmd/export.go
index 1d8c8cde9e..85f00c12a9 100644
--- a/kernel/cli/cmd/export.go
+++ b/kernel/cli/cmd/export.go
@@ -95,7 +95,7 @@ var exportPreviewCmd = &cobra.Command{
return nil
}
- html := model.ExportPreview(id, false)
+ html := model.ExportPreview(id, false, nil)
return writeExportContent(html, output)
},
}
diff --git a/kernel/mcp/tools/export.go b/kernel/mcp/tools/export.go
index 9bf091c009..a26aad9076 100644
--- a/kernel/mcp/tools/export.go
+++ b/kernel/mcp/tools/export.go
@@ -96,7 +96,7 @@ func exportPreview(args map[string]any) (CallToolResult, error) {
if id == "" {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id is required"}}, IsError: true}, nil
}
- html := model.ExportPreview(id, false)
+ html := model.ExportPreview(id, false, nil)
if html == "" {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: "export preview failed or empty"}}, IsError: true}, nil
}
diff --git a/kernel/model/asset_download_regression_test.go b/kernel/model/asset_download_regression_test.go
index 4357cca4d0..19671f67b2 100644
--- a/kernel/model/asset_download_regression_test.go
+++ b/kernel/model/asset_download_regression_test.go
@@ -78,7 +78,7 @@ func TestAssetDownloadFootnoteExport(t *testing.T) {
Conf.Export.BlockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, true)
+ Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, true, nil)
if prepareErr != nil {
t.Fatal(prepareErr)
}
diff --git a/kernel/model/export.go b/kernel/model/export.go
index 7d100fec8f..2db88136b9 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -481,7 +481,7 @@ func Export2Liandi(id string) (err error) {
".md", 3, 1, 1,
"#", "#",
"", "",
- false, false, nil, true, false, nil)
+ false, false, nil, true, false, nil, nil)
result := gulu.Ret.NewResult()
request := httpclient.NewCloudRequest30s()
request = request.
@@ -1024,7 +1024,8 @@ func exportResourcesEncryptedBox(resourcePaths []string) (encryptedBoxID string,
return
}
-func ExportPreview(id string, fillCSSVar bool, accessChecker ...EmbedBlockAccessChecker) (retStdHTML string) {
+func ExportPreview(id string, fillCSSVar bool, avPublishFilter AVExportPublishFilter,
+ accessChecker ...EmbedBlockAccessChecker) (retStdHTML string) {
if exportErr := withExportReadLockByBlockID(id, func() error {
blockRefMode := Conf.Export.BlockRefMode
bt := getExportBlockTree(id)
@@ -1040,7 +1041,7 @@ func ExportPreview(id string, fillCSSVar bool, accessChecker ...EmbedBlockAccess
blockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
"#", "#", // 这里固定使用 # 包裹标签,否则无法正确解析标签 https://github.com/siyuan-note/siyuan/issues/13857
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, true, accessChecker...)
+ Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, true, avPublishFilter, accessChecker...)
if nil != exportTreeErr {
return exportTreeErr
}
@@ -1229,7 +1230,7 @@ func exportMarkdownHTML(id, savePath string, docx, merge bool, mergeHeadingOptio
blockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, true)
+ Conf.Export.AddTitle, "", Conf.Export.InlineMemo, true, true, nil)
if nil != exportTreeErr {
return exportTreeErr
}
@@ -1397,7 +1398,7 @@ func ExportHTMLWithTitle(id, savePath string, pdf, keepFold, merge, addTitle boo
blockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- addTitle, customTitle, Conf.Export.InlineMemo, true, true)
+ addTitle, customTitle, Conf.Export.InlineMemo, true, true, nil)
if nil != exportTreeErr {
return exportTreeErr
}
@@ -2059,7 +2060,7 @@ func processPDFLinkEmbedAssets(pdfCtx *model.Context, assetDests []string, boxID
}
func ExportStdMarkdown(id string, assetsDestSpace2Underscore, fillCSSVar, adjustHeadingLevel, imgTag bool,
- accessChecker ...EmbedBlockAccessChecker) string {
+ avPublishFilter AVExportPublishFilter, accessChecker ...EmbedBlockAccessChecker) string {
var ret string
if exportErr := withExportReadLockByBlockID(id, func() error {
bt := getExportBlockTree(id)
@@ -2104,7 +2105,7 @@ func ExportStdMarkdown(id string, assetsDestSpace2Underscore, fillCSSVar, adjust
".md", Conf.Export.BlockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- Conf.Export.AddTitle, Conf.Export.InlineMemo, defBlockIDs, true, fillCSSVar, nil, accessChecker...)
+ Conf.Export.AddTitle, Conf.Export.InlineMemo, defBlockIDs, true, fillCSSVar, nil, avPublishFilter, accessChecker...)
return nil
}); exportErr != nil {
logging.LogErrorf("export std markdown [%s] failed: %s", id, exportErr)
@@ -3027,7 +3028,7 @@ func ExportMarkdownContent(id string, refMode, embedMode int, addYfm, fillCSSVar
".md", refMode, embedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- addTitle, Conf.Export.InlineMemo, nil, true, fillCSSVar, nil)
+ addTitle, Conf.Export.InlineMemo, nil, true, fillCSSVar, nil, nil)
docIAL := parse.IAL2Map(tree.Root.KramdownIAL)
if addYfm {
exportedMd = yfm(docIAL) + exportedMd
@@ -3067,7 +3068,7 @@ func exportMarkdownContent(rootID, ext string, exportRefMode int, defBlockIDs []
ext, exportRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
- Conf.Export.AddTitle, Conf.Export.InlineMemo, defBlockIDs, singleFile, false, boxPaths)
+ Conf.Export.AddTitle, Conf.Export.InlineMemo, defBlockIDs, singleFile, false, boxPaths, nil)
docIAL := parse.IAL2Map(tree.Root.KramdownIAL)
if Conf.Export.MarkdownYFM {
// 导出 Markdown 时在文档头添加 YFM 开关 https://github.com/siyuan-note/siyuan/issues/7727
@@ -3080,12 +3081,12 @@ func exportMarkdownContent0(id string, tree *parse.Tree, cloudAssetsBase string,
ext string, blockRefMode, blockEmbedMode, fileAnnotationRefMode int,
tagOpenMarker, tagCloseMarker string, blockRefTextLeft, blockRefTextRight string,
addTitle, inlineMemo bool, defBlockIDs []string, singleFile, fillCSSVar bool, boxPaths map[string]string,
- accessChecker ...EmbedBlockAccessChecker) (ret string) {
+ avPublishFilter AVExportPublishFilter, accessChecker ...EmbedBlockAccessChecker) (ret string) {
tree, exportTreeErr := exportTree(tree, false, false, false, false,
blockRefMode, blockEmbedMode, fileAnnotationRefMode,
tagOpenMarker, tagCloseMarker,
blockRefTextLeft, blockRefTextRight,
- addTitle, "", inlineMemo, 0 < len(defBlockIDs), singleFile, accessChecker...)
+ addTitle, "", inlineMemo, 0 < len(defBlockIDs), singleFile, avPublishFilter, accessChecker...)
if nil != exportTreeErr {
logging.LogErrorf("prepare Markdown export failed: %s", exportTreeErr)
return ""
@@ -3242,7 +3243,7 @@ func exportTree(tree *parse.Tree, wysiwyg, richTableCells, keepFold, avHiddenCol
tagOpenMarker, tagCloseMarker string,
blockRefTextLeft, blockRefTextRight string,
addTitle bool, customTitle string, inlineMemo, addDocAnchorSpan, singleFile bool,
- accessChecker ...EmbedBlockAccessChecker) (ret *parse.Tree, err error) {
+ avPublishFilter AVExportPublishFilter, accessChecker ...EmbedBlockAccessChecker) (ret *parse.Tree, err error) {
luteEngine := NewLute()
ret = tree
id := tree.Root.ID
@@ -3614,6 +3615,9 @@ func exportTree(tree *parse.Tree, wysiwyg, richTableCells, keepFold, avHiddenCol
}
av.Sort(table, attrView)
+ // 发布读者的导出结果不得包含绑定到不可访问文档的行和单元格,与渲染路径保持同一套过滤规则
+ table = filterExportAttributeViewTableRows(avPublishFilter, table, avID, n.ID)
+
aligns := getAttrViewTableAligns(table, avHiddenCol)
mdTable := &ast.Node{Type: ast.NodeTable, TableAligns: aligns}
mdTableHead := &ast.Node{Type: ast.NodeTableHead}
@@ -4797,6 +4801,23 @@ func getAttrViewTableAligns(table *av.Table, hiddenCol bool) (ret []int) {
return
}
+// filterExportAttributeViewTableRows 按发布访问规则过滤导出用的表格,filter 为 nil 时原样返回。
+// 过滤在表格副本上进行,发布读者导出数据库时不会看到绑定到不可访问文档的行和单元格。
+func filterExportAttributeViewTableRows(filter AVExportPublishFilter, table *av.Table,
+ avID, blockID string) *av.Table {
+ if nil == filter || nil == table {
+ return table
+ }
+
+ cloned := *table
+ cloned.Groups = nil
+ viewable := filter(&cloned, avID, blockID)
+ if filtered, ok := viewable.(*av.Table); ok {
+ return filtered
+ }
+ return table
+}
+
// adjustHeadingLevel 聚焦导出(即非文档块)的情况下,将第一个标题层级提升为一级(如果开启了添加文档标题的话提升为二级)。
// Export preview mode supports focus use https://github.com/siyuan-note/siyuan/issues/15340
func adjustHeadingLevel(bt *treenode.BlockTree, tree *parse.Tree, addTitle bool) {
diff --git a/kernel/model/export_attribute_view_publish_test.go b/kernel/model/export_attribute_view_publish_test.go
new file mode 100644
index 0000000000..c011dc8d3f
--- /dev/null
+++ b/kernel/model/export_attribute_view_publish_test.go
@@ -0,0 +1,241 @@
+// SiYuan - From thought to insight, with agents
+// 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/>.
+
+//go:build fts5
+
+package model
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/parse"
+ "github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/av"
+ "github.com/siyuan-note/siyuan/kernel/filesys"
+ "github.com/siyuan-note/siyuan/kernel/sql"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+// exportAVPublishTestFixture 构造发布读者导出数据库的场景:
+// 可发布笔记本中的文档 HOST 内嵌数据库,其关联列指向禁止发布笔记本中的数据库,
+// 目标行绑定禁止发布文档中的块。
+type exportAVPublishTestFixture struct {
+ hostDocID string
+ privateID string
+ blockID string
+ sourceAvID string
+ targetAvID string
+ publicBlkID string
+ orphanRowID string
+ canary string
+ detachedID string
+ detached string
+ orphanBlk string
+ orphan string
+}
+
+func setupExportAVPublishTest(t *testing.T) *exportAVPublishTestFixture {
+ t.Helper()
+
+ setupExportPublishTest(t)
+ oldLang, oldAttrViewLangs := util.Lang, util.AttrViewLangs
+ util.Lang = "en"
+ util.AttrViewLangs = map[string]map[string]any{
+ "en": {"key": "Key", "select": "Select", "table": "Table"},
+ }
+ t.Cleanup(func() {
+ util.Lang, util.AttrViewLangs = oldLang, oldAttrViewLangs
+ })
+
+ fixture := &exportAVPublishTestFixture{
+ hostDocID: "20260921000001-hostdoc",
+ privateID: "20260921000002-privdoc",
+ blockID: "20260921000003-avblock",
+ sourceAvID: "20260921000004-sourcea",
+ targetAvID: "20260921000005-targeta",
+ publicBlkID: "20260921000010-publblk",
+ orphanRowID: "20260921000009-orphanr",
+ detachedID: "20260921000007-detachd",
+ orphanBlk: "20260921000008-orphanb",
+ canary: "PRIVATE_AV_CANARY_20260921",
+ detached: "DETACHED_AV_ROW_20260921",
+ orphan: "ORPHAN_AV_ROW_20260921",
+ }
+
+ const (
+ publicBoxID = "20260921000100-pubbox1"
+ privateBoxID = "20260921000101-prvbox1"
+ )
+
+ // 目标数据库位于禁止发布的笔记本中,其行绑定禁止发布文档中的块
+ targetAttrView := av.NewAttributeView(fixture.targetAvID)
+ targetBlockKey := targetAttrView.GetBlockKey()
+ targetAttrView.GetBlockKeyValues().Values = []*av.Value{{
+ ID: ast.NewNodeID(), KeyID: targetBlockKey.ID, BlockID: fixture.privateID, Type: av.KeyTypeBlock,
+ Block: &av.ValueBlock{ID: fixture.privateID, Content: fixture.canary},
+ }}
+ if err := av.SaveAttributeView(targetAttrView); nil != err {
+ t.Fatal(err)
+ }
+
+ // 源数据库位于可发布文档中,关联列指向目标数据库
+ sourceAttrView := av.NewAttributeView(fixture.sourceAvID)
+ sourceBlockKey := sourceAttrView.GetBlockKey()
+ relationKey := av.NewKey(ast.NewNodeID(), "Relation", "", av.KeyTypeRelation)
+ relationKey.Relation = &av.Relation{AvID: fixture.targetAvID}
+ // 视图列固定为 主键 + 关联列,保证关联内容参与导出渲染
+ sourceAttrView.Views[0].Table.Columns = []*av.ViewTableColumn{
+ {BaseField: &av.BaseField{ID: sourceBlockKey.ID}},
+ {BaseField: &av.BaseField{ID: relationKey.ID}},
+ }
+ sourceAttrView.GetBlockKeyValues().Values = []*av.Value{
+ // 公开行:绑定宿主文档中的块
+ {
+ ID: ast.NewNodeID(), KeyID: sourceBlockKey.ID, BlockID: fixture.publicBlkID, Type: av.KeyTypeBlock,
+ Block: &av.ValueBlock{ID: fixture.publicBlkID, Content: "public row"},
+ },
+ // 游离行:不对应任何文档块,发布读者应当仍然可见
+ {
+ ID: ast.NewNodeID(), KeyID: sourceBlockKey.ID, BlockID: fixture.detachedID, Type: av.KeyTypeBlock,
+ IsDetached: true, Block: &av.ValueBlock{Content: fixture.detached},
+ },
+ // 孤儿行:绑定的块不在块树中,按不可确认可达处理,发布读者不可见
+ {
+ ID: ast.NewNodeID(), KeyID: sourceBlockKey.ID, BlockID: fixture.orphanRowID, Type: av.KeyTypeBlock,
+ Block: &av.ValueBlock{ID: fixture.orphanBlk, Content: fixture.orphan},
+ },
+ }
+ sourceAttrView.KeyValues = append(sourceAttrView.KeyValues, &av.KeyValues{
+ Key: relationKey, Values: []*av.Value{{
+ ID: ast.NewNodeID(), KeyID: relationKey.ID, BlockID: fixture.publicBlkID, Type: av.KeyTypeRelation,
+ Relation: &av.ValueRelation{BlockIDs: []string{fixture.privateID}},
+ }},
+ })
+ if err := av.SaveAttributeView(sourceAttrView); nil != err {
+ t.Fatal(err)
+ }
+
+ hostTree := treenode.NewTree(publicBoxID, "/"+fixture.hostDocID+".sy", "/Host", "Host")
+ for nil != hostTree.Root.FirstChild {
+ hostTree.Root.FirstChild.Unlink()
+ }
+ database := &ast.Node{
+ Type: ast.NodeAttributeView, ID: fixture.blockID, AttributeViewID: fixture.sourceAvID,
+ AttributeViewType: string(av.LayoutTypeTable),
+ }
+ database.SetIALAttr("id", fixture.blockID)
+ database.SetIALAttr(av.NodeAttrView, sourceAttrView.Views[0].ID)
+ hostTree.Root.AppendChild(database)
+ // 公开行绑定的块必须真实存在于块树中,否则会被发布访问过滤按不可确认可达处理
+ publicParagraph := &ast.Node{Type: ast.NodeParagraph, ID: fixture.publicBlkID}
+ publicParagraph.SetIALAttr("id", fixture.publicBlkID)
+ publicParagraph.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("public row")})
+ hostTree.Root.AppendChild(publicParagraph)
+
+ privateTree := treenode.NewTree(privateBoxID, "/"+fixture.privateID+".sy", "/Private", "Private")
+ privateTree.Root.FirstChild.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(fixture.canary)})
+
+ for _, tree := range []*parse.Tree{hostTree, privateTree} {
+ if _, err := filesys.WriteTree(tree); nil != err {
+ t.Fatalf("write synthetic tree: %v", err)
+ }
+ treenode.UpsertBlockTree(tree)
+ sql.IndexTreeQueue(tree)
+ }
+ sql.FlushQueue()
+ av.UpsertBlockRel(fixture.sourceAvID, fixture.hostDocID)
+
+ t.Cleanup(func() {
+ treenode.RemoveBlockTree(publicBoxID, fixture.hostDocID)
+ treenode.RemoveBlockTree(privateBoxID, fixture.privateID)
+ os.Remove(filepath.Join(util.DataDir, privateBoxID, fixture.privateID+".sy"))
+ })
+ return fixture
+}
+
+// newExportAVPublishReaderFilter 模拟 /api/export/preview 与 /api/lute/copyStdMarkdown 中
+// 发布读者请求构造的属性视图过滤器,禁止发布笔记本对该读者不可访问。
+func newExportAVPublishReaderFilter(t *testing.T, privateID string) AVExportPublishFilter {
+ t.Helper()
+
+ c, _ := gin.CreateTestContext(httptest.NewRecorder())
+ c.Request = httptest.NewRequest(http.MethodPost, "/api/export/preview", nil)
+ c.Set(RoleContextKey, RoleReader)
+ return func(view av.Viewable, avID, blockID string) av.Viewable {
+ return FilterAttributeViewByPublishAccess(c, PublishAccess{{ID: privateID, Disable: true}},
+ avID, blockID, view)
+ }
+}
+
+// TestExportAttributeViewFilteredByPublishAccess 验证发布读者导出内嵌数据库的文档时,
+// 绑定到禁止发布文档的行和关联内容都不会出现在导出结果中,可访问的行保留,管理员导出不受影响。
+func TestExportAttributeViewFilteredByPublishAccess(t *testing.T) {
+ fixture := setupExportAVPublishTest(t)
+ filter := newExportAVPublishReaderFilter(t, fixture.privateID)
+
+ markdown := ExportStdMarkdown(fixture.hostDocID, false, false, false, false, filter)
+ if strings.Contains(markdown, "PRIVATE") {
+ t.Fatalf("publish reader received private attribute view content in markdown: %s", markdown)
+ }
+ if !strings.Contains(markdown, "public row") {
+ t.Fatalf("publish reader lost the accessible row in markdown: %s", markdown)
+ }
+
+ stdHTML := ExportPreview(fixture.hostDocID, false, filter)
+ if strings.Contains(stdHTML, "PRIVATE") {
+ t.Fatalf("publish reader received private attribute view content in preview: %s", stdHTML)
+ }
+ if !strings.Contains(stdHTML, "public row") {
+ t.Fatalf("publish reader lost the accessible row in preview: %s", stdHTML)
+ }
+
+ adminMarkdown := ExportStdMarkdown(fixture.hostDocID, false, false, false, false, nil)
+ if !strings.Contains(adminMarkdown, "PRIVATE") {
+ t.Fatalf("administrator export should include private attribute view content: %s", adminMarkdown)
+ }
+
+ // 导出过滤只针对本次导出的副本,不得改动属性视图存储的数据
+ stored, err := av.ParseAttributeViewInBox(fixture.sourceAvID, "")
+ if nil != err {
+ t.Fatal(err)
+ }
+ if 3 != len(stored.GetBlockKeyValues().Values) {
+ t.Fatalf("export filtering mutated the stored attribute view: %d rows left",
+ len(stored.GetBlockKeyValues().Values))
+ }
+}
+
+// TestExportAttributeViewRowPublishAccessSemantics 固定导出过滤的行可见性语义:
+// 游离行可见,绑定块无法解析的行按不可确认可达处理并丢弃,与渲染路径保持一致。
+func TestExportAttributeViewRowPublishAccessSemantics(t *testing.T) {
+ fixture := setupExportAVPublishTest(t)
+ filter := newExportAVPublishReaderFilter(t, fixture.privateID)
+
+ markdown := ExportStdMarkdown(fixture.hostDocID, false, false, false, false, filter)
+ if !strings.Contains(markdown, "DETACHED") {
+ t.Fatalf("detached row should stay visible to publish readers: %s", markdown)
+ }
+ if strings.Contains(markdown, "ORPHAN") {
+ t.Fatalf("row bound to an unresolvable block should be dropped for publish readers: %s", markdown)
+ }
+}
diff --git a/kernel/model/export_attribute_view_test.go b/kernel/model/export_attribute_view_test.go
index 25177b831d..16c078f3fa 100644
--- a/kernel/model/export_attribute_view_test.go
+++ b/kernel/model/export_attribute_view_test.go
@@ -126,7 +126,7 @@ func TestAttributeViewExportDoesNotCrossEncryptedBoundary(t *testing.T) {
t.Fatal("CSV export crossed from a normal document to an encrypted attribute view")
}
- exported, exportErr := exportTree(tree, false, false, false, true, 0, 0, 0, "", "", "", "", false, "", false, false, false)
+ exported, exportErr := exportTree(tree, false, false, false, true, 0, 0, 0, "", "", "", "", false, "", false, false, false, nil)
if exportErr != nil {
t.Fatal(exportErr)
}
diff --git a/kernel/model/export_publish_test.go b/kernel/model/export_publish_test.go
index d6fb58fa06..155f185c68 100644
--- a/kernel/model/export_publish_test.go
+++ b/kernel/model/export_publish_test.go
@@ -36,9 +36,10 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
-// TestExportEmbedFilteredByPublishAccess 验证发布读者导出公共文档时,查询嵌入块匹配到的
-// 发布禁用文档内容会被访问检查过滤,而管理员导出不受影响。
-func TestExportEmbedFilteredByPublishAccess(t *testing.T) {
+// setupExportPublishTest 为发布访问导出用例准备独立的临时工作空间和数据库连接。
+func setupExportPublishTest(t *testing.T) {
+ t.Helper()
+
oldConf := Conf
oldWorkspaceDir := util.WorkspaceDir
oldConfDir := util.ConfDir
@@ -94,6 +95,12 @@ func TestExportEmbedFilteredByPublishAccess(t *testing.T) {
treenode.InitBlockTree(false)
}
})
+}
+
+// TestExportEmbedFilteredByPublishAccess 验证发布读者导出公共文档时,查询嵌入块匹配到的
+// 发布禁用文档内容会被访问检查过滤,而管理员导出不受影响。
+func TestExportEmbedFilteredByPublishAccess(t *testing.T) {
+ setupExportPublishTest(t)
const (
boxID = "20260826000000-box0001"
@@ -140,16 +147,16 @@ func TestExportEmbedFilteredByPublishAccess(t *testing.T) {
return checkBlockTreeAccessableByPublishAccess(c, publishAccess, bt)
}
- markdown := ExportStdMarkdown(publicID, false, false, false, false, accessChecker)
+ markdown := ExportStdMarkdown(publicID, false, false, false, false, nil, accessChecker)
if strings.Contains(markdown, canary) {
t.Fatalf("publish reader received private embed content: %s", markdown)
}
- stdHTML := ExportPreview(publicID, false, accessChecker)
+ stdHTML := ExportPreview(publicID, false, nil, accessChecker)
if strings.Contains(stdHTML, canary) {
t.Fatalf("publish reader received private embed content in preview: %s", stdHTML)
}
- adminMarkdown := ExportStdMarkdown(publicID, false, false, false, false)
+ adminMarkdown := ExportStdMarkdown(publicID, false, false, false, false, nil)
if !strings.Contains(adminMarkdown, canary) {
t.Fatalf("administrator export should include embed content: %s", adminMarkdown)
}
diff --git a/kernel/model/file_annotation_test.go b/kernel/model/file_annotation_test.go
index ca455d80f4..c0b676bc7c 100644
--- a/kernel/model/file_annotation_test.go
+++ b/kernel/model/file_annotation_test.go
@@ -170,7 +170,7 @@ func TestFileAnnotationExportRejectsInvalidData(t *testing.T) {
root := &ast.Node{Type: ast.NodeDocument, ID: "20260912000000-root001"}
root.AppendChild(node.Parent)
tree := &parse.Tree{Root: root, ID: root.ID}
- if _, err := exportTree(tree, false, false, false, false, 0, 0, 0, "#", "#", "", "", false, "", false, false, true); err == nil {
+ if _, err := exportTree(tree, false, false, false, false, 0, 0, 0, "#", "#", "", "", false, "", false, false, true, nil); err == nil {
t.Fatalf("invalid annotation data exported: %s", data)
}
if node.Parent == nil || node.Previous != nil {
diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.go
index 630d10db10..ba05c88010 100644
--- a/kernel/model/publish_access.go
+++ b/kernel/model/publish_access.go
@@ -670,6 +670,21 @@ func FilterAttributeViewByPublishAccess(c *gin.Context, publishAccess PublishAcc
return viewable
}
+// AVExportPublishFilter 为导出路径提供发布访问过滤,nil 表示调用方已确认无需过滤(管理员导出)。
+type AVExportPublishFilter func(view av.Viewable, avID, blockID string) av.Viewable
+
+// NewAVExportPublishFilter 为发布读者构造导出用的属性视图过滤器,非读者角色返回 nil。
+func NewAVExportPublishFilter(c *gin.Context) AVExportPublishFilter {
+ if !IsReadOnlyRoleContext(c) {
+ return nil
+ }
+
+ publishAccess := GetPublishAccess()
+ return func(view av.Viewable, avID, blockID string) av.Viewable {
+ return FilterAttributeViewByPublishAccess(c, publishAccess, avID, blockID, view)
+ }
+}
+
func parseAttributeViewForPublishAccess(avID, blockID string) (attrView *av.AttributeView, boxID string) {
if "" != blockID {
blockTree := treenode.GetBlockTree(blockID)
diff --git a/kernel/model/table_cell_rich_test.go b/kernel/model/table_cell_rich_test.go
index 5e36faab23..c77a508b2f 100644
--- a/kernel/model/table_cell_rich_test.go
+++ b/kernel/model/table_cell_rich_test.go
@@ -235,7 +235,7 @@ func TestTableCellRichFootnoteExport(t *testing.T) {
TextMarkBlockRefID: table.ID, TextMarkBlockRefSubtype: "s", TextMarkTextContent: "Rich target"})
writeAssetDownloadDocumentTest(t, referring)
exported, err := exportTree(prepareExportTree(getExportBlockTree(referring.ID)), true, true, false, true,
- 4, 0, 0, "#", "#", "", "", false, "", false, true, true)
+ 4, 0, 0, "#", "#", "", "", false, "", false, true, true, nil)
if err != nil {
t.Fatal(err)
}