Skip to content

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

:lock: Escape AV card cover attributes https://github.com/siyuan-note/siyuan/security/advisories/GHSA-5rxg-wh59-mg34

Daniel676c182ac8d5
diff --git a/app/src/protyle/render/av/cover.test.ts b/app/src/protyle/render/av/cover.test.ts
new file mode 100644
index 0000000000..4c5fde4c2a
--- /dev/null
+++ b/app/src/protyle/render/av/cover.test.ts
@@ -0,0 +1,33 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {getCardCoverImageHTML} from "./cover";
+
+describe("getCardCoverImageHTML", () => {
+ it("escapes a title image used as inline style", () => {
+ const html = getCardCoverImageHTML("background:red;\" onload=\"require('child_process')\" x=\"", "", false);
+
+ assert.equal(html.includes(" onload=\""), false);
+ assert.match(html, /style="background:red;" onload="/);
+ });
+
+ it("escapes a title image used as image source", () => {
+ const payload = "missing\" onerror=\"require('child_process')";
+ const html = getCardCoverImageHTML(payload, payload, false);
+
+ assert.equal(html.includes(" onerror=\""), false);
+ assert.match(html, /src="missing" onerror="/);
+ });
+
+ it("preserves built-in background styles", () => {
+ const coverStyle = "background:linear-gradient(#fff 50%, transparent 0);background-size:20px 20px;";
+
+ assert.equal(getCardCoverImageHTML(coverStyle, "", false).includes(`style="${coverStyle}"`), true);
+ });
+
+ it("preserves image compression and fitting", () => {
+ const html = getCardCoverImageHTML("assets/cover.png", "assets/cover.png?style=thumb", true);
+
+ assert.match(html, /class="av__gallery-img av__gallery-img--fit"/);
+ assert.match(html, /src="assets\/cover\.png\?style=thumb"/);
+ });
+});
diff --git a/app/src/protyle/render/av/cover.ts b/app/src/protyle/render/av/cover.ts
new file mode 100644
index 0000000000..ebd1fe92f1
--- /dev/null
+++ b/app/src/protyle/render/av/cover.ts
@@ -0,0 +1,10 @@
+import {escapeAttr} from "../../../util/escape";
+
+const TRANSPARENT_IMAGE = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
+
+export const getCardCoverImageHTML = (coverURL: string, imageURL: string, fitImage: boolean) => {
+ if (coverURL.startsWith("background")) {
+ return `<img class="av__gallery-img" src="${TRANSPARENT_IMAGE}" style="${escapeAttr(coverURL)}">`;
+ }
+ return `<img loading="lazy" class="av__gallery-img${fitImage ? " av__gallery-img--fit" : ""}" src="${escapeAttr(imageURL)}">`;
+};
diff --git a/kernel/treenode/node_test.go b/kernel/treenode/node_test.go
new file mode 100644
index 0000000000..8b2a7f146a
--- /dev/null
+++ b/kernel/treenode/node_test.go
@@ -0,0 +1,59 @@
+// 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 treenode
+
+import (
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/88250/lute/html"
+)
+
+func TestGetDocTitleImgPath(t *testing.T) {
+ tests := []struct {
+ name string
+ titleImg string
+ expected string
+ }{
+ {
+ name: "image URL",
+ titleImg: `background-image:url("assets/cover.png");object-position:center 20%;`,
+ expected: "assets/cover.png",
+ },
+ {
+ name: "built-in gradient",
+ titleImg: "background:linear-gradient(#fff 50%, transparent 0);background-size:20px 20px;",
+ },
+ {
+ name: "attribute injection",
+ titleImg: `background:red;" onload="require('child_process')" x="`,
+ },
+ {
+ name: "missing closing parenthesis",
+ titleImg: `background-image:url("assets/cover.png"`,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ root := &ast.Node{KramdownIAL: [][]string{{"title-img", html.EscapeAttrVal(test.titleImg)}}}
+ if actual := GetDocTitleImgPath(root); test.expected != actual {
+ t.Fatalf("expected %q, got %q", test.expected, actual)
+ }
+ })
+ }
+}