Problem/Motivation

#3573259: Prevent new expects($this->any()) in tests introduced spaze/phpstan-disallowed-calls as a dev dependency.

We can now leverage on the extension to disallow usage of uniqid(), md5(), sha1().

Proposed resolution

do it

Remaining tasks

decide if we want to do this

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3574204

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

mondrake created an issue. See original summary.

mondrake’s picture

mondrake’s picture

Status: Postponed » Active

Parent was committed, this is now actionable.

mondrake changed the visibility of the branch 3574204-disallow-usage-of to hidden.

mondrake’s picture

Status: Active » Needs review

Is this worth doing?

smustgrave’s picture

Do we have existing instances that need replaced? How often do they appear if it warrants a rule?

mondrake’s picture

Component: phpunit » base system

#7 the raw output of the PHPStan job tells where the functions are used. The error could be baselined in the MR, and this would then prevent more usage once committed.

mondrake’s picture

sivaji_ganesh_jojodae’s picture

Status: Needs review » Needs work

The pipeline has failed due to a PHP linting error.

smustgrave’s picture

If you read the comments it was being discussed not for code review

mondrake’s picture

Issue summary: View changes
Status: Needs work » Needs review

Yes please, let’s first discuss if this is ok to do.

longwave’s picture

I think this is a good idea for md5 and sha1, we should point users to sha256 or xxhash instead I guess? Not sure what to do about uniqid().

mondrake’s picture

#13 one of these maybe? #2972100-24: Remove usage of uniqid

There was some consensus on crypt::randomBytesBase64(16) afaics

idebr’s picture

#3307718: Implement xxHash for non-cryptographic use-cases suggests replacing md5/sha1 with xxHash alternatives

mondrake’s picture

Reflected latest comments in the MR - it now redirects sha1() and md5() to use sha256() or hash() with xxHash algo, and uniqid() to use Crypt:: randomBytesBase64().

Rabased and now added errors to the baseline.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Awesome to see this one come together. Replacements make sense and probably could spin up follow ups to do the replacements.

godotislate’s picture

Status: Reviewed & tested by the community » Needs work

NW for merge conflict in phpstan baseline.

mondrake’s picture

Status: Needs work » Reviewed & tested by the community

Merged with main and resolved a conflict.

poker10’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

