CRSF tokens are generated using SecureRandom class (maybe later a JWT with a "time out").
They are stored in the user sessions (for AJAX calls and unauthenticated HTTP calls) or OFBiz UtilCache (for authenticated HTTP calls), and verified during POST request.
- In controllers a new csrf-token attribute is added to the security tag to exempt or force CSRF token check.
- In Widget Forms a hidden token field is auto-generated.
- In FTL form a CSRF token is passed through <@ofbizUrl> to automatise the change. Using <@ofbizUrl> macro to generate the CSRF token means there is no need to manually add the CSRF token field to each form in the ftl files. It will save time for users doing custom implementation and maintenance. While there is CSRF token in the form URL, the token is invalidated during form submission. So it's unique and harmless even though the CSRF token of the form submission is shown in the browser address bar.
- For Ajax calls an ajaxPrefilter function (observer on DOM ready) is added through OfbizUtil.js (itself called at start in decorators and such)
- The html metadata is storing the csrf token used by JQuery AJAX. This token will not change to another value after it is consumed
- Csrf tokens for the user are removed from the UtilCache when the user logs out or session invalidated.
The general rule are as follows:
- RequestMap configured with 'get' method will be exempted from CSRF token check.
- RequestMap configured with 'post' or 'all' method will be subjected to CSRF token check. (Note there are discussions that RequestMap with ‘all’ method should also not be subjected to CSRF token check. This will be done after ensuring a separate uri is used when posting changes.)
- "main" request URIs are exempted from CSRF token check.
- Setting csrf-token to false or true on the Request Map will override the general rules above.
- Invalidate authenticated user session when CSRF token check fails.
- Configure the general rules in a Service method (which will be run inside the constructor of RequestMap class) when determining the final securityCsrfToken value.