originMiddleware()
Rejects requests that change data when they come from another site. It reads the Sec-Fetch-Site header that browsers send, and falls back to Origin for browsers that do not send it yet. There is no token to issue, store, or send back, so client code needs no changes.
Type Signature
function originMiddleware(config?: OriginConfig): ActionMiddlewareParameters
interface OriginConfig {
/** Extra origins to trust, compared without case or a trailing slash, for example 'https://admin.example.com' */
allowedOrigins?: string[]
/** Reject mutations that carry neither Sec-Fetch-Site nor Origin. Default: false */
requireOrigin?: boolean
/** Error message. Default: 'Cross-origin request rejected' */
message?: string
}| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
allowedOrigins | string[] | No | [] | Origins that may call the action from another site |
requireOrigin | boolean | No | false | Reject mutations without either header |
message | string | No | 'Cross-origin request rejected' | Error message for rejected requests |
Behavior
GET, HEAD, and OPTIONS pass without a check. For POST, PUT, PATCH, and DELETE:
Sec-Fetch-Site: same-originpasses, and so doesnone, which the browser sends when the user started the request, for example from a bookmark.- An
Originlisted inallowedOriginspasses. - Without
Sec-Fetch-Site, anOriginwhose host matches the request host passes. This covers browsers that do not send Fetch Metadata. - Everything else that names a source is rejected: another
Origin,Origin: null(sandboxed frames, some redirects), a malformedOrigin, andSec-Fetch-Sitevaluessame-siteandcross-site. Across-siterequest is rejected even when itsOriginhost matches, such as anhttppage posting to thehttpssite. - A request with neither header passes, because browsers always send one of them on a cross-site mutation. Calls from curl or another server look like this. Set
requireOrigin: trueto reject them.
Rejected requests fail with CSRF_ERROR and status 403, the same code csrfMiddleware uses.
The request host comes from the Host header. X-Forwarded-Host is not trusted, because a client can set it. Behind a proxy that rewrites Host, list your public origin in allowedOrigins, so browsers without Fetch Metadata still pass.
Each check is constant time: a header read, a Set lookup for allowedOrigins, and for the host fallback one URL parse. The middleware keeps no state.
Only the request's own method counts. An action called with callAction from a GET route is not checked, because the outer request is not a mutation; expose mutations only through POST, PUT, PATCH, or DELETE routes.
Which CSRF middleware?
originMiddleware protects every browser that sends Sec-Fetch-Site (Chrome and Edge 76+, Firefox 90+, Safari 16.4+) and older browsers through Origin, with nothing to wire up on the client. Use csrfMiddleware when you also need a token, for example to protect form posts from browsers that send neither header.
Examples
Protect Every Mutation of a Client
// server/utils/clients.ts
export const actionClient = createActionClient()
.use(originMiddleware())// server/actions/todos/create.post.ts
export default actionClient
.schema(z.object({ title: z.string().min(1) }))
.action(async ({ input }) => db.todos.create(input))Allow a Second Site
export default defineAction({
middleware: [originMiddleware({ allowedOrigins: ['https://admin.example.com'] })],
input: z.object({ id: z.number() }),
handler: async ({ input }) => db.todos.delete(input.id),
})Browser-Only Actions
// Reject calls that do not come from a browser at all
const browserOnly = originMiddleware({ requireOrigin: true })Auto-Import
originMiddleware is auto-imported in the server/ directory when the module is installed.
See Also
- csrfMiddleware -- Token-based CSRF protection
- Security Guide -- Security best practices
- Testing -- Test the origin check with
runAction