lovable
How to add security headers to a Lovable app (when Ask AI does not work)
Why vercel.json from chat often never reaches *.lovable.app, how to verify with curl, and concrete Vercel, Netlify, and Cloudflare fixes for CSP and clickjacking—then re-scan with the free grader.
Paste your production URL into the free Vibe Code Health grader for a Semrush-style score on secrets, headers, TLS, and common leaks—then come back to this guide for the fixes.
You pasted this into Lovable (or Bolt):
“Add security headers: CSP, X-Frame-Options, HSTS…”
The chat said it created vercel.json or _headers. You redeployed. You feel safer.
Then you run:
curl -sI https://your-app.lovable.app | grep -iE 'content-security|x-frame|strict-transport|x-content|referrer|permissions'
…and get almost nothing beyond Cloudflare’s defaults. Missing Content-Security-Policy and clickjacking protection is why so many Lovable apps land around a C on Vibe Code Health—not because you skipped a chat prompt, but because the host that serves your HTML never applied the file the AI wrote.
This guide is only about that gap: how to actually get security headers onto a Lovable (or similar Vite) app when “Ask AI” is not enough. It is not a full header encyclopedia (that lives in Security headers for vibe coders) and not a general Lovable checklist (Is my Lovable app secure?).
What security headers are (30 seconds)
Security headers are short instructions your server sends with the HTML response. The browser reads them before deciding whether scripts may run, whether another site may iframe you, or whether HTTP is allowed next time.
They are not something React “renders.” Vite’s build output is static files. Headers come from the host or CDN in front of those files—Vercel, Netlify, Cloudflare, and similar—not from a component.
MDN’s overview of HTTP headers is the right mental model: response headers are metadata about the message, applied by the server layer (MDN: HTTP headers).
Why the AI “fix” often lies to you
Lovable’s public docs emphasize HTTPS, previews, custom domains, and exporting or deploying the frontend elsewhere. What they do not document is a project switch that lets *.lovable.app pick up a random vercel.json from chat and start sending Content-Security-Policy.
So this loop is common:
- AI writes
vercel.json/public/_headers/ a Netlify file. - You stay on Lovable-managed hosting.
- That host never reads those files.
curl -sIon the URL you share still lacks CSP andX-Frame-Options.- Graders (including ours) still flag
HDR_CSP_MISSINGandHDR_FRAME_MISSING.
Bolt, Replit static deploys, and other builders have the same class of problem: headers are a property of whoever answers HTTP, not of whoever generated the JSX.
Step 1 — Measure the URL users actually hit
Only probe sites you own. Against your published URL (custom domain if you have one—not only a preview):
curl -sI https://your-app.lovable.app/ | grep -iE \
'strict-transport|x-content-type|x-frame|content-security|referrer-policy|permissions-policy|access-control'
Also check a deep route and a JS asset; some stacks differ by path:
curl -sI https://your-app.lovable.app/dashboard | grep -iE 'content-security|x-frame'
curl -sI https://your-app.lovable.app/assets/index.js | grep -iE 'strict-transport|x-frame'
| What you see | What it means |
|---|---|
| CSP + frame + nosniff lines present | Something upstream already sets them—note exact values before changing anything |
| HSTS / nosniff only (common on Cloudflare-fronted Lovable) | Platform hygiene is partial; you still need CSP + framing protection |
| Almost nothing useful | You need another layer that can set response headers |
Then paste the same URL into the free Vibe Code Health grader. Missing CSP and framing protection currently cap typical unhardened vibe apps around a C on rubric v2.1—see /learn for how grades work.
The five headers beginners should ship first
Keep this short. Details and copy-paste Next.js snippets are in the security headers guide.
- Content-Security-Policy (CSP) — allow-list for scripts, styles, images, connections. Hardens XSS. Start with
default-src 'self'and add only what you need (MDN: Content-Security-Policy). - Clickjacking protection —
X-Frame-Options: DENYand/or CSPframe-ancestors 'none'(OWASP Clickjacking Defense Cheat Sheet). - X-Content-Type-Options: nosniff — stop MIME sniffing.
- Referrer-Policy: strict-origin-when-cross-origin — limit URL leakage to third parties.
- Strict-Transport-Security — after HTTPS works everywhere, tell browsers to stick to HTTPS (MDN: Strict-Transport-Security).
Optional but easy: Permissions-Policy: camera=(), microphone=(), geolocation=().
Do not “fix” security by adding Access-Control-Allow-Origin: * on HTML. That is CORS, not CSP, and it is usually unwanted on documents.
Fix A — Deploy the Vite build somewhere that reads header config
Lovable apps are typically a Vite SPA: npm run build → dist/. Lovable’s own external-deployment path is “you take the frontend to Vercel, Netlify, Cloudflare Pages, …” and then you own production config.
Vercel — vercel.json at the repo root
{
"headers": [
{
"source": "/(.*)",
"headers": [
{ "key": "X-Content-Type-Options", "value": "nosniff" },
{ "key": "X-Frame-Options", "value": "DENY" },
{ "key": "Referrer-Policy", "value": "strict-origin-when-cross-origin" },
{
"key": "Permissions-Policy",
"value": "camera=(), microphone=(), geolocation=()"
},
{
"key": "Strict-Transport-Security",
"value": "max-age=63072000; includeSubDomains; preload"
},
{
"key": "Content-Security-Policy",
"value": "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self' https://*.supabase.co wss://*.supabase.co; frame-ancestors 'none'; base-uri 'self'; form-action 'self'"
}
]
}
],
"rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
}
Import the GitHub repo on Vercel, set build npm run build, output dist, point your custom domain there, then curl -sI that hostname.
Netlify — public/_headers (or _headers in the publish directory)
/*
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
Strict-Transport-Security: max-age=63072000; includeSubDomains; preload
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; connect-src 'self' https://*.supabase.co wss://*.supabase.co; frame-ancestors 'none'; base-uri 'self'; form-action 'self'
Netlify documents _headers as the static way to attach response headers (Netlify: custom headers).
Tune connect-src / img-src for your Supabase project and any real CDNs you use. If the page goes blank after enabling CSP, open DevTools → Console for blocked URLs, then loosen one directive at a time. Prefer starting with Content-Security-Policy-Report-Only while you learn (MDN: CSP Report-Only).
Fix B — Keep Lovable origin, put Cloudflare in front of the custom domain
If DNS for your custom domain is already on Cloudflare (orange-cloud proxy), you can add Response Header Transform Rules so every response gains the security headers on the way to the browser (Cloudflare Transform Rules).
Checklist:
- Proxy the hostname users type (not only the
lovable.apporigin). - Add rules that set (not only “if missing”) CSP,
X-Frame-Options, nosniff, Referrer-Policy, Permissions-Policy, and HSTS as appropriate. - Re-check with
curl -sI https://your.custom.domain/— graders and users see the CDN hostname.
Exact dashboard clicks change; follow Cloudflare’s current “modify response header” docs rather than a screenshot from a blog post.
The meta-tag trap
People ask the AI for a <meta http-equiv="Content-Security-Policy" …> in index.html. That is a partial backup:
- It can limit scripts/styles/images in many cases.
- Per MDN, the meta form does not support
frame-ancestors(or some reporting features), so clickjacking protection still needs a real HTTP header (MDN: CSP delivery). - HSTS,
X-Frame-Options, andX-Content-Type-Optionsare not something you can fully replace with a meta tag.
Use a meta CSP only as a seatbelt while you finish Fix A or B—not as the finish line.
After you deploy — prove it
curl -sIthe production hostname again.- Re-run Vibe Code Health and confirm
HDR_CSP_MISSING/HDR_FRAME_MISSINGclear (or improve). - Click through login, Supabase calls, and images—CSP mistakes show up as blank widgets, not always as a red error page.
- If you use Supabase, remember headers are not RLS. Headers do not replace Row Level Security or keeping
service_roleoff the client (Supabase keys guide).
Quick FAQ
Will Lovable’s AI ever “just set headers” on *.lovable.app?
Maybe someday via a host feature—but today you should verify with curl, not with chat confidence.
Is a C grade “hacked”?
No. It usually means “browser defenses are thin.” Fix headers before you share widely; still treat leaked secrets as emergencies (API keys in frontend code).
Where do I read plain-English definitions of CSP and HSTS?
The glossary on /learn.
Ship the headers on the hostname people open. Then grade again—and keep the AI prompt as a helper for the config file, not as proof the internet received it.