Add CSRF Protection
Cross-site form posts to your API get a 403 before they reach a cookie-reading route.
Install
npm i @dapplelight/signet-server honoMount it before the routes
import { corsMiddleware } from "@dapplelight/signet-server/middleware/cors"
import { csrfMiddleware } from "@dapplelight/signet-server/middleware/csrf"
import { Hono } from "hono"
const app = new Hono()
app.use("*", corsMiddleware({ originEnvKeys: ["WEB_URL", "ADMIN_URL"] }))
app.use("/api/*", csrfMiddleware({ originEnvKeys: ["WEB_URL", "ADMIN_URL"] }))Set the cookie that needs it
import { app } from "../app"
app.get("/api/me", (c) => {
const opts = { cookieName: "app_session", secret: c.env.SESSION_SECRET, sameSite: "none" as const }
return c.json({ cookie: opts.cookieName })
})sameSite: "lax" already blocks cross-site posts. You need the middleware when the frontend and API are on different origins.
A form-style POST, PUT, PATCH or DELETE needs Sec-Fetch-Site: same-origin or a trusted Origin. Requests with a bearer header and JSON requests are not checked.