panel-component-xss-safe
它能做什么
这项技能指导智能体用纯 TypeScript 构建能安全显示外部数据的仪表板面板。适合面板显示第三方 API 中的文本,例如新闻或日历事件的开发者。
适用场景
- ✓当面板显示来自外部 API 的标题、名称或描述的时候。
- ✓当不可信的文本可能被放入页面的 innerHTML 中的时候。
- ✓当你希望所有面板都使用统一的转义辅助函数的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/panel-component-xss-safe/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请构建一个日历面板,显示来自外部 API 的事件标题。请确保任何事件文本都无法向页面注入 HTML。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体创建一个 esc 转义辅助函数,将 &、尖括号、引号和撇号转换为 HTML 实体。每个不可信的值在进入面板的 HTML 之前都会经过该辅助函数处理。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Panel Component Pattern (XSS-Safe)
Create dashboard panel components using vanilla TypeScript (no framework, no JSX). Each panel is a class extending a Panel base class.
Security note: Panels that consume external API data (calendar events, news feeds, stock names, user-supplied content, etc.) MUST escape all untrusted values before injecting them into
innerHTML. Use theesc()helper documented below.
Architecture Overview
Panel (base class)
├── element: HTMLElement (outer container, .panel)
│ ├── header: HTMLElement (.panel-header)
│ │ ├── headerLeft (.panel-header-left)
│ │ │ ├── title (.panel-title)
│ │ │ └── newBadge (.panel-new-badge) [optional]
│ │ ├── statusBadge (.panel-data-badge) [optional]
│ │ └── countEl (.panel-count) [optional]
│ ├── content: HTMLElement (.panel-content)
│ └── resizeHandle (.panel-resize-handle)
XSS-Safe Rendering: the esc() Helper
Always use esc() when interpolating untrusted data into an HTML string.
Untrusted data includes anything from external APIs: event titles, locations, URLs, names, descriptions, symbols, etc.
Create src/utils/esc.ts:
/**
* Escapes a value for safe interpolation into an innerHTML string.
* Converts &, <, >, ", and ' to their HTML entity equivalents.
*
* Usage:
* this.setContent(`<div class="title">${esc(item.title)}</div>`);
*
* Do NOT use for:
* - href/src attributes with user-controlled URLs — validate scheme instead
* - CSS values — use a separate sanitizer
*/
export function esc(value: unknown): string {
return String(value ?? '')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
When to use esc()
| Data source | Safe? | Action |
|---|---|---|
| Hardcoded string literal in source | ✅ Safe | No escaping needed |
| Enum / controlled constant | ✅ Safe | No escaping needed |
| External API string field (title, name, body…) | ❌ Unsafe | Use esc() |
| User input / localStorage value | ❌ Unsafe | Use esc() |
| Number/boolean (rendered as text) | ✅ Safe | String(n) is fine |
| URL from external API | ⚠️ Unsafe | Validate scheme + esc() |
URL Safety
For URLs from external sources, validate the scheme before injecting:
/** Returns the URL only if it uses http or https; otherwise returns '#'. */
export function safeUrl(raw: unknown): string {
const s = String(raw ?? '').trim();
return /^https?:\/\//i.test(s) ? s : '#';
}
Usage: <a href="${safeUrl(item.url)}">${esc(item.title)}</a>
Base Panel Class
Create src/components/Panel.ts:
export interface PanelOptions {
id: string;
title: string;
showCount?: boolean;
className?: string;
}
export class Panel {
protected element: HTMLElement;
protected content: HTMLElement;
protected header: HTMLElement;
protected countEl: HTMLElement | null = null;
protected panelId: string;
private _fetching = false;
constructor(options: PanelOptions) {
this.panelId = options.id;
this.element = document.createElement('div');
this.element.className = `panel ${options.className || ''}`;
this.element.dataset.panel = options.id;
// Header
this.header = document.createElement('div');
this.header.className = 'panel-header';
const headerLeft = document.createElement('div');
headerLeft.className = 'panel-header-left';
const title = document.createElement('span');
title.className = 'panel-title';
title.textContent = options.title; // textContent is always safe
headerLeft.appendChild(title);
this.header.appendChild(headerLeft);
// Count badge (optional)
if (options.showCount) {
this.countEl = document.createElement('span');
this.countEl.className = 'panel-count';
this.countEl.textContent = '0';
this.header.appendChild(this.countEl);
}
// Content area
this.content = document.createElement('div');
this.content.className = 'panel-content';
this.content.id = `${options.id}Content`;
this.element.appendChild(this.header);
this.element.appendChild(this.content);
this.showLoading();
}
public getElement(): HTMLElement { return this.element; }
public showLoading(message = 'Loading...'): void {
// message is a hardcoded string — no escaping needed here
this.content.innerHTML = `
<div class="panel-loading">
常见问题
esc 辅助函数做什么?
它把尖括号和引号等特殊字符转换为 HTML 实体,使它们作为文本显示。
哪些数据需要转义?
技能说明,来自外部 API 的任何不可信值,例如标题、地点、网址和名称,都必须转义。
这会改变面板的外观吗?
不会,它只改变文本的插入方式,布局保持不变。
详情
作者的原始描述
Create a dashboard panel component using vanilla TypeScript DOM API, following the worldmonitor Panel architecture. Panels have a header with title/count, scrollable content area, loading/error states, and resize handles. Includes XSS-safe rendering pattern with esc() helper for safely interpolating untrusted external API data into innerHTML.
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #13068
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。