<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Hasan Kemal Demirci</title>
    <description>The latest articles on Forem by Hasan Kemal Demirci (@hasankemaldemirci).</description>
    <link>https://forem.com/hasankemaldemirci</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3667804%2F5630536b-810d-4586-a0c9-5dc67d81d4b0.jpg</url>
      <title>Forem: Hasan Kemal Demirci</title>
      <link>https://forem.com/hasankemaldemirci</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hasankemaldemirci"/>
    <language>en</language>
    <item>
      <title>How I Built a Security-First SaaS Boilerplate with 100% Test Coverage</title>
      <dc:creator>Hasan Kemal Demirci</dc:creator>
      <pubDate>Thu, 18 Dec 2025 10:55:12 +0000</pubDate>
      <link>https://forem.com/hasankemaldemirci/how-i-built-a-security-first-saas-boilerplate-with-100-test-coverage-30dl</link>
      <guid>https://forem.com/hasankemaldemirci/how-i-built-a-security-first-saas-boilerplate-with-100-test-coverage-30dl</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"We'll add security later."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you've ever said this, you're not alone. I've been there too. But after seeing countless data breaches, emergency security patches at 2 AM, and the $4.45 million average cost of a data breach, I decided to take a different approach.&lt;/p&gt;

&lt;p&gt;This is the story of how I built &lt;a href="https://shipsecure.dev" rel="noopener noreferrer"&gt;ShipSecure&lt;/a&gt; — a Next.js boilerplate where security isn't an afterthought, it's the foundation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem: Security as a TODO Item
&lt;/h2&gt;

