fix: Pass host to tls.connect for certificate validation - #3756
Open
jhnns wants to merge 1 commit into
Open
Conversation
…P address tls.connect verifies the server identity against `servername`, falling back to `host` and then to 'localhost'. Since `servername` must not be set to an IP address (RFC 6066 section 3), certificates were validated against 'localhost' whenever the connection host was an IP address. Passing `host` to tls.connect fixes this. Continues brianc#2273 (originally by Frazer McLean) with tests. Fixes brianc#2263 Co-authored-by: Frazer McLean <frazer@frazermclean.co.uk>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Continues #2273 by @RazerM (with tests, as requested in this comment). Fixes #2263.
The problem
When connecting with
sslenabled, pg hands an already-connected socket totls.connect(), so Node.js validates the server certificate against:pg only sets
servernamefor hostnames, since SNI must not contain IP addresses (RFC 6066, section 3 — the reason for the check introduced in #1890). Butservernamewas doing double duty as the name for certificate validation, andhostwas never set. So when connecting to an IP address, the certificate was silently validated against'localhost'— a certificate for an entirely different server would be accepted (see #2263).The fix
Also pass
hosttotls.connect(). Node.js then validates the certificate against the IP being connected to (via its IP SANs). Hostname connections are unaffected (servernamestill takes precedence), as are Unix domain sockets (hostisundefinedthere, as before). Per Node's docs,hostis ignored except for certificate validation when a socket is supplied. Both sides are now documented with code comments inupgradeToSSL.Tests
Added
test/unit/connection/ssl-tests.js, following the existing unit-test conventions:hostto the secure stream and leavesservernameunset.hostandservername.test/tlscertificates), assertingvia acheckServerIdentityoverride that Node.js verifies the certificate against the IP address. Without the fix, it receives'localhost'and the test fails.