Skip to content

Commit c7c7c54

Browse files
robertSt7dvesh3
authored andcommitted
Fix: csrf
1 parent e53f4b7 commit c7c7c54

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/Controller/CartController.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\HttpFoundation\RedirectResponse;
2828
use Symfony\Component\HttpFoundation\Request;
2929
use Symfony\Component\HttpFoundation\Response;
30+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
3031
use Symfony\Component\Routing\Annotation\Route;
3132

3233
class CartController extends FrontendController
@@ -105,15 +106,23 @@ public function cartListingAction(Request $request, BreadcrumbHelperService $bre
105106
$cart = $this->getCart();
106107

107108
if ($request->getMethod() == Request::METHOD_POST) {
109+
if (!$this->isCsrfTokenValid('cartListing', $request->get('_csrf_token'))) {
110+
throw new AccessDeniedHttpException('Invalid request');
111+
}
112+
108113
$items = $request->get('items');
109114

110115
foreach ($items as $itemKey => $quantity) {
116+
if (!is_numeric($quantity)) {
117+
continue;
118+
}
119+
111120
if ($cart->getItemCount() > 99) {
112121
break;
113122
}
114123
$product = AbstractProduct::getById($itemKey);
115124
if ($product instanceof CheckoutableInterface) {
116-
$cart->updateItem($itemKey, $product, $quantity, true);
125+
$cart->updateItem($itemKey, $product, floor($quantity), true);
117126
}
118127
}
119128
$cart->save();

templates/cart/cart_listing.html.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<h4 class="mb-3">{{ 'cart.title' | trans }}</h4>
6464
<div class="card shopping-cart">
6565
<form method="post">
66+
<input type="hidden" name="_csrf_token" value="{{ csrf_token('cartListing') }}">
6667
<div class="card-body">
6768

6869
{% for item in cart.items %}

0 commit comments

Comments
 (0)