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 missing port in proxy CONNECT when using the default HTTPS port #2608

Merged

Conversation

segevfiner
Copy link
Contributor

When the target gRPC URI doesn't contain a port, and as such defaults to the default HTTPS port, the code didn't send the port as part of the HTTP CONNECT request when a proxy is configured, leading to proxies rejected the request with a 400 Bad Request, as the request line is malformed.

We saw this while using firebase/firestore which uses gRPC underneath by default, and connect to "firestore.googleapis.com" without a port by default.

Contributed by courtesy of swimm.io

@Omerr
Copy link

Omerr commented Oct 30, 2023

Indeed it works and solved us a problem

Copy link
Member

@murgatroid99 murgatroid99 left a comment

Choose a reason for hiding this comment

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

This seems like a reasonable change, functionally, but it could be cleaner:

  1. I don't like "443" as a magic string. It would be better to export DEFAULT_PORT from resolver-dns.ts, and reference that here.
  2. With nullish coalescing, (targetHostPost.port != null ? targetHostPost.port : '443') can be shortened to targetHostPort.port ?? 443 (or, integrating the previous point, targetHostPort.port ?? DEFAULT_PORT).
  3. Use string templates to make the string construction more concise: `${targetHostPort.host}:${targetHostPort.port ?? DEFAULT_PORT}`
  4. You are constructing the same string twice, once for options.path and once for headers.Host. Instead create it once, assign it to a variable, and use that variable in the other two lines.

@segevfiner
Copy link
Contributor Author

This seems like a reasonable change, functionally, but it could be cleaner:

  1. I don't like "443" as a magic string. It would be better to export DEFAULT_PORT from resolver-dns.ts, and reference that here.
  2. With nullish coalescing, (targetHostPost.port != null ? targetHostPost.port : '443') can be shortened to targetHostPort.port ?? 443 (or, integrating the previous point, targetHostPort.port ?? DEFAULT_PORT).
  3. Use string templates to make the string construction more concise: `${targetHostPort.host}:${targetHostPort.port ?? DEFAULT_PORT}`
  4. You are constructing the same string twice, once for options.path and once for headers.Host. Instead create it once, assign it to a variable, and use that variable in the other two lines.

Yeah. Wasn't really sure what ECMAScript version this was targeting, but will do.

@murgatroid99
Copy link
Member

One of the great things about TypeScript is that it will allow the use of ECMAScript features that are incompatible with the target, and it will transpile shims as necessary. The library currently uses typescript ^5.1.3 per the package.json, so any code that works in that version of TypeScript should be acceptable.

@segevfiner
Copy link
Contributor Author

One of the great things about TypeScript is that it will allow the use of ECMAScript features that are incompatible with the target, and it will transpile shims as necessary. The library currently uses typescript ^5.1.3 per the package.json, so any code that works in that version of TypeScript should be acceptable.

Yeah. Guess it was late when I debugged this, and didn't even notice I was in TypeScript 😋

Copy link
Member

@murgatroid99 murgatroid99 left a comment

Choose a reason for hiding this comment

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

OK, that looks good now. If you rebase the change onto the @grpc/grpc-js@1.9.x branch and change the PR target, it will go out in the next patch release.

@segevfiner segevfiner force-pushed the fix-proxy-connect-missing-port branch from 121ba8b to 0854192 Compare October 30, 2023 23:43
@segevfiner segevfiner changed the base branch from master to @grpc/grpc-js@1.9.x October 30, 2023 23:44
@segevfiner
Copy link
Contributor Author

OK, that looks good now. If you rebase the change onto the @grpc/grpc-js@1.9.x branch and change the PR target, it will go out in the next patch release.

Rebased.

@murgatroid99 murgatroid99 merged commit 993835a into grpc:@grpc/grpc-js@1.9.x Oct 30, 2023
2 checks passed
@murgatroid99
Copy link
Member

This is now out in 1.9.10.

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.

None yet

4 participants