&lt;p&gt;Most developers approach security like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Build landing page&lt;/li&gt;
&lt;li&gt;✅ Add authentication&lt;/li&gt;
&lt;li&gt;✅ Integrate payments&lt;/li&gt;
&lt;li&gt;⬜ Security (we'll get to it later)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sound familiar? The issue is that "later" often means "after the first incident." And by then, you've already:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lost customer trust&lt;/li&gt;
&lt;li&gt;Faced potential regulatory fines (GDPR, CCPA, SOC 2)&lt;/li&gt;
&lt;li&gt;Spent weeks retrofitting security into an architecture that wasn't designed for it&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Solution: Security-First Development
&lt;/h2&gt;

&lt;p&gt;Instead of treating security as a feature, I treated it as &lt;strong&gt;architecture&lt;/strong&gt;. Every line of code was written with security in mind, and every security feature was covered by tests.&lt;/p&gt;

&lt;p&gt;Let me walk you through the key concepts.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Security Headers: Your First Line of Defense
&lt;/h2&gt;

&lt;p&gt;Most developers know about &lt;code&gt;Content-Security-Policy&lt;/code&gt;, but how many actually implement it correctly? Here's the thing — it's not just about adding one header. You need a &lt;strong&gt;complete security header strategy&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Header&lt;/th&gt;
&lt;th&gt;What It Prevents&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Content-Security-Policy&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;XSS attacks, code injection&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Strict-Transport-Security&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Protocol downgrade attacks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;X-Content-Type-Options&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;MIME sniffing vulnerabilities&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;X-Frame-Options&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Clickjacking attacks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Referrer-Policy&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Information leakage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Permissions-Policy&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Unauthorized browser feature access&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The tricky part? Getting these headers to work together without breaking your app. CSP alone has dozens of directives that need careful configuration — one wrong setting and your OAuth flow breaks, your analytics stop working, or your styles don't load.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The approach I took:&lt;/strong&gt; A centralized &lt;code&gt;getSecurityHeaders()&lt;/code&gt; function that returns a properly configured header object, applied consistently across all routes through middleware.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Rate Limiting: The Art of Saying "Slow Down"
&lt;/h2&gt;

&lt;p&gt;Without rate limiting, your API is an open invitation for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔓 Brute force attacks on login&lt;/li&gt;
&lt;li&gt;💥 DDoS attempts&lt;/li&gt;
&lt;li&gt;🎭 Credential stuffing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The challenge? &lt;strong&gt;Production and development have different needs.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In production, you need distributed rate limiting (Upstash Redis) so limits work across serverless instances. But in development, you don't want to set up Redis just to run locally.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My solution:&lt;/strong&gt; A hybrid architecture with automatic fallback.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Production: Upstash Redis (distributed, persistent)
     ↓ (automatic fallback if Redis not configured)
Development: In-memory Map (simple, no setup)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here's the key insight: &lt;strong&gt;use sliding window, not fixed window.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Why? With fixed window rate limiting, an attacker can make 20 requests in 2 seconds by timing it at the window boundary (10 at :59, 10 at :01). Sliding window eliminates this vulnerability entirely.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Test-Driven Security: The Game Changer
&lt;/h2&gt;

&lt;p&gt;Here's what truly sets a security-first approach apart: &lt;strong&gt;every security feature is backed by tests.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Why does this matter?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Tests prove claims.&lt;/strong&gt; Anyone can say "we use CSP." Tests prove it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tests prevent regression.&lt;/strong&gt; That intern can't accidentally remove security headers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tests document behavior.&lt;/strong&gt; Security requirements become executable specifications.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I use a three-layer testing strategy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Layer 1: Unit Tests (Vitest)
├── Individual security functions
├── Validation logic
└── Edge cases

Layer 2: Integration Tests  
├── Middleware behavior
├── Auth flows
└── API protection

Layer 3: E2E Tests (Playwright)
├── Real browser behavior
├── Header verification
└── User journey security
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The result:&lt;/strong&gt; 75+ tests covering every security mechanism. CI/CD runs them on every PR. No security feature ships without test coverage.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Input Validation: Where Most Breaches Start
&lt;/h2&gt;

&lt;p&gt;SQL injection, XSS, command injection — they all share one thing in common: &lt;strong&gt;unvalidated input.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The traditional approach looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ❌ This is asking for trouble&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No validation. No type checking. No protection.&lt;/p&gt;

&lt;p&gt;The security-first approach:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Define schemas&lt;/strong&gt; for every input&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validate at the boundary&lt;/strong&gt; (API routes, form submissions)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fail fast&lt;/strong&gt; with clear error messages&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never trust&lt;/strong&gt; - even authenticated users can send malicious data&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I use Zod for this — type-safe at compile time, validated at runtime. But the tool matters less than the discipline of &lt;strong&gt;validating everything, everywhere&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Authentication Done Right
&lt;/h2&gt;

&lt;p&gt;Authentication is where most security vulnerabilities live. Password storage, session management, CSRF protection, OAuth flows — each is a potential attack vector.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My approach:&lt;/strong&gt; Don't reinvent the wheel. I use Auth.js v5 because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ HttpOnly, secure cookies by default&lt;/li&gt;
&lt;li&gt;✅ Built-in CSRF protection&lt;/li&gt;
&lt;li&gt;✅ Battle-tested OAuth implementations&lt;/li&gt;
&lt;li&gt;✅ Session management that actually works&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The key insight: &lt;strong&gt;authentication isn't just about login/logout.&lt;/strong&gt; It's about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How sessions are stored and validated&lt;/li&gt;
&lt;li&gt;How cookies are configured&lt;/li&gt;
&lt;li&gt;How tokens are handled&lt;/li&gt;
&lt;li&gt;How logout actually invalidates sessions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Getting any of these wrong creates vulnerabilities. Auth.js handles them correctly by default.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Hard-Won Lessons
&lt;/h2&gt;

&lt;p&gt;After building this, here's what I wish I knew earlier:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Security is 10x cheaper when designed in
&lt;/h3&gt;

&lt;p&gt;Retrofitting security into an existing codebase is painful. Every decision you made without security in mind becomes a constraint.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Tests are your best security documentation
&lt;/h3&gt;

&lt;p&gt;When someone asks "how do you prevent X?", point them to the test file. It's proof, not promises.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Developer experience matters
&lt;/h3&gt;

&lt;p&gt;If security measures are annoying, developers find workarounds. The in-memory fallback for rate limiting? That's about DX. The centralized header function? That's about DX. Security should be invisible to developers using your codebase.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Automate everything
&lt;/h3&gt;

&lt;p&gt;Security tests in CI/CD mean no one can accidentally (or intentionally) ship insecure code. It's not about trust — it's about systems.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Bottom Line
&lt;/h2&gt;

&lt;p&gt;Here's the reality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;60%&lt;/strong&gt; of startups shut down within 6 months of a security breach&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;73%&lt;/strong&gt; of enterprise customers require security certifications&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SOC 2&lt;/strong&gt; compliance can shorten sales cycles by &lt;strong&gt;40%&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Security isn't a feature — it's a competitive advantage.&lt;/p&gt;




&lt;h2&gt;
  
  
  Want the Full Implementation?
&lt;/h2&gt;

&lt;p&gt;I've packaged everything I learned into &lt;a href="https://shipsecure.dev" rel="noopener noreferrer"&gt;ShipSecure&lt;/a&gt; — a Next.js 15 boilerplate that's secure from day one:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🛡️ All 7 security headers pre-configured and working&lt;/li&gt;
&lt;li&gt;⚡ Rate limiting with Redis + in-memory fallback&lt;/li&gt;
&lt;li&gt;🔐 Auth.js v5 with secure defaults&lt;/li&gt;
&lt;li&gt;✅ 75+ tests for 100% security coverage&lt;/li&gt;
&lt;li&gt;📦 Stripe integration included&lt;/li&gt;
&lt;li&gt;📚 Complete documentation&lt;/li&gt;
&lt;li&gt;🔄 Lifetime updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;One-time purchase. Skip the security research. Start building.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;You build. We secure.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://shipsecure.dev" rel="noopener noreferrer"&gt;shipsecure.dev&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Have questions about security in Next.js? Drop a comment — I read every one.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>nextjs</category>
      <category>security</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
