-
Notifications
You must be signed in to change notification settings - Fork 151
Automatically use secure WebSocket on HTTPS pages #371
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,27 +29,20 @@ class WebSocketChannel { | |
/** | ||
* Create new instance | ||
* @param {ChannelConfig} config - configuration for this channel. | ||
* @param {function(): string} protocolSupplier - function that detects protocol of the web page. Should only be used in tests. | ||
*/ | ||
constructor(config) { | ||
constructor(config, protocolSupplier = detectWebPageProtocol) { | ||
|
||
this._open = true; | ||
this._pending = []; | ||
this._error = null; | ||
this._handleConnectionError = this._handleConnectionError.bind(this); | ||
this._config = config; | ||
|
||
let scheme = "ws"; | ||
//Allow boolean for backwards compatibility | ||
if (config.encrypted === true || config.encrypted === ENCRYPTION_ON) { | ||
if ((!config.trust) || config.trust === 'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES') { | ||
scheme = "wss"; | ||
} else { | ||
this._error = newError("The browser version of this driver only supports one trust " + | ||
'strategy, \'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES\'. ' + config.trust + ' is not supported. Please ' + | ||
"either use TRUST_CUSTOM_CA_SIGNED_CERTIFICATES or disable encryption by setting " + | ||
"`encrypted:\"" + ENCRYPTION_OFF + "\"` in the driver configuration."); | ||
return; | ||
} | ||
const {scheme, error} = determineWebSocketScheme(config, protocolSupplier); | ||
if (error) { | ||
this._error = error; | ||
return; | ||
} | ||
|
||
this._ws = createWebSocket(scheme, config.url); | ||
|
@@ -114,10 +107,6 @@ class WebSocketChannel { | |
} | ||
} | ||
|
||
isEncrypted() { | ||
return this._config.encrypted; | ||
} | ||
|
||
/** | ||
* Write the passed in buffer to connection | ||
* @param {HeapBuffer} buffer - Buffer to write | ||
|
@@ -233,4 +222,46 @@ function asWindowsFriendlyIPv6Address(scheme, parsedUrl) { | |
return `${scheme}://${ipv6Host}:${parsedUrl.port}`; | ||
} | ||
|
||
/** | ||
* @param {ChannelConfig} config - configuration for the channel. | ||
* @param {function(): string} protocolSupplier - function that detects protocol of the web page. | ||
* @return {{scheme: string|null, error: Neo4jError|null}} object containing either scheme or error. | ||
*/ | ||
function determineWebSocketScheme(config, protocolSupplier) { | ||
const encrypted = config.encrypted; | ||
const trust = config.trust; | ||
|
||
if (encrypted === false || encrypted === ENCRYPTION_OFF) { | ||
// encryption explicitly turned off in the config | ||
return {scheme: 'ws', error: null}; | ||
} | ||
|
||
const protocol = typeof protocolSupplier === 'function' ? protocolSupplier() : ''; | ||
if (protocol && protocol.toLowerCase().indexOf('https') >= 0) { | ||
// driver is used in a secure https web page, use 'wss' | ||
return {scheme: 'wss', error: null}; | ||
} | ||
|
||
if (encrypted === true || encrypted === ENCRYPTION_ON) { | ||
// encryption explicitly requested in the config | ||
if (!trust || trust === 'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is totally out of scope of this PR, but I find There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll create a card for this |
||
// trust strategy not specified or the only supported strategy is specified | ||
return {scheme: 'wss', error: null}; | ||
} else { | ||
const error = newError('The browser version of this driver only supports one trust ' + | ||
'strategy, \'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES\'. ' + trust + ' is not supported. Please ' + | ||
'either use TRUST_CUSTOM_CA_SIGNED_CERTIFICATES or disable encryption by setting ' + | ||
'`encrypted:"' + ENCRYPTION_OFF + '"` in the driver configuration.'); | ||
return {scheme: null, error: error}; | ||
} | ||
} | ||
|
||
// default to unencrypted web socket | ||
return {scheme: 'ws', error: null}; | ||
} | ||
|
||
function detectWebPageProtocol() { | ||
return window && window.location ? window.location.protocol : null; | ||
} | ||
|
||
export default _websocketChannelModule |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the information given in Security Considerations section here, it may make sense generating warning message about mixing protocols may not work - if encryption is explicitly turned off or on (i.e. https/ws or http/wss).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a warn print in c623d8a