Skip to content

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

:lock: Parameterize backlink mention search https://github.com/siyuan-note/siyuan/security/advisories/GHSA-q2vg-7qgx-x5fc

Daniel1a5b3431d5ab
diff --git a/kernel/model/backlink.go b/kernel/model/backlink.go
index 33ebe925a9..8f78c2298d 100644
--- a/kernel/model/backlink.go
+++ b/kernel/model/backlink.go
@@ -21,7 +21,6 @@ import (
"fmt"
"path"
"sort"
- "strconv"
"strings"
"github.com/88250/gulu"
@@ -967,6 +966,17 @@ func searchBackmention(mentionKeywords []string, keyword string, excludeBacklink
return searchBackmentionInBox(mentionKeywords, keyword, excludeBacklinkIDs, rootID, beforeLen, "")
}
+func quoteFTSPhrase(phrase string) string {
+ return "\"" + strings.ReplaceAll(phrase, "\"", "\"\"") + "\""
+}
+
+func buildBackmentionQuery(matchExpression, rootID string, limit int) (query string, args []any) {
+ query = "SELECT * FROM blocks_fts WHERE blocks_fts MATCH ? AND root_id != ?" +
+ " AND type IN ('d', 'h', 'p', 't') ORDER BY id DESC LIMIT ?"
+ args = []any{matchExpression, rootID, limit}
+ return
+}
+
// searchBackmentionInBox 与 searchBackmention 一致,但按 boxID 路由到加密 db 或全局 db。
func searchBackmentionInBox(mentionKeywords []string, keyword string, excludeBacklinkIDs *hashset.Set, rootID string, beforeLen int, boxID string) (retMentionKeywords []string, ret []*Block) {
ret = []*Block{}
@@ -974,36 +984,27 @@ func searchBackmentionInBox(mentionKeywords []string, keyword string, excludeBac
return
}
- table := "blocks_fts"
-
buf := bytes.Buffer{}
- buf.WriteString("SELECT * FROM " + table + " WHERE " + table + " MATCH '" + columnFilter() + ":(")
+ buf.WriteString(columnFilter() + ":(")
for i, mentionKeyword := range mentionKeywords {
if Conf.Search.BacklinkMentionKeywordsLimit < i {
util.PushMsg(fmt.Sprintf(Conf.Language(38), len(mentionKeywords)), 5000)
- mentionKeyword = strings.ReplaceAll(mentionKeyword, "\"", "\"\"")
- buf.WriteString("\"" + mentionKeyword + "\"")
+ buf.WriteString(quoteFTSPhrase(mentionKeyword))
break
}
- mentionKeyword = strings.ReplaceAll(mentionKeyword, "\"", "\"\"")
- buf.WriteString("\"" + mentionKeyword + "\"")
+ buf.WriteString(quoteFTSPhrase(mentionKeyword))
if i < len(mentionKeywords)-1 {
buf.WriteString(" OR ")
}
}
buf.WriteString(")")
if "" != keyword {
- keyword = strings.ReplaceAll(keyword, "\"", "\"\"")
- buf.WriteString(" AND (\"" + keyword + "\")")
+ buf.WriteString(" AND (" + quoteFTSPhrase(keyword) + ")")
}
- buf.WriteString("'")
- buf.WriteString(" AND root_id != '" + rootID + "'") // 不在定义块所在文档中搜索
- buf.WriteString(" AND type IN ('d', 'h', 'p', 't')")
- buf.WriteString(" ORDER BY id DESC LIMIT " + strconv.Itoa(Conf.Search.Limit))
- query := buf.String()
+ query, args := buildBackmentionQuery(buf.String(), rootID, Conf.Search.Limit)
- sqlBlocks := sql.SelectBlocksRawStmtInBox(query, 1, Conf.Search.Limit, boxID)
+ sqlBlocks := sql.SelectBlocksRawStmtArgsInBox(query, args, Conf.Search.Limit, boxID)
terms := mentionKeywords
if "" != keyword {
terms = append(terms, keyword)
diff --git a/kernel/model/backlink_test.go b/kernel/model/backlink_test.go
new file mode 100644
index 0000000000..36d03094b6
--- /dev/null
+++ b/kernel/model/backlink_test.go
@@ -0,0 +1,60 @@
+// 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 (
+ "reflect"
+ "strings"
+ "testing"
+)
+
+func TestQuoteFTSPhrase(t *testing.T) {
+ tests := []struct {
+ name string
+ input string
+ expected string
+ }{
+ {name: "client keyword with apostrophe", input: "a'b", expected: `"a'b"`},
+ {name: "stored title with SQL syntax", input: `doc'); UPDATE blocks SET content = 'changed`, expected: `"doc'); UPDATE blocks SET content = 'changed"`},
+ {name: "FTS quote", input: `a"b`, expected: `"a""b"`},
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ if actual := quoteFTSPhrase(test.input); actual != test.expected {
+ t.Fatalf("expected %q, got %q", test.expected, actual)
+ }
+ })
+ }
+}
+
+func TestBuildBackmentionQueryBindsValues(t *testing.T) {
+ matchExpression := `content:("stored'title") AND ("client'keyword")`
+ rootID := "root'id"
+ query, args := buildBackmentionQuery(matchExpression, rootID, 64)
+
+ if strings.Contains(query, matchExpression) || strings.Contains(query, rootID) {
+ t.Fatalf("query contains an unbound value: %s", query)
+ }
+ if strings.Count(query, "?") != 3 {
+ t.Fatalf("expected three placeholders, got query %q", query)
+ }
+ expectedArgs := []any{matchExpression, rootID, 64}
+ if !reflect.DeepEqual(args, expectedArgs) {
+ t.Fatalf("expected arguments %#v, got %#v", expectedArgs, args)
+ }
+}