![]() |
VOOZH | about |
Next.js security headers help protect your application from common web vulnerabilities by enforcing security policies at the HTTP level. By configuring these headers, you enhance your app's security and ensure safer interactions for your users.
In this article, we’ll learn about security headers, their roles in website security, examples of security headers, and how to implement them in Next.js.
Table of Content
HTTP headers are used to pass additional information or metadata using HTTP requests or responses. These headers are case-insensitive, in key-value pair format, and separated by a colon.
Security Headers are a set of rules used to communicate between the user and the websites. Security headers are HTTP headers used to enhance the security of web applications by controlling various aspects of how browsers handle content. They help protect against a range of web security threats, including cross-site scripting (XSS), clickjacking, and data injection attacks.
Step 1: To create a new NextJs App run the below command in your terminal:
npx create-next-app app_nameStep 2: After creating your project folder (i.e. GFG ), move to it by using the following command:
cd app_nameYou can check the headers at your end by doing the following steps:
These are all default security headers. Security headers are not even mentioned in this response header and this does not mention security headers like Content-Security-Policy, X-Frame, HSTS, Permission-Policy, etc. If these policies are not mentioned, the security of the website can be damaged by threats like XSS, Code injection, and Clickjacking.
Examples of Security Headers: In the below section, we are going to see some examples, and along with that implementation of the same.
CSP headers are used to protect websites from malicious attacks. CSP allows you to set a policy that which domain can execute scripts and which one cannot. Using CSP we can define what content sources are allowed to load on a page.
Syntax:
Content-Security-Policy: default-src <set_origin>You can Set Origins as:
Example: Write the code in the belonging files:
Step to run the application: Run your Next app using the following command:
npm run devOutput:
X-Frame-Options allow thwarting our own content which can be used in the invisible frames by attackers. Many sites use this security header to avoid Clickjacking attacks. This option actually allows whether or not a browser should be allowed to render a page in <frame>, <iframe>, <embed>, or <object>.
Syntax:
X-Frame-Options: DENY X-Frame-Options: SAMEORIGIN
Example: In the following code, we are going to set the X-Frame-Option which is going to be in the Key-Value pair. In value, we can set Deny or Sameorigin. We are going to keep Denying it which stops the site from being rendered in <frame>.
Output:
It is also known as Feature policy. Permission policy helps you to define which browser API to use and which not. For example, if your site doesn't need a camera and microphone, you can simply disable them to protect your website from attackers.
Syntax:
Permissions-Policy: camera=(); battery=(self); geolocation=(); microphone=('https://a-domain.com/')Example: In the following code, we are going to set the Permission-policy which is going to be in the Key-Value pair. In value, we can set the camera, battery, geolocation, and microphone to simply disable them to protect your website from attackers. We can set the origin for values also for eg. microphone=('https://abc_domain.com')" which allowed for stated origin only.
Output:
When we click on a link that moves from one domain to another domain. Then the main domain is considered as the referrer. Using the Referrer Policy we can control the information(information about where the user came from) sent by the referrer domain to another domain.
Syntax:
Referrer-Policy: origin-when-cross-origin Referrer-Policy: strict-origin-when-cross-origin
Example: In the following code, we are going to set the Referrer policy which is going to be in the Key-Value pair. In value, we can set origin-when-cross-origin orstrict-origin-when-cross-origin. We are going to set strict-origin-when-cross-origin which is secure.
Output: