Deutsch
GuideSecurity · Web basics

Security headers for Vite and Lovable apps: which ones and where to set them

Which security headers a Vite single-page app needs (CSP, HSTS, nosniff, Referrer-Policy, Permissions-Policy) and how to set them on Vercel, Netlify and Cloudflare.

By Tim GeithnerPublished 6 min read

Short answer

Security headers are HTTP response headers that switch on extra browser protections. A Vite single-page app should send a Content Security Policy (start in report-only mode), HSTS, X-Content-Type-Options: nosniff, a Referrer-Policy, a Permissions-Policy and frame-ancestors ‘none’ against clickjacking. Vite only builds static files, so the headers are set by your host: vercel.json on Vercel, a _headers file on Netlify and Cloudflare Pages. Check what you really send with curl -I.

Ask an AI about this page:ChatGPTClaude

On this page

Key takeaways

  • Security headers are switches for browser protections. They don’t fix bugs in your code; they make some bugs harder to exploit.
  • Six matter for a Vite SPA: CSP, HSTS, X-Content-Type-Options, Referrer-Policy, Permissions-Policy and frame-ancestors (plus X-Frame-Options as backup).
  • Vite builds static files. Your host sets the headers: vercel.json, or a _headers file in public/ for Netlify and Cloudflare Pages.
  • Roll out the CSP in report-only mode first, then enforce it.
  • Check the result with curl -I https://your-app.example, not by reading your config.

What security headers do, and what they don’t

When a browser loads your app, the server’s response carries headers next to the HTML. Some of them tell the browser to be stricter: only load scripts from these places, always use HTTPS, never show this page inside someone else’s frame. OWASP’s cheat sheet sums it up: the right response headers help against cross-site scripting, clickjacking and information disclosure.

They are a second layer. A Content Security Policy makes an XSS bug much harder to exploit, but the bug is still there. Row level security, auth checks in Edge Functions and keeping secrets out of the frontend matter more. Headers are cheap, though, and once set they protect every page.

The six headers, with values you can start from

HeaderStarter valueWhat it does
Content-Security-PolicySee the policy belowLimits where scripts, styles, images and connections may come from
Strict-Transport-Securitymax-age=63072000; includeSubDomainsBrowsers use HTTPS only for two years after the first visit
X-Content-Type-OptionsnosniffBrowsers don’t guess file types, so a text upload can’t run as a script
Referrer-Policystrict-origin-when-cross-originOther sites see only your domain, not full URLs with IDs or tokens
Permissions-Policygeolocation=(), camera=(), microphone=()Turns off browser features your app doesn’t use
frame-ancestors (in the CSP) + X-Frame-Options'none' + DENYNobody can load your app in an invisible frame (clickjacking)

The HSTS, nosniff, Referrer-Policy and Permissions-Policy values follow OWASP’s cheat sheet. Two notes on HSTS from MDN: browsers ignore the header when it arrives over plain HTTP, and once a browser has it, users can no longer click past certificate errors. Only add includeSubDomains if every subdomain of your domain serves HTTPS, and leave preload out until you have read the preload requirements.

OWASP also says not to send X-XSS-Protection (or to send it as 0), because the old browser filter it controlled could create new problems. Some example configs online still include X-XSS-Protection: 1; mode=block; don’t copy that line.

A starter CSP for a Vite + Supabase app

A typical Lovable or Vite app loads its own bundled scripts, talks to Supabase and sometimes shows images from elsewhere. This policy fits that shape:

Content-Security-Policy (start in report-only mode)
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self' https://your-project.supabase.co wss://your-project.supabase.co; frame-ancestors 'none'; base-uri 'self'; form-action 'self'; object-src 'none'

Line by line:

  • script-src 'self' allows only scripts from your own domain. No inline scripts, no eval. MDN warns against 'unsafe-inline' for scripts because inline JavaScript is one of the most common XSS paths. Vite’s production build normally loads scripts as files; if your index.html contains an inline <script>, move it into a file or add its hash.
  • style-src 'self' 'unsafe-inline' is a compromise. Many React component libraries set inline styles at runtime. Allowing inline styles is far less risky than allowing inline scripts. Remove it if your app works without it.
  • img-src and font-src with data:: Vite’s docs explain that small assets are inlined as data: URIs by default, so allow data: there, or set build.assetsInlineLimit to 0. Vite’s docs also say never to allow data: in script-src.
  • connect-src lists where fetch may go. Replace your-project with your Supabase project ref. Add your AI proxy, analytics or payment endpoints here when you use them. The wss:// entry is only needed for Supabase Realtime.
  • frame-ancestors 'none' blocks framing. base-uri, form-action and object-src close three older tricks at no cost.

Where to set the headers

Vite has no say in production headers; the server.headers option in vite.config only affects npm run dev. The host that serves your dist folder decides. This is what each host documents today (checked on 3 December 2026).

Lovable hosting

