Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix loading websockets when node is built w/ --without-ssl #2282

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/websocket/connection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const { randomBytes, createHash } = require('crypto')
const diagnosticsChannel = require('diagnostics_channel')
const { uid, states } = require('./constants')
const {
Expand All @@ -22,6 +21,14 @@
channels.close = diagnosticsChannel.channel('undici:websocket:close')
channels.socketError = diagnosticsChannel.channel('undici:websocket:socket_error')

/** @type {import('crypto')} */
let crypto
try {
crypto = require('crypto')
} catch {

}

/**
* @see https://websockets.spec.whatwg.org/#concept-websocket-establish
* @param {URL} url
Expand Down Expand Up @@ -66,7 +73,7 @@
// 5. Let keyValue be a nonce consisting of a randomly selected
// 16-byte value that has been forgiving-base64-encoded and
// isomorphic encoded.
const keyValue = randomBytes(16).toString('base64')
const keyValue = crypto.randomBytes(16).toString('base64')

// 6. Append (`Sec-WebSocket-Key`, keyValue) to request’s
// header list.
Expand Down Expand Up @@ -148,7 +155,7 @@
// trailing whitespace, the client MUST _Fail the WebSocket
// Connection_.
const secWSAccept = response.headersList.get('Sec-WebSocket-Accept')
const digest = createHash('sha1').update(keyValue + uid).digest('base64')
const digest = crypto.createHash('sha1').update(keyValue + uid).digest('base64')
Dismissed Show dismissed Hide dismissed
if (secWSAccept !== digest) {
failWebsocketConnection(ws, 'Incorrect hash received in Sec-WebSocket-Accept header.')
return
Expand Down
11 changes: 9 additions & 2 deletions lib/websocket/frame.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
'use strict'

const { randomBytes } = require('crypto')
const { maxUnsigned16Bit } = require('./constants')

/** @type {import('crypto')} */
let crypto
try {
crypto = require('crypto')
} catch {

}

class WebsocketFrameSend {
/**
* @param {Buffer|undefined} data
*/
constructor (data) {
this.frameData = data
this.maskKey = randomBytes(4)
this.maskKey = crypto.randomBytes(4)
}

createFrame (opcode) {
Expand Down