Skip to content

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

:lock: Enforce publish access for search, backlink, asset, storage and graph endpoints https://github.com/siyuan-note/siyuan/security/advisories/GHSA-48p5-pffc-5r9p

Daniel590e4ecd26da
diff --git a/kernel/api/filetree.go b/kernel/api/filetree.go
index f30647bdca..4ae6f1b82b 100644
--- a/kernel/api/filetree.go
+++ b/kernel/api/filetree.go
@@ -1293,10 +1293,12 @@ func listDocsByPath(c *gin.Context) {
// 过滤掉发布不可见的文件
if model.IsReadOnlyRoleContext(c) {
publishAccess := model.GetPublishAccess()
- publishIgnore := model.GetInvisiblePublishAccess(publishAccess)
+ publishInvisible := model.GetInvisiblePublishAccess(publishAccess)
+ publishDisable := model.GetDisablePublishAccess(publishAccess)
tempFiles := []*model.File{}
for _, file := range files {
- if model.CheckPathAccessableByPublishIgnore(notebook, file.Path, publishIgnore) {
+ if model.CheckPathAccessableByPublishIgnore(notebook, file.Path, publishInvisible) &&
+ model.CheckPathAccessableByPublishIgnore(notebook, file.Path, publishDisable) {
tempFiles = append(tempFiles, file)
}
}
diff --git a/kernel/model/box_doc.go b/kernel/model/box_doc.go
index f72b081fd0..73bbc40f56 100644
--- a/kernel/model/box_doc.go
+++ b/kernel/model/box_doc.go
@@ -279,9 +279,11 @@ func BoxDocSubFileCount(boxID string) int {
// BoxDocSubFileCountForPublish 返回发布访问控制下笔记本顶层文档的可见下级文档数。
func BoxDocSubFileCountForPublish(boxID string, publishAccess PublishAccess) int {
- publishIgnore := GetInvisiblePublishAccess(publishAccess)
+ publishInvisible := GetInvisiblePublishAccess(publishAccess)
+ publishDisable := GetDisablePublishAccess(publishAccess)
return boxDocSubFileCount(boxID, func(p string) bool {
- return CheckPathAccessableByPublishIgnore(boxID, p, publishIgnore)
+ return CheckPathAccessableByPublishIgnore(boxID, p, publishInvisible) &&
+ CheckPathAccessableByPublishIgnore(boxID, p, publishDisable)
})
}
diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.go
index 3181796576..33f9b12204 100644
--- a/kernel/model/publish_access.go
+++ b/kernel/model/publish_access.go
@@ -1253,7 +1253,8 @@ func FilterPathsByPublishAccess(c *gin.Context, publishAccess PublishAccess, pat
ret = []*Path{}
IDs := []string{}
- publishIgnore := GetInvisiblePublishAccess(publishAccess)
+ publishInvisible := GetInvisiblePublishAccess(publishAccess)
+ publishDisable := GetDisablePublishAccess(publishAccess)
IDtoPathIndexMap := make(map[string]int)
for i, path := range paths {
@@ -1268,7 +1269,9 @@ func FilterPathsByPublishAccess(c *gin.Context, publishAccess PublishAccess, pat
pathIndex := IDtoPathIndexMap[bt.ID]
path := paths[pathIndex]
passwordID, password := GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess)
- if CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) && (password == "" || CheckPublishAuthCookie(c, passwordID, password)) {
+ if CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishInvisible) &&
+ CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishDisable) &&
+ (password == "" || CheckPublishAuthCookie(c, passwordID, password)) {
ret = append(ret, path)
}
}
@@ -1295,7 +1298,8 @@ func FilterBlocksByPublishAccess(c *gin.Context, publishAccess PublishAccess, bl
func FilterSearchDocsByPublishAccess(c *gin.Context, publishAccess PublishAccess, docs []map[string]string) (ret []map[string]string) {
ret = []map[string]string{}
- publishIgnore := GetInvisiblePublishAccess(publishAccess)
+ publishInvisible := GetInvisiblePublishAccess(publishAccess)
+ publishDisable := GetDisablePublishAccess(publishAccess)
for _, doc := range docs {
box, docPath := doc["box"], doc["path"]
@@ -1303,7 +1307,8 @@ func FilterSearchDocsByPublishAccess(c *gin.Context, publishAccess PublishAccess
continue
}
passwordID, password := GetPathPasswordByPublishAccess(box, docPath, publishAccess)
- if CheckPathAccessableByPublishIgnore(box, docPath, publishIgnore) &&
+ if CheckPathAccessableByPublishIgnore(box, docPath, publishInvisible) &&
+ CheckPathAccessableByPublishIgnore(box, docPath, publishDisable) &&
(password == "" || CheckPublishAuthCookie(c, passwordID, password)) {
ret = append(ret, doc)
}
@@ -1380,7 +1385,8 @@ func FilterGraphByPublishAccess(c *gin.Context, publishAccess PublishAccess, nod
retNodes = []*GraphNode{}
retLinks = []*GraphLink{}
- publishIgnore := GetInvisiblePublishAccess(publishAccess)
+ publishInvisible := GetInvisiblePublishAccess(publishAccess)
+ publishDisable := GetDisablePublishAccess(publishAccess)
nodeByID := make(map[string]*GraphNode, len(nodes))
virtualNodeIDs := map[string]bool{}
nodeBaseSizes := make(map[string]float64, len(nodes))
@@ -1389,7 +1395,8 @@ func FilterGraphByPublishAccess(c *gin.Context, publishAccess PublishAccess, nod
nodeByID[node.ID] = node
virtualNodeIDs[node.ID] = true
} else {
- if node.Box == "" || node.Path == "" || !CheckPathAccessableByPublishIgnore(node.Box, node.Path, publishIgnore) {
+ if node.Box == "" || node.Path == "" || !CheckPathAccessableByPublishIgnore(node.Box, node.Path, publishInvisible) ||
+ !CheckPathAccessableByPublishIgnore(node.Box, node.Path, publishDisable) {
continue
}
passwordID, password := GetPathPasswordByPublishAccess(node.Box, node.Path, publishAccess)
@@ -1514,12 +1521,15 @@ func FilterLocalStorageByPublishAccess(localStorage map[string]any) (ret map[str
}
func FilterAssetContentByPublishAccess(c *gin.Context, publishAccess PublishAccess, assetContent []*AssetContent) (ret []*AssetContent) {
- publishIgnore := GetInvisiblePublishAccess(publishAccess)
+ publishInvisible := GetInvisiblePublishAccess(publishAccess)
+ publishDisable := GetDisablePublishAccess(publishAccess)
validAssets := []string{}
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)) {
+ if CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishInvisible) &&
+ CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishDisable) &&
+ (password == "" || CheckPublishAuthCookie(c, passwordID, password)) {
assets, err := DocAssets(bt.ID, false)
if err == nil {
validAssets = append(validAssets, assets...)
@@ -1543,12 +1553,15 @@ func FilterAssetContentByPublishAccess(c *gin.Context, publishAccess PublishAcce
func FilterRecentDocsByPublishAccess(c *gin.Context, publishAccess PublishAccess, recentDocs []*RecentDoc) (ret []*RecentDoc) {
ret = []*RecentDoc{}
- publishIgnore := GetInvisiblePublishAccess(publishAccess)
+ publishInvisible := GetInvisiblePublishAccess(publishAccess)
+ publishDisable := GetDisablePublishAccess(publishAccess)
for _, recentDoc := range recentDocs {
bt := treenode.GetBlockTree(recentDoc.RootID)
if bt != nil {
passwordID, password := GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess)
- if CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) && (passwordID == "" || CheckPublishAuthCookie(c, passwordID, password)) {
+ if CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishInvisible) &&
+ CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishDisable) &&
+ (passwordID == "" || CheckPublishAuthCookie(c, passwordID, password)) {
ret = append(ret, recentDoc)
}
}
@@ -1558,7 +1571,8 @@ func FilterRecentDocsByPublishAccess(c *gin.Context, publishAccess PublishAccess
func FilterCriteriaByPublishAccess(c *gin.Context, publishAccess PublishAccess, criteria []*Criterion) (ret []*Criterion) {
ret = []*Criterion{}
- publishIgnore := GetInvisiblePublishAccess(publishAccess)
+ publishInvisible := GetInvisiblePublishAccess(publishAccess)
+ publishDisable := GetDisablePublishAccess(publishAccess)
// IDPath 元素可能是笔记本 ID、文档 ID,或 "笔记本ID/文档ID[.sy]" 路径串,这里统一解析出文档 ID
blockIDs := map[string]struct{}{}
for _, criterion := range criteria {
@@ -1597,7 +1611,9 @@ func FilterCriteriaByPublishAccess(c *gin.Context, publishAccess PublishAccess,
break
}
passwordID, password := GetPathPasswordByPublishAccess(bt.BoxID, bt.Path, publishAccess)
- if !CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishIgnore) || (passwordID != "" && !CheckPublishAuthCookie(c, passwordID, password)) {
+ if !CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishInvisible) ||
+ !CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishDisable) ||
+ (passwordID != "" && !CheckPublishAuthCookie(c, passwordID, password)) {
accessible = false
break
}
diff --git a/kernel/model/publish_access_test.go b/kernel/model/publish_access_test.go
index 8c407a53d7..0dc40c12e2 100644
--- a/kernel/model/publish_access_test.go
+++ b/kernel/model/publish_access_test.go
@@ -27,6 +27,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/conf"
+ "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"
@@ -840,18 +841,21 @@ func TestFilterSearchDocsByPublishAccess(t *testing.T) {
hiddenBoxID = "20260720000001-boxid02"
hiddenDocID = "20260720000002-hiddend"
protectedDocID = "20260720000003-protect"
+ disabledDocID = "20260720000006-disable"
protectedPassword = "password"
)
publishAccess := PublishAccess{
{ID: hiddenBoxID, Visible: false},
{ID: hiddenDocID, Visible: false},
{ID: protectedDocID, Visible: true, Password: protectedPassword},
+ {ID: disabledDocID, Visible: true, Disable: true},
}
docs := []map[string]string{
{"box": boxID, "path": "/20260720000004-public1.sy"},
{"box": hiddenBoxID, "path": "/"},
{"box": boxID, "path": "/" + hiddenDocID + "/20260720000005-child01.sy"},
{"box": boxID, "path": "/" + protectedDocID + ".sy"},
+ {"box": boxID, "path": "/" + disabledDocID + ".sy"},
{"box": boxID, "path": ""},
}
@@ -879,6 +883,7 @@ func TestFilterGraphByPublishAccess(t *testing.T) {
protectedDocID = "20260724000002-protect"
protectedBlockID = "20260724000003-block01"
hiddenDocID = "20260724000004-hidden1"
+ disabledDocID = "20260724000005-disable"
protectedPassword = "password"
sharedTagID = "shared"
protectedTagID = "protected"
@@ -886,12 +891,14 @@ func TestFilterGraphByPublishAccess(t *testing.T) {
publishAccess := PublishAccess{
{ID: protectedDocID, Visible: true, Password: protectedPassword},
{ID: hiddenDocID, Visible: false},
+ {ID: disabledDocID, Visible: true, Disable: true},
}
newGraph := func() ([]*GraphNode, []*GraphLink) {
return []*GraphNode{
{ID: publicDocID, Box: boxID, Path: "/" + publicDocID + ".sy", Size: 10, Type: "NodeDocument"},
{ID: protectedBlockID, Box: boxID, Path: "/" + protectedDocID + "/" + protectedBlockID + ".sy", Size: 10, Type: "NodeParagraph"},
{ID: hiddenDocID, Box: boxID, Path: "/" + hiddenDocID + ".sy", Size: 10, Type: "NodeDocument"},
+ {ID: disabledDocID, Box: boxID, Path: "/" + disabledDocID + ".sy", Size: 10, Type: "NodeDocument"},
{ID: sharedTagID, Label: sharedTagID, Size: 10, Type: "NodeTag"},
{ID: protectedTagID, Label: protectedTagID, Size: 10, Type: "NodeTag"},
}, []*GraphLink{
@@ -900,6 +907,7 @@ func TestFilterGraphByPublishAccess(t *testing.T) {
{From: protectedTagID, To: protectedBlockID},
{From: publicDocID, To: protectedBlockID, Ref: true},
{From: publicDocID, To: hiddenDocID, Ref: true},
+ {From: publicDocID, To: disabledDocID, Ref: true},
}
}
@@ -1047,3 +1055,266 @@ func TestFilterEmbedBlocksByPublishAccessDropsInaccessibleResults(t *testing.T)
t.Fatalf("密码验证后应仅返回已授权结果:%+v", filtered)
}
}
+
+func TestFilterPathsByPublishAccess(t *testing.T) {
+ const (
+ boxID = "20260804000000-boxid01"
+ publicID = "20260804000001-public1"
+ protectedID = "20260804000002-protect"
+ hiddenID = "20260804000003-hidden1"
+ disabledID = "20260804000004-disable"
+ protectedPassword = "password"
+ )
+
+ oldDataDir := util.DataDir
+ oldBlockTreeDBPath := util.BlockTreeDBPath
+ util.DataDir = t.TempDir()
+ util.BlockTreeDBPath = filepath.Join(util.DataDir, "blocktree.db")
+ treenode.InitBlockTree(true)
+ invalidateEncryptedPublishAccessCache()
+ t.Cleanup(func() {
+ treenode.CloseDatabase()
+ util.DataDir = oldDataDir
+ util.BlockTreeDBPath = oldBlockTreeDBPath
+ invalidateEncryptedPublishAccessCache()
+ if "" != oldBlockTreeDBPath {
+ treenode.InitBlockTree(false)
+ }
+ })
+
+ for _, id := range []string{publicID, protectedID, hiddenID, disabledID} {
+ tree := treenode.NewTree(boxID, "/"+id+".sy", "/"+id, id)
+ treenode.UpsertBlockTree(tree)
+ }
+
+ newPaths := func() []*Path {
+ return []*Path{
+ {ID: publicID, Name: "/" + publicID + ".sy", HPath: "/public", Type: "path", NodeType: "NodeDocument"},
+ {ID: protectedID, Name: "/" + protectedID + ".sy", HPath: "/protected", Type: "path", NodeType: "NodeDocument"},
+ {ID: hiddenID, Name: "/" + hiddenID + ".sy", HPath: "/hidden", Type: "path", NodeType: "NodeDocument"},
+ {ID: disabledID, Name: "/" + disabledID + ".sy", HPath: "/disabled", Type: "path", NodeType: "NodeDocument"},
+ }
+ }
+ publishAccess := PublishAccess{
+ {ID: protectedID, Visible: true, Password: protectedPassword},
+ {ID: hiddenID, Visible: false},
+ {ID: disabledID, Visible: true, Disable: true},
+ }
+
+ c, _ := gin.CreateTestContext(httptest.NewRecorder())
+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
+ filtered := FilterPathsByPublishAccess(c, publishAccess, newPaths())
+ if len(filtered) != 1 || filtered[0].ID != publicID {
+ t.Fatalf("unexpected unauthenticated backlink paths: %+v", filtered)
+ }
+
+ c.Request.AddCookie(&http.Cookie{
+ Name: "publish-auth-" + protectedID,
+ Value: util.SHA256Hash([]byte(protectedID + protectedPassword)),
+ })
+ filtered = FilterPathsByPublishAccess(c, publishAccess, newPaths())
+ if len(filtered) != 2 || filtered[0].ID != publicID || filtered[1].ID != protectedID {
+ t.Fatalf("unexpected authenticated backlink paths: %+v", filtered)
+ }
+}
+
+func TestFilterRecentDocsByPublishAccess(t *testing.T) {
+ const (
+ boxID = "20260804010000-boxid01"
+ publicID = "20260804010001-public1"
+ protectedID = "20260804010002-protect"
+ hiddenID = "20260804010003-hidden1"
+ disabledID = "20260804010004-disable"
+ protectedPassword = "password"
+ )
+
+ oldDataDir := util.DataDir
+ oldBlockTreeDBPath := util.BlockTreeDBPath
+ util.DataDir = t.TempDir()
+ util.BlockTreeDBPath = filepath.Join(util.DataDir, "blocktree.db")
+ treenode.InitBlockTree(true)
+ invalidateEncryptedPublishAccessCache()
+ t.Cleanup(func() {
+ treenode.CloseDatabase()
+ util.DataDir = oldDataDir
+ util.BlockTreeDBPath = oldBlockTreeDBPath
+ invalidateEncryptedPublishAccessCache()
+ if "" != oldBlockTreeDBPath {
+ treenode.InitBlockTree(false)
+ }
+ })
+
+ for _, id := range []string{publicID, protectedID, hiddenID, disabledID} {
+ tree := treenode.NewTree(boxID, "/"+id+".sy", "/"+id, id)
+ treenode.UpsertBlockTree(tree)
+ }
+
+ newRecentDocs := func() []*RecentDoc {
+ return []*RecentDoc{
+ {RootID: publicID, Title: "Public"},
+ {RootID: protectedID, Title: "Protected"},
+ {RootID: hiddenID, Title: "Hidden"},
+ {RootID: disabledID, Title: "Disabled"},
+ }
+ }
+ publishAccess := PublishAccess{
+ {ID: protectedID, Visible: true, Password: protectedPassword},
+ {ID: hiddenID, Visible: false},
+ {ID: disabledID, Visible: true, Disable: true},
+ }
+
+ c, _ := gin.CreateTestContext(httptest.NewRecorder())
+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
+ filtered := FilterRecentDocsByPublishAccess(c, publishAccess, newRecentDocs())
+ if len(filtered) != 1 || filtered[0].RootID != publicID {
+ t.Fatalf("unexpected unauthenticated recent docs: %+v", filtered)
+ }
+
+ c.Request.AddCookie(&http.Cookie{
+ Name: "publish-auth-" + protectedID,
+ Value: util.SHA256Hash([]byte(protectedID + protectedPassword)),
+ })
+ filtered = FilterRecentDocsByPublishAccess(c, publishAccess, newRecentDocs())
+ if len(filtered) != 2 || filtered[0].RootID != publicID || filtered[1].RootID != protectedID {
+ t.Fatalf("unexpected authenticated recent docs: %+v", filtered)
+ }
+}
+
+func TestFilterCriteriaByPublishAccess(t *testing.T) {
+ const (
+ boxID = "20260804020000-boxid01"
+ publicID = "20260804020001-public1"
+ protectedID = "20260804020002-protect"
+ hiddenID = "20260804020003-hidden1"
+ disabledID = "20260804020004-disable"
+ protectedPassword = "password"
+ )
+
+ oldDataDir := util.DataDir
+ oldBlockTreeDBPath := util.BlockTreeDBPath
+ util.DataDir = t.TempDir()
+ util.BlockTreeDBPath = filepath.Join(util.DataDir, "blocktree.db")
+ treenode.InitBlockTree(true)
+ invalidateEncryptedPublishAccessCache()
+ t.Cleanup(func() {
+ treenode.CloseDatabase()
+ util.DataDir = oldDataDir
+ util.BlockTreeDBPath = oldBlockTreeDBPath
+ invalidateEncryptedPublishAccessCache()
+ if "" != oldBlockTreeDBPath {
+ treenode.InitBlockTree(false)
+ }
+ })
+
+ for _, id := range []string{publicID, protectedID, hiddenID, disabledID} {
+ tree := treenode.NewTree(boxID, "/"+id+".sy", "/"+id, id)
+ treenode.UpsertBlockTree(tree)
+ }
+
+ newCriteria := func() []*Criterion {
+ return []*Criterion{
+ {Name: "public", HPath: "/public", IDPath: []string{publicID}},
+ {Name: "protected", HPath: "/protected", IDPath: []string{protectedID}},
+ {Name: "hidden", HPath: "/hidden", IDPath: []string{hiddenID}},
+ {Name: "disabled", HPath: "/disabled", IDPath: []string{disabledID}},
+ }
+ }
+ publishAccess := PublishAccess{
+ {ID: protectedID, Visible: true, Password: protectedPassword},
+ {ID: hiddenID, Visible: false},
+ {ID: disabledID, Visible: true, Disable: true},
+ }
+
+ c, _ := gin.CreateTestContext(httptest.NewRecorder())
+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
+ filtered := FilterCriteriaByPublishAccess(c, publishAccess, newCriteria())
+ if len(filtered) != 1 || filtered[0].Name != "public" {
+ t.Fatalf("unexpected unauthenticated criteria: %+v", filtered)
+ }
+
+ c.Request.AddCookie(&http.Cookie{
+ Name: "publish-auth-" + protectedID,
+ Value: util.SHA256Hash([]byte(protectedID + protectedPassword)),
+ })
+ filtered = FilterCriteriaByPublishAccess(c, publishAccess, newCriteria())
+ if len(filtered) != 2 || filtered[0].Name != "public" || filtered[1].Name != "protected" {
+ t.Fatalf("unexpected authenticated criteria: %+v", filtered)
+ }
+}
+
+func TestFilterAssetContentByPublishAccess(t *testing.T) {
+ const (
+ boxID = "20260804030000-boxid01"
+ publicDocID = "20260804030001-public1"
+ protectedDocID = "20260804030002-protect"
+ hiddenDocID = "20260804030003-hidden1"
+ disabledDocID = "20260804030004-disable"
+ publicAsset = "assets/public.png"
+ protectedAsset = "assets/protected.png"
+ hiddenAsset = "assets/hidden.png"
+ disabledAsset = "assets/disabled.png"
+ protectedPassword = "password"
+ )
+
+ oldDataDir := util.DataDir
+ oldBlockTreeDBPath := util.BlockTreeDBPath
+ util.DataDir = t.TempDir()
+ util.BlockTreeDBPath = filepath.Join(util.DataDir, "blocktree.db")
+ treenode.InitBlockTree(true)
+ invalidateEncryptedPublishAccessCache()
+ t.Cleanup(func() {
+ treenode.CloseDatabase()
+ util.DataDir = oldDataDir
+ util.BlockTreeDBPath = oldBlockTreeDBPath
+ invalidateEncryptedPublishAccessCache()
+ if "" != oldBlockTreeDBPath {
+ treenode.InitBlockTree(false)
+ }
+ })
+
+ newDocTree := func(docID, asset string) {
+ tree := treenode.NewTree(boxID, "/"+docID+".sy", "/"+docID, docID)
+ tree.Root.FirstChild.Unlink()
+ dom := `<div data-node-id="` + docID + `" data-type="htmlblock"><img src="` + asset + `" data-src="` + asset + `" /></div>`
+ node := util.NewLute().BlockDOM2Tree(dom).Root.FirstChild
+ tree.Root.AppendChild(node)
+ if _, err := filesys.WriteTree(tree); err != nil {
+ t.Fatal(err)
+ }
+ treenode.UpsertBlockTree(tree)
+ }
+ newDocTree(publicDocID, publicAsset)
+ newDocTree(protectedDocID, protectedAsset)
+ newDocTree(hiddenDocID, hiddenAsset)
+ newDocTree(disabledDocID, disabledAsset)
+
+ newAssetContents := func() []*AssetContent {
+ return []*AssetContent{
+ {ID: publicDocID, Name: "public.png", Path: publicAsset},
+ {ID: protectedDocID, Name: "protected.png", Path: protectedAsset},
+ {ID: hiddenDocID, Name: "hidden.png", Path: hiddenAsset},
+ {ID: disabledDocID, Name: "disabled.png", Path: disabledAsset},
+ }
+ }
+ publishAccess := PublishAccess{
+ {ID: protectedDocID, Visible: true, Password: protectedPassword},
+ {ID: hiddenDocID, Visible: false},
+ {ID: disabledDocID, Visible: true, Disable: true},
+ }
+
+ c, _ := gin.CreateTestContext(httptest.NewRecorder())
+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
+ filtered := FilterAssetContentByPublishAccess(c, publishAccess, newAssetContents())
+ if len(filtered) != 1 || filtered[0].Path != publicAsset {
+ t.Fatalf("unexpected unauthenticated asset contents: %+v", filtered)
+ }
+
+ c.Request.AddCookie(&http.Cookie{
+ Name: "publish-auth-" + protectedDocID,
+ Value: util.SHA256Hash([]byte(protectedDocID + protectedPassword)),
+ })
+ filtered = FilterAssetContentByPublishAccess(c, publishAccess, newAssetContents())
+ if len(filtered) != 2 || filtered[0].Path != publicAsset || filtered[1].Path != protectedAsset {
+ t.Fatalf("unexpected authenticated asset contents: %+v", filtered)
+ }
+}