-
Couldn't load subscription status.
- Fork 274
Add security headers to Next.js config #1338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Consider adding additional security headers for enhanced protection.
While the current implementation covers essential security headers, consider adding these additional headers for comprehensive security:
headers: [ { key: 'X-DNS-Prefetch-Control', value: 'on' }, { key: 'Strict-Transport-Security', value: 'max-age=31536000; includeSubDomains; preload' }, { key: 'X-Content-Type-Options', value: 'nosniff' }, { key: 'X-Frame-Options', value: 'DENY' }, + { + key: 'Content-Security-Policy', + value: "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';" + }, + { + key: 'Referrer-Policy', + value: 'strict-origin-when-cross-origin' + }, + { + key: 'Permissions-Policy', + value: 'camera=(), microphone=(), geolocation=()' + } ],🌐 Web query:
💡 Result:
Next.js applications require robust security headers to mitigate common web vulnerabilities like cross-site scripting (XSS), clickjacking, and data leaks. Based on best practices from security experts and Next.js documentation[1][2][3][6], here are the essential security headers:
Core Security Headers
Strict-Transport-Security(HSTS)Enforces HTTPS-only connections with:
Strict-Transport-Security: max-age=63072000; includeSubDomains; preloadContent-Security-Policy(CSP)Restricts content sources to prevent XSS:
default-src 'self') and gradually expand[1][7]X-Content-Type-OptionsBlocks MIME-type sniffing:
X-Content-Type-Options: nosniffX-Frame-OptionsMitigates clickjacking via:
X-Frame-Options: DENYframe-ancestorsdirective[1]Additional Protections
Referrer-PolicyControls referrer data leakage:
Referrer-Policy: origin-when-cross-origin[3][5]Permissions-PolicyRestricts browser features:
Permissions-Policy: camera=(), geolocation=(), microphone=()Security Header Tools
Implementation Guide
Configure via
next.config.js:For complex CSP configurations, use middleware with libraries like Nosecone or next-secure-headers[1][4].
Key Considerations
X-XSS-Protection[1]server-onlypackage to prevent server code leakage[1]NEXT_PUBLIC_environment variables[1]These measures create a defense-in-depth strategy while maintaining compatibility with Next.js features like Static Site Generation and Server Components[7].
Citations:
Enhanced Security Headers – Actionable Improvement Required
The current code correctly implements the core security headers. However, based on best practices for Next.js applications, you should consider adding additional headers to further harden security. For example, supplementing the existing configuration with headers such as Content-Security-Policy, Referrer-Policy, and Permissions-Policy can help mitigate risks like XSS, clickjacking, and data leakage. (Note: Some sources recommend a higher HSTS max-age value—ensure this aligns with your overall security strategy.)
Key locations for attention:
next.config.mjsSuggested diff:
headers: [ { key: 'X-DNS-Prefetch-Control', value: 'on' }, { key: 'Strict-Transport-Security', - value: 'max-age=31536000; includeSubDomains; preload' + value: 'max-age=31536000; includeSubDomains; preload' }, { key: 'X-Content-Type-Options', value: 'nosniff' }, { key: 'X-Frame-Options', value: 'DENY' }, + { + key: 'Content-Security-Policy', + value: "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';" + }, + { + key: 'Referrer-Policy', + value: 'strict-origin-when-cross-origin' + }, + { + key: 'Permissions-Policy', + value: 'camera=(), microphone=(), geolocation=()' + } ],Please review and consider incorporating these additional headers to ensure a more robust security posture.
📝 Committable suggestion