Security
What Session kit protects, what stays your backend's job, and where tokens live in each setup.
Your app holds tokens that act for the user, so you need to know who can read them and where they're sent. Session kit saves the tokens your backend issues and manages the session around them. It doesn't decide what a user may do.
This page shows what stays your backend's job, where the tokens live in each setup, how the session route refuses other sites, and where the access token goes.
Your backend decides what's allowed
Your backend must authenticate every request and authorize private operations. Route guards, screen guards and the user's fields in the client decide what to show, not what's allowed.
With server rendering, signIn seals what it's given into the cookie. If the
browser signs in with your backend and sends the tokens and the user to your
server, the cookie's user is what the browser sent, until a refresh or
updateUser replaces it. Use that user to show who is signed in. Let your
backend authorize requests with the access token, or check the tokens before
signIn.
Where tokens live
Each setup saves the tokens in a different place, and each place protects them differently:
- In a browser app,
webStorage()saves them inlocalStorageorsessionStorage. Any JavaScript on your origin can read them there, as it can read anything else in that storage. They never go in cookies, so the browser doesn't send them on its own. - With server rendering, they're sealed in an
encrypted
httpOnlycookie, which page scripts can't read. That includes the refresh token. Only the access token reaches page scripts, and only withclientCredentials: see The access token in the browser. - In an Expo app,
secureStorage()saves them in the iOS keychain, or encrypted with an Android Keystore key. Theexpo-secure-storeconfig plugin keeps the session out of Android backups. By default,secureStorage()also removes a session that an earlier install left in the iOS keychain (itsclearOnReinstalloption). See Reinstalls and Android backups.
To keep tokens off disk in a browser, memoryStorage() holds them in memory,
and the session ends when the page unloads.
Storage lists every store.
The session route
With server rendering, the browser's client calls a route in your app, the
session route, to sign out and to get the access token. In most setups, it
reads the session there too. sessionHandler from
@monarcode/session-kit/server answers it, through each framework's
sessionRoute.
The session route answers your own site's pages only, so another site can't sign the user out or ask for the access token. It makes two checks, and answers 403 to a request that fails either one:
- A
Sec-Fetch-Siteheader, when the request has one, must besame-origin. Browsers send it with requests to https and localhost sites. - On plain http, where browsers don't send that header, a
POSTmust have anOriginheader that matches the request's URL. AGETpasses: it changes nothing, and another site can't read the answer.
These checks stop other sites, not a script injected into your own pages. Such
a script passes them, so it can sign the user out, and with
clientCredentials, get the access token. Protect your pages against
cross-site scripting (XSS), for example with a Content Security Policy.
Check your own routes
Signing in stays your own server code. A route of your own that changes the session must refuse other sites too, wherever your framework doesn't check for you. Session kit doesn't export its check, so copy it into your app:
/**
* Whether a request comes from this site's own pages, by the same two checks
* as the session route.
*/
export function (: ): boolean {
const = ..("Sec-Fetch-Site");
if ( !== null) return === "same-origin";
// Plain http: let a GET through, so change the session on POST only.
if (. === "GET") return true;
return ..("Origin") === new (.).;
}Call it first in the route's handler or action, with the incoming Request.
When it returns false, answer 403 and change nothing. Change the session
only on a POST: on plain http, the check lets any GET through, as the
session route does.
Where tokens go
createAuthFetch and the effetch plugin
sessionBearer send the access token only to your API's origin:
- A request to another origin, or with a username or password in its URL, is
refused before anything is sent.
authFetchrejects withINVALID_OPTIONS. WithsessionBearer, the Effect dies with it, soEffect.catchdoesn't catch it. - A redirect isn't followed, so the token doesn't reach the server it points
to. On React Native before Expo SDK 58,
fetchfollows a redirect whatever it's told, so the redirected request has already gone out.authFetchstill refuses the response, andsessionBearerreturns it. Don't let your API redirect requests that carry a token.
Authenticated requests has the full list of what
authFetch refuses, and Effect and effetch
how sessionBearer differs.
Debug events leave out tokens unless you
turn on sensitive.
Keep tokens out of page data
Sessions, the auth state, route context and the loader results Session kit
makes hold no tokens: a Session has the user and its sessionId only.
Don't put tokens in loader results or hydrated page data yourself.
Next steps
- Server rendering: keep the tokens in an
httpOnlycookie, out of page scripts' reach. - Storage: choose where a browser or Expo app saves the session.
- Authenticated requests: send the access token to your API, and only there.