Skip to content

Commit 6359b3e

Browse files
committed
SA-CORE-2021-007 by samuel.mortenson, Wim Leers, greggles, xjm, larowlan, vijaycs85, Heine, effulgentsia, phenaproxima, mcdruid, nod_
1 parent 6b60b88 commit 6359b3e

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

modules/quickedit/js/models/EntityModel.es6.js

+3
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@
526526
options.success.call(entityModel);
527527
}
528528
};
529+
entitySaverAjax.options.headers = entitySaverAjax.options.headers || {};
530+
entitySaverAjax.options.headers['X-Drupal-Quickedit-CSRF-Token'] =
531+
drupalSettings.quickedit.csrf_token;
529532
// Trigger the AJAX request, which will will return the
530533
// quickeditEntitySaved AJAX command to which we then react.
531534
entitySaverAjax.execute();

modules/quickedit/js/models/EntityModel.js

+2
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@
243243
options.success.call(entityModel);
244244
}
245245
};
246+
entitySaverAjax.options.headers = entitySaverAjax.options.headers || {};
247+
entitySaverAjax.options.headers['X-Drupal-Quickedit-CSRF-Token'] = drupalSettings.quickedit.csrf_token;
246248

247249
entitySaverAjax.execute();
248250
},

modules/quickedit/quickedit.module

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function quickedit_page_attachments(array &$page) {
5353
return;
5454
}
5555

56+
$page['#attached']['drupalSettings']['quickedit']['csrf_token'] = \Drupal::csrfToken()->get('X-Drupal-Quickedit-CSRF-Token');
5657
$page['#attached']['library'][] = 'quickedit/quickedit';
5758
}
5859

modules/quickedit/src/QuickEditController.php

+30
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
use Drupal\Core\Entity\EntityRepositoryInterface;
77
use Drupal\Core\Form\FormState;
88
use Drupal\Core\Render\RendererInterface;
9+
use Drupal\Core\Session\AccountInterface;
910
use Drupal\Core\TempStore\PrivateTempStoreFactory;
1011
use Symfony\Component\DependencyInjection\ContainerInterface;
1112
use Symfony\Component\HttpFoundation\JsonResponse;
1213
use Symfony\Component\HttpFoundation\Request;
14+
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
1315
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1416
use Drupal\Core\Ajax\AjaxResponse;
1517
use Drupal\Core\Entity\EntityInterface;
@@ -165,6 +167,32 @@ public function metadata(Request $request) {
165167
return new JsonResponse($metadata);
166168
}
167169

170+
/**
171+
* Throws an AccessDeniedHttpException if the request fails CSRF validation.
172+
*
173+
* This is used instead of \Drupal\Core\Access\CsrfAccessCheck, in order to
174+
* allow access for anonymous users.
175+
*
176+
* @todo Refactor this to an access checker.
177+
*/
178+
private static function checkCsrf(Request $request, AccountInterface $account) {
179+
$header = 'X-Drupal-Quickedit-CSRF-Token';
180+
181+
if (!$request->headers->has($header)) {
182+
throw new AccessDeniedHttpException();
183+
}
184+
if ($account->isAnonymous()) {
185+
// For anonymous users, just the presence of the custom header is
186+
// sufficient protection.
187+
return;
188+
}
189+
// For authenticated users, validate the token value.
190+
$token = $request->headers->get($header);
191+
if (!\Drupal::csrfToken()->validate($token, $header)) {
192+
throw new AccessDeniedHttpException();
193+
}
194+
}
195+
168196
/**
169197
* Returns AJAX commands to load in-place editors' attachments.
170198
*
@@ -315,6 +343,8 @@ protected function renderField(EntityInterface $entity, $field_name, $langcode,
315343
* The Ajax response.
316344
*/
317345
public function entitySave(EntityInterface $entity) {
346+
self::checkCsrf(\Drupal::request(), \Drupal::currentUser());
347+
318348
// Take the entity from PrivateTempStore and save in entity storage.
319349
// fieldForm() ensures that the PrivateTempStore copy exists ahead.
320350
$tempstore = $this->tempStoreFactory->get('quickedit');

0 commit comments

Comments
 (0)