Skip to content

Add yescrypt support for pw_hash function - #1458

Merged
span786 merged 5 commits into
puppetlabs:mainfrom
teluq-pbrideau:feat/yescrypt
Dec 4, 2026
Merged

span786 merged 5 commits into
puppetlabs:mainfrom
teluq-pbrideau:feat/yescrypt

Conversation

@teluq-pbrideau

@teluq-pbrideau teluq-pbrideau commented Nov 24, 2025 •

Copy link
Copy Markdown
Contributor

Summary

Add support for yescrypt hashing algorithm in the pw_hash function.

Additional Context

The underlying OS must support yescrypt. For exemple, it does not work on RockyLinux 8, because libxcrypt does not support it, but it does work in RockyLinux 9.

Related Issues (if any)

#1445

Checklist

  • 🟢 Spec tests.
  • 🟢 Acceptance tests.
  • Manually verified. (For example puppet apply)

@teluq-pbrideau
teluq-pbrideau marked this pull request as draft November 24, 2025 17:14
@teluq-pbrideau
teluq-pbrideau marked this pull request as ready for review November 24, 2025 18:00
@teluq-pbrideau

Copy link
Copy Markdown
Contributor Author

I deployed this change in my environment, had no problem in development, but now I observe crash of the puppetserver once in a while in a test environment when I execute an agent with this new hash function yescrypt, but not everytime, and only on this environment, everything run fine in the pre-production environment with the same function usage... Puppetserver restart without notice and journalctl -u puppetserver have this log:

puppetserver[32714]: #
puppetserver[32714]: # A fatal error has been detected by the Java Runtime Environment:
puppetserver[32714]: #
puppetserver[32714]: #  SIGSEGV (0xb) at pc=0x00007f2ecc59e83c, pid=32714, tid=2720104
puppetserver[32714]: #
puppetserver[32714]: # JRE version: OpenJDK Runtime Environment (Red_Hat-17.0.17.0.10-1) (17.0.17+10) (build 17.0.17+10-LTS)
puppetserver[32714]: # Java VM: OpenJDK 64-Bit Server VM (Red_Hat-17.0.17.0.10-1) (17.0.17+10-LTS, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, linux-amd64)
puppetserver[32714]: # Problematic frame:
puppetserver[32714]: # C  [libcrypt.so.1+0x1383c]
puppetserver[32714]: #
puppetserver[32714]: # Core dump will be written. Default location: Core dumps may be processed with "/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h" (or dumping to //core.32714)
puppetserver[32714]: #
puppetserver[32714]: # An error report file with more information is saved as:
puppetserver[32714]: # /var/log/puppetlabs/puppetserver/puppetserver_err_pid32714.log
puppetserver[32714]: #
puppetserver[32714]: # If you would like to submit a bug report, please visit:
puppetserver[32714]: #   https://access.redhat.com/support/cases/
puppetserver[32714]: # The crash happened outside the Java Virtual Machine in native code.
puppetserver[32714]: # See problematic frame for where to report the bug.
puppetserver[32714]: #

Not sure what the problem is, but it is clearly not ready for production use!

@teluq-pbrideau
teluq-pbrideau marked this pull request as draft December 5, 2025 17:30
@teluq-pbrideau

Copy link
Copy Markdown
Contributor Author

OK, problem was not with the function, but with what I was passing to the function. I was within an epp template, and was unwraping the Sensitive variable before passing to the pw_hash function:

pw_hash($password.unwrap,'yescrypt',"j9T$${stdlib::fqdn_rand_string(24,'',$username)}")

No problem since removing the unwrap:

pw_hash($password,'yescrypt',"j9T$${stdlib::fqdn_rand_string(24,'',$username)}")

I haven’t tested, but I suspect this is also a problem with other hash algorithms.

@teluq-pbrideau
teluq-pbrideau marked this pull request as ready for review December 9, 2025 13:10
@jdelzor

jdelzor commented Jan 22, 2026

Copy link
Copy Markdown

Hi,
Tested on my environment and it works fine.

Thanks for your job :⁠-⁠)

@jst-cyr

jst-cyr commented Jul 27, 2026

Copy link
Copy Markdown
Member

Thank you for the submission, @teluq-pbrideau and for the subsequent fixes! I'm going through old PRs and trying to find out if they are still issues for people. Are you still needing this PR reviewed and merged up?

@teluq-pbrideau

Copy link
Copy Markdown
Contributor Author

Yes, still need review and merge if possible! Thanks!

@jst-cyr

jst-cyr commented Jul 29, 2026

Copy link
Copy Markdown
Member

Thank you for the confirmation @teluq-pbrideau ! We'll keep it open and add to the list for review.

@jst-cyr

jst-cyr commented Jul 29, 2026

