Skip to content

feat(tools): serve local images/video for Flex preview via LINE_FLEX_MCP_ASSET_DIR - #7

Merged
pierre3 merged 2 commits into
mainfrom
feat-flex-preview-local-assets
Sep 4, 2026
Merged

feat(tools): serve local images/video for Flex preview via LINE_FLEX_MCP_ASSET_DIR#7
pierre3 merged 2 commits into
mainfrom
feat-flex-preview-local-assets

Conversation

@pierre3

@pierre3 pierre3 commented Sep 4, 2026

Copy link
Copy Markdown
Owner

概要

Flex プレビューサーバ(Line.OpenApi.Tools)に、環境変数 LINE_FLEX_MCP_ASSET_DIR で指定したフォルダ内のメディアファイルを配信する機能を追加します。

利用シーン: 用意した画像/動画をフォルダに配置 → Flex JSON からは相対 url(例 "assets/hero.png")で参照 → 本番移行時は origin だけ HTTPS の CDN/ホストに差し替える(相対パス部分は不変=1:1 マッピング)。あくまでプレビュー用の利便機能(LINE 本体はローカル URL も data: URL もレンダリングしない)。

配信対象(LINE Flex 実仕様に準拠)

  • 画像: JPEG / PNG(APNG は .png
  • 動画: .mp4type:"video" コンポーネント)
  • GIF/WebP 等は LINE 非対応のため意図的に除外

video コンポーネントは renderer が previewUrl(JPEG/PNG のポスター)+▶ で描画し mp4 本体は取りに行かない(LINE アプリと同じ挙動)。プレビュー表示は previewUrl 配信で足りるが、mp4 の url も相対参照で解決可能にした。renderer.js は無改修(web assets parity 維持)。

セキュリティ(多層防御)

  • opt-inLINE_FLEX_MCP_ASSET_DIR 未設定なら完全無効・404)。ディレクトリは人が out-of-band 設定=LLM 非制御(AIFunction スキーマに露出しない)
  • 拡張子ホワイトリスト+content-type も image/*video/mp4・octet-stream に限定
  • パス封じ込め: Path.GetFullPath 正規化 → ベース配下 Ordinal 判定(../..\%2e%2e%5c・rooted/UNC を捕捉)+制御文字拒否
  • シンボリックリンク物理封じ込めFile.ResolveLinkTarget
  • ループバック Host ガード(/api と同軸の DNS リバインド対策)/read-only 安全(LINE API 非呼び出し・シークレット非保持)

3 役ゲート

code-reviewer / security-reviewer / test-arch-reviewer = すべて PASS・BLOCKING なし。収束指摘(シンボリックリンク越え・e2e トラバーサルのクライアント正規化退化・content-type 未網羅)を反映済み。記録: docs/reviews/2026-09-04-flex-preview-local-image-serving-review.md

テスト・検証

  • Line.OpenApi.Tools.Tests 131/131 緑・ビルド 0 警告
  • 純粋関数の封じ込め(traversal/rooted/symlink/大文字拡張子)+content-type マッピング+ループバック e2e(画像・video/mp4・エンコードパス・raw ソケット非正規化トラバーサル)
  • 変更は /tools 支援ティア内の HTTP プレビュー機能。生成コード・R1 ルーティング・公開 API snapshot・Kiota 版ピンに非接触(pack 12 パッケージ契約は Tools 除外で不変)

変更ファイル

  • 実装: tools/Line.OpenApi.Tools/Services/FlexPreviewService.cs
  • テスト: tests/Line.OpenApi.Tools.Tests/FlexPreviewAssetServingTests.cs(新規)
  • ドキュメント: tools/README.md / tools/README_ja.md
  • レビュー記録: docs/reviews/2026-09-04-flex-preview-local-image-serving-review.md

🤖 Generated with Claude Code

pierre3 and others added 2 commits September 4, 2026 12:34
…MCP_ASSET_DIR

Add opt-in local media serving to the Flex preview server so a Flex message
can reference artwork by a relative url during preview, then swap only the
origin for the production HTTPS URL later (relative path stays 1:1).

- Serve files under LINE_FLEX_MCP_ASSET_DIR; disabled unless configured.
- Served set matches what LINE renders in a Flex message: JPEG/PNG (APNG is
  .png) images and .mp4 for the video component. GIF/WebP intentionally
  excluded. The video component renders its previewUrl poster in the preview
  (LINE app behavior); the mp4 url also resolves for consistency.
- Confinement: extension allowlist, full-path normalization + under-base
  Ordinal check (rejects ../, backslash, rooted/UNC), control-char reject,
  and symlink physical containment via File.ResolveLinkTarget.
- Loopback-host guard (same axis as /api DNS-rebinding defense); read-only
  safe (no LINE API, no secrets). renderer.js unchanged (parity preserved).

Tests: pure-function confinement (traversal/rooted/symlink/uppercase-ext),
content-type mapping, and loopback e2e (image + video/mp4 + encoded path +
raw-socket non-normalized traversal). Tools tests 131/131, 0 warnings.

3-role gate (code/security/test-arch) = PASS, no blocking; findings applied.
Record: docs/reviews/2026-09-04-flex-preview-local-image-serving-review.md

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- Bump Line.OpenApi.Tools <Version> 1.2.0 -> 1.3.0 (minor: additive feature).
- Add CHANGELOG (en/ja) [1.3.0] entry for LINE_FLEX_MCP_ASSET_DIR local
  image/video serving.

Publish is a separate cadence: after merge to main, push tag tools-v1.3.0
to trigger release.yml publish-tool (OIDC/Trusted Publishing). Verified
locally: dotnet pack yields Line.OpenApi.Tools.1.3.0.nupkg (+snupkg),
DotnetTool command `line`, 0 warnings.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@pierre3
pierre3 merged commit 68ce172 into main Sep 4, 2026
2 checks passed
@pierre3
pierre3 deleted the feat-flex-preview-local-assets branch September 4, 2026 03:48
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.

1 participant