Skip to content

Pass host to tls.connect for certificate validation - #2273

Open
RazerM wants to merge 1 commit into
brianc:masterfrom
RazerM:fix-ip-tls-validation
Open

Pass host to tls.connect for certificate validation#2273
RazerM wants to merge 1 commit into
brianc:masterfrom
RazerM:fix-ip-tls-validation

Conversation

@RazerM

@RazerM RazerM commented Jul 10, 2020

Copy link
Copy Markdown

Fixes #2263

@charmander

Copy link
Copy Markdown
Collaborator

Someone’ll need to write a test for this. I wonder what it does when host is a Unix domain socket?

@RazerM
RazerM force-pushed the fix-ip-tls-validation branch from e426771 to 839b6b4 Compare July 22, 2020 22:19
@RazerM

RazerM commented Jul 22, 2020

Copy link
Copy Markdown
Author

I wonder what it does when host is a Unix domain socket?

I think from the links I put in #2263 it's clear that host is only used for checking the server identity, and that for non-IP addresses this is currently set by servername. So nothing has changed for unix domain sockets in that regard.

@RazerM
RazerM force-pushed the fix-ip-tls-validation branch from 839b6b4 to 03c4ea5 Compare May 3, 2021 09:14
@RazerM

RazerM commented May 3, 2021

Copy link
Copy Markdown
Author

What can I do to get this merged?

@vitaly-t

vitaly-t commented Jun 3, 2021

Copy link
Copy Markdown
Contributor

When indeed? :) I got another issue open about the same, it seems.

@charmander

charmander commented Jun 3, 2021

Copy link
Copy Markdown
Collaborator

@vitaly-t That doesn’t sound like the same issue; the host in that one isn’t an IP address, and the claimed behaviour is missing SNI, not certificate validation failure.

@RazerM It needs a test.

@vitaly-t

vitaly-t commented Jun 3, 2021

Copy link
Copy Markdown
Contributor

That doesn’t sound like the same issue; the host in that one isn’t an IP address, and the claimed behaviour is missing SNI

Yeah, sorry, I didn't look up close. But what's valid there is something else in the configuration here, since pg-promise just passes the config object on to this driver, without any modification.

@charmander

Copy link
Copy Markdown
Collaborator

@vitaly-t It looks like pg sets servername unconditionally, but we want to allow it to be overridden; working on a fix for that. (That should mean the SNI is the same as the host instead of missing, but that’s probably what the issue reporter means.)

@RazerM

RazerM commented Jun 6, 2021

Copy link
Copy Markdown
Author

@charmander #1890 was merged without a test despite the consequences for IP addresses so I get the impression the test setup wouldn't easily support such a test. The existing tests all pass for this PR too.

I think it should be clear from the description in the issue what is wrong. It's quite frustrating because it's fairly easy to see why IP addresses don't work and how pg got like this:

  1. pg passes a socket to tls.connect.
  2. tls.connect has no idea which hostname it needs to check the identity for when it has a socket.
  3. tls.connect uses options.servername || options.host || 'localhost' to verify server identity.
  4. pg used to always pass servername, which as noted should not be done for IP addresses.
  5. PR Skip TLS SNI if host is IP address #1890 changed this to only do it for non-IP addresses, therefore the tls.connect identity verification uses 'localhost'. This is bad!
  6. The snippet above shows why removing servername for IP addresses results in 'localhost' being used. You need to pass host!

@charmander

charmander commented Jun 7, 2021

Copy link
Copy Markdown
Collaborator

Of course it’s clear what’s wrong. That doesn’t mean it doesn’t need a test. It was able to break in the first place because there was no test.

I can write the test, but it’ll be a while.

@penous

penous commented May 15, 2023

Copy link
Copy Markdown

Hey all, any updates on this?

@jhnns

jhnns commented Mar 27, 2025

Copy link
Copy Markdown

Just stumbled upon this issue. I can confirm that this PR would fix the problem. Node's docs are quite clear in that regard that "path, host, and port are ignored, except for certificate validation".

@charmander Would you still merge it if it had tests?

@charmander

Copy link
Copy Markdown
Collaborator

@jhnns Yep!

jhnns added a commit to jhnns/node-postgres that referenced this pull request Aug 24, 2026
…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>
@jhnns

jhnns commented Aug 24, 2026

Copy link
Copy Markdown

@charmander I created a PR with the implementation and the tests: #3756

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IP hosts are not validated correctly against certificate altnames

5 participants