Webセキュリティ
Webセキュリティ完全ガイド
Section titled “Webセキュリティ完全ガイド”Webアプリケーションのセキュリティ対策について、実務で使える実装例とともに詳しく解説します。
1. セキュリティヘッダー
Section titled “1. セキュリティヘッダー”Content-Security-Policy (CSP)
Section titled “Content-Security-Policy (CSP)”Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline'- XSS攻撃を防ぐ
- リソースの読み込み元を制限
X-Frame-Options
Section titled “X-Frame-Options”X-Frame-Options: DENY- クリックジャッキングを防ぐ
X-Content-Type-Options
Section titled “X-Content-Type-Options”X-Content-Type-Options: nosniff- MIMEタイプスニッフィングを防ぐ
2. CORS(Cross-Origin Resource Sharing)
Section titled “2. CORS(Cross-Origin Resource Sharing)”CORSとは
Section titled “CORSとは”異なるオリジン間でのリソース共有を制御する仕組みです。
オリジン = スキーム + ホスト + ポート例: https://example.com:443CORSの設定
Section titled “CORSの設定”Access-Control-Allow-Origin: https://example.comAccess-Control-Allow-Methods: GET, POST, PUT, DELETEAccess-Control-Allow-Headers: Content-Type, AuthorizationAccess-Control-Allow-Credentials: trueプリフライトリクエスト
Section titled “プリフライトリクエスト”OPTIONS /api/users HTTP/1.1Origin: https://example.comAccess-Control-Request-Method: POSTAccess-Control-Request-Headers: Content-Type3. 外部リンクのセキュリティ対策
Section titled “3. 外部リンクのセキュリティ対策”target=“_blank”のセキュリティリスク
Section titled “target=“_blank”のセキュリティリスク”target="_blank"を使用して外部リンクを開く際、セキュリティリスクが存在します。
問題のあるコード:
<!-- 問題: window.openerが利用可能 --><a href="https://example.com" target="_blank">外部リンク</a>リスク:
- 新しいタブから元のページにアクセス可能(
window.opener) - フィッシング攻撃の可能性
- パフォーマンスの問題
rel=“noopener noreferrer”の使用
Section titled “rel=“noopener noreferrer”の使用”解決策:
<!-- 解決: rel="noopener noreferrer"を追加 --><a href="https://example.com" target="_blank" rel="noopener noreferrer"> 外部リンク</a>各属性の役割:
rel="noopener":window.openerへのアクセスを防ぐrel="noreferrer": Refererヘッダーを送信しないrel="noopener noreferrer": 両方の対策を実施(推奨)
基本的な外部リンク
Section titled “基本的な外部リンク”<!-- 外部リンクの基本形 --><a href="https://example.com" target="_blank" rel="noopener noreferrer"> 外部リンク</a>Reactでの実装
Section titled “Reactでの実装”// Reactコンポーネントinterface ExternalLinkProps { href: string; children: React.ReactNode;}
function ExternalLink({ href, children }: ExternalLinkProps) { return ( <a href={href} target="_blank" rel="noopener noreferrer" > {children} </a> );}
// 使用例<ExternalLink href="https://example.com"> 外部リンク</ExternalLink>Next.jsでの実装
Section titled “Next.jsでの実装”import Link from 'next/link';
// Next.jsのLinkコンポーネントを使用する場合<Link href="https://example.com" target="_blank" rel="noopener noreferrer"> 外部リンク</Link>セキュリティヘッダーとの組み合わせ
Section titled “セキュリティヘッダーとの組み合わせ”# Content-Security-Policyで外部リンクを制御Content-Security-Policy: default-src 'self'; base-uri 'self'; form-action 'self';よくある問題と解決策
Section titled “よくある問題と解決策”問題1: rel属性を忘れる
Section titled “問題1: rel属性を忘れる”原因:
target="_blank"を使用しているが、rel属性を設定していない
解決策:
<!-- 常にrel="noopener noreferrer"を追加 --><a href="https://example.com" target="_blank" rel="noopener noreferrer"> 外部リンク</a>問題2: 内部リンクにも適用してしまう
Section titled “問題2: 内部リンクにも適用してしまう”原因:
- すべてのリンクに
target="_blank"を適用している
解決策:
<!-- 内部リンクにはtarget="_blank"を付けない --><a href="/about">内部リンク</a>
<!-- 外部リンクのみtarget="_blank"を使用 --><a href="https://example.com" target="_blank" rel="noopener noreferrer"> 外部リンク</a>4. その他のセキュリティ対策
Section titled “4. その他のセキュリティ対策”Referrer Policy
Section titled “Referrer Policy”Referrer-Policy: strict-origin-when-cross-origin設定値:
no-referrer: Refererヘッダーを送信しないsame-origin: 同一オリジンのみ送信strict-origin-when-cross-origin: クロスオリジン時はオリジンのみ送信(推奨)
Permissions Policy
Section titled “Permissions Policy”Permissions-Policy: geolocation=(), microphone=(), camera=()設定例:
geolocation=(): 位置情報へのアクセスを禁止microphone=(): マイクへのアクセスを禁止camera=(): カメラへのアクセスを禁止
Strict-Transport-Security (HSTS)
Section titled “Strict-Transport-Security (HSTS)”Strict-Transport-Security: max-age=31536000; includeSubDomains; preload設定値:
max-age: HSTSの有効期限(秒)includeSubDomains: サブドメインにも適用preload: HSTSプリロードリストに含める
これで、Webセキュリティの基礎知識と外部リンクのセキュリティ対策を理解できるようになりました。