Sites without users
Every file in server/api is a public HTTP endpoint. On a site without accounts, that includes the route that sends your contact form by mail or the one that calls a paid search API with your key. Anyone can call them, from any website or from a script.
Set access: 'visitor' and those routes answer only to your own pages.
export default defineNuxtConfig({
modules: ['@kurashina-design/nuxt-bff-guard'],
bffGuard: { access: 'visitor' },
})
import { defineBffHandler } from '#bff-guard'
export default defineBffHandler(async (event) => {
await sendMail(await readValidatedBody(event, contactSchema))
return { ok: true }
})
No verifyUser, no secret, no Cookie. The build-time check works the same way: a route under server/api without defineBffHandler fails the build.
What a visitor route checks
Browsers add Sec-Fetch-Site to every request. A visitor route accepts only same-origin, the value a fetch() from your own pages carries, for every method, reads included.
| Request | Sec-Fetch-Site | Visitor route |
|---|---|---|
$fetch('/api/contact') from your page | same-origin | ✅ handler runs |
| JavaScript on another website | cross-site | 403 |
| A sibling subdomain | same-site | 403 |
| URL typed into the address bar | none | 403 |
curl without the header | absent | 403 |
The handler does not run for a refused request, so a refused call costs nothing upstream. All current browsers send Sec-Fetch-Site; very old ones, such as Safari before 16.4, do not and are refused.
What it cannot stop
A script can send Sec-Fetch-Site: same-origin itself. Without accounts there is nothing else to tell your page from a copy of its requests. For routes that send mail or spend money, add:
- a bot check such as Turnstile,
- rate limits,
- input validation and size limits.
Data loaded during SSR
Data that a page fetches during server-side rendering is not something to protect: anyone can open the page and read it. Mark such routes public.
A visitor route would behave inconsistently there. During SSR, useFetch forwards the headers of the page request, including Sec-Fetch-Site:
| How the visitor opened the page | SSR call to a visitor route |
|---|---|
| Reloaded a page of your site | passes (same-origin) |
| Came from a search engine or another site | 403 (cross-site) |
| Typed the URL or used a bookmark | 403 (none) |
import { defineBffHandler } from '#bff-guard'
// Loaded by pages/index.vue with useFetch during SSR.
export default defineBffHandler(() => listNews(), { access: 'public' })
In development, the guard logs every request a visitor route refuses, with its Sec-Fetch-Site, so such SSR calls show up while you test.
Mixing with user routes
access in nuxt.config.ts is only the default. Each route can choose:
defineBffHandler(handler, { access: 'user' }) // verifyUser must accept the request
defineBffHandler(handler, { access: 'visitor' }) // only fetches from your own pages
defineBffHandler(handler, { access: 'public' }) // anyone; writes still need your origin
A member app keeps the default 'user' and marks its marketing contact form 'visitor'. A site without accounts uses 'visitor' as the default and needs no verifyUser at all.