| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 43d28f5 commit 482459e
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -11,6 +11,7 @@ const { | |||
| 11 | 11 | } = primordials; | |
| 12 | 12 | ||
| 13 | 13 | const Buffer = require('buffer').Buffer; | |
| 14 | + const crypto = require('crypto'); | ||
| 14 | 15 | const { ERR_DEBUGGER_ERROR } = require('internal/errors').codes; | |
| 15 | 16 | const { EventEmitter } = require('events'); | |
| 16 | 17 | const http = require('http'); | |
@@ -35,13 +36,30 @@ const kTwoBytePayloadLengthField = 126; | |||
| 35 | 36 | const kEightBytePayloadLengthField = 127; | |
| 36 | 37 | const kMaskingKeyWidthInBytes = 4; | |
| 37 | 38 | ||
| 39 | + // This guid is defined in the Websocket Protocol RFC | ||
| 40 | + // https://tools.ietf.org/html/rfc6455#section-1.3 | ||
| 41 | + const WEBSOCKET_HANDSHAKE_GUID = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; | ||
| 42 | + | ||
| 38 | 43 | function unpackError({ code, message, data }) { | |
| 39 | 44 | const err = new ERR_DEBUGGER_ERROR(`${message} - ${data}`); | |
| 40 | 45 | err.code = code; | |
| 41 | 46 | ErrorCaptureStackTrace(err, unpackError); | |
| 42 | 47 | return err; | |
| 43 | 48 | } | |
| 44 | 49 | ||
| 50 | + function validateHandshake(requestKey, responseKey) { | ||
| 51 | + const expectedResponseKeyBase = requestKey + WEBSOCKET_HANDSHAKE_GUID; | ||
| 52 | + const shasum = crypto.createHash('sha1'); | ||
| 53 | + shasum.update(expectedResponseKeyBase); | ||
| 54 | + const shabuf = shasum.digest(); | ||
| 55 | + | ||
| 56 | + if (shabuf.toString('base64') !== responseKey) { | ||
| 57 | + throw new ERR_DEBUGGER_ERROR( | ||
| 58 | + `WebSocket secret mismatch: ${requestKey} did not match ${responseKey}` | ||
| 59 | + ); | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 45 | 63 | function encodeFrameHybi17(payload) { | |
| 46 | 64 | var i; | |
| 47 | 65 | ||
@@ -292,8 +310,8 @@ class Client extends EventEmitter { | |||
| 292 | 310 | _connectWebsocket(urlPath) { | |
| 293 | 311 | this.reset(); | |
| 294 | 312 | ||
| 295 | - const key1 = require('crypto').randomBytes(16).toString('base64'); | ||
| 296 | - debuglog('request websocket', key1); | ||
| 313 | + const requestKey = crypto.randomBytes(16).toString('base64'); | ||
| 314 | + debuglog('request WebSocket', requestKey); | ||
| 297 | 315 | ||
| 298 | 316 | const httpReq = this._http = http.request({ | |
| 299 | 317 | host: this._host, | |
@@ -302,7 +320,7 @@ class Client extends EventEmitter { | |||
| 302 | 320 | headers: { | |
| 303 | 321 | 'Connection': 'Upgrade', | |
| 304 | 322 | 'Upgrade': 'websocket', | |
| 305 | - 'Sec-WebSocket-Key': key1, | ||
| 323 | + 'Sec-WebSocket-Key': requestKey, | ||
| 306 | 324 | 'Sec-WebSocket-Version': '13', | |
| 307 | 325 | }, | |
| 308 | 326 | }); | |
@@ -319,7 +337,7 @@ class Client extends EventEmitter { | |||
| 319 | 337 | }); | |
| 320 | 338 | ||
| 321 | 339 | const handshakeListener = (res, socket) => { | |
| 322 | - // TODO: we *could* validate res.headers[sec-websocket-accept] | ||
| 340 | + validateHandshake(requestKey, res.headers['sec-websocket-accept']); | ||
| 323 | 341 | debuglog('websocket upgrade'); | |
| 324 | 342 | ||
| 325 | 343 | this._socket = socket; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,6 +71,7 @@ void NativeModuleLoader::InitializeModuleCategories() { | |||
| 71 | 71 | std::vector<std::string> prefixes = { | |
| 72 | 72 | #if !HAVE_OPENSSL | |
| 73 | 73 | "internal/crypto/", | |
| 74 | + "internal/debugger/", | ||
| 74 | 75 | #endif // !HAVE_OPENSSL | |
| 75 | 76 | ||
| 76 | 77 | "internal/bootstrap/", | |
| Back | FazBrowse Home | New Git URL |
0 commit comments