16
16
* See the License for the specific language governing permissions and
17
17
* limitations under the License.
18
18
*/
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" ;
28
27
29
28
let _CONNECTION_IDGEN = 0 ;
30
29
@@ -106,7 +105,7 @@ function storeFingerprint( serverId, knownHostsPath, fingerprint, cb ) {
106
105
107
106
const TrustStrategy = {
108
107
/**
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} .
110
109
*/
111
110
TRUST_SIGNED_CERTIFICATES : function ( opts , onSuccess , onFailure ) {
112
111
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 = {
119
118
"to verify trust for encrypted connections, but have not configured any " +
120
119
"trustedCertificates. You must specify the path to at least one trusted " +
121
120
"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 + "\"" +
123
122
"in your driver configuration." ) ) ;
124
123
return ;
125
124
}
@@ -169,7 +168,13 @@ const TrustStrategy = {
169
168
socket . on ( 'error' , onFailure ) ;
170
169
return socket ;
171
170
} ,
171
+ /**
172
+ * @deprecated in 1.1 in favour of {@link #TRUST_ALL_CERTIFICATES}. Will be deleted in a future version.
173
+ */
172
174
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
+
173
178
let tlsOpts = {
174
179
// Because we manually verify the certificate against known_hosts
175
180
rejectUnauthorized : false
@@ -221,21 +226,40 @@ const TrustStrategy = {
221
226
} ) ;
222
227
socket . on ( 'error' , onFailure ) ;
223
228
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 ;
224
249
}
225
250
} ;
226
251
227
252
function connect ( opts , onSuccess , onFailure = ( ( ) => null ) ) {
228
253
//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 ) {
231
255
var conn = net . connect ( opts . port , opts . host , onSuccess ) ;
232
256
conn . on ( 'error' , onFailure ) ;
233
257
return conn ;
234
258
} else if ( TrustStrategy [ opts . trust ] ) {
235
259
return TrustStrategy [ opts . trust ] ( opts , onSuccess , onFailure ) ;
236
260
} else {
237
261
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 " +
239
263
"configuration. Alternatively, you can disable encryption by setting " +
240
264
"`encrypted:\"" + ENCRYPTION_OFF + "\"`. There is no mechanism to use encryption without trust verification, " +
241
265
"because this incurs the overhead of encryption without improving security. If " +
0 commit comments