Skip to content

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

:lock: Validate and escape attribute view column width https://github.com/siyuan-note/siyuan/security/advisories/GHSA-rj55-w3xr-gj62

Daniel837cf1bb8f21
diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.ts
index fdb943b901..e40ba3e68c 100644
--- a/app/src/protyle/render/av/render.ts
+++ b/app/src/protyle/render/av/render.ts
@@ -203,7 +203,7 @@ data-icon="${escapeAttr(column.icon)}" data-dtype="${column.type}" data-wrap="${
data-date-format="${column.dateFormat || ""}"
data-freeze="${freezeIndex === index}"
data-desc="${escapeAttr(column.desc)}" data-align="${column.align || ""}" data-position="north"
-style="width: ${column.width || "200px"};">
+style="width: ${escapeAttr(column.width) || "200px"};">
${column.icon ? unicode2Emoji(column.icon, "av__cellheadericon", true) : `<svg class="av__cellheadericon"><use xlink:href="#${getColIconByType(column.type)}"></use></svg>`}
<span class="av__celltext fn__flex-1">${escapeHtml(column.name)}</span>
<div class="av__widthdrag"></div>
@@ -213,10 +213,10 @@ style="width: ${column.width || "200px"};">
}
if (column.type === "lineNumber") {
// lineNumber type 不参与计算操作
- calcHTML += `<div data-col-id="${column.id}" data-dtype="${column.type}" class="av__calc" style="width: ${column.width || "200px"}">&nbsp;</div>`;
+ calcHTML += `<div data-col-id="${column.id}" data-dtype="${column.type}" class="av__calc" style="width: ${escapeAttr(column.width) || "200px"}">&nbsp;</div>`;
} else {
calcHTML += `<div class="av__calc${column.calc && column.calc.operator !== "" ? " av__calc--ashow" : ""}" data-col-id="${column.id}" data-dtype="${column.type}" data-operator="${column.calc?.operator || ""}"
-style="width: ${column.width || "200px"}">${getCalcValue(column) || `<svg><use xlink:href="#iconDown"></use></svg><small>${window.siyuan.languages.calc}</small>`}</div>`;
+style="width: ${escapeAttr(column.width) || "200px"}">${getCalcValue(column) || `<svg><use xlink:href="#iconDown"></use></svg><small>${window.siyuan.languages.calc}</small>`}</div>`;
}
if (column.calc && column.calc.operator !== "") {
hasCalc = true;
diff --git a/app/src/protyle/render/av/row.ts b/app/src/protyle/render/av/row.ts
index 0f8b64b38a..158f503b21 100644
--- a/app/src/protyle/render/av/row.ts
+++ b/app/src/protyle/render/av/row.ts
@@ -221,7 +221,7 @@ data-dtype="${column.type}"
data-date-format="${column.dateFormat || ""}"
data-align="${column.align || ""}"
${cell.value?.isDetached ? ' data-detached="true"' : ""}
-style="width: ${column.width || "200px"};
+style="width: ${escapeAttr(column.width) || "200px"};
${cell.bgColor ? `background-color:${cell.bgColor};` : ""}
${cell.color ? `color:${cell.color};` : ""}">${renderCell(cell.value, options.rowIndex, tableData.showIcon, "table", column.options, column.dateFormat)}</div>`;
diff --git a/kernel/av/av.go b/kernel/av/av.go
index edeaf89809..3229029cc1 100644
--- a/kernel/av/av.go
+++ b/kernel/av/av.go
@@ -23,6 +23,7 @@ import (
"fmt"
"os"
"path/filepath"
+ "regexp"
"sort"
"strconv"
"strings"
@@ -275,6 +276,20 @@ func FilterColorValue(color string) string {
return strconv.Itoa(n)
}
+// FilterWidthValue 校验列宽度值,仅允许空字符串或合法的 CSS 长度值,非法值返回空字符串,防止注入到样式属性中
+func FilterWidthValue(width string) string {
+ width = strings.TrimSpace(width)
+ if "" == width {
+ return ""
+ }
+ if !widthValuePattern.MatchString(width) {
+ return ""
+ }
+ return width
+}
+
+var widthValuePattern = regexp.MustCompile(`^\d+(\.\d+)?(px|em|rem|%)$`)
+
// View 描述了视图的结构。
type View struct {
ID string `json:"id"` // 视图 ID
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 1a1260efe1..1bdca208ab 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -6213,7 +6213,7 @@ func setAttributeViewColWidth(operation *Operation) (err error) {
case av.LayoutTypeTable:
for _, column := range view.Table.Columns {
if column.ID == operation.ID {
- column.Width = operation.Data.(string)
+ column.Width = av.FilterWidthValue(operation.Data.(string))
break
}
}
@@ -6260,7 +6260,7 @@ func setAttributeViewColsWidth(operation *Operation) (err error) {
}
for _, column := range view.Table.Columns {
if width, found := widths[column.ID]; found {
- column.Width = width
+ column.Width = av.FilterWidthValue(width)
}
}
return av.SaveAttributeView(attrView)