Authentication strategies
Grain Tag sends events without a credential. It uses the tenant alias only.
Read Authentication for the setup of each strategy.
Secret keys
A secret key belongs on a server. A browser bundle is public, so a key in it is public.- Read the key from an environment variable. Never write it in source.
- Never use
SERVER_SIDEin browser JavaScript, in a mobile app, or in any code that ships to a client. - Rotate the key on a schedule.
- Send every request over HTTPS.
- Keep the SDK up to date.
JWT and user IDs
WithJWT, the API compares the user ID on each event with the sub claim of the token. An
event with a different user ID is rejected. This stops one user from sending events as another.
getToken() on each call. Do not cache the token in your own code.
The auth library refreshes it.
User ID overrides
setProperty(properties, { userId }) writes properties for a user other than the current one.
Grain can block a source that writes properties for many distinct user IDs in a short time, or
that sends an unusual pattern of property updates.
Rules for overrides:
- Use
setUserId()for the current user. Use theuserIdoption only when the server acts for another user. - Make sure that the caller is allowed to change that user.
- With
JWT, the override must match the token subject.
Sensitive data
Never send a password, a government ID number, a card number, or a health record as an event property or a user property. Send a category, not the value.Allowed origins
The Grain API checks theOrigin header of a browser request against the allowed origins of
the tenant. Set them in the dashboard under Settings, Allowed Origins.
A request from an origin that is not on the list fails with a CORS error in the browser console.
Content Security Policy
If the site sends aContent-Security-Policy header, allow these hosts.
Configuration values and XSS
A remote configuration value is a string that anyone with dashboard access can edit. Do not insert it as HTML.{heroText} in JSX. element.innerHTML = heroText does not escape it.