Cert++
Close

CSRF protection for SFRA storefront forms

Key Takeaway

In SFRA, add csrfProtection.generateToken to the GET route that renders a form. Add csrfProtection.validateAjaxRequest to the POST route that receives the form's AJAX submission, so a request without a valid CSRF token is rejected.

Cross-site request forgery (CSRF) protection lets the server reject a storefront form submission that doesn't carry a valid token. In SFRA, you add the protection to controller routes as middleware. Without middleware, the dw.web.CSRFProtection script class generates and validates tokens directly.

Synchronizer tokens

B2C Commerce protects forms with synchronizer tokens, which are a best practice for protecting against CSRF attacks. The flow runs in this order:

  1. The server generates a unique token for the shopper's session.
  2. The token goes into the form.
  3. The form sends the token back when the shopper submits it.
  4. The server checks the token and can reject a submission that doesn't carry a valid one.

Each token is tied to the shopper's session. A token is valid for 60 minutes after the server creates it.

B2C Commerce Developer Study Guide

Checklist progress

0/237Learned

Given a requirement to accept, validate, and persist information from a storefront customer, modify the appearance of a form, add validation and cross-site request forgery (CSRF) protection, and use bindings to process fields

0/9

Prepare for the Exam

Play Today's Certle

Study Community

Ask questions and get the latest info from other B2C Commerce Developer studiers.