Public method Redirect::generateHash() should unify path before generating the hash

Created on 21 May 2024, 7 months ago
Updated 24 August 2024, 4 months ago

Problem/Motivation

Redirect::generateHash() is a public method and can therefor be used by extending modules, which makes sense and is helpful.

On the other hand, it should (eventually) ensure consistent handling.

  /**
   * Generates a unique hash for identification purposes.
   *
   * @param string $source_path
   *   Source path of the redirect.
   * @param array $source_query
   *   Source query as an array.
   * @param string $language
   *   Redirect language.
   *
   * @return string
   *   Base 64 hash.
   */
  public static function generateHash($source_path, array $source_query, $language) {
    $hash = [
      'source' => mb_strtolower($source_path),
      'language' => $language,
    ];

    if (!empty($source_query)) {
      $hash['source_query'] = $source_query;
    }
    redirect_sort_recursive($hash, 'ksort');
    return Crypt::hashBase64(serialize($hash));
  }

In cases like 🐛 "Delete redirects defined in the spreadsheet" option without functionality? Active a project could provide $source_path differently, with other or no preprocessing that currently happens internally in the Redirect module, like:
$this->redirect_source->path = rtrim($this->redirect_source->path, '/'); in the preSave() method.

I guess the method should ensure that the hash is always created on the same normalization of the path as it is saved in the database. For redirect entities that means for example:

  • Without leading slash for internal URLs
  • Without trailings slash
  • Lowercase?
  • ...?

so it can be used more safely and unified by the outside world.

Steps to reproduce

See above

Proposed resolution

Implement consistent handling and normalization for the parameters provided.

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Fixed

Version

1.0

Component

Code

Created by

🇩🇪Germany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024