Skip to content

fix: use createHash fallback for WebSocket accept digest#5334

Closed
cyphercodes wants to merge 1 commit into
nodejs:mainfrom
cyphercodes:fix-websocket-crypto-hash-fallback
Closed

fix: use createHash fallback for WebSocket accept digest#5334
cyphercodes wants to merge 1 commit into
nodejs:mainfrom
cyphercodes:fix-websocket-crypto-hash-fallback

Conversation

@cyphercodes
Copy link
Copy Markdown

Fixes #5333.

Summary

  • use feature detection for crypto.hash when validating the WebSocket accept digest
  • fall back to crypto.createHash when crypto.hash is unavailable
  • add a regression test that simulates crypto.hash being absent

Tests

  • npm run lint
  • npx borp --timeout 180000 -p "test/websocket/opening-handshake.js"
  • npm run test:websocket

Signed-off-by: cyphercodes <cyphercodes@users.noreply.github.com>
const digest = crypto.hash('sha1', keyValue + uid, 'base64')
const digest = typeof crypto.hash === 'function'
? crypto.hash('sha1', keyValue + uid, 'base64')
: crypto.createHash('sha1').update(keyValue + uid).digest('base64')
function createSha1Base64Hash (value) {
return typeof crypto.hash === 'function'
? crypto.hash('sha1', value, 'base64')
: crypto.createHash('sha1').update(value).digest('base64')
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.22%. Comparing base (d863782) to head (04f0d1e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5334   +/-   ##
=======================================
  Coverage   93.22%   93.22%           
=======================================
  Files         110      110           
  Lines       36603    36605    +2     
=======================================
+ Hits        34123    34126    +3     
+ Misses       2480     2479    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

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

this is fair but I'm having troubles to understand under which circumstances this can happen

@KhafraDev
Copy link
Copy Markdown
Member

@metcoder95 when the person is using node v20 (prior to v20.12.0), a version undici v8 does not support.

@KhafraDev KhafraDev closed this May 30, 2026
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.

TypeError: crypto.hash is not a function in connection.js

5 participants