<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/securing-your-next-js-13-application-z0aq5e6a2" -->

---
title: Securing your Next.js 13 application | daily.dev
description: A practical walkthrough of hardening a Next.js 13 application, covering security headers such as Content-Security-Policy, Referrer-Policy, X-Frame-Options,...
canonical: https://daily.dev/posts/securing-your-next-js-13-application-z0aq5e6a2
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Securing your Next.js 13 application | daily.dev
og:description: A practical walkthrough of hardening a Next.js 13 application, covering security headers such as Content-Security-Policy, Referrer-Policy, X-Frame-Options,...
og:url: https://daily.dev/posts/securing-your-next-js-13-application-z0aq5e6a2
og:image: https://api.daily.dev/og/posts/Z0AQ5e6a2.png
og:image:alt: Securing your Next.js 13 application
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Securing your Next.js 13 application

**[Yagiz Nizipli](https://daily.dev/sources/yagiz)** · 5 min read · 0 upvotes · 0 comments

## Summary

A practical walkthrough of hardening a Next.js 13 application, covering security headers such as Content-Security-Policy, Referrer-Policy, X-Frame-Options, X-Content-Type-Options, X-DNS-Prefetch-Control, Strict-Transport-Security, and Permissions-Policy. It also explains cross-site request forgery attacks and offers two mitigation approaches: a manual middleware-based CSRF token implementation and using the edge-csrf npm package.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.yagiz.co/securing-your-nextjs-13-application>

## Questions this post answers

### How do I protect a Next.js app from CSRF attacks without a third-party library?

Implement a middleware.ts file that filters GET requests, creates a secure signed cookie with short expiration, and adds an X-CSRF-Token header to the response. On protected routes, validate that the header exists, a matching cookie is present, and the signed token matches the cookie value before processing the request; fail with an error if any check fails.

_daily.dev surfaces implementation patterns like this for developers hardening their Next.js apps against CSRF._

### What npm package can I use to add CSRF protection to a Next.js middleware?

The edge-csrf package can be installed via npm install edge-csrf or yarn add edge-csrf and wired into a middleware.ts file at the project root to handle CSRF token generation and validation without building the logic manually.

_Developers comparing manual CSRF setups against ready-made packages can track these options on daily.dev._

### Which HTTP security headers should a Next.js application set?

A hardened Next.js app should set Content-Security-Policy to restrict resource sources and mitigate XSS, Referrer-Policy to limit referrer leakage across origins, X-Frame-Options to block clickjacking via framing, X-Content-Type-Options to stop MIME sniffing, X-DNS-Prefetch-Control, Strict-Transport-Security to enforce HTTPS with a long max-age, and Permissions-Policy to restrict APIs like camera and geolocation.

_daily.dev helps developers keep a checklist of headers like these front of mind while securing web apps._

## Similar posts on daily.dev

- [The HTTP security headers every site should send](https://daily.dev/posts/the-http-security-headers-every-site-should-send-skotnz1rg) · Flavio Copes · 1 upvotes · 0 comments
- [Protecting Next.js Applications in the Era of Server Actions](https://daily.dev/posts/protecting-next-js-applications-in-the-era-of-server-actions-wqyz3uq9e) · JS Dev Space · 2 upvotes · 0 comments
- [Next.js May 2026 security release](https://daily.dev/posts/next-js-may-2026-security-release-hmqrmcava) · Vercel · 131 upvotes · 3 comments
- [How to Build Secure-by-Default Node.js APIs](https://daily.dev/posts/how-to-build-secure-by-default-node-js-apis-kgzyotifm) · freeCodeCamp · 10 upvotes · 0 comments
- [A Practical Guide to Secure Authentication for Developers](https://daily.dev/posts/a-practical-guide-to-secure-authentication-for-developers-p5rufwxgq) · SitePoint · 0 upvotes · 0 comments

---

Tags: [#security](https://daily.dev/tags/security), [#nextjs](https://daily.dev/tags/nextjs), [#web-security](https://daily.dev/tags/web-security)

[View this post on daily.dev](https://daily.dev/posts/securing-your-next-js-13-application-z0aq5e6a2)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Securing your Next.js 13 application","url":"https://daily.dev/posts/securing-your-next-js-13-application-z0aq5e6a2","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/securing-your-next-js-13-application-z0aq5e6a2"},"datePublished":"2026-09-13T19:57:46.030Z","dateModified":"2026-09-13T20:01:49.010Z","description":"A practical walkthrough of hardening a Next.js 13 application, covering security headers such as Content-Security-Policy, Referrer-Policy, X-Frame-Options,...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/27c846dd80a74af3f70c0be45ed6dba9?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/27c846dd80a74af3f70c0be45ed6dba9?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Yagiz Nizipli","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Yagiz Nizipli","logo":"https://media.daily.dev/image/upload/s--9Cscmaqk--/f_auto,q_auto/v1789329427/logos/yagiz?_a=BAMAMicg0","url":"https://daily.dev/sources/yagiz"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/securing-your-next-js-13-application-z0aq5e6a2","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"security,nextjs,web-security","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Yagiz Nizipli","item":"https://daily.dev/sources/yagiz"},{"@type":"ListItem","position":3,"name":"Securing your Next.js 13 application"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/securing-your-next-js-13-application-z0aq5e6a2#faq","mainEntity":[{"@type":"Question","name":"How do I protect a Next.js app from CSRF attacks without a third-party library?","acceptedAnswer":{"@type":"Answer","text":"Implement a middleware.ts file that filters GET requests, creates a secure signed cookie with short expiration, and adds an X-CSRF-Token header to the response. On protected routes, validate that the header exists, a matching cookie is present, and the signed token matches the cookie value before processing the request; fail with an error if any check fails. daily.dev surfaces implementation patterns like this for developers hardening their Next.js apps against CSRF."}},{"@type":"Question","name":"What npm package can I use to add CSRF protection to a Next.js middleware?","acceptedAnswer":{"@type":"Answer","text":"The edge-csrf package can be installed via npm install edge-csrf or yarn add edge-csrf and wired into a middleware.ts file at the project root to handle CSRF token generation and validation without building the logic manually. Developers comparing manual CSRF setups against ready-made packages can track these options on daily.dev."}},{"@type":"Question","name":"Which HTTP security headers should a Next.js application set?","acceptedAnswer":{"@type":"Answer","text":"A hardened Next.js app should set Content-Security-Policy to restrict resource sources and mitigate XSS, Referrer-Policy to limit referrer leakage across origins, X-Frame-Options to block clickjacking via framing, X-Content-Type-Options to stop MIME sniffing, X-DNS-Prefetch-Control, Strict-Transport-Security to enforce HTTPS with a long max-age, and Permissions-Policy to restrict APIs like camera and geolocation. daily.dev helps developers keep a checklist of headers like these front of mind while securing web apps."}}]}
```

