Chuyển đến nội dung
Skillpedia
Website và giao diện (frontend)7.8k · #13068

panel-component-xss-safe

Skill này làm gì

Skill này hướng dẫn AI agent xây dựng các panel bảng điều khiển bằng TypeScript thuần để hiển thị an toàn dữ liệu từ nguồn bên ngoài. Nó dành cho nhà phát triển có panel hiển thị văn bản từ API bên thứ ba, như tin tức hoặc sự kiện lịch.

Khi nào nên dùng

  • ✓Khi một panel hiển thị tiêu đề, tên hoặc mô tả từ một API bên ngoài.
  • ✓Khi văn bản không đáng tin có thể được đặt vào innerHTML trên trang.
  • ✓Khi bạn muốn một hàm thoát ký tự chuẩn dùng cho mọi panel.

Liên kết skill

https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/panel-component-xss-safe/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/panel-component-xss-safe/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Xây dựng một panel lịch hiển thị tiêu đề sự kiện từ một API bên ngoài. Đảm bảo không có nội dung sự kiện nào có thể chèn HTML vào trang.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent tạo một hàm esc chuyển các ký tự như dấu &, ngoặc nhọn, dấu nháy kép và nháy đơn thành thực thể HTML. Mọi giá trị không đáng tin đều đi qua hàm này trước khi đưa vào HTML của panel.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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 the esc() 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, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#39;');
}

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">
 
Câu hỏi thường gặp

Hàm esc làm gì?

Nó chuyển các ký tự đặc biệt như ngoặc nhọn và dấu nháy thành thực thể HTML, để chúng hiển thị như văn bản.

Những dữ liệu nào cần được thoát?

Skill nói mọi giá trị không đáng tin từ API bên ngoài, như tiêu đề, địa điểm, URL và tên, đều phải được thoát.

Điều này có thay đổi giao diện panel không?

Không, nó chỉ thay đổi cách văn bản được chèn vào, nên bố cục vẫn giữ nguyên.

Chi tiết

Mô tả gốc của tác giả

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

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#13068
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự