Skip to content

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

:recycle: Consolidate escaping into single-pass character classes

Jeffrey Chen3b534ed2357f
diff --git a/app/src/util/escape.test.ts b/app/src/util/escape.test.ts
index 34bfff67c5..1b3408c497 100644
--- a/app/src/util/escape.test.ts
+++ b/app/src/util/escape.test.ts
@@ -1,6 +1,9 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {escapeHtmlTextAndAttr, escapeSearchHighlight, stripSearchMark} from "./escape";
+import {
+ escapeAriaLabel, escapeAttr, escapeHtml, escapeHtmlTextAndAttr, escapeMarkdownPlainText,
+ escapeSearchHighlight, stripSearchMark,
+} from "./escape";
describe("escape HTML strings", () => {
it("matches the bundled Lute engine for text and double-quoted attributes", () => {
@@ -27,6 +30,31 @@ describe("escape HTML strings", () => {
});
});
+describe("escape HTML fragments and attributes", () => {
+ it("escapes only ampersands and less-than signs for HTML fragments", () => {
+ assert.equal(escapeHtml(""), "");
+ assert.equal(escapeHtml("a&b<c>d\"e'f"), "a&amp;b&lt;c>d\"e'f");
+ });
+
+ it("escapes both quote kinds for attributes", () => {
+ assert.equal(escapeAttr(""), "");
+ assert.equal(escapeAttr("&<>\"'"), "&<>&quot;&apos;");
+ });
+
+ it("escapes aria-label text with the inner attribute level", () => {
+ assert.equal(escapeAriaLabel(""), "");
+ assert.equal(escapeAriaLabel("&lt;a<b>'c\"d"), "&amp;lt;a&amp;lt;b>&apos;c&quot;d");
+ });
+});
+
+describe("escape Markdown plain text", () => {
+ it("prefixes every escapable marker with a backslash", () => {
+ assert.equal(escapeMarkdownPlainText("plain text"), "plain text");
+ assert.equal(escapeMarkdownPlainText("\\*_[]!`<>&~{}()=#$^|."),
+ "\\\\\\*\\_\\[\\]\\!\\`\\<\\>\\&\\~\\{\\}\\(\\)\\=\\#\\$\\^\\|\\.");
+ });
+});
+
describe("strip search mark", () => {
it("removes search highlights without decoding escaped block names", () => {
assert.equal(stripSearchMark("<mark>xw</mark>"), "xw");
diff --git a/app/src/util/escape.ts b/app/src/util/escape.ts
index 447298b524..f2a6e68445 100644
--- a/app/src/util/escape.ts
+++ b/app/src/util/escape.ts
@@ -1,6 +1,17 @@
// 转义 HTML 文本和双引号属性值,保持 Lute.EscapeHTMLStr 的字符替换规则。
export const escapeHtmlTextAndAttr = (html: unknown) => {
- return String(html).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
+ return String(html).replace(/[&<>"]/g, (char) => {
+ switch (char) {
+ case "&":
+ return "&amp;";
+ case "<":
+ return "&lt;";
+ case ">":
+ return "&gt;";
+ default:
+ return "&quot;";
+ }
+ });
};
// 纯文本写入 HTML 模板中的提示属性,需分别保护属性解析及提示框的 innerHTML 解析。
@@ -8,6 +19,8 @@ export const escapeHtmlTextAndAttr = (html: unknown) => {
// setAttribute 接受的提示 HTML 不再转义属性层。
export const escapeTooltipTextAttr = (text: string) => escapeHtmlTextAndAttr(escapeHtmlTextAndAttr(text));
+// 两个字符的转义保持逐字面量替换:V8 对单字面量正则有专用扫描路径,
+// 字符类的逐字符成本约为其四倍,长正文上单次字符类扫描反而更慢。
export const escapeHtml = (html: string) => {
if (!html) {
return html;
@@ -17,27 +30,7 @@ export const escapeHtml = (html: string) => {
// 将纯文本中的 Markdown 标记转义,保留编辑器重新解析时的字面内容。
export const escapeMarkdownPlainText = (text: string) => {
- return text.replace(/\\/g, "\\\\")
- .replace(/\*/g, "\\*")
- .replace(/_/g, "\\_")
- .replace(/\[/g, "\\[")
- .replace(/]/g, "\\]")
- .replace(/!/g, "\\!")
- .replace(/`/g, "\\`")
- .replace(/</g, "\\<")
- .replace(/>/g, "\\>")
- .replace(/&/g, "\\&")
- .replace(/~/g, "\\~")
- .replace(/\{/g, "\\{")
- .replace(/}/g, "\\}")
- .replace(/\(/g, "\\(")
- .replace(/\)/g, "\\)")
- .replace(/=/g, "\\=")
- .replace(/#/g, "\\#")
- .replace(/\$/g, "\\$")
- .replace(/\^/g, "\\^")
- .replace(/\|/g, "\\|")
- .replace(/\./g, "\\.");
+ return text.replace(/[\\*_[\]!`<>&~{}()=#$^|.]/g, "\\$&");
};
export const stripSearchMark = (html: string) => {
@@ -53,6 +46,7 @@ export const escapeLessThans = (html: string) => {
return html.replace(/</g, "&lt;");
};
+// 理由同 escapeHtml:两个字符的转义保持逐字面量替换。
export const escapeAttr = (html: string) => {
if (!html) {
return html;
@@ -64,8 +58,16 @@ export const escapeAriaLabel = (html: string) => {
if (!html) {
return html;
}
- return html.replace(/"/g, "&quot;").replace(/'/g, "&apos;")
- .replace(/</g, "&amp;lt;").replace(/&lt;/g, "&amp;lt;");
+ return html.replace(/&lt;|["'<]/g, (match) => {
+ switch (match) {
+ case '"':
+ return "&quot;";
+ case "'":
+ return "&apos;";
+ default:
+ return "&amp;lt;";
+ }
+ });
};
export const decodeHTML = (html: string) => {