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: Escape gutter block button attributes and strip data/on keys from pasted block IAL https://github.com/siyuan-note/siyuan/security/advisories/GHSA-2cmm-9x9j-rvff
Danielc40329fb2302
diff --git a/app/src/protyle/gutter/button.test.ts b/app/src/protyle/gutter/button.test.tsindex ba6430f202..79f2be06b5 100644--- a/app/src/protyle/gutter/button.test.ts+++ b/app/src/protyle/gutter/button.test.ts@@ -38,6 +38,43 @@ describe("genGutterBlockButtonHTML", () => {assert.match(html, /data-view-occurrence-id="fragment%2Fsecond"/);});++ it("escapes quote breakout payloads in the block subtype", () => {+ const html = genGutterBlockButtonHTML({+ ...baseOptions,+ subtype: 'x" autofocus onfocus=globalThis.__probe_marker=1 data-pad="y',+ });++ assert.match(html, /data-subtype="x" autofocus onfocus=globalThis\.__probe_marker=1 data-pad="y"/);+ assert.match(html, /data-subtype="[^"]*"/);+ assert.doesNotMatch(html, /data-subtype="x"[^>]*\sautofocus/);+ });++ it("escapes quote breakout payloads in the block id", () => {+ const html = genGutterBlockButtonHTML({+ ...baseOptions,+ nodeID: 'x" onmouseover=alert(1)',+ });++ assert.match(html, /data-node-id="x" onmouseover=alert\(1\)"/);+ assert.doesNotMatch(html, /data-node-id="x"[^>]*\sonmouseover=/);+ });++ it("escapes quotes in block type, tooltip and embed id", () => {+ const html = genGutterBlockButtonHTML({+ ...baseOptions,+ ariaLabel: 'tip" onfocus=alert(1)',+ type: 'NodeParagraph" autofocus',+ embedID: 'embed" onclick=alert(1)',+ });++ assert.match(html, /aria-label="tip" onfocus=alert\(1\)"/);+ assert.match(html, /data-type="NodeParagraph" autofocus"/);+ assert.match(html, /data-embed-id="embed" onclick=alert\(1\)"/);+ assert.match(html, /aria-label="[^"]*"/);+ assert.match(html, /data-type="[^"]*"/);+ assert.match(html, /data-embed-id="[^"]*"/);+ });});describe("canShowGutterInsert", () => {diff --git a/app/src/protyle/gutter/button.ts b/app/src/protyle/gutter/button.tsindex 16ebce5224..56132cc011 100644--- a/app/src/protyle/gutter/button.ts+++ b/app/src/protyle/gutter/button.ts@@ -1,3 +1,5 @@+import {escapeAttr} from "../../util/escape";+export interface IGutterBlockButtonOptions {ariaLabel: string;type: string;@@ -10,13 +12,14 @@ export interface IGutterBlockButtonOptions {draggable: boolean;}+// 动态属性值统一转义后再拼接,避免块属性携带的引号破坏属性边界注入事件处理属性export const genGutterBlockButtonHTML = (options: IGutterBlockButtonOptions) => {- const embedHTML = options.embedID ? ` data-embed-id="${options.embedID}"` : "";+ const embedHTML = options.embedID ? ` data-embed-id="${escapeAttr(options.embedID)}"` : "";const viewOccurrenceHTML = options.viewOccurrenceID ?- ` data-view-occurrence-id="${encodeURIComponent(options.viewOccurrenceID)}"` : "";- return `<button class="ariaLabel" data-delay="500" data-position="parentW" aria-label="${options.ariaLabel}"-data-type="${options.type}" data-subtype="${options.subtype}" data-node-id="${options.nodeID}"${embedHTML}${viewOccurrenceHTML}>- <svg><use xlink:href="#${options.icon}"></use></svg>+ ` data-view-occurrence-id="${escapeAttr(encodeURIComponent(options.viewOccurrenceID))}"` : "";+ return `<button class="ariaLabel" data-delay="500" data-position="parentW" aria-label="${escapeAttr(options.ariaLabel)}"+data-type="${escapeAttr(options.type)}" data-subtype="${escapeAttr(options.subtype)}" data-node-id="${escapeAttr(options.nodeID)}"${embedHTML}${viewOccurrenceHTML}>+ <svg><use xlink:href="#${escapeAttr(options.icon)}"></use></svg><span ${options.popoverHTML || ""} ${options.draggable ? 'draggable="true"' : ""}></span></button>`;};diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.tsindex 7fb7c7e83b..e2337e9147 100644--- a/app/src/protyle/gutter/index.ts+++ b/app/src/protyle/gutter/index.ts@@ -51,6 +51,7 @@ import {} from "../wysiwyg/getBlock";import * as dayjs from "dayjs";import {fetchPost, fetchSyncPost} from "../../util/fetch";+import {escapeAttr} from "../../util/escape";import {confirmBlockRef} from "../../util/checkBlockRef";import {cancelSB,@@ -3784,7 +3785,7 @@ export class Gutter {let popoverHTML = "";if (protyle.options.backlinkData) {- popoverHTML = `class="popover__block" data-id="${dataNodeId}"`;+ popoverHTML = `class="popover__block" data-id="${escapeAttr(dataNodeId)}"`;}const viewOccurrenceID = hasViewFoldContext(protyle) ?getViewFoldOccurrenceID(protyle, nodeElement) : "";@@ -3871,7 +3872,7 @@ data-type="fold"${viewOccurrenceID ? ` data-view-occurrence-id="${encodeURICompoviewOccurrenceID: hasViewFoldContext(protyle) ?getViewFoldOccurrenceID(protyle, embedElement) : "",popoverHTML: protyle.options.backlinkData ?- `class="popover__block" data-id="${embedElement.getAttribute("data-node-id")}"` : "",+ `class="popover__block" data-id="${escapeAttr(embedElement.getAttribute("data-node-id"))}"` : "",draggable: !protyle.disabled,}) + html;}diff --git a/app/src/protyle/util/paste.test.ts b/app/src/protyle/util/paste.test.tsindex 419c6c832b..1a3ba2bed4 100644--- a/app/src/protyle/util/paste.test.ts+++ b/app/src/protyle/util/paste.test.ts@@ -47,7 +47,7 @@ const createHarness = (disabled = false) => {target: {}, stopPropagation() {}, preventDefault() {},clipboardData: {files, types: [], getData: (type: string) => type === "text/siyuan" ? siyuanHTML : ""},} as unknown as ClipboardEvent & {target: HTMLElement});- return {paste, uploads, position, restrictedFallback,+ return {paste, uploads, position, restrictedFallback, api: module.exports as typeof import("./paste"),setLocalFiles: (files: unknown[]) => { localFiles = files; },invalidate: () => { available = false; }};};@@ -92,3 +92,24 @@ describe("restricted cell image paste", () => {});}});++describe("strip pasted IAL data attributes", () => {+ const harness = createHarness();++ it("removes data-* and on* keys from pasted block IAL", () => {+ assert.equal(harness.api.stripPastedIALDataAttributes(+ 'x\n{: id="20240101000000-abc123" data-subtype="x" autofocus" onmouseover="alert(1)" custom-foo="bar"}'),+ 'x\n{: id="20240101000000-abc123" custom-foo="bar"}');+ });++ it("keeps data-assets for legacy asset references", () => {+ assert.equal(harness.api.stripPastedIALDataAttributes(+ 'x\n{: data-assets="assets/a.png"}'),+ 'x\n{: data-assets="assets/a.png"}');+ });++ it("leaves ordinary markdown and non-IAL lines untouched", () => {+ const markdown = "paragraph\n\n{: not an ial line\n\n# heading\n{: id=\"20240101000000-abc123\"}";+ assert.equal(harness.api.stripPastedIALDataAttributes(markdown), markdown);+ });+});diff --git a/app/src/protyle/util/paste.ts b/app/src/protyle/util/paste.tsindex 4e313abd1a..da92de64b1 100644--- a/app/src/protyle/util/paste.ts+++ b/app/src/protyle/util/paste.ts@@ -263,6 +263,17 @@ export const pasteEscaped = async (protyle: IProtyle, nodeElement: Element, prep}};+// 清理外部纯文本粘贴内容块级 IAL 中的 data-* 与 on* 属性,+// 避免数据属性被注入后参与块标等后续 HTML 序列化+export const stripPastedIALDataAttributes = (textPlain: string) => {+ return textPlain.split("\n").map((line) => {+ if (!/^\s*\{:.*\}\s*$/.test(line)) {+ return line;+ }+ return line.replace(/\s(?:(?:data-(?!assets)[\w-]+)|on[\w-]+)\s*=\s*(?:"[^"]*"|'[^']*')/g, "");+ }).join("\n");+};+export const pasteAsPlainText = async (protyle: IProtyle, prepareInsertion?: () => boolean) => {let localFiles: ILocalFiles[] = [];/// #if !BROWSER@@ -317,7 +328,8 @@ export const pasteAsPlainText = async (protyle: IProtyle, prepareInsertion?: ()enableLuteMarkdownSyntax(protyle);let content: string;try {- content = protyle.lute.BlockDOM2EscapeMarkerContent(protyle.lute.Md2BlockDOM(textPlain));+ content = protyle.lute.BlockDOM2EscapeMarkerContent(+ protyle.lute.Md2BlockDOM(stripPastedIALDataAttributes(textPlain)));} finally {restoreLuteMarkdownSyntax(protyle);}@@ -1470,6 +1482,7 @@ export const paste = async (protyle: IProtyle, event: (ClipboardEvent | DragEven}}let textPlainDom: string;+ textPlain = stripPastedIALDataAttributes(textPlain);// Auto-convert pasted URL to link format https://github.com/siyuan-note/siyuan/issues/17337if (window.siyuan.config.editor.pasteURLAutoConvert) {