Skip to content

Default to true for options.encrypt - #694

Closed
arthurschreiber wants to merge 3 commits into
masterfrom
arthur/default-encryption
Closed

arthurschreiber wants to merge 3 commits into
masterfrom
arthur/default-encryption

Conversation

@arthurschreiber

@arthurschreiber arthurschreiber commented Jan 28, 2018 •

Copy link
Copy Markdown
Collaborator

This changes the default of options.encrypt from false to true, effectively switching on connection encryption unless explicitly turned off.

It also makes the type checking of the options.encrypt value stricter, only accepting boolean values.

Both of these changes are breaking backwards compatibility. The plan is to deprecate the current behaviour, and then later release these changes on a major version bump.

Fixes #606.

@Suraiya-Hameed

Copy link
Copy Markdown
Member

On a side note, while updating the documentation, remember to update the requirements for older versions of SQL Server.
For SQL Server <2008 SP4, users should have options.cryptoCredentialsDetails.ciphers in conenction config (from issue#610) if options.encrypt : true.

@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

@v-suhame Thanks for reminding me! We probably should support this out-of-the box.

I read the documentation you linked to in #610. From what I understand, SQL Server supports ciphers based on the operating system it is running on. In the case of SQL Server 2008 running on Windows Server 2003, the list looks like this:

  • TLS_RSA_WITH_RC4_128_MD5
  • TLS_RSA_WITH_RC4_128_SHA
  • TLS_RSA_WITH_3DES_EDE_CBC_SHA
  • TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA
  • TLS_RSA_WITH_DES_CBC_SHA
  • TLS_DHE_DSS_WITH_DES_CBC_SHA
  • TLS_RSA_EXPORT1024_WITH_RC4_56_SHA
  • TLS_RSA_EXPORT1024_WITH_DES_CBC_SHA
  • TLS_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA
  • TLS_RSA_EXPORT_WITH_RC4_40_MD5
  • TLS_RSA_EXPORT_WITH_RC2_CBC_40_MD5
  • TLS_RSA_WITH_NULL_MD5
  • TLS_RSA_WITH_NULL_SHA

None of these ciphers is accepted by newer Node versions, as their security is rather questionable.

According to https://wiki.mozilla.org/Security/Server_Side_TLS, the only cipher out of this list that might be used for backwards-compatibility without totally compromising security is TLS_RSA_WITH_3DES_EDE_CBC_SHA.

This specific suite can be enabled by using DES-CBC3-SHA in the ciphers option.

I think we can make tedious inform the user more thoroughly when the encryption handshake fails, and let them know that potentially the cipher suite configuration they're using is too strict? I'd rather not include that cipher by default (because of security implications), but the current behaviour is pretty opaque and unfortunate from a user experience perspective.

@Suraiya-Hameed

Copy link
Copy Markdown
Member

I think we can make tedious inform the user more thoroughly when the encryption handshake fails, and let them know that potentially the cipher suite configuration they're using is too strict?

👍 to this. It will be more helpful than putting them on GitHub Pages.

Right now, there is no error thrown if handshake fails due to incompatible cipher suite. Error gets thrown at the later phase during prelogin and its totally unrelated to the root of failure (error msg). So yes, it would be super helpful if right message is thrown at right place.

@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

Right now, there is no error thrown if handshake fails due to incompatible cipher suite. Error gets thrown at the later phase during prelogin and its totally unrelated to the root of failure (error msg). So yes, it would be super helpful if right message is thrown at right place.

It's actually worse than that. If the handshake fails, the server side closes the socket, and tedious does not report any error whatsoever. 😞 I think we need to re-visit handling of sockets that get closed from the server side before tedious has entered the FINAL state. 🤔

@Suraiya-Hameed

Copy link
Copy Markdown
Member

I'm not sure if there are other ways to verify the socket closed on server side, apart from sending data/packet to it 🤔

@tvrprasad

Copy link
Copy Markdown
Contributor

I think you just need to look into how socket close, end and error events are being handled. As long as these events are handled correctly in all states of the TDS state machine, server closing the socket will just fold into one of those... I think :-)

@Suraiya-Hameed

