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

Check for and report bad protocol in TLSClientConfig.NextProtos #788

Merged
merged 10 commits into from Jun 21, 2022

Conversation

ChannyClaus
Copy link
Contributor

@ChannyClaus ChannyClaus commented Jun 12, 2022

Fixes #760

Summary of Changes

  1. Adds an explicit check for unsupported protocol (seems to be only HTTP/1.1 for the time being as shown here)
    Proto: "HTTP/1.1",
    and returns an error more helpful for application developer.

PS: Make sure your PR includes/updates tests! If you need help with this part, just ask!

did run the unit test included in the issue and got

$ go test -run TestNextProto -v
=== RUN   TestNextProto
    client_server_test.go:1125: Dial: protocol "h2" was given but is not supported;
                                        sharing tls.Config with net/http Transport can cause this error
--- FAIL: TestNextProto (0.00s)
FAIL
exit status 1
FAIL    github.com/gorilla/websocket    0.176s

which is expected; can add this or some other form of unit test to assert on the error message if desired (seemed somewhat unnatural to assert on the error message, hence the omission)

Copy link

@amustaque97 amustaque97 left a comment

Choose a reason for hiding this comment

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

@ChannyClaus thank you for taking this forward. Changes look good to me.

cc @garyburd

@ghost
Copy link

ghost commented Jun 12, 2022

  • Testing for the known bad case "h2" reduces the likelihood of a false positive.
  • Error strings with embedded newlines are not conventional in Go. Use string concatenation to construct a single line error string on multiple source lines.
  • Many of the error strings in this package have the prefix "websocket:". This one should as well.
  • There should be a test that ensures that an error is returned when configs are shared with net/http.

// license that can be found in the LICENSE file.

//go:build go1.14
// +build go1.14
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ChannyClaus
Copy link
Contributor Author

thanks for the quick turnaround @amustaque97 @easterf - made the suggested changes, let me know if there's anything else!

@garyburd
Copy link
Contributor

Thank you for working on this.

The PR is what I was looking for when I wrote the issue, but the comment about false positives got me thinking. To avoid breaking code that happens to work today, move the test inside this if block. Return fmt.Errorf("<error text from PR>: %w", proto, err).

@ChannyClaus
Copy link
Contributor Author

@garyburd done! can also move the check into a separate function if need be (the indenting gets a little crazy now)

@garyburd
Copy link
Contributor

Please wrap the original error with the fmt %w verb.

@ChannyClaus
Copy link
Contributor Author

seems like using %w breaks compatibility with older versions of go + the rest of the code base seems to be using %v for formatting the error object:

$ git grep -rF "%v" | head
client.go:							"sharing tls.Config with net/http Transport can cause this error: %v",
client_server_http2_test.go:		t.Fatalf("Get: %v", err)
client_server_test.go:		t.Logf("path=%v, want %v", r.URL.Path, cstPath)
client_server_test.go:		t.Logf("query=%v, want %v", r.URL.RawQuery, cstRawQuery)
client_server_test.go:		t.Logf("subprotols=%v, want %v", subprotos, cstDialer.Subprotocols)
client_server_test.go:		t.Logf("Upgrade: %v", err)
client_server_test.go:		t.Logf("NextReader: %v", err)
client_server_test.go:		t.Logf("NextWriter: %v", err)
client_server_test.go:		t.Logf("NextWriter: %v", err)
client_server_test.go:		t.Logf("Close: %v", err)
chankang@Chans-MBP ~/websocket -  (master)
$ git grep -rF "%w"

@garyburd
Copy link
Contributor

The %v verb is appropriate in the other examples because those examples format the error as text.

This PR creates a new error value. The %w verb should be used to wrap the original error with the new error.

Somebody should submit a separate PR to remove Go versions < 1.13 from the CircleCi config. I made an attempt at that and failed. I don't have time to continue. We can proceed here once that PR is merged.

@amustaque97
Copy link

I’m checking on Circle CI from past few days because of some issues I’m facing in other project gorilla mux. CI doesn’t trigger all the time if we create branch n PR at the same time. Here is the link of same issue mentioned by Circle CI https://support.circleci.com/hc/en-us/articles/360008097173-Why-aren-t-pull-requests-triggering-jobs-on-my-organization-

If we all agree we can move from circle ci to GitHub actions else I can raise a PR to remove golang versions in circle ci.

Let me know what do you think?

@ChannyClaus
Copy link
Contributor Author

ChannyClaus commented Jun 21, 2022

