Skip to content

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

:lock: Validate attribute view IDs before file access https://github.com/siyuan-note/siyuan/security/advisories/GHSA-7hm9-v7vf-7g4w

Daniel0f5a0e7c67b0
diff --git a/kernel/av/av.go b/kernel/av/av.go
index cd39b22911..198cab2eff 100644
--- a/kernel/av/av.go
+++ b/kernel/av/av.go
@@ -581,6 +581,11 @@ func IsAttributeViewExist(avID string) bool {
}
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
+ if !ast.IsNodeIDPattern(avID) {
+ err = ErrInvalidAttributeViewID
+ return
+ }
+
// 加密笔记本的 AV 定义存笔记本级路径,通过 fallback 自动查找并解密
avJSONPath, boxID := FindAttributeViewPath(avID)
if avJSONPath == "" {
@@ -595,6 +600,15 @@ func ParseAttributeView(avID string) (ret *AttributeView, err error) {
}
func ParseAttributeViewInBox(avID, boxID string) (ret *AttributeView, err error) {
+ if !ast.IsNodeIDPattern(avID) {
+ err = ErrInvalidAttributeViewID
+ return
+ }
+ if boxID != "" && !ast.IsNodeIDPattern(boxID) {
+ err = ErrInvalidBoxID
+ return
+ }
+
avJSONPath, avBoxID := FindAttributeViewPathInBox(avID, boxID)
if avJSONPath == "" {
avJSONPath = attributeViewDataPathByBox(avID, boxID)
@@ -710,8 +724,8 @@ func parseAttributeViewByPathInBox(avJSONPath, boxID string) (ret *AttributeView
}
func SaveAttributeView(av *AttributeView) (err error) {
- if "" == av.ID {
- err = errors.New("av id is empty")
+ if !ast.IsNodeIDPattern(av.ID) {
+ err = ErrInvalidAttributeViewID
logging.LogErrorf("save attribute view failed: %s", err)
return
}
@@ -1068,6 +1082,10 @@ func (av *AttributeView) Clone() (ret *AttributeView) {
}
func GetAttributeViewDataPath(avID string) (ret string) {
+ if !ast.IsNodeIDPattern(avID) {
+ return
+ }
+
av := filepath.Join(util.DataDir, "storage", "av")
ret = filepath.Join(av, avID+".json")
if !gulu.File.IsDir(av) {
@@ -1084,13 +1102,15 @@ func GetAttributeViewI18n(key string) string {
}
var (
- ErrAttributeViewNotFound = errors.New("attribute view not found")
- ErrViewNotFound = errors.New("view not found")
- ErrKeyNotFound = errors.New("key not found")
- ErrWrongLayoutType = errors.New("wrong layout type")
- ErrInvalidColumnAlign = errors.New("invalid column align")
- ErrSpecTooNew = errors.New("attribute view spec is too new")
- ErrFilterTooDeep = errors.New("filter nesting depth exceeds the maximum allowed")
+ ErrAttributeViewNotFound = errors.New("attribute view not found")
+ ErrInvalidAttributeViewID = errors.New("invalid attribute view id")
+ ErrInvalidBoxID = errors.New("invalid box id")
+ ErrViewNotFound = errors.New("view not found")
+ ErrKeyNotFound = errors.New("key not found")
+ ErrWrongLayoutType = errors.New("wrong layout type")
+ ErrInvalidColumnAlign = errors.New("invalid column align")
+ ErrSpecTooNew = errors.New("attribute view spec is too new")
+ ErrFilterTooDeep = errors.New("filter nesting depth exceeds the maximum allowed")
)
const (
diff --git a/kernel/av/encrypted_hook.go b/kernel/av/encrypted_hook.go
index eefb3a1689..a829136785 100644
--- a/kernel/av/encrypted_hook.go
+++ b/kernel/av/encrypted_hook.go
@@ -13,6 +13,7 @@ import (
"strings"
"sync"
+ "github.com/88250/lute/ast"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/util"
@@ -68,6 +69,10 @@ func GetAVBoxID(avID string) string {
// 加密 box:<DataDir>/<boxID>/storage/av/<avID>.json
// 普通 box(boxID 为空):<DataDir>/storage/av/<avID>.json
func attributeViewDataPathByBox(avID, boxID string) string {
+ if !ast.IsNodeIDPattern(avID) || (boxID != "" && !ast.IsNodeIDPattern(boxID)) {
+ return ""
+ }
+
if boxID != "" {
return filepath.Join(util.DataDir, boxID, "storage", "av", avID+".json")
}
@@ -80,6 +85,10 @@ func attributeViewDataPathByBox(avID, boxID string) string {
// 3. 遍历已打开的加密笔记本查找
// 返回找到的路径和对应的 boxID(普通 box 返回空 boxID)。找不到返回空串。
func FindAttributeViewPath(avID string) (path string, boxID string) {
+ if !ast.IsNodeIDPattern(avID) {
+ return
+ }
+
// 先查 pendingAVBox(首次创建场景),不要求文件存在
if pendingBoxID := GetAVBoxID(avID); pendingBoxID != "" {
encPath := attributeViewDataPathByBox(avID, pendingBoxID)
@@ -105,6 +114,10 @@ func FindAttributeViewPath(avID string) (path string, boxID string) {
// FindAttributeViewPathInBox 只在指定 box 内查找 AV 定义,避免加密上下文落回全局路径。
func FindAttributeViewPathInBox(avID, boxID string) (path string, retBoxID string) {
+ if !ast.IsNodeIDPattern(avID) || (boxID != "" && !ast.IsNodeIDPattern(boxID)) {
+ return
+ }
+
if pendingBoxID := GetAVBoxID(avID); pendingBoxID != "" {
if pendingBoxID == boxID {
// pending 匹配目标 box,直接返回(首次创建时文件尚不存在)
@@ -167,6 +180,13 @@ func ReadAttributeViewDataInBox(avID, boxID string) ([]byte, error) {
// writeAttributeViewData 写入 AV 定义数据(自动加密)。
// boxID 为空时写全局路径(普通 box),非空时写加密笔记本路径并加密。
func writeAttributeViewData(avID, boxID string, data []byte) error {
+ if !ast.IsNodeIDPattern(avID) {
+ return ErrInvalidAttributeViewID
+ }
+ if boxID != "" && !ast.IsNodeIDPattern(boxID) {
+ return ErrInvalidBoxID
+ }
+
path := attributeViewDataPathByBox(avID, boxID)
// 确保目录存在
dir := filepath.Dir(path)
diff --git a/kernel/av/path_test.go b/kernel/av/path_test.go
new file mode 100644
index 0000000000..083a3b10a2
--- /dev/null
+++ b/kernel/av/path_test.go
@@ -0,0 +1,62 @@
+// 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 av
+
+import (
+ "errors"
+ "testing"
+
+ "github.com/88250/lute/ast"
+)
+
+func TestRejectInvalidAttributeViewPathIDs(t *testing.T) {
+ validID := ast.NewNodeID()
+ invalidIDs := []string{
+ "",
+ "../outside",
+ `..\outside`,
+ "nested/outside",
+ `nested\outside`,
+ }
+
+ for _, invalidID := range invalidIDs {
+ t.Run(invalidID, func(t *testing.T) {
+ if path := attributeViewDataPathByBox(invalidID, ""); path != "" {
+ t.Fatalf("invalid attribute view ID [%s] produced path [%s]", invalidID, path)
+ }
+ if path := GetAttributeViewDataPath(invalidID); path != "" {
+ t.Fatalf("invalid attribute view ID [%s] produced path [%s]", invalidID, path)
+ }
+ if path, boxID := FindAttributeViewPath(invalidID); path != "" || boxID != "" {
+ t.Fatalf("invalid attribute view ID [%s] resolved to path [%s] in box [%s]", invalidID, path, boxID)
+ }
+ if path, boxID := FindAttributeViewPathInBox(invalidID, validID); path != "" || boxID != "" {
+ t.Fatalf("invalid attribute view ID [%s] resolved to path [%s] in box [%s]", invalidID, path, boxID)
+ }
+ if _, err := ParseAttributeView(invalidID); !errors.Is(err, ErrInvalidAttributeViewID) {
+ t.Fatalf("invalid attribute view ID [%s] returned error [%v]", invalidID, err)
+ }
+ })
+ }
+
+ if path := attributeViewDataPathByBox(validID, "../outside"); path != "" {
+ t.Fatalf("invalid box ID produced path [%s]", path)
+ }
+ if _, err := ParseAttributeViewInBox(validID, "../outside"); !errors.Is(err, ErrInvalidBoxID) {
+ t.Fatalf("invalid box ID returned error [%v]", err)
+ }
+}
diff --git a/kernel/model/attribute_view_render.go b/kernel/model/attribute_view_render.go
index 6cde412159..ec2f1774ef 100644
--- a/kernel/model/attribute_view_render.go
+++ b/kernel/model/attribute_view_render.go
@@ -52,6 +52,11 @@ func RenderAttributeView(blockID, avID, viewID, query string, page, pageSize int
}
func RenderAttributeViewWithTarget(blockID, avID, viewID, query string, page, pageSize int, groupPaging map[string]any, createIfNotExist, ignoreRows bool, targetItemID, targetGroupID string) (viewable av.Viewable, attrView *av.AttributeView, target *AttributeViewRenderTarget, err error) {
+ if !ast.IsNodeIDPattern(avID) {
+ err = ErrInvalidID
+ return
+ }
+
waitForSyncingStorages()
// 加密笔记本的 AV 定义存笔记本级路径,通过 blockID 反查 boxID
@@ -93,11 +98,6 @@ func RenderAttributeViewWithTarget(blockID, avID, viewID, query string, page, pa
return
}
- if !ast.IsNodeIDPattern(avID) {
- err = ErrInvalidID
- return
- }
-
// 加密笔记本首次创建:仅设置 pending 用于 SaveAttributeView 路径路由,创建后立即清除
if avBoxID != "" {
av.SetAVBoxID(avID, avBoxID)
@@ -687,6 +687,11 @@ func avBoxIDFromRepoPath(repoPath string) string {
}
func RenderRepoSnapshotAttributeView(indexID, avID string) (viewable av.Viewable, attrView *av.AttributeView, err error) {
+ if !ast.IsNodeIDPattern(avID) {
+ err = ErrInvalidID
+ return
+ }
+
repo, err := newRepository()
if err != nil {
return
@@ -711,11 +716,6 @@ func RenderRepoSnapshotAttributeView(indexID, avID string) (viewable av.Viewable
}
if nil == avFile {
- if !ast.IsNodeIDPattern(avID) {
- err = ErrInvalidID
- return
- }
-
attrView = av.NewAttributeView(avID)
err = av.ErrAttributeViewNotFound
return
@@ -739,11 +739,6 @@ func RenderRepoSnapshotAttributeView(indexID, avID string) (viewable av.Viewable
data = dec
}
- if !ast.IsNodeIDPattern(avID) {
- err = ErrInvalidID
- return
- }
-
attrView = av.NewAttributeView(avID)
if err = gulu.JSON.UnmarshalJSON(data, attrView); err != nil {
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", avID, err)
@@ -755,6 +750,11 @@ func RenderRepoSnapshotAttributeView(indexID, avID string) (viewable av.Viewable
}
func RenderHistoryAttributeView(blockID, avID, viewID, query string, page, pageSize int, groupPaging map[string]any, created string) (viewable av.Viewable, attrView *av.AttributeView, err error) {
+ if !ast.IsNodeIDPattern(avID) {
+ err = ErrInvalidID
+ return
+ }
+
createdUnix, parseErr := strconv.ParseInt(created, 10, 64)
if nil != parseErr {
logging.LogErrorf("parse created [%s] failed: %s", created, parseErr)
@@ -801,11 +801,6 @@ func RenderHistoryAttributeView(blockID, avID, viewID, query string, page, pageS
}
if !gulu.File.IsExist(avJSONPath) {
logging.LogWarnf("attribute view [%s] not found in current data", avID)
- if !ast.IsNodeIDPattern(avID) {
- err = ErrInvalidID
- return
- }
-
attrView = av.NewAttributeView(avID)
err = av.ErrAttributeViewNotFound
return
@@ -848,11 +843,6 @@ func RenderHistoryAttributeView(blockID, avID, viewID, query string, page, pageS
}
}
- if !ast.IsNodeIDPattern(avID) {
- err = ErrInvalidID
- return
- }
-
attrView = av.NewAttributeView(avID)
if err = gulu.JSON.UnmarshalJSON(data, attrView); err != nil {
logging.LogErrorf("unmarshal attribute view [%s] failed: %s", avID, err)
diff --git a/kernel/model/attribute_view_render_test.go b/kernel/model/attribute_view_render_test.go
new file mode 100644
index 0000000000..cca6c2ccb5
--- /dev/null
+++ b/kernel/model/attribute_view_render_test.go
@@ -0,0 +1,34 @@
+// 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 (
+ "errors"
+ "testing"
+)
+
+func TestRenderAttributeViewRejectsInvalidIDBeforeLookup(t *testing.T) {
+ invalidIDs := []string{"../outside", `..\outside`}
+ for _, invalidID := range invalidIDs {
+ t.Run(invalidID, func(t *testing.T) {
+ _, _, err := RenderAttributeView("", invalidID, "", "", 1, -1, nil, false, false)
+ if !errors.Is(err, ErrInvalidID) {
+ t.Fatalf("invalid attribute view ID [%s] returned error [%v]", invalidID, err)
+ }
+ })
+ }
+}