セキュリティベストプラクティス
セキュリティベストプラクティス
Section titled “セキュリティベストプラクティス”Content Security Policy (CSP)
Section titled “Content Security Policy (CSP)”{ "content_security_policy": { "extension_pages": "script-src 'self'; object-src 'self'" }}最小権限の原則
Section titled “最小権限の原則”{ "permissions": [ "storage" // 必要な権限のみ ], "host_permissions": [ "https://api.example.com/*" // 特定のドメインのみ ]}ユーザー入力の検証
Section titled “ユーザー入力の検証”function sanitizeInput(input) { // XSS対策 const div = document.createElement('div'); div.textContent = input; return div.innerHTML;}
function validateURL(url) { try { const parsed = new URL(url); // 許可されたドメインのみ const allowedDomains = ['example.com', 'api.example.com']; return allowedDomains.includes(parsed.hostname); } catch { return false; }}機密情報の保護
Section titled “機密情報の保護”// 機密情報はストレージに保存しない// 悪い例chrome.storage.local.set({ apiKey: 'secret-key' });
// 良い例: 環境変数や暗号化chrome.storage.local.set({ encryptedData: encrypt('secret-key', userPassword)});メッセージの検証
Section titled “メッセージの検証”chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { // 送信元の検証 if (sender.id !== chrome.runtime.id) { return; }
// メッセージの型検証 if (!message.type || typeof message.type !== 'string') { sendResponse({ error: 'Invalid message type' }); return; }
// 処理});