Lovable’s hosting docs say that every published URL is served over HTTPS and that plain http:// requests are redirected to https://. Lovable’s Trust Center for published apps lists browser checks such as telling browsers to keep using HTTPS, not guessing file types, restricting framing, and rules for where scripts may load from. We could not find a page in Lovable’s docs that explains how to add or change your own response headers. Check what your published app actually sends with the curl command below.

Vercel

Vercel reads custom headers from vercel.json in the project root:

vercel.json
{
  "$schema": "https://openapi.vercel.sh/vercel.json",
  "headers": [
    {
      "source": "/(.*)",
      "headers": [
        { "key": "Strict-Transport-Security", "value": "max-age=63072000; includeSubDomains" },
        { "key": "X-Content-Type-Options", "value": "nosniff" },
        { "key": "Referrer-Policy", "value": "strict-origin-when-cross-origin" },
        { "key": "Permissions-Policy", "value": "geolocation=(), camera=(), microphone=()" },
        { "key": "X-Frame-Options", "value": "DENY" }
      ]
    }
  ]
}

Add the CSP as one more entry once you have tested it in report-only mode.

Netlify and Cloudflare Pages

Both read a plain-text file called _headers from the folder that gets deployed. In a Vite project, put it in public/: Vite copies files from public/ into the root of dist unchanged.

public/_headers
/*
  Strict-Transport-Security: max-age=63072000; includeSubDomains
  X-Content-Type-Options: nosniff
  Referrer-Policy: strict-origin-when-cross-origin
  Permissions-Policy: geolocation=(), camera=(), microphone=()
  X-Frame-Options: DENY

Each path starts a line; its headers follow, indented. Details from the hosts’ docs:

  • Netlify also accepts the same rules as [[headers]] blocks in netlify.toml. Custom headers there do not apply to responses from Netlify Functions, Edge Functions or proxied content; those must set their own headers.
  • Cloudflare Pages allows up to 100 header rules and 2,000 characters per line, which matters for long CSPs. Headers from _headers are not applied to responses generated by Pages Functions.

Check what you actually send

Your config file is a plan; the response is the truth. Run this against your own deployed app:

Show the response headers of your app
curl -sI https://your-app.example/ | grep -iE "content-security|strict-transport|x-content-type|referrer-policy|permissions-policy|x-frame-options"

Every header from the table should appear once. Also check one deep link such as https://your-app.example/settings: single-page apps serve index.html for every route through a rewrite, and the headers should be there too.

A fix prompt for your AI builder

Add security headers for a Vite SPA

This is a Vite single-page app hosted on [Vercel | Netlify | Cloudflare Pages]. Add security response headers for all paths:

  • Strict-Transport-Security: max-age=63072000; includeSubDomains
  • X-Content-Type-Options: nosniff
  • Referrer-Policy: strict-origin-when-cross-origin
  • Permissions-Policy: geolocation=(), camera=(), microphone=() (keep any feature the app really uses)
  • X-Frame-Options: DENY
  • Content-Security-Policy-Report-Only with default-src ‘self’; script-src ‘self’; style-src ‘self’ ‘unsafe-inline’; img-src ‘self’ data: https:; font-src ‘self’ data:; connect-src ‘self’ plus our Supabase URL (https and wss) and every other API the frontend calls; frame-ancestors ‘none’; base-uri ‘self’; form-action ‘self’; object-src ‘none’.

Use vercel.json on Vercel, or public/_headers on Netlify and Cloudflare Pages. Do not add X-XSS-Protection. List every external domain you added to connect-src and why. Do not switch the CSP to enforcing mode.

Checklist

Security headers checklist

0/9 done

Your progress is saved in this browser only.

Frequently asked questions

Can I set security headers in Vite’s config?

Not for production. Vite builds static files; whoever serves them decides the response headers. The server options in vite.config only affect the local dev server. Set production headers in your host’s configuration, such as vercel.json or a _headers file.

Can I put the CSP in a meta tag instead?

Partly. MDN notes that a CSP in a meta element works for simple single-page apps but does not support every feature: frame-ancestors and report-only mode only work as real HTTP headers. HSTS, nosniff and the other headers in this guide can’t be set by a meta tag at all.

Will a strict CSP break my Lovable or Vite app?

It can, which is why you start with Content-Security-Policy-Report-Only. The browser then reports what would have been blocked without blocking it. Fix the policy until the console stays clean, then switch the header name to Content-Security-Policy.

Do I still need X-Frame-Options?

OWASP recommends the CSP frame-ancestors directive, which replaces X-Frame-Options in browsers that support it. Sending X-Frame-Options: DENY as well costs nothing and covers older browsers and the time while your CSP is still in report-only mode.

Sources

  1. 1.Content Security Policy (CSP) — MDN Web Docs
  2. 2.Strict-Transport-Security — MDN Web Docs
  3. 3.HTTP Security Response Headers Cheat Sheet — OWASP Cheat Sheet Series
  4. 4.Features: Content Security Policy — Vite Docs
  5. 5.How Lovable hosts your app — Lovable Docs
  6. 6.Static configuration with vercel.json: headers — Vercel Docs
  7. 7.Custom headers — Netlify Docs
  8. 8.Headers — Cloudflare Docs

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt