Skip to content

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

:lock: Sanitize unicode2Emoji output and validate icon values to prevent RCE via document icon https://github.com/siyuan-note/siyuan/security/advisories/GHSA-vx5w-qrvp-mmcq

Daniel53c40172ab31
diff --git a/app/src/emoji/index.ts b/app/src/emoji/index.ts
index 35a80980a1..560cea19ad 100644
--- a/app/src/emoji/index.ts
+++ b/app/src/emoji/index.ts
@@ -49,13 +49,14 @@ export const unicode2Emoji = (unicode: string, className = "", needSpan = false,
emoji += String.fromCodePoint(parseInt(item, 16));
}
});
- if (needSpan) {
- emoji = `<span class="${className}">${emoji}</span>`;
- }
} catch (e) {
// 自定义表情搜索报错 https://github.com/siyuan-note/siyuan/issues/5883
// 这里忽略错误不做处理
}
+ emoji = Lute.Sanitize(emoji);
+ if (needSpan) {
+ emoji = `<span class="${className}">${emoji}</span>`;
+ }
}
return emoji;
};
diff --git a/kernel/av/new_item_template.go b/kernel/av/new_item_template.go
index 885f11cbad..5dadf32b27 100644
--- a/kernel/av/new_item_template.go
+++ b/kernel/av/new_item_template.go
@@ -22,6 +22,8 @@ import (
"strings"
"github.com/88250/lute/ast"
+
+ "github.com/siyuan-note/siyuan/kernel/util"
)
// SetNewItemTemplates 校验并替换数据库的新增条目模板配置。
@@ -43,6 +45,13 @@ func (av *AttributeView) SetNewItemTemplates(config *NewItemTemplatesConfig) err
}
itemTemplate.Name = strings.TrimSpace(itemTemplate.Name)
itemTemplate.Icon = strings.TrimSpace(itemTemplate.Icon)
+ if filteredIcon, valid := util.FilterIconValue(itemTemplate.Icon); valid {
+ itemTemplate.Icon = filteredIcon
+ } else {
+ // 非法图标值置空,防止存储可执行标记
+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-vx5w-qrvp-mmcq
+ itemTemplate.Icon = ""
+ }
if "" == itemTemplate.Name {
return errors.New("new item template name is empty")
}
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 729bfacbb4..0584f0a3bd 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -4865,7 +4865,7 @@ func (tx *Transaction) doSetAttrViewViewIcon(operation *Operation) (ret *TxErr)
return &TxErr{code: TxErrHandleAttributeView, id: viewID}
}
- view.Icon = operation.Data.(string)
+ view.Icon = filterAttrViewIconValue(operation.Data.(string))
if err = av.SaveAttributeView(attrView); err != nil {
logging.LogErrorf("save attribute view [%s] failed: %s", avID, err)
return &TxErr{code: TxErrHandleAttributeView, msg: err.Error(), id: avID}
@@ -4873,6 +4873,15 @@ func (tx *Transaction) doSetAttrViewViewIcon(operation *Operation) (ret *TxErr)
return
}
+// filterAttrViewIconValue 过滤属性视图图标值,非法值置空,防止存储可执行标记
+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-vx5w-qrvp-mmcq
+func filterAttrViewIconValue(icon string) string {
+ if filtered, valid := util.FilterIconValue(icon); valid {
+ return filtered
+ }
+ return ""
+}
+
func (tx *Transaction) doSetAttrViewViewDesc(operation *Operation) (ret *TxErr) {
var err error
avID := operation.AvID
@@ -6340,7 +6349,7 @@ func setAttributeViewColIcon(operation *Operation) (err error) {
for _, keyValues := range attrView.KeyValues {
if keyValues.Key.ID == operation.ID {
- keyValues.Key.Icon = operation.Data.(string)
+ keyValues.Key.Icon = filterAttrViewIconValue(operation.Data.(string))
break
}
}
@@ -6700,7 +6709,7 @@ func AddAttributeViewKey(avID, keyID, keyName, keyType, keyIcon, previousKeyID s
av.KeyTypePhone, av.KeyTypeMAsset, av.KeyTypeTemplate, av.KeyTypeCreated, av.KeyTypeUpdated, av.KeyTypeCheckbox,
av.KeyTypeRelation, av.KeyTypeRollup, av.KeyTypeLineNumber:
- key := av.NewKey(keyID, keyName, keyIcon, keyTyp)
+ key := av.NewKey(keyID, keyName, filterAttrViewIconValue(keyIcon), keyTyp)
if av.KeyTypeDate == keyTyp || av.KeyTypeCreated == keyTyp || av.KeyTypeUpdated == keyTyp {
if !dateFormat.IsValid() {
return errors.New("invalid date display format")
diff --git a/kernel/model/blockial.go b/kernel/model/blockial.go
index 45eb5758d9..49adc26cec 100644
--- a/kernel/model/blockial.go
+++ b/kernel/model/blockial.go
@@ -472,6 +472,11 @@ func normalizeIconValue(value string) string {
}
}
if allASCII {
+ if !util.IsValidIconUnicode(value) {
+ // 非法图标值,置空以删除该属性,防止存储可执行标记
+ // https://github.com/siyuan-note/siyuan/security/advisories/GHSA-vx5w-qrvp-mmcq
+ return ""
+ }
return value
}
@@ -479,7 +484,11 @@ func normalizeIconValue(value string) string {
for _, r := range value {
parts = append(parts, strconv.FormatInt(int64(r), 16))
}
- return strings.Join(parts, "-")
+ ret := strings.Join(parts, "-")
+ if !util.IsValidIconUnicode(ret) {
+ return ""
+ }
+ return ret
}
func pushBlockAttrs(oldAttrs map[string]string, node *ast.Node) {
diff --git a/kernel/model/blockinfo.go b/kernel/model/blockinfo.go
index 791182c291..ee77b8cb56 100644
--- a/kernel/model/blockinfo.go
+++ b/kernel/model/blockinfo.go
@@ -160,7 +160,9 @@ func GetDocInfoInBox(blockID, boxID string) (ret *BlockInfo, err error) {
}
}
ret.SubFileCount = subFileCount
- ret.Icon = tree.Root.IALAttr("icon")
+ if icon, ok := util.FilterIconValue(tree.Root.IALAttr("icon")); ok {
+ ret.Icon = icon
+ }
return
}
@@ -268,7 +270,9 @@ func GetDocsInfo(blockIDs []string, queryRefCount bool, queryAv bool) (rets []*B
}
}
ret.SubFileCount = subFileCount
- ret.Icon = tree.Root.IALAttr("icon")
+ if icon, ok := util.FilterIconValue(tree.Root.IALAttr("icon")); ok {
+ ret.Icon = icon
+ }
rets = append(rets, ret)
@@ -346,6 +350,11 @@ func getNodeAvBlockText(node *ast.Node, avID string) (icon, content string) {
}
icon = node.IALAttr("icon")
+ if filteredIcon, valid := util.FilterIconValue(icon); valid {
+ icon = filteredIcon
+ } else {
+ icon = ""
+ }
if name := node.IALAttr("name"); "" != name {
name = strings.TrimSpace(name)
name = util.EscapeHTML(name)
diff --git a/kernel/model/file.go b/kernel/model/file.go
index 0dab1e8549..a8e0ff2063 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -82,7 +82,9 @@ func (box *Box) docFromFileInfo(fileInfo *FileInfo, ial map[string]string) (ret
ret.Size = uint64(fileInfo.size)
ret.Name = ial["title"]
ret.TitleEmpty = ial[NodeAttrTitleEmpty] == "true"
- ret.Icon = ial["icon"]
+ if icon, ok := util.FilterIconValue(ial["icon"]); ok {
+ ret.Icon = icon
+ }
ret.ID = ial["id"]
ret.Name1 = ial["name"]
ret.Alias = ial["alias"]
diff --git a/kernel/model/storage.go b/kernel/model/storage.go
index 685d59bebc..476af04f1c 100644
--- a/kernel/model/storage.go
+++ b/kernel/model/storage.go
@@ -532,7 +532,9 @@ func getRecentDocs(sortBy string) (ret []*RecentDoc, err error) {
for rootID, doc := range mergedDocs {
if ial, ok := attrs[rootID]; ok {
if icon, ok := ial["icon"]; ok && icon != "" {
- doc.Icon = icon
+ if filteredIcon, valid := util.FilterIconValue(icon); valid {
+ doc.Icon = filteredIcon
+ }
}
}
ret = append(ret, doc)
diff --git a/kernel/util/file.go b/kernel/util/file.go
index ca34687d3f..69d620d0f4 100644
--- a/kernel/util/file.go
+++ b/kernel/util/file.go
@@ -235,9 +235,47 @@ func FilterIconValue(icon string) (ret string, valid bool) {
if strings.Contains(ret, ".") {
ret = FilterUploadEmojiFileName(ret)
}
+ if !strings.ContainsAny(ret, "./") && !IsValidIconUnicode(ret) {
+ return "", false
+ }
return ret, true
}
+// IsValidIconUnicode 校验图标值是否为合法的十六进制码点序列(连字符分隔):
+// 解码后不允许包含 HTML 元字符,防止图标值被渲染为可执行标记
+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-vx5w-qrvp-mmcq
+func IsValidIconUnicode(icon string) bool {
+ parts := strings.Split(icon, "-")
+ if 32 < len(parts) {
+ return false
+ }
+ isHexSequence := true
+ for _, part := range parts {
+ if "" == part || 6 < len(part) {
+ return false
+ }
+ if _, parseErr := strconv.ParseUint(part, 16, 32); nil != parseErr {
+ isHexSequence = false
+ break
+ }
+ }
+ if !isHexSequence {
+ // 不是十六进制码点序列,比如直接存储的 emoji 字符,保持原有行为
+ return true
+ }
+ for _, part := range parts {
+ n, _ := strconv.ParseUint(part, 16, 32)
+ if 0x10FFFF < n || (0xD800 <= n && 0xDFFF >= n) {
+ return false
+ }
+ r := rune(n)
+ if '<' == r || '>' == r || '"' == r || '\'' == r || '&' == r {
+ return false
+ }
+ }
+ return true
+}
+
func FilterRecentIconValue(icon string) (ret string, valid bool) {
ret, valid = FilterIconValue(icon)
if !valid || !strings.HasPrefix(ret, "api/icon/getDynamicIcon") {