Copy link
Copy Markdown
Member

Hey @tvrprasad 👋
Socket closed on server side doesn't trigger any socket events in client side (as far as I've checked) 😅 Apparently JDBC driver & SqlClient too have this issue with encrypt ON for <=SQL Server 2008.

@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

Socket closed on server side doesn't trigger any socket events in client side (as far as I've checked) 😅 Apparently JDBC driver & SqlClient too have this issue with encrypt ON for <=SQL Server 2008.

I think it does, as well. At least I had a POC that could detect the handshake failed by listening to the socket close event. I'm not sure how feasible that actually is, tho.

@tvrprasad

Copy link
Copy Markdown
Contributor

What's the feasibility issue?

@Suraiya-Hameed

Copy link
Copy Markdown
Member

😕 Let me re-iterate what is happening, to see if we are on the same page :
If older version of SQL Server is used with encrypt: true, handshake fails and server closes the socket. tedious is unaware of this (we already have listener for socket close, it doesn't get triggered) and sends Login7, then it waits for the response, and eventually timeout throwing ETIMEOUT. tedious enters FINAL state at this point and closes the socket.

So, we should find a way to let the user know about handshake failure. Isn't that what we are looking at?

@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

handshake fails and server closes the socket. tedious is unaware of this (we already have listener for socket close, it doesn't get triggered)

I don't think that's what is happening. See the handler here

tedious/src/connection.js

Lines 956 to 970 in 76c7d06

socketClose() {
this.debug.log('connection to ' + this.config.server + ':' + this.config.options.port + ' closed');
if (this.state === this.STATE.REROUTING) {
this.debug.log('Rerouting to ' + this.routingData.server + ':' + this.routingData.port);
this.dispatchEvent('reconnect');
} else if (this.state === this.STATE.TRANSIENT_FAILURE_RETRY) {
const server = this.routingData ? this.routingData.server : this.server;
const port = this.routingData ? this.routingData.port : this.config.options.port;
this.debug.log('Retry after transient failure connecting to ' + server + ':' + port);
this.dispatchEvent('retry');
} else {
this.transitionTo(this.STATE.FINAL);
}
}

If the close event happens, tedious just switches to the Final state and closes the connection - silently. I think we should instead raise an error if the connection is closed when we don't expect it to close. What do you think? 🤔

@Suraiya-Hameed

Suraiya-Hameed commented Feb 8, 2018 •

Copy link
Copy Markdown
Member

The close event actually happens after ETIMEOUT. Here is the log,

--log : connected to sql-2k8-sp3-1.mdomain.ad:1433
--log : Sent
--log : type:0x12(PRELOGIN), status:0x01(EOM), length:0x002F, spid:0x0000, packetId:0x01, window:0x00
--log : PreLogin - version:0.0.0.1 1, encryption:0x01(ON), instopt:0x00, threadId:0x00000000, mars:0x00(OFF)
--log : State change: Connecting -> SentPrelogin
--log : Received
--log : type:0x04(TABULAR_RESULT), status:0x01(EOM), length:0x002B, spid:0x0000, packetId:0x01, window:0x00
--log : PreLogin - version:10.0.21.124 0, encryption:0x01(ON), instopt:0x00, threadId:0x00000000, mars:0x00(OFF)
--log : State change: SentPrelogin -> SentTLSSSLNegotiation
--log : Sent
--log : type:0x12(PRELOGIN), status:0x01(EOM), length:0x00E4, spid:0x0000, packetId:0x01, window:0x00
--log : Received
--log : type:0x12(PRELOGIN), status:0x01(EOM), length:0x0360, spid:0x0000, packetId:0x00, window:0x00
--log : Sent
--log : type:0x12(PRELOGIN), status:0x01(EOM), length:0x014E, spid:0x0000, packetId:0x01, window:0x00
--log : Received
--log : type:0x12(PRELOGIN), status:0x01(EOM), length:0x0043, spid:0x0000, packetId:0x00, window:0x00
--log : TLS negotiated (AES128-SHA, TLSv1/SSLv3)
--log : Sent
--log : type:0x10(LOGIN7), status:0x01(EOM), length:0x00F6, spid:0x0000, packetId:0x01, window:0x00
--log : Login7 - TDS:0x74000004, PacketSize:0x00001000, ClientProgVer:0x00000000, ClientPID:0x00004EA0, ConnectionID:0x00000000
Flags1:0xE0, Flags2:0x00, TypeFlags:0x00, Flags3:0x08, ClientTimezone:480, ClientLCID:0x00000409
Hostname:'<>', Username:'<>', Password:'<>', AppName:'Tedious', ServerName:'sql-2k8-sp3-1.mdomain.ad', LibraryName:'Tedious'
Language:'us_english', Database:'test', SSPI:'', AttachDbFile:'', ChangePassword:''
--log : State change: SentTLSSSLNegotiation -> SentLogin7WithStandardLogin
--log : Failed to connect to sql-2k8-sp3-1.mdomain.ad:1433 in 15000ms
Error Caught in 'connect' callback: { ConnectionError: Failed to connect to sql-2k8-sp3-1.mdomain.ad:1433 in 15000ms
at ConnectionError (F:/tedious/src/errors.js:10:12)
at Connection.connectTimeout (F:/tedious/src/connection.js:866:26)
at ontimeout (timers.js:458:11)
at tryOnTimeout (timers.js:296:5)
at Timer.listOnTimeout (timers.js:259:5)
message: 'Failed to connect to sql-2k8-sp3-1.mdomain.ad:1433 in 15000ms',
code: 'ETIMEOUT' }
--log : State change: SentLogin7WithStandardLogin -> Final
--log : connection to sql-2k8-sp3-1.mdomain.ad:1433 closed
--log : State is already Final

log is from simple code:

connection.on('debug', function(text) {
  console.log('--log :', text);
});
connection.on('connect', function (err) {
  if (err) { console.log('Error Caught in connect callback: ', err) }
});

I think we should instead raise an error if the connection is closed when we don't expect it to close. What do you think? 🤔

As you can see from the logs, socketClose() is called after connect event is fired with timeout error.

@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

Mh, that is weird. I'll try to dig up the test example where I've run into this.

@arthurschreiber
arthurschreiber force-pushed the arthur/default-encryption branch from 7f0147a to 02a24e0 Compare February 18, 2018 14:13
@arthurschreiber
arthurschreiber changed the base branch from master to arthur/bundled-breaking-changes July 31, 2018 19:45
@arthurschreiber
arthurschreiber changed the base branch from arthur/bundled-breaking-changes to arhtur/gitter July 31, 2018 19:45
@arthurschreiber
arthurschreiber changed the base branch from arhtur/gitter to master July 31, 2018 19:46
@arthurschreiber arthurschreiber changed the title BREAKING CHANGE: options.encrypt handling Default to true for options.encrypt Oct 1, 2018
@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

@Suraiya-Hameed @tvrprasad I figured out what was causing these issues!

The SSL/TLS implementation on older Windows versions is buggy and does not handle the countermeasures against the BEAST attack that were put into OpenSSL well. (OpenSSL inserts empty fragments to fix the exploit). This caused the connection process to hang during the TLS/SSL handshake forever, which then lead to the connection timeout.

Fortunately, there is an option to disable the countermeasure, which in turn fixes connecting to SQLServer instances that still use that buggy SSL/TLS implementation. 🎉

I'll open a separate PR for the fix, and then we can re-evaluate changing this option.

@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

I opened (and merged) #820, so we can move forward with this PR now.

BREAKING CHANGE:

If no `encrypt` setting was specified before, `tedious` would fall back
to not using any encryption by default. This changes the default to
encrypt connections unless `encrypt` is explicitly set to `false`.

This _should_ work without issues with most SQL Server setups, but might
cause connections to fail if encryption between `tedious` and SQL Server
can not be established.
@arthurschreiber
arthurschreiber force-pushed the arthur/default-encryption branch from eeb4326 to 61d51fc Compare November 2, 2018 20:45
@arthurschreiber

Copy link
Copy Markdown
Collaborator Author

This was part of #864.

@arthurschreiber
arthurschreiber deleted the arthur/default-encryption branch August 1, 2019 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants