Skip to content

セキュリティテスト完全ガイド

セキュリティテスト完全ガイド

Section titled “セキュリティテスト完全ガイド”

セキュリティテストの実践的な手法を、実務で使える実装例とベストプラクティスとともに詳しく解説します。

セキュリティテストの種類
├─ 脆弱性スキャン
├─ ペネトレーションテスト
├─ 認証・認可テスト
└─ データ保護テスト
Terminal window
# OWASP ZAPでの脆弱性スキャン
docker run -t owasp/zap2docker-stable zap-baseline.py \
-t http://localhost:3000 \
-J zap-report.json
Terminal window
# npm auditでの依存関係の脆弱性チェック
npm audit
# 自動修正
npm audit fix
// SQLインジェクションテスト
describe('SQL Injection Tests', () => {
it('should prevent SQL injection', async () => {
const maliciousInput = "'; DROP TABLE users; --";
const response = await request(app)
.get(`/api/users?name=${maliciousInput}`);
// テーブルが削除されていないことを確認
const users = await db.query('SELECT * FROM users');
expect(users.length).toBeGreaterThan(0);
});
});
// XSSテスト
describe('XSS Tests', () => {
it('should prevent XSS', async () => {
const maliciousInput = '<script>alert("XSS")</script>';
const response = await request(app)
.post('/api/comments')
.send({ content: maliciousInput });
// スクリプトがエスケープされていることを確認
expect(response.body.content).not.toContain('<script>');
});
});
// 認証テスト
describe('Authentication Tests', () => {
it('should require authentication', async () => {
const response = await request(app)
.get('/api/protected');
expect(response.status).toBe(401);
});
it('should allow authenticated users', async () => {
const token = await getAuthToken();
const response = await request(app)
.get('/api/protected')
.set('Authorization', `Bearer ${token}`);
expect(response.status).toBe(200);
});
});

セキュリティテスト完全ガイドのポイント:

  • 脆弱性スキャン: OWASP ZAP、npm audit
  • ペネトレーションテスト: SQLインジェクション、XSS
  • 認証・認可テスト: 認証・認可の検証

適切なセキュリティテストにより、安全なシステムを構築できます。