Skip to content

Fix missing CSRF protection across storefront controllers#740

Merged
KrzysztofPajak merged 3 commits into
developfrom
fix/storefront-csrf-protection
Jul 20, 2026
Merged

Fix missing CSRF protection across storefront controllers#740
KrzysztofPajak merged 3 commits into
developfrom
fix/storefront-csrf-protection

Conversation

@KrzysztofPajak

Copy link
Copy Markdown
Member

Summary

  • Apply [AutoValidateAntiforgeryToken] globally on BasePublicController (storefront), matching the convention already used by BaseAdminController / BaseVendorController / BaseStoreController. Previously only ~35 of ~70 POST actions in Grand.Web had the attribute applied manually, which is what the security report flagged as "Missing cross-site request forgery token validation" across many controllers.
  • Remove the now-redundant per-action [AutoValidateAntiforgeryToken] attributes (35 occurrences across 12 controllers) since the base controller covers them.
  • Render @Html.AntiForgeryToken() once in _Layout.cshtml / _LayoutPopup.cshtml (same pattern as _AdminLayout.cshtml), and attach that token as an X-CSRF-TOKEN header by default to all axios/jQuery AJAX requests in public.common.js. This was necessary because several existing AJAX call sites (add to cart, wishlist add/update, reservation date fill, push notification registration) posted without any CSRF token at all — enabling global validation without this fix would have broken those flows.

Test plan

  • dotnet build src/Web/Grand.Web/Grand.Web.csproj succeeds with 0 errors
  • Manually verify in a browser: add to cart, wishlist add/update, login/register/password recovery forms, and product reservation date picker all still work without 400 errors

Apply [AutoValidateAntiforgeryToken] globally on BasePublicController
(matching the existing Admin/Vendor/Store convention) instead of only
on a subset of POST actions, closing the CSRF gaps flagged in the
security report. Render the antiforgery token once in the storefront
layouts and attach it as a header to all axios/jQuery AJAX requests by
default so existing add-to-cart, wishlist, and reservation flows keep
working under the new global validation.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings July 19, 2026 16:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

KrzysztofPajak and others added 2 commits July 19, 2026 19:47
Theme.Modern has its own _Layout.cshtml (separate from the default
theme's), which didn't render @Html.AntiForgeryToken(). Since it
loads the same public.common.js/public.axios.js that now attach the
token as an X-CSRF-TOKEN header, the hidden token was never found on
pages using this theme, so AJAX requests (e.g. addproducttocart)
were rejected with 400 once AutoValidateAntiforgeryToken went live.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Neither variable is reassigned.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@KrzysztofPajak
KrzysztofPajak merged commit 9d19918 into develop Jul 20, 2026
6 of 8 checks passed
@KrzysztofPajak
KrzysztofPajak deleted the fix/storefront-csrf-protection branch July 20, 2026 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants