An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:zap: Reuse published resource references with fresh authorization https://github.com/siyuan-note/siyuan/issues/20258
Danielcb36ba94a553
diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.goindex 4df0b09d09..0e773476ea 100644--- a/kernel/model/publish_access.go+++ b/kernel/model/publish_access.go@@ -444,11 +444,14 @@ func CheckBlockTreeDiscoverableByPublishAccess(publishAccess PublishAccess, bt *}func GetBlockTreePublishAccessStatus(c *gin.Context, publishAccess PublishAccess, bt *treenode.BlockTree) PublishAccessStatus {+ return getBlockTreePublishAccessStatus(c, publishAccess, filterDisablePublishAccess(publishAccess), bt)+}++func getBlockTreePublishAccessStatus(c *gin.Context, publishAccess, publishDisable PublishAccess, bt *treenode.BlockTree) PublishAccessStatus {if bt == nil || IsEncryptedBoxDeniedByPublishAccess(bt.BoxID) {return PublishAccessDenied}- publishDisable := filterDisablePublishAccess(publishAccess)if !CheckPathAccessableByPublishIgnore(bt.BoxID, bt.Path, publishDisable) {return PublishAccessDenied}diff --git a/kernel/model/publish_resource_access.go b/kernel/model/publish_resource_access.goindex 9a3c9f7afc..08dd370b89 100644--- a/kernel/model/publish_resource_access.go+++ b/kernel/model/publish_resource_access.go@@ -97,23 +97,49 @@ func CheckEmojiAccessableByPublishAccess(c *gin.Context, relativePath string, pu}func checkResourceAccessableByPublishAccess(c *gin.Context, resource string, publishAccess PublishAccess, widget bool) bool {+ publishDisable := filterDisablePublishAccess(publishAccess)+ // 缓存只提供引用候选,每次请求仍校验当前文档版本和发布权限。+ found := false+ publishResourceRefsCache.Range(func(key, value any) bool {+ refs := value.(*publishResourceRefs)+ if !refs.contains(resource, widget) {+ return true+ }+ boxID, rootID, _ := strings.Cut(key.(string), "/")+ bt := treenode.GetBlockTreeInBox(rootID, boxID)+ if bt != nil && bt.Type == "d" && bt.Updated == refs.updated &&+ getBlockTreePublishAccessStatus(c, publishAccess, publishDisable, bt) == PublishAccessAllowed {+ found = true+ return false+ }+ return true+ })+ if found {+ return true+ }+for _, bt := range treenode.GetBlockTreesByType("d") {- if !checkBlockTreeAccessableByPublishAccess(c, publishAccess, bt) {+ if getBlockTreePublishAccessStatus(c, publishAccess, publishDisable, bt) != PublishAccessAllowed {continue}refs := getPublishResourceRefs(bt)- if widget {- if _, ok := refs.widgets[resource]; ok {- return true- }- } else if _, ok := refs.emojis[resource]; ok {+ if refs.contains(resource, widget) {return true}}return false}+func (refs *publishResourceRefs) contains(resource string, widget bool) bool {+ if widget {+ _, ok := refs.widgets[resource]+ return ok+ }+ _, ok := refs.emojis[resource]+ return ok+}+func getPublishResourceRefs(bt *treenode.BlockTree) *publishResourceRefs {cacheKey := bt.BoxID + "/" + bt.RootIDif cached, ok := publishResourceRefsCache.Load(cacheKey); ok {diff --git a/kernel/model/publish_resource_access_test.go b/kernel/model/publish_resource_access_test.goindex ff6d58c7c2..760ee067c6 100644--- a/kernel/model/publish_resource_access_test.go+++ b/kernel/model/publish_resource_access_test.go@@ -17,16 +17,93 @@package modelimport (+ "net/http"+ "net/http/httptest""os""path/filepath""testing""github.com/88250/lute/ast""github.com/88250/lute/parse"+ "github.com/gin-gonic/gin""github.com/siyuan-note/siyuan/kernel/conf"+ "github.com/siyuan-note/siyuan/kernel/treenode""github.com/siyuan-note/siyuan/kernel/util")+func TestCachedPublishResourceRechecksDocumentAndAuthorization(t *testing.T) {+ const boxID, rootID = "20261008000000-box0001", "20261008000000-root001"+ previousDataDir, previousDBPath := util.DataDir, util.BlockTreeDBPath+ util.DataDir = t.TempDir()+ util.BlockTreeDBPath = filepath.Join(util.DataDir, "blocktree.db")+ treenode.InitBlockTree(true)+ if err := os.MkdirAll(filepath.Join(util.DataDir, boxID), 0755); err != nil {+ t.Fatal(err)+ }+ cacheKey := boxID + "/" + rootID+ t.Cleanup(func() {+ forgetRuntimeEncryptedBox(boxID)+ invalidateEncryptedPublishAccessCache()+ publishResourceRefsCache.Delete(cacheKey)+ treenode.CloseDatabase()+ util.DataDir, util.BlockTreeDBPath = previousDataDir, previousDBPath+ })+ root := &ast.Node{Type: ast.NodeDocument, ID: rootID}+ root.SetIALAttr("updated", "20261008000000")+ tree := &parse.Tree{ID: rootID, Box: boxID, Path: "/" + rootID + ".sy", Root: root}+ treenode.IndexBlockTree(tree)+ bt := treenode.GetBlockTreeInBox(rootID, boxID)+ publishResourceRefsCache.Store(cacheKey, &publishResourceRefs{+ updated: bt.Updated, emojis: map[string]struct{}{"icon.png": {}}, widgets: map[string]struct{}{"example": {}},+ })+ newContext := func() *gin.Context {+ c, _ := gin.CreateTestContext(httptest.NewRecorder())+ c.Request = httptest.NewRequest(http.MethodGet, "/emojis/icon.png", nil)+ return c+ }+ c := newContext()+ for _, widget := range []bool{false, true} {+ resource := "icon.png"+ if widget {+ resource = "example"+ }+ if !checkResourceAccessableByPublishAccess(c, resource, nil, widget) {+ t.Fatal("cached reference did not authorize the resource")+ }+ if checkResourceAccessableByPublishAccess(c, resource, PublishAccess{{ID: rootID, Disable: true}}, widget) {+ t.Fatal("disabled document authorized a cached resource")+ }+ protected := PublishAccess{{ID: rootID, Password: "secret"}}+ if checkResourceAccessableByPublishAccess(c, resource, protected, widget) {+ t.Fatal("unauthorized reader reused cached access")+ }+ authorized := newContext()+ authorized.Request.AddCookie(&http.Cookie{Name: "publish-auth-" + rootID, Value: util.SHA256Hash([]byte(rootID + "secret"))})+ if !checkResourceAccessableByPublishAccess(authorized, resource, protected, widget) {+ t.Fatal("authorized reader could not use cached references")+ }+ if checkResourceAccessableByPublishAccess(c, resource, protected, widget) {+ t.Fatal("authorized request leaked permission to another reader")+ }+ }+ markRuntimeEncryptedBox(boxID)+ invalidateEncryptedPublishAccessCache()+ if CheckEmojiAccessableByPublishAccess(c, "icon.png", nil) {+ t.Fatal("encrypted notebook authorized a cached resource")+ }+ forgetRuntimeEncryptedBox(boxID)+ invalidateEncryptedPublishAccessCache()+ root.SetIALAttr("updated", "20261008000001")+ treenode.UpsertBlockTree(tree)+ if CheckEmojiAccessableByPublishAccess(c, "icon.png", nil) {+ t.Fatal("changed document authorized a stale reference")+ }+ treenode.RemoveBlockTreesByRootID(boxID, rootID)+ if CheckEmojiAccessableByPublishAccess(c, "icon.png", nil) {+ t.Fatal("deleted document authorized a cached resource")+ }+}+func TestPublishResourceURLPath(t *testing.T) {tests := []struct {value stringdiff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.goindex 5067f342ec..0b14995fa8 100644--- a/kernel/treenode/blocktree.go+++ b/kernel/treenode/blocktree.go@@ -80,6 +80,9 @@ func initDatabase(forceRebuild bool) {if _, err := db.Exec("CREATE INDEX IF NOT EXISTS idx_blocktrees_box_id ON blocktrees(box_id)"); err != nil {logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "create blocktree box index failed: %s", err)}+ if err := ensureBlockTreeTypeIndex(db); err != nil {+ logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "create blocktree type index failed: %s", err)+ }if err := cleanupInvalidBlockTrees(db); err != nil {logging.LogErrorf("cleanup invalid blocktrees failed: %s", err)}@@ -119,6 +122,14 @@ func initDBTables() {if err = ensureHPathIndexes(db); err != nil {logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "create hpath indexes failed: %s", err)}+ if err = ensureBlockTreeTypeIndex(db); err != nil {+ logging.LogFatalf(logging.ExitCodeUnavailableDatabase, "create blocktree type index failed: %s", err)+ }+}++func ensureBlockTreeTypeIndex(database *sql.DB) (err error) {+ _, err = database.Exec("CREATE INDEX IF NOT EXISTS idx_blocktrees_type ON blocktrees(type)")+ return}func cleanupInvalidBlockTrees(database *sql.DB) (err error) {@@ -1012,6 +1023,9 @@ func initEncryptedBlockTreeTables(boxDB *sql.DB) (err error) {if err = ensureHPathIndexes(boxDB); err != nil {return}+ if err = ensureBlockTreeTypeIndex(boxDB); err != nil {+ return+ }if err = cleanupInvalidBlockTrees(boxDB); err != nil {return}diff --git a/kernel/treenode/type_index_test.go b/kernel/treenode/type_index_test.gonew file mode 100644index 0000000000..df03c58f6e--- /dev/null+++ b/kernel/treenode/type_index_test.go@@ -0,0 +1,40 @@+package treenode++import (+ "database/sql"+ "strings"+ "testing"+)++func TestBlockTreeTypeIndexMigration(t *testing.T) {+ for _, initialize := range []func(*sql.DB) error{ensureBlockTreeTypeIndex, initEncryptedBlockTreeTables} {+ database, err := sql.Open("sqlite3_extended", ":memory:")+ if err != nil {+ t.Fatal(err)+ }+ t.Cleanup(func() { database.Close() })+ if _, err = database.Exec("CREATE TABLE blocktrees (id, root_id, parent_id, box_id, path, hpath, updated, type)"); err != nil {+ t.Fatal(err)+ }+ if _, err = database.Exec("INSERT INTO blocktrees VALUES ('doc', 'doc', '', 'box', '/doc.sy', '/Document', '2026', 'd')"); err != nil {+ t.Fatal(err)+ }+ for range 2 {+ if err = initialize(database); err != nil {+ t.Fatal(err)+ }+ }+ var id string+ if err = database.QueryRow("SELECT id FROM blocktrees WHERE type = ?", "d").Scan(&id); err != nil || id != "doc" {+ t.Fatalf("index migration changed the document: %q %v", id, err)+ }+ var node, parent, unused int+ var plan string+ if err = database.QueryRow("EXPLAIN QUERY PLAN SELECT * FROM blocktrees WHERE type = ?", "d").Scan(&node, &parent, &unused, &plan); err != nil {+ t.Fatal(err)+ }+ if !strings.Contains(plan, "SEARCH blocktrees USING INDEX idx_blocktrees_type") {+ t.Fatalf("document lookup did not use the type index: %s", plan)+ }+ }+}