Webアプリケーションのセキュリティ対策入門
2024-01-25
Webアプリケーションのセキュリティ対策入門
Webアプリケーションを脆弱性から守るための重要なセキュリティ対策について、実践的な例を交えて解説します。
1. クロスサイトスクリプティング(XSS)対策
入力値のサニタイズ
ユーザーからの入力値は必ずサニタイズする必要があります。
// 悪い例:直接HTMLとして挿入
const dangerousHtml = `<div>${userInput}</div>`
// 良い例:エスケープ処理を行う
import { escape } from 'html-escaper'
const safeHtml = `<div>${escape(userInput)}</div>`
// Reactでの例(自動でエスケープされる)
const SafeComponent = () => {
return <div>{userInput}</div>
}
2. CSRF(クロスサイトリクエストフォージェリ)対策
トークンの使用
// Next.jsでのCSRFトークン実装例
import { csrf } from 'edge-csrf'
// ミドルウェアでの設定
export const middleware = csrf({
cookie: {
secure: process.env.NODE_ENV === 'production',
},
})
// フォームでの使用
const Form = () => {
return (
<form method="POST">
<input type="hidden" name="csrf" value={csrf.token} />
<input type="text" name="username" />
<button type="submit">送信</button>
</form>
)
}
3. SQLインジェクション対策
プリペアドステートメントの使用
// 悪い例:文字列結合
const query = `SELECT * FROM users WHERE id = ${userId}`
// 良い例:プリペアドステートメント
const query = 'SELECT * FROM users WHERE id = ?'
const [rows] = await connection.execute(query, [userId])
4. セキュアな認証システム
パスワードのハッシュ化
import { hash, compare } from 'bcrypt'
// パスワードのハッシュ化
const hashPassword = async (password: string) => {
const saltRounds = 10
return await hash(password, saltRounds)
}
// パスワードの検証
const verifyPassword = async (password: string, hash: string) => {
return await compare(password, hash)
}
5. HTTPセキュリティヘッダー
重要なセキュリティヘッダーの設定
// Next.jsでのセキュリティヘッダー設定
const securityHeaders = [
{
key: 'X-XSS-Protection',
value: '1; mode=block',
},
{
key: 'X-Frame-Options',
value: 'SAMEORIGIN',
},
{
key: 'X-Content-Type-Options',
value: 'nosniff',
},
]
module.exports = {
async headers() {
return [
{
source: '/:path*',
headers: securityHeaders,
},
]
},
}
セキュリティチェックリスト
実装時の確認事項
-
入力値の検証
- すべてのユーザー入力をバリデーション
- 特殊文字のエスケープ処理
- ファイルアップロードの制限
-
認証・認可
- 強力なパスワードポリシー
- 多要素認証の実装
- セッション管理の適切な実装
-
データ保護
- 機密情報の暗号化
- 安全な通信(HTTPS)
- アクセス制御の実装
セキュリティ対策は常に最新の脅威に対応する必要があります。定期的なセキュリティ監査と、脆弱性情報のモニタリングを行うことが重要です。