@garyburd garyburd merged commit bc7ce89 into gorilla:master Jun 21, 2022
nono added a commit to cozy/cozy-stack that referenced this pull request Nov 6, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [github.com/gorilla/websocket](https://togithub.com/gorilla/websocket)
| require | patch | `v1.5.0` -> `v1.5.1` |

---

### Release Notes

<details>
<summary>gorilla/websocket (github.com/gorilla/websocket)</summary>

###
[`v1.5.1`](https://togithub.com/gorilla/websocket/releases/tag/v1.5.1)

[Compare
Source](https://togithub.com/gorilla/websocket/compare/v1.5.0...v1.5.1)

#### What's Changed

- Add check for Sec-WebSocket-Key header by
[@&#8203;hirasawayuki](https://togithub.com/hirasawayuki) in
[gorilla/websocket#752
- Changed the method name UnderlyingConn to NetConn by
[@&#8203;JWSong](https://togithub.com/JWSong) in
[gorilla/websocket#773
- remove all versions < 1.16 and add 1.18 by
[@&#8203;ChannyClaus](https://togithub.com/ChannyClaus) in
[gorilla/websocket#793
- Check for and report bad protocol in TLSClientConfig.NextProtos by
[@&#8203;ChannyClaus](https://togithub.com/ChannyClaus) in
[gorilla/websocket#788
- check err before GotConn for trace by
[@&#8203;junnplus](https://togithub.com/junnplus) in
[gorilla/websocket#798
- Update README.md by
[@&#8203;coreydaley](https://togithub.com/coreydaley) in
[gorilla/websocket#839
- Correct way to save memory using write buffer pool and freeing
net.http default buffers by [@&#8203;FMLS](https://togithub.com/FMLS) in
[gorilla/websocket#761
- Update go version & add verification/testing tools by
[@&#8203;coreydaley](https://togithub.com/coreydaley) in
[gorilla/websocket#840
- update golang.org/x/net by
[@&#8203;coreydaley](https://togithub.com/coreydaley) in
[gorilla/websocket#856
- update GitHub workflows by
[@&#8203;coreydaley](https://togithub.com/coreydaley) in
[gorilla/websocket#857

#### New Contributors

- [@&#8203;hirasawayuki](https://togithub.com/hirasawayuki) made their
first contribution in
[gorilla/websocket#752
- [@&#8203;JWSong](https://togithub.com/JWSong) made their first
contribution in
[gorilla/websocket#773
- [@&#8203;ChannyClaus](https://togithub.com/ChannyClaus) made their
first contribution in
[gorilla/websocket#793
- [@&#8203;junnplus](https://togithub.com/junnplus) made their first
contribution in
[gorilla/websocket#798
- [@&#8203;coreydaley](https://togithub.com/coreydaley) made their first
contribution in
[gorilla/websocket#839
- [@&#8203;FMLS](https://togithub.com/FMLS) made their first
contribution in
[gorilla/websocket#761

**Full Changelog**:
gorilla/websocket@v1.5.0...v1.5.1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 6am on Monday" in timezone
Europe/Paris, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/cozy/cozy-stack).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zMS41IiwidXBkYXRlZEluVmVyIjoiMzcuMzEuNSIsInRhcmdldEJyYW5jaCI6Im1hc3RlciJ9-->
algitbot pushed a commit to alpinelinux/build-server-status that referenced this pull request May 5, 2024
This MR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [github.com/gorilla/websocket](https://github.com/gorilla/websocket) | require | patch | `v1.5.0` -> `v1.5.1` |

---

### Release Notes

<details>
<summary>gorilla/websocket (github.com/gorilla/websocket)</summary>

### [`v1.5.1`](https://github.com/gorilla/websocket/releases/tag/v1.5.1)

[Compare Source](gorilla/websocket@v1.5.0...v1.5.1)

#### What's Changed

-   Add check for Sec-WebSocket-Key header by [@&#8203;hirasawayuki](https://github.com/hirasawayuki) in gorilla/websocket#752
-   Changed the method name UnderlyingConn to NetConn by [@&#8203;JWSong](https://github.com/JWSong) in gorilla/websocket#773
-   remove all versions < 1.16 and add 1.18 by [@&#8203;ChannyClaus](https://github.com/ChannyClaus) in gorilla/websocket#793
-   Check for and report bad protocol in TLSClientConfig.NextProtos by [@&#8203;ChannyClaus](https://github.com/ChannyClaus) in gorilla/websocket#788
-   check err before GotConn for trace by [@&#8203;junnplus](https://github.com/junnplus) in gorilla/websocket#798
-   Update README.md by [@&#8203;coreydaley](https://github.com/coreydaley) in gorilla/websocket#839
-   Correct way to save memory using write buffer pool and freeing net.http default buffers by [@&#8203;FMLS](https://github.com/FMLS) in gorilla/websocket#761
-   Update go version & add verification/testing tools by [@&#8203;coreydaley](https://github.com/coreydaley) in gorilla/websocket#840
-   update golang.org/x/net by [@&#8203;coreydaley](https://github.com/coreydaley) in gorilla/websocket#856
-   update GitHub workflows by [@&#8203;coreydaley](https://github.com/coreydaley) in gorilla/websocket#857

#### New Contributors

-   [@&#8203;hirasawayuki](https://github.com/hirasawayuki) made their first contribution in gorilla/websocket#752
-   [@&#8203;JWSong](https://github.com/JWSong) made their first contribution in gorilla/websocket#773
-   [@&#8203;ChannyClaus](https://github.com/ChannyClaus) made their first contribution in gorilla/websocket#793
-   [@&#8203;junnplus](https://github.com/junnplus) made their first contribution in gorilla/websocket#798
-   [@&#8203;coreydaley](https://github.com/coreydaley) made their first contribution in gorilla/websocket#839
-   [@&#8203;FMLS](https://github.com/FMLS) made their first contribution in gorilla/websocket#761

**Full Changelog**: gorilla/websocket@v1.5.0...v1.5.1

</details>

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

&nbsp;
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4yODYuMSIsInVwZGF0ZWRJblZlciI6IjM3LjI4Ni4xIiwidGFyZ2V0QnJhbmNoIjoibWFzdGVyIiwibGFiZWxzIjpbXX0=-->

See merge request alpine/infra/build-server-status!9
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.

[feature] Check for and report bad protocol in TLSClientConfig.NextProtos
3 participants