Thanks for working on this. Added a comment to the MR, as I think it is not a good idea to link to the Drupal 7 documentation. Also think we should add a CR for this (a similar from the past https://www.drupal.org/node/2833433). Moving to NW for this.

mondrake’s picture

mondrake’s picture

Title: Disallow usage of uniqid(), md5(), sha1() » Disallow usage of uniqid(), md5(), sha1(), crc32() and hash() with weak algorithms
Issue tags: -Needs change record
mondrake’s picture

Status: Needs work » Needs review

Addressed @poker10 review, added a draft CR, added more disallows based on what I read. Thanks!

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Feedback and CR look good

catch’s picture

crc32 shouldn't use Crypt::hashBase64(), it should be replaced with xxHash, the other exclusions in the MR say this.

The errorTip links to the change record, but then the change record links back to the errorTip.

I am not sure we should duplicate documentation on e.g. https://xxhash.com/ but I think we probably need to say at least something like:

'Use a cryptographic hashing algorithm when appropriate. Use a fast, low collision, non-cryptographic algorithm when approprate (usually one of the xxHash variants), don't use weak cryptographic algorithms.'

Also while this will stop further cases being added to core, I'm a little bit concerned about committing it with the core usages not converted yet, because when we (hopefully) set a good example in core that's an easy place to follow and refer to.

catch’s picture

Status: Reviewed & tested by the community » Needs work
mondrake’s picture

Status: Needs work » Postponed
Related issues: +#3581442: Replace usage of uniqid() in the Database system
mondrake’s picture

Status: Postponed » Active
mondrake’s picture

Assigned: Unassigned » mondrake
Status: Active » Needs work
mondrake’s picture

Status: Needs work » Needs review

Addressed feedback and adjusted uniqid() to be replaced by bin2hex(random_bytes()) as it was finally done in #3581442: Replace usage of uniqid() in the Database system. Rebased and updated baseline.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new91 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Needs work » Needs review

rebased

smustgrave’s picture

@catch changes look good?

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Going on a limb and believe feedback for this one has been addressed.

longwave’s picture

Status: Reviewed & tested by the community » Needs review
+$ignoreErrors[] = [
+	'message' => '#^Calling uniqid\\(\\) is forbidden, use bin2hex\\(random_bytes\\(\\)\\) instead\\.$#',
+	'identifier' => 'disallowed.function',
+	'count' => 1,
+	'path' => __DIR__ . '/tests/Drupal/FunctionalTests/Core/Recipe/RecipeCommandTest.php',
+];

This is in RecipeTestTrait, so any new recipe test will need this adding to the baseline.

Wondering if we should allow uniqid (or any of these functions) in tests? Or if we should just fix this case here?

mondrake’s picture

#35 changed the test.

IMHO if we determine to disallow, we should do it consistently in both runtime and test code.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

https://git.drupalcode.org/project/drupal/-/merge_requests/15081/diffs?c... appears to have fixed #35 so going to mark it again, fingers crossed

catch’s picture

Status: Reviewed & tested by the community » Needs work

Discussed this with @alexpott in slack.

The various hash algorithms switching to xxHash or an actual cryptographic hash when needed seems fine.

However:

uniqid\\(\\) is forbidden, use bin2hex\\(random_bytes\\(\\)\\)

This is not a straight replacement and requires thinking about what to send to random_bytes() etc., we also use uniqid in some places where the potential for collisions is really not an issue like creating a directory name for almost one-off operations.

Alex also pointed out that https://wiki.php.net/rfc/deprecate-uniqid is stalled. If it was unstalled, we wouldn't need this at all because it'd be a PHP deprecation, but with it stalled it's hard to tell what any eventual direction might be.

So I think we should split uniqid() out to its own issue and only cover hash algorithms here.

mondrake’s picture

Title: Disallow usage of uniqid(), md5(), sha1(), crc32() and hash() with weak algorithms » Disallow usage of md5(), sha1(), crc32() and hash() with weak algorithms
mondrake’s picture

Status: Needs work » Needs review

Removed uniqid from disallow list.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new547 bytes

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

mondrake’s picture

Status: Needs work » Needs review

rebased and updated CR

dcam’s picture

@mondrake given #38 what are your thoughts on undoing the change to RecipeTestTrait? I don't know if it matters or not.

mondrake’s picture

#43 sure, missed that in all the turns here.

dcam’s picture

Status: Needs review » Reviewed & tested by the community

Understandable. To be honest, I felt kind of bad asking about that after all of the changes and updates this has been through.

The changes related to uniqid() have been removed. This can go back to RTBC.

mondrake’s picture

No worries!

  • catch committed 0db7b644 on main
    task: #3574204 Disallow usage of md5(), sha1(), crc32() and hash() with...
catch’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +Needs followup

I added this to the CR:

For cryptographic hashing, see the <a href="https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Component%21Utility%21Crypt.php/class/Crypt/main">Crypt</a> class for common use-cases.

Hopefully no-one is still using md5() or sha1() for cryptographic purposes but just in case.

Committed/pushed to main, thanks! This doesn't cherry-pick to 11.x and not sure we need to necessarily, it'll prevent new core usages in all branches.

Tagging for follow-ups to remove the baseline entries we're adding here.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

idebr’s picture

mondrake’s picture

Also, #2972100: Remove usage of uniqid is now a sibling of this, and will focus exclusively on uniqid(). Re #38: it's not just about upstream deprecation decisions IMHO - uniqid() is also performing much worse than alternatives as proven in the related issue.

Status: Fixed » Closed (fixed)

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