Unverified Commit 5a2c58c4 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3076259 by oknate, ThomasDik, phenaproxima: Media library does not...

Issue #3076259 by oknate, ThomasDik, phenaproxima: Media library does not enforce order which can lead to different hashes
parent 5b9bb165
Loading
Loading
Loading
Loading
+9 −3
Changes for core/modules/media_library/src/MediaLibraryState.php: 9 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -179,13 +179,19 @@ protected function validateRequiredParameters($opener_id, array $allowed_media_t
   */
  public function getHash() {
    // Create a hash from the required state parameters and the serialized
    // optional opener-specific parameters.
    // optional opener-specific parameters. Sort the allowed types and
    // opener parameters so that differences in order do not result in
    // different hashes.
    $allowed_media_type_ids = array_values($this->getAllowedTypeIds());
    sort($allowed_media_type_ids);
    $opener_parameters = $this->getOpenerParameters();
    ksort($opener_parameters);
    $hash = implode(':', [
      $this->getOpenerId(),
      implode(':', $this->getAllowedTypeIds()),
      implode(':', $allowed_media_type_ids),
      $this->getSelectedTypeId(),
      $this->getAvailableSlots(),
      serialize($this->getOpenerParameters()),
      serialize($opener_parameters),
    ]);

    return Crypt::hmacBase64($hash, \Drupal::service('private_key')->get() . Settings::getHashSalt());
+24 −0
Changes for core/modules/media_library/tests/src/Kernel/MediaLibraryStateTest.php: 24 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -364,4 +364,28 @@ public function testOpenerParameters() {
    $this->assertSame(['foo' => 'baz'], $state->getOpenerParameters());
  }

  /**
   * Test that hash is unaffected by allowed media type order.
   */
  public function testHashUnaffectedByMediaTypeOrder() {
    $state1 = MediaLibraryState::create('test', ['file', 'image'], 'image', 2);
    $state2 = MediaLibraryState::create('test', ['image', 'file'], 'image', 2);
    $this->assertSame($state1->getHash(), $state2->getHash());
  }

  /**
   * Test that hash is unaffected by opener parameter order.
   */
  public function testHashUnaffectedByOpenerParamOrder() {
    $state1 = MediaLibraryState::create('test', ['file'], 'file', -1, [
      'foo' => 'baz',
      'baz' => 'foo',
    ]);
    $state2 = MediaLibraryState::create('test', ['file'], 'file', -1, [
      'baz' => 'foo',
      'foo' => 'baz',
    ]);
    $this->assertSame($state1->getHash(), $state2->getHash());
  }

}