Copy link
Copy Markdown
Member

@teluq-pbrideau : I just ran the Spec tests and it seems that the Rubocop check failed with an error around the use of downcase vs campcase:

Error: Performance/Casecmp: Use args[1].casecmp('yescrypt').zero?instead ofargs[1].downcase == 'yescrypt'. (https://github.com/JuanitoFatas/fast-ruby#stringcasecmp-vs-stringdowncase---code)

@teluq-pbrideau

Copy link
Copy Markdown
Contributor Author

@jst-cyr Fixed and rebased. Thanks for moving this forward!

@jst-cyr

jst-cyr commented Jul 30, 2026

Copy link
Copy Markdown
Member

Thank you @teluq-pbrideau for the fix! I reran the CI here and it looks like a lot more errors showing in the CI run: https://github.com/puppetlabs/puppetlabs-stdlib/actions/runs/30543857265/job/90998927150?pr=1458

@teluq-pbrideau

Copy link
Copy Markdown
Contributor Author

@jst-cyr Oh yes, my change had the md5 and sha-* completely disabled. Wow, I did not catch this. This should be better now. Good thing there are these tests!

@jst-cyr

jst-cyr commented Aug 10, 2026

Copy link
Copy Markdown
Member

CI test PR created to run tests for this PR: #1487

@jst-cyr

jst-cyr commented Aug 10, 2026

Copy link
Copy Markdown
Member

I rebased the PR and ran acceptance tests. Results:
https://github.com/puppetlabs/puppetlabs-stdlib/actions/runs/31432862845/job/93600297275?pr=1487

All green! Still requires a code review by engineering, but tests look good.

@jst-cyr

jst-cyr commented Nov 10, 2026

Copy link
Copy Markdown
Member

Hey @teluq-pbrideau : I ran the spec tests against your latest changes, but they seem to have broken the unit tests. I didn't run acceptance tests because of that.

You can see the full failed CI run here: https://github.com/puppetlabs/puppetlabs-stdlib/actions/runs/41106770030/job/129569442382?pr=1458

This doesn't look like it's caused by your change. I'm seeing that we are having nightly failures too, starting a few days ago. I'm going to try to track down with engineering what's going on.

@teluq-pbrideau

Copy link
Copy Markdown
Contributor Author

@jst-cyr Thanks. I tried to run the tests on my side just to be sure, and as you thought, all seems OK:

$ bundle exec rake parallel_spec

[...]

2430 examples, 0 failures, 42 pendings

Took 13 seconds

@jst-cyr

jst-cyr commented Nov 21, 2026

Copy link
Copy Markdown
Member

Tests for this PR are being executed in #1506

@jst-cyr

jst-cyr commented Nov 21, 2026

Copy link
Copy Markdown
Member

✅ Acceptance Test Results: https://github.com/puppetlabs/puppetlabs-stdlib/actions/runs/44908951564?pr=1506

All acceptance tests are green, so testing looks good. Still needs engineering review on latest changes.

@teluq-pbrideau

Copy link
Copy Markdown
Contributor Author

Thanks @jst-cyr for all your help and patience on this PR!

Here is what I discovered while implementing this: Yescrypt is stricter on its salt than bcrypt. The salt must be an exact byte length, without residual bits...

For example:

irb(main):033> 'test'.crypt('$y$j9T$abcdef')
=> "*0"
irb(main):034> 'test'.crypt('$y$j9T$abcde.')
=> "$y$j9T$abcde.$KmlI6zl8oJet.TUEPNf0B/LLYVUZOafwyAjwj1fP.s0"

In this example,

String Decoded bytes Residual bits
abcdef e6 89 a6 ea 1010
abcde. e6 89 a6 2a 0000

A six-character encoded salt contains 36 bits. The salt abcde. is valid because its four unused trailing bits are zero, producing a four-byte salt. In contrast, abcdef has non-zero trailing bits and may therefore be rejected by crypt(3).

In the case of bcrypt, the salt is simply changed to be valid.

irb(main):036> 'test'.crypt('$2y$05$aaaaaaaaaaaaaaaaaaaaaa')
=> "$2y$05$aaaaaaaaaaaaaaaaaaaaaOu72CRq6n7CUMSAv4JhGJIQmAVPnpZoa"

Provided Salt: aaaaaaaaaaaaaaaaaaaaaa
Actual Salt: aaaaaaaaaaaaaaaaaaaaaO

Not sure if it is the ruby implementation that differ, or the actual yescrypt code...

What to do?

I actually raised an error to the user in the current implementation. But maybe I should normalize the string instead of raising an error to the user? I would like to have engineering input on that!

It is probably be cleaner than failing to the user when the salt is slightly invalid...

@teluq-pbrideau

teluq-pbrideau commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

With this new change, no error would be raised if a slightly invalid salt is created with fqdn_rand_string. It would simply be normalized, just like what bcrypt does silently in the background.

pw_hash($password,'yescrypt',"j9T$${stdlib::fqdn_rand_string(24,'','foo')}"),

@jst-cyr

jst-cyr commented Nov 23, 2026

Copy link
Copy Markdown
Member

Thanks for the update @teluq-pbrideau , and I agree that we should have engineering include these details in the review to confirm if this is the approach that they feel should be taken. Your reasoning seems sound to me, to align with bcrypt, but users of yescrypt might not expect that? Good for us to get some other input on it before going forward.

@span786 span786 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @teluq-pbrideau, this has come a long way. The rework from my last comment is all in: the yescrypt-specific probe is gone, it uses #1505's prefix check, the spec is gated on crypt_supports?, and yescrypt stays off the Commons Codec path on JRuby.

On your normalize-or-raise question: normalizing is the right call. It's deterministic, so the same input always gives the same hash and runs stay idempotent, and it matches what libxcrypt already does silently for bcrypt. I checked the logic against real libxcrypt (plain Ruby String#crypt in a ruby:3.2-slim container):

  • $y$j9T$abcdef → *0, $y$j9T$abcde. → valid, and $y$j9T$abcde1 (what your mask turns abcdef into) → valid.
  • $y$j9T$salt.salt → *0. The chopped salt.sal and salt.salt.sal/ produce exactly the hashes your spec expects, and so does plain salt.
  • $y$abc$salt → *0, which your code correctly reports as "invalid yescrypt parameters".

That matches libxcrypt's decode64, which needs at least one full byte per group and zero leftover bits.

Before this can merge:

  1. Rebase on main (blocking). pw_hash.rb now conflicts with main. af52256 (MODULES-12031, rubocop fixes under the template profile) added a trailing comma to the 'bcrypt-y' entry, so after the rebase the new 'yescrypt' entry needs a trailing comma too.
  2. Anchor the yescrypt salt pattern and stop stripping characters (blocking). See the inline comments. Right now the pattern is only anchored at the start, so anything can follow a valid prefix, and the normalizer then deletes the characters that don't belong. For example, j9T$ab#cd quietly hashes with salt abcd, and j9T$salt$junk with saltjunk. MD5 and SHA reject salts like these with "characters in salt must ...". yescrypt should reject them the same way instead of hashing with a salt the user didn't give. Keep the trailing-bits normalization; only invalid characters should be an error. A spec for this would be good, e.g. j9T$ab#cd raising characters in salt must match.
  3. CI on the new head. The normalization commit (aa4ff82) came after the green acceptance run in #1506, so once you've rebased we'll rerun the spec tests and acceptance through a CI test PR.

Non-blocking: REFERENCE.md isn't regenerated. It's fine to leave that to release prep, or to run bundle exec rake strings:generate:reference if you prefer.

Comment thread lib/puppet/parser/functions/pw_hash.rb Outdated
'bcrypt-x' => { prefix: '2x', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} },
'bcrypt-y' => { prefix: '2y', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} }
'bcrypt-y' => { prefix: '2y', salt: %r{^(0[4-9]|[12][0-9]|3[01])\$[./A-Za-z0-9]{22}} },
'yescrypt' => { prefix: 'y', salt: %r{^[./A-Za-z0-9]+\$[./A-Za-z0-9]{0,86}} }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern is only anchored at the start, and ^ also matches after a newline. So j9T$ab#cd and j9T$salt$junk both pass, and the gsub below then quietly removes the invalid characters. Anchoring the whole value makes yescrypt reject bad characters the way MD5 and SHA do (the default pattern is \A[a-zA-Z0-9./]+\z):

Suggested change
'yescrypt' => { prefix: 'y', salt: %r{^[./A-Za-z0-9]+\$[./A-Za-z0-9]{0,86}} }
'yescrypt' => { prefix: 'y', salt: %r{\A[./A-Za-z0-9]+\$[./A-Za-z0-9]{0,86}\z} },

The trailing comma is for the rubocop profile that's now on main (see the conflict with af52256).

Comment thread lib/puppet/parser/functions/pw_hash.rb Resolved
@span786 span786 self-assigned this Dec 1, 2026
@teluq-pbrideau
teluq-pbrideau force-pushed the feat/yescrypt branch 2 times, most recently from 5beb337 to aca6dc3 Compare December 2, 2026 18:44
teluq-pbrideau and others added 2 commits December 2, 2026 13:45
Co-authored-by: Saurabh Pandit <139841984+span786@users.noreply.github.com>
Co-authored-by: Saurabh Pandit <139841984+span786@users.noreply.github.com>
@teluq-pbrideau

Copy link
Copy Markdown
Contributor Author

@span786 Thanks for your inputs!

I fixed as suggested:

  • Anchor of salt pattern
  • Add test with invalid character (j9T$sa#lt) to raise an error
  • Cleanup the "normalization"
  • Updated the REFERENCE.md

@jst-cyr Ready for the tests

@jst-cyr

jst-cyr commented Dec 3, 2026

Copy link
Copy Markdown
Member

Tests for this PR are being executed in #1510

@jst-cyr

jst-cyr commented Dec 3, 2026

Copy link
Copy Markdown
Member

🛑 Acceptance Test Results: https://github.com/puppetlabs/puppetlabs-stdlib/actions/runs/47011715924?pr=1510

@teluq-pbrideau : The new changes seem to have contradicted the Rubocop style rules.

Error: Style/Lambda: Use the `-> *** ... ***` lambda literal syntax for all lambdas. ([https://rubystyle.guide#lambda-multi-line](https://rubystyle.guide/#lambda-multi-line))
Error: Metrics/BlockNesting: Avoid more than 3 levels of block nesting. ([https://rubystyle.guide#three-is-the-number-thou-shalt-count](https://rubystyle.guide/#three-is-the-number-thou-shalt-count))
Error: Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.
Error: Process completed with exit code 1.

@teluq-pbrideau

Copy link
Copy Markdown
Contributor Author

Ok, now both tests should pass, they at least pass locally

$ bundle exec rake spec SPEC=spec/functions/pw_hash_spec.rb
[...]
Finished in 0.66415 seconds (files took 0.66142 seconds to load)
54 examples, 0 failures

$ bundle exec rake rubocop
[...]
304 files inspected, no offenses detected

@jst-cyr Sorry, I should have tested them before.

@jst-cyr

jst-cyr commented Dec 3, 2026

Copy link
Copy Markdown
Member

No worries, @teluq-pbrideau ! I'll get those tests kicked off

@jst-cyr

jst-cyr commented Dec 3, 2026

Copy link
Copy Markdown
Member

Tests for this PR are being executed in #1511

@jst-cyr

jst-cyr commented Dec 3, 2026

Copy link
Copy Markdown
Member

✅ ** Acceptance Test Results:** https://github.com/puppetlabs/puppetlabs-stdlib/actions/runs/47217159408?pr=1511

We have a full green across the board for the latest changes. Good for next round of review.

@span786 span786 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @teluq-pbrideau, all three points from my last review are in:

  1. Rebased on main, and the 'yescrypt' entry has its trailing comma.
  2. The salt pattern is anchored with \A...\z and the gsub is gone, so invalid characters are now rejected (j9T$sa#lt raises characters in salt must match) and only the trailing-bits normalization remains. The bit masking matches libxcrypt's little-endian decode64, and the expected hashes in the spec match what I got from real libxcrypt last time.
  3. CI on the current head is green across the board in #1511 (spec and acceptance).

Approving. Two non-blocking notes, fine as a follow-up:

  • Error message on DES-only crypt(3) (inline). Where crypt(3) falls back to DES instead of returning NULL, as on macOS or Windows, a valid yescrypt call reports invalid yescrypt parameters instead of system does not support enhanced salts. Linux is unaffected: libxcrypt without yescrypt (e.g. Rocky 8's 4.1.1) returns NULL, so you correctly get the "enhanced salts" error there. A matching spec would be it { is_expected.to run.with_params('password', 'yescrypt', 'j9T$salt').and_raise_error(Puppet::ParseError, %r{system does not support enhanced salts}) } in the "weak String#crypt implementation" context.
  • REFERENCE.md: the regeneration also drops the stdlib::crc32 Note (its source has @note before @summary, which this puppet-strings version doesn't render). That's unrelated to this PR, and release prep regenerates the file anyway.

unless result&.start_with?("$#{hash_type[:prefix]}$")
if args[1].casecmp('yescrypt').zero?
yescrypt_probe = crypt.call('', '$y$j9T$')
raise Puppet::ParseError, 'system does not support enhanced salts' if yescrypt_probe.nil?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: a DES-only crypt(3) (macOS, Windows) doesn't return NULL here. It returns a DES hash (''.crypt('$y$j9T$') gives $yRDSVYBbSTik on macOS), so this guard passes and a valid call ends up raising invalid yescrypt parameters. Checking the prefix as well keeps the "enhanced salts" error for any system without yescrypt:

Suggested change
raise Puppet::ParseError, 'system does not support enhanced salts' if yescrypt_probe.nil?
raise Puppet::ParseError, 'system does not support enhanced salts' unless yescrypt_probe&.start_with?('$y$')

@span786
span786 merged commit 4e9cc10 into puppetlabs:main Dec 4, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants