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 #3448469: "Delete redirects defined in the spreadsheet" misses to remove leading slash normalization 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

Issue fork redirect-3448498

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Anybody created an issue. See original summary.

ameymudras made their first commit to this issue’s fork.

anybody’s picture

Status: Active » Needs review
berdir’s picture

Makes sense, should we make this an mb_strtolower() as it may contain special characters? Not sure if it matters, as long as the hash is consistent?

anybody’s picture

Thanks @Berdir, yes, to be sure I did that.

  • Berdir committed 1109f979 on 8.x-1.x authored by ameymudras
    Issue #3448498 by ameymudras, Anybody: Public method Redirect::...
berdir’s picture

Status: Needs review » Fixed

Merged.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.