Skip to content

fix: prevent HTML injection in Live2D messages - #88

Open
LIlGG wants to merge 2 commits into
mainfrom
feat/safe-tip-rendering
Open

LIlGG wants to merge 2 commits into
mainfrom
feat/safe-tip-rendering

Conversation

@LIlGG

@LIlGG LIlGG commented Oct 13, 2026 •

Copy link
Copy Markdown
Owner

What this PR does / why we need it:

页面标题、选择器提示中的 {text}、一言内容及来源/作者此前直接进入提示语 HTML,可能创建 iframe 等活动元素。现在复用 markdown-it 转义动态文本,并使用 DOMPurify 过滤普通提示语,保留基础排版。AI Markdown 渲染保持现有行为。

自定义普通提示语不再支持嵌入内容、事件和样式属性,支持的排版标签已在 README 中说明。{text} 会替换全部占位符,并保持 $& 等文字原样显示。

Which issue(s) this PR fixes:

Fixes #86

Does this PR introduce a user-facing change?

修复看板娘提示语将页面标题等文本当作 HTML 渲染的问题,并过滤不安全的标签和属性。

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The HTML escape helper imports an internal markdown-it module path that may fail strict TypeScript builds and should be replaced with a local or typed wrapper.

1 open issue
1 file not reviewed
  • pnpm-lock.yaml: Generated file
What changed in this PR

This pull request prevents HTML injection in Live2D tip messages by escaping dynamic values and sanitizing ordinary tip HTML while preserving AI Markdown rendering.

Changes:

  • Adds DOMPurify sanitization with a restricted formatting allowlist.
  • Escapes page titles, selector substitutions, and Hitokoto response fields.
  • Adds documentation, dependency updates, and regression tests.
File Description
README.md Documents supported ordinary-tip formatting and sanitization behavior.
pnpm-lock.yaml Locks the new DOMPurify dependency.
packages/​live2d/​src/​live2d/​tools/​hitokoto.ts Escapes Hitokoto content, source, and creator values.
packages/​live2d/​src/​live2d/​tools/​__tests__/​hitokoto.test.ts Tests safe Hitokoto message rendering.
packages/​live2d/​src/​helpers/​renderTipMessage.ts Sanitizes ordinary tip HTML and provides a safe fallback.
packages/​live2d/​src/​helpers/​escapeHtml.ts Provides HTML escaping for dynamic text.
packages/​live2d/​src/​helpers/​__tests__/​renderTipMessage.test.ts Tests sanitizer fallback behavior.
packages/​live2d/​src/​events/​tip-events.ts Escapes page titles, referrer domains, and selector substitutions.
packages/​live2d/​src/​events/​__tests__/​tip-events.test.ts Tests safe dynamic-text interpolation.
packages/​live2d/​src/​components/​Live2dTips.tsx Applies ordinary-tip sanitization while retaining Markdown rendering.
packages/​live2d/​src/​components/​__tests__/​Live2dTips.test.ts Tests ordinary and streamed message rendering.
packages/​live2d/​package.json Adds DOMPurify as a runtime dependency.

Review effort: Lite

@@ -0,0 +1 @@
export { escapeHtml } from "markdown-it/lib/common/utils.mjs";

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已在 e1ca0e6 改用公开的 new MarkdownIt().utils.escapeHtml,继续复用库实现。当前锁定的 @types/markdown-it@14.1.2 实际包含原路径的类型声明,原代码构建也能通过;此次调整消除了对内部路径的依赖。244 项前端测试及生产构建通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

看板娘发消息不带转义的QAQ

2 participants