Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TLS bug for TLS over TLS proxy #65

Merged
merged 19 commits into from
May 2, 2022
Merged

Conversation

ckcr4lyf
Copy link
Contributor

@ckcr4lyf ckcr4lyf commented May 2, 2022

First off, thanks to @freeqaz for the initial investigation and fix (#51). Since they seem MIA, I've decided to write the unit tests for it since I want to be able to use the fix!!!

Previously the tests were using process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0 , which bypassed all TLS checks. Additionally, the self-signed cert was for fastify.

In order to properly test the TLS part, including handshakes and correct SNI, I generated two certs for "fake" domains (b9d8ae1#diff-56d361e6893cf9d961de277f3986602b66ace5d977e446d4161c664a95a48f5bR13) , and then for the tests used the node option NODE_EXTRA_CA_CERTS to tell it to trust these certs.

I also had to monkey patch DNS lookups in node.js to make these fake domains resolve to 127.0.0.1, which seems to work fine.

I then changed the test proxy / server to use their respective TLS keys & certs, and used the fake domain wherever the hostname was needed.

The scope of the bug can be realized by running the new tests, with Free's fix commented out:
image

Other misc:

  • To pass the env var for windows test in CI, I changed workflow to add env var, and added a test-ci step which is used, while keeping test easy to run locally on unix systems (with the CA var included)
  • The tests showed that in Node 10, Free's fix was not sufficient due to how it passes the HTTP hostname to HTTPS proxy! I added the servername option to that class as well.

Cheers

freeqaz and others added 17 commits February 19, 2022 03:00
Without this fix, you see this error when attempting to use an HTTPS proxy.

`Error [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate's altnames: Host: foo.com. is not in the cert's altnames: DNS:*.bar.com, DNS:bar.com`

The `servername` attribute is able to fix this behavior when passed to the underlying `tls.connect()` call inside of the `https` module. (see the Node [docs](https://nodejs.org/api/https.html#httpsrequesturl-options-callback))

This was very painful to debug but now I understand how `CONNECT` works extremely well! Cheers.

FIxes delvedor#43
Add unit tests, backport Free's fix for Node v10, setup workflow for windows env var
@Enzime
Copy link

Enzime commented May 2, 2022

You can use the following package instead of needing to create two separate npm scripts to set environment variables:

https://www.npmjs.com/package/cross-env

@arnaudruffin
Copy link

thanks @ckcr4lyf I had planned to do the exact same pull request today :)

Copy link
Owner

@delvedor delvedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Can you move the certificates in test/fixtures and then add test/fixtures to .gitignore?

package.json Outdated Show resolved Hide resolved
Copy link
Owner

@delvedor delvedor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@delvedor delvedor changed the title Fix TLS bug for TLS over TLS proxy (w/ unit tests!) Fix TLS bug for TLS over TLS proxy May 2, 2022
@delvedor delvedor merged commit a9bf98d into delvedor:master May 2, 2022
@freeqaz
Copy link
Contributor

freeqaz commented May 6, 2022

Thanks for landing this! I didn't mean to ghost y'all -- I just don't see GitHub's notifications unless I look for them. Please email me if you need a response from me in the future -- I'll see that immediately. 🙏

@binarymist
Copy link

FYI: There's some weird setting in Github that after 10 years of opening an account, these notifications need to be reactivated, I had the same issue until I found this setting a year or two ago @freeqaz

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.

6 participants