@@ -228,21 +228,23 @@ function asWindowsFriendlyIPv6Address(scheme, parsedUrl) {
228
228
* @return {{scheme: string|null, error: Neo4jError|null} } object containing either scheme or error.
229
229
*/
230
230
function determineWebSocketScheme ( config , protocolSupplier ) {
231
- const encrypted = config . encrypted ;
231
+ const encryptionOn = isEncryptionExplicitlyTurnedOn ( config ) ;
232
+ const encryptionOff = isEncryptionExplicitlyTurnedOff ( config ) ;
232
233
const trust = config . trust ;
234
+ const secureProtocol = isProtocolSecure ( protocolSupplier ) ;
235
+ verifyEncryptionSettings ( encryptionOn , encryptionOff , secureProtocol ) ;
233
236
234
- if ( encrypted === false || encrypted === ENCRYPTION_OFF ) {
237
+ if ( encryptionOff ) {
235
238
// encryption explicitly turned off in the config
236
239
return { scheme : 'ws' , error : null } ;
237
240
}
238
241
239
- const protocol = typeof protocolSupplier === 'function' ? protocolSupplier ( ) : '' ;
240
- if ( protocol && protocol . toLowerCase ( ) . indexOf ( 'https' ) >= 0 ) {
242
+ if ( secureProtocol ) {
241
243
// driver is used in a secure https web page, use 'wss'
242
244
return { scheme : 'wss' , error : null } ;
243
245
}
244
246
245
- if ( encrypted === true || encrypted === ENCRYPTION_ON ) {
247
+ if ( encryptionOn ) {
246
248
// encryption explicitly requested in the config
247
249
if ( ! trust || trust === 'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES' ) {
248
250
// trust strategy not specified or the only supported strategy is specified
@@ -260,6 +262,45 @@ function determineWebSocketScheme(config, protocolSupplier) {
260
262
return { scheme : 'ws' , error : null } ;
261
263
}
262
264
265
+ /**
266
+ * @param {ChannelConfig } config - configuration for the channel.
267
+ * @return {boolean } <code>true</code> if encryption enabled in the config, <code>false</code> otherwise.
268
+ */
269
+ function isEncryptionExplicitlyTurnedOn ( config ) {
270
+ return config . encrypted === true || config . encrypted === ENCRYPTION_ON ;
271
+ }
272
+
273
+ /**
274
+ * @param {ChannelConfig } config - configuration for the channel.
275
+ * @return {boolean } <code>true</code> if encryption disabled in the config, <code>false</code> otherwise.
276
+ */
277
+ function isEncryptionExplicitlyTurnedOff ( config ) {
278
+ return config . encrypted === false || config . encrypted === ENCRYPTION_OFF ;
279
+ }
280
+
281
+ /**
282
+ * @param {function(): string } protocolSupplier - function that detects protocol of the web page.
283
+ * @return {boolean } <code>true</code> if protocol returned by the given function is secure, <code>false</code> otherwise.
284
+ */
285
+ function isProtocolSecure ( protocolSupplier ) {
286
+ const protocol = typeof protocolSupplier === 'function' ? protocolSupplier ( ) : '' ;
287
+ return protocol && protocol . toLowerCase ( ) . indexOf ( 'https' ) >= 0 ;
288
+ }
289
+
290
+ function verifyEncryptionSettings ( encryptionOn , encryptionOff , secureProtocol ) {
291
+ if ( encryptionOn && ! secureProtocol ) {
292
+ // encryption explicitly turned on for a driver used on a HTTP web page
293
+ console . warn ( 'Neo4j driver is configured to use secure WebSocket on a HTTP web page. ' +
294
+ 'WebSockets might not work in a mixed content environment. ' +
295
+ 'Please consider configuring driver to not use encryption.' ) ;
296
+ } else if ( encryptionOff && secureProtocol ) {
297
+ // encryption explicitly turned off for a driver used on a HTTPS web page
298
+ console . warn ( 'Neo4j driver is configured to use insecure WebSocket on a HTTPS web page. ' +
299
+ 'WebSockets might not work in a mixed content environment. ' +
300
+ 'Please consider configuring driver to use encryption.' ) ;
301
+ }
302
+ }
303
+
263
304
function detectWebPageProtocol ( ) {
264
305
return window && window . location ? window . location . protocol : null ;
265
306
}
0 commit comments