Skip to content

Instantly share code, notes, and snippets.

@manabuyasuda
Last active June 16, 2025 05:46
Show Gist options
  • Save manabuyasuda/ded0526f835ddfe46c52aeed85ddccae to your computer and use it in GitHub Desktop.
Save manabuyasuda/ded0526f835ddfe46c52aeed85ddccae to your computer and use it in GitHub Desktop.
next.config.tsに設定しておくといいheaders
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
async headers() {
return [
{
source: '/(.*)',
headers: [
// frame-ancestors:他のドメインで自分たちのサイトをiframeで読み込むのを禁止させることで、iframeを使ったクリックジャッキングを防ぎます
// https://developer.mozilla.org/ja/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors
// https://www.ipa.go.jp/security/vuln/websecurity/clickjacking.html
// 可能であれば使用するソースだけを許可するように設定してください
// https://vercel.com/docs/headers/security-headers
{
key: 'Content-Security-Policy',
value: "frame-ancestors 'self'",
},
// ブラウザがContent-Typeを信頼することで、悪意のあるスクリプトをHTMLやJSとして誤って解釈するMIMEタイプスニッフィングや、
// JSONハイジャック攻撃を緩和します
// https://developer.mozilla.org/ja/docs/Web/HTTP/Reference/Headers/X-Content-Type-Options
{
key: 'X-Content-Type-Options',
value: 'nosniff',
},
// クロスドメインアクセスやHTTP通信時に、リファラーにパスやクエリ情報が含まれないよう制限することで、機密情報(トークン、IDなど)の漏洩リスクを軽減します
// strict-origin-when-cross-originが規定値ですが、明示するために記述しています
// https://developer.mozilla.org/ja/docs/Web/HTTP/Reference/Headers/Referrer-Policy
{
key: 'Referrer-Policy',
value: 'strict-origin-when-cross-origin',
},
// 使用していない機能
// X-XSS-Protectionは効果が限定的かつ脆弱性を生み出す可能性もあるため使用しません
// https://developer.mozilla.org/ja/docs/Web/HTTP/Reference/Headers/X-XSS-Protection
// Content-Security-Policy: reflected-xss blockで反射型クロスサイト・スクリプティング攻撃を防止することができるようですが、まだ情報が少ないため使用していません
// https://www.ipa.go.jp/security/vuln/websecurity/cross-site-scripting.html
// X-Frame-Optionsは非推奨になっているため、推奨されているframe-ancestorsを使用しています
// https://developer.mozilla.org/ja/docs/Web/HTTP/Reference/Headers/X-Frame-Options
// HTTP Strict Transport Securityを有効化して、HTTPS接続を強制し、中間者攻撃(MITM)を防ぎます
// 初回からのHTTP接続を防ぐためにincludeSubDomains; preloadも検討してください
// HTTPSの強制が運用上問題になる可能性があるため必要性を計画したうえで導入してください
// https://developer.mozilla.org/ja/docs/Web/HTTP/Reference/Headers/Strict-Transport-Security
// VercelではHSTSが自動で適用されるので、ここで記述する必要はありません
// https://vercel.com/docs/encryption
// {
// key: 'Strict-Transport-Security',
// value: 'max-age=63072000',
// },
],
},
];
},
};
export default nextConfig;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment