1616 * See the License for the specific language governing permissions and
1717 * limitations under the License.
1818 */
19-
20- import net from 'net' ;
21- import tls from 'tls' ;
22- import fs from 'fs' ;
23- import path from 'path' ;
24- import { EOL } from 'os' ;
25- import { NodeBuffer } from './buf' ;
26- import { isLocalHost , ENCRYPTION_NON_LOCAL , ENCRYPTION_OFF } from './util' ;
27- import { newError , SESSION_EXPIRED } from './../error' ;
19+ import net from "net" ;
20+ import tls from "tls" ;
21+ import fs from "fs" ;
22+ import path from "path" ;
23+ import { EOL } from "os" ;
24+ import { NodeBuffer } from "./buf" ;
25+ import { ENCRYPTION_OFF , isEmptyObjectOrNull } from "./util" ;
26+ import { newError , SESSION_EXPIRED } from "./../error" ;
2827
2928let _CONNECTION_IDGEN = 0 ;
3029
@@ -106,7 +105,7 @@ function storeFingerprint( serverId, knownHostsPath, fingerprint, cb ) {
106105
107106const TrustStrategy = {
108107 /**
109- * @deprecated Since version 1.0. Will be deleted in a future version. TRUST_CUSTOM_CA_SIGNED_CERTIFICATES.
108+ * @deprecated Since version 1.0. Will be deleted in a future version. { @link # TRUST_CUSTOM_CA_SIGNED_CERTIFICATES} .
110109 */
111110 TRUST_SIGNED_CERTIFICATES : function ( opts , onSuccess , onFailure ) {
112111 console . log ( "`TRUST_SIGNED_CERTIFICATES` has been deprecated as option and will be removed in a future version of " +
@@ -119,7 +118,7 @@ const TrustStrategy = {
119118 "to verify trust for encrypted connections, but have not configured any " +
120119 "trustedCertificates. You must specify the path to at least one trusted " +
121120 "X.509 certificate for this to work. Two other alternatives is to use " +
122- "TRUST_ON_FIRST_USE or to disable encryption by setting encrypted=\"" + ENCRYPTION_OFF + "\"" +
121+ "TRUST_ALL_CERTIFICATES or to disable encryption by setting encrypted=\"" + ENCRYPTION_OFF + "\"" +
123122 "in your driver configuration." ) ) ;
124123 return ;
125124 }
@@ -169,7 +168,13 @@ const TrustStrategy = {
169168 socket . on ( 'error' , onFailure ) ;
170169 return socket ;
171170 } ,
171+ /**
172+ * @deprecated in 1.1 in favour of {@link #TRUST_ALL_CERTIFICATES}. Will be deleted in a future version.
173+ */
172174 TRUST_ON_FIRST_USE : function ( opts , onSuccess , onFailure ) {
175+ console . log ( "`TRUST_ON_FIRST_USE` has been deprecated as option and will be removed in a future version of " +
176+ "the driver. Please use `TRUST_ALL_CERTIFICATES` instead." ) ;
177+
173178 let tlsOpts = {
174179 // Because we manually verify the certificate against known_hosts
175180 rejectUnauthorized : false
@@ -221,21 +226,40 @@ const TrustStrategy = {
221226 } ) ;
222227 socket . on ( 'error' , onFailure ) ;
223228 return socket ;
229+ } ,
230+
231+ TRUST_ALL_CERTIFICATES : function ( opts , onSuccess , onFailure ) {
232+ const tlsOpts = {
233+ rejectUnauthorized : false
234+ } ;
235+ const socket = tls . connect ( opts . port , opts . host , tlsOpts , function ( ) {
236+ const certificate = socket . getPeerCertificate ( ) ;
237+ if ( isEmptyObjectOrNull ( certificate ) ) {
238+ onFailure ( newError ( "Secure connection was successful but server did not return any valid " +
239+ "certificates. Such connection can not be trusted. If you are just trying " +
240+ " Neo4j out and are not concerned about encryption, simply disable it using " +
241+ "`encrypted=\"" + ENCRYPTION_OFF + "\"` in the driver options. " +
242+ "Socket responded with: " + socket . authorizationError ) ) ;
243+ } else {
244+ onSuccess ( ) ;
245+ }
246+ } ) ;
247+ socket . on ( 'error' , onFailure ) ;
248+ return socket ;
224249 }
225250} ;
226251
227252function connect ( opts , onSuccess , onFailure = ( ( ) => null ) ) {
228253 //still allow boolean for backwards compatibility
229- if ( opts . encrypted === false || opts . encrypted === ENCRYPTION_OFF ||
230- ( opts . encrypted === ENCRYPTION_NON_LOCAL && isLocalHost ( opts . host ) ) ) {
254+ if ( opts . encrypted === false || opts . encrypted === ENCRYPTION_OFF ) {
231255 var conn = net . connect ( opts . port , opts . host , onSuccess ) ;
232256 conn . on ( 'error' , onFailure ) ;
233257 return conn ;
234258 } else if ( TrustStrategy [ opts . trust ] ) {
235259 return TrustStrategy [ opts . trust ] ( opts , onSuccess , onFailure ) ;
236260 } else {
237261 onFailure ( newError ( "Unknown trust strategy: " + opts . trust + ". Please use either " +
238- "trust:'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES' or trust:'TRUST_ON_FIRST_USE ' in your driver " +
262+ "trust:'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES' or trust:'TRUST_ALL_CERTIFICATES ' in your driver " +
239263 "configuration. Alternatively, you can disable encryption by setting " +
240264 "`encrypted:\"" + ENCRYPTION_OFF + "\"`. There is no mechanism to use encryption without trust verification, " +
241265 "because this incurs the overhead of encryption without improving security. If " +
0 commit comments