本文へスキップ
Skillpedia
WebサイトとUI(フロントエンド)7.8k · #13068

panel-component-xss-safe

できること

このスキルは、AIエージェントが、外部のデータソースのデータを安全に表示する、プレーン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に貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/HKUDS/AgentPilot/blob/main/openspace/legacy/v1/showcase/skills/panel-component-xss-safe/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“外部のAPIからイベントのタイトルを表示するカレンダーのパネルを作ってください。イベントのテキストで、ページにHTMLが挿入されないようにしてください。”

毎週5つの新しいスキル。1分で読めます。

仕組み

エージェントは、アンパサンド、山かっこ、引用符、アポストロフィをHTMLエンティティに変換するescapeのヘルパーを作ります。信頼できない値はすべて、パネルの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 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">
 
よくある質問

escヘルパーは何をしますか?

山かっこや引用符などの特殊文字をHTMLエンティティに変換し、テキストとして表示されるようにします。

どのデータをエスケープする必要がありますか?

タイトル、場所、URL、名前など、外部の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

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル