An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:lock: Harden document search in publish mode https://github.com/siyuan-note/siyuan/security/advisories/GHSA-33jq-p8c2-q3q4
Daniel942091659227
diff --git a/kernel/api/filetree.go b/kernel/api/filetree.goindex e642bb427c..a694c252e3 100644--- a/kernel/api/filetree.go+++ b/kernel/api/filetree.go@@ -1096,7 +1096,12 @@ func searchDocs(c *gin.Context) {}k := arg["k"].(string)- ret.Data = model.SearchDocs(k, flashcard, excludeIDs)+ docs := model.SearchDocs(k, flashcard, excludeIDs)+ if model.IsReadOnlyRoleContext(c) {+ publishAccess := model.GetPublishAccess()+ docs = model.FilterSearchDocsByPublishAccess(c, publishAccess, docs)+ }+ ret.Data = docs}func listDocsByPath(c *gin.Context) {diff --git a/kernel/model/file_test.go b/kernel/model/file_test.goindex 91f17304b7..50cab7bb1f 100644--- a/kernel/model/file_test.go+++ b/kernel/model/file_test.go@@ -189,3 +189,21 @@ func TestBuildSearchDocsCondition(t *testing.T) {}}}++func TestBuildSearchDocsConditionBindsInjectionPayload(t *testing.T) {+ payload := "poc%')/**/union/**/select/**/'poc'--"+ condition, args := buildSearchDocsCondition([]string{payload}, nil, true, true, true)+ if strings.Contains(condition, payload) || strings.Contains(strings.ToLower(condition), "union") {+ t.Fatalf("search condition should not contain payload SQL: %q", condition)+ }++ expected := "%" + escapeSearchDocLikePattern(payload) + "%"+ if len(args) != 4 {+ t.Fatalf("unexpected search arg count: got %d, want 4", len(args))+ }+ for i, arg := range args {+ if actual := arg.(string); expected != actual {+ t.Fatalf("unexpected search arg at %d: got %q, want %q", i, actual, expected)+ }+ }+}diff --git a/kernel/model/publish_access.go b/kernel/model/publish_access.goindex a168672a43..02ccfe0f39 100644--- a/kernel/model/publish_access.go+++ b/kernel/model/publish_access.go@@ -639,6 +639,25 @@ func FilterBlocksByPublishAccess(c *gin.Context, publishAccess PublishAccess, blreturn}+func FilterSearchDocsByPublishAccess(c *gin.Context, publishAccess PublishAccess, docs []map[string]string) (ret []map[string]string) {+ ret = []map[string]string{}++ publishIgnore := GetInvisiblePublishAccess(publishAccess)++ for _, doc := range docs {+ box, docPath := doc["box"], doc["path"]+ if !ast.IsNodeIDPattern(box) || (docPath != "/" && !strings.HasPrefix(docPath, "/")) {+ continue+ }+ passwordID, password := GetPathPasswordByPublishAccess(box, docPath, publishAccess)+ if CheckPathAccessableByPublishIgnore(box, docPath, publishIgnore) &&+ (password == "" || CheckPublishAuthCookie(c, passwordID, password)) {+ ret = append(ret, doc)+ }+ }+ return+}+func FilterBlockTreesByPublishIgnore(publishIgnore PublishAccess, bts map[string]*treenode.BlockTree) (ret map[string]*treenode.BlockTree) {ret = map[string]*treenode.BlockTree{}for id, bt := range bts {diff --git a/kernel/model/publish_access_test.go b/kernel/model/publish_access_test.gonew file mode 100644index 0000000000..9fc6d33a20--- /dev/null+++ b/kernel/model/publish_access_test.go@@ -0,0 +1,64 @@+// SiYuan - Refactor your thinking+// 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/>.++package model++import (+ "net/http"+ "net/http/httptest"+ "testing"++ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestFilterSearchDocsByPublishAccess(t *testing.T) {+ const (+ boxID = "20260720000000-boxid01"+ hiddenBoxID = "20260720000001-boxid02"+ hiddenDocID = "20260720000002-hiddend"+ protectedDocID = "20260720000003-protect"+ protectedPassword = "password"+ )+ publishAccess := PublishAccess{+ {ID: hiddenBoxID, Visible: false},+ {ID: hiddenDocID, Visible: false},+ {ID: protectedDocID, Visible: true, Password: protectedPassword},+ }+ 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": ""},+ }++ c, _ := gin.CreateTestContext(httptest.NewRecorder())+ c.Request = httptest.NewRequest(http.MethodGet, "/", nil)+ filtered := FilterSearchDocsByPublishAccess(c, publishAccess, docs)+ if len(filtered) != 1 || filtered[0]["path"] != docs[0]["path"] {+ t.Fatalf("unexpected unauthenticated search docs: %v", filtered)+ }++ c.Request.AddCookie(&http.Cookie{+ Name: "publish-auth-" + protectedDocID,+ Value: util.SHA256Hash([]byte(protectedDocID + protectedPassword)),+ })+ filtered = FilterSearchDocsByPublishAccess(c, publishAccess, docs)+ if len(filtered) != 2 || filtered[1]["path"] != docs[3]["path"] {+ t.Fatalf("unexpected authenticated search docs: %v", filtered)+ }+}