function EntityQueryTest::testCaseInsensitiveInWithAssociativeArrayKeys

Same name and namespace in other branches
  1. main core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php \Drupal\KernelTests\Core\Entity\EntityQueryTest::testCaseInsensitiveInWithAssociativeArrayKeys()

Tests that array keys cannot inject SQL.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php, line 1195

Class

EntityQueryTest
Tests Entity Query functionality.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testCaseInsensitiveInWithAssociativeArrayKeys() : void {
  $bundle = $this->randomMachineName();
  EntityTestHelper::createBundle($bundle, entity_type: 'entity_test_mulrev');
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_ci',
    'entity_type' => 'entity_test_mulrev',
    'type' => 'string',
    'cardinality' => 1,
    'translatable' => FALSE,
    'settings' => [
      'case_sensitive' => FALSE,
    ],
  ]);
  $field_storage->save();
  FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $bundle,
  ])->save();
  $entity = EntityTestMulRev::create([
    'type' => $bundle,
    'name' => $this->randomMachineName(),
    'langcode' => 'en',
    'field_ci' => 'Needle',
  ]);
  $entity->save();
  $result = $this->storage
    ->getQuery()
    ->accessCheck(FALSE)
    ->condition('field_ci', [
    "evil) OR 1=1 --" => 'needle',
    "x'); DROP TABLE {entity_test_mulrev} --" => 'NEEDLE',
  ], 'IN')
    ->execute();
  $this->assertCount(1, $result);
  $this->assertSame([
    (string) $entity->getRevisionId() => (string) $entity->id(),
  ], $result);
  // Verify the table still exists.
  $second = $this->storage
    ->getQuery()
    ->accessCheck(FALSE)
    ->condition('field_ci', [
    'needle',
  ], 'IN')
    ->execute();
  $this->assertCount(1, $second);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.