Skip to content

Potential fix for code scanning alert no. 3: DOM text reinterpreted as HTML - #153

Merged
poad merged 1 commit into
mainfrom
alert-autofix-3
Jan 1, 2026
Merged

Potential fix for code scanning alert no. 3: DOM text reinterpreted as HTML#153
poad merged 1 commit into
mainfrom
alert-autofix-3

Conversation

@poad

@poad poad commented Jan 1, 2026

Copy link
Copy Markdown
Owner

Potential fix for https://github.com/poad/llm-ts-example/security/code-scanning/3

In general terms, the problem should be fixed by validating and constraining the user-provided string before using it as the src of the <iframe>. The validation should ensure that only well-formed URLs with allowed schemes (e.g., http: and https:) are accepted, and anything else is rejected or results in no navigation. This avoids interpreting arbitrary DOM text as a URL/HTML-like context and blocks vectors such as javascript:alert(1) or malformed URLs.

For this specific code, the least invasive fix is:

  • Introduce a small helper that "sanitizes" a URL string: attempts to construct a URL object and confirms that the protocol is one of an allowlist, like http: or https:. If parsing fails or the protocol is not allowed, return undefined.
  • Use this helper when setting the iframe src: instead of (src ?? url) || undefined, compute a safeSrc as either the sanitized src prop or the sanitized context url, and pass that to src. If both are invalid, the iframe will have src={undefined}, effectively loading nothing.
  • Keep the existing sandbox attribute unchanged.

These changes all live in chat-app/components/ai-elements/web-preview.tsx. We will:

  1. Add a sanitizeUrl helper function near the top of the file (where it’s easy to see and reuse).
  2. In WebPreviewBody, compute const safeSrc = sanitizeUrl(src ?? url); and use safeSrc || undefined for the iframe src.

No external libraries are strictly required; we can safely use the built-in URL class in modern browsers/Next.js.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…s HTML

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@poad
poad marked this pull request as ready for review January 1, 2026 09:33
@poad
poad enabled auto-merge (squash) January 1, 2026 09:34

@amazon-q-developer amazon-q-developer Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

セキュリティ修正のレビュー結果

このPRは、DOM text reinterpretation脆弱性(Code Scanning Alert #3)に対する重要なセキュリティ修正を実装しています。

主な改善点

  • sanitizeUrl関数の追加により、iframe srcでの危険なプロトコル(javascript:など)の実行を防止
  • URL検証により、HTTPSとHTTPのみを許可する適切な制限を実装

修正が必要な問題

  1. サーバーサイドレンダリング対応: window.location.originへの依存を削除し、絶対URLのみを受け入れるように修正
  2. プロトコル検証の強化: より厳密な否定的検証ロジックの実装
  3. 入力検証の一貫性: URL入力フィールドでも同様の検証を適用することを推奨

セキュリティ修正の方向性は正しいですが、上記の改善により、より堅牢な実装になります。


You can now have the agent implement changes and create commits directly on your pull request's source branch. Simply comment with /q followed by your request in natural language to ask the agent to make changes.

const sanitizeUrl = (value: string | undefined | null): string | undefined => {
if (!value) return undefined;
try {
const url = new URL(value, typeof window !== 'undefined' ? window.location.origin : undefined);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛑 セキュリティ脆弱性: sanitizeUrl関数でサーバーサイドレンダリング時にベースURLが未定義になる可能性があります。window.location.originが利用できない場合、相対URLが予期しない動作を引き起こす可能性があります1

Suggested change
const url = new URL(value, typeof window !== 'undefined' ? window.location.origin : undefined);
const url = new URL(value);

Footnotes

  1. CWE-20: Improper Input Validation - https://cwe.mitre.org/data/definitions/20.html

Comment on lines +25 to +27
if (url.protocol === 'http:' || url.protocol === 'https:') {
return url.toString();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

プロトコル検証が不完全です。`

Suggested change
if (url.protocol === 'http:' || url.protocol === 'https:') {
return url.toString();
}
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
return undefined;
}

@poad
poad merged commit afcdd78 into main Jan 1, 2026
7 checks passed
@poad
poad deleted the alert-autofix-3 branch January 1, 2026 09:36
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