Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: ensure websocket close messages are truncated to 123 bytes#779

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
coadler merged 6 commits intomainfromcolin/ws-max-close-frame
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
NextNext commit
fix: ensure websocket close messages are truncated to 123 bytes
It's possible for websocket close messages to be too long, which causethem to silently fail without a proper close message. See error below:```2022-03-31 17:08:34.862 [INFO](stdlib)<close_notjs.go:72>"2022/03/31 17:08:34 websocket: failed to marshal close frame: reason string max is 123 but got \"insert provisioner daemon:Cannot encode []database.ProvisionerType into oid 19098 - []database.ProvisionerType must implement Encoder or be converted to a string\" with length 161"```
  • Loading branch information
@coadler
coadler committedMar 31, 2022
commit9472717144766dbebce546d1acd252eff26a76d2
3 changes: 2 additions & 1 deletioncli/configssh_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,10 +4,11 @@ import (
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/pty/ptytest"
"github.com/stretchr/testify/require"
)

func TestConfigSSH(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletioncli/ssh.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,11 +13,12 @@ import (
gossh "golang.org/x/crypto/ssh"
"golang.org/x/xerrors"

"golang.org/x/crypto/ssh/terminal"

"github.com/coder/coder/cli/cliflag"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/codersdk"
"golang.org/x/crypto/ssh/terminal"
)

func ssh() *cobra.Command {
Expand Down
8 changes: 4 additions & 4 deletionscoderd/provisionerdaemons.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -56,7 +56,7 @@ func (api *api) provisionerDaemonsListen(rw http.ResponseWriter, r *http.Request
Provisioners: []database.ProvisionerType{database.ProvisionerTypeEcho, database.ProvisionerTypeTerraform},
})
if err != nil {
_ = conn.Close(websocket.StatusInternalError,fmt.Sprintf("insert provisioner daemon:%s", err))
_ = conn.Close(websocket.StatusInternalError,fmtWebsocketCloseMsg("insert provisioner daemon: %s", err))
return
}

Expand All@@ -67,7 +67,7 @@ func (api *api) provisionerDaemonsListen(rw http.ResponseWriter, r *http.Request
config.LogOutput = io.Discard
session, err := yamux.Server(websocket.NetConn(r.Context(), conn, websocket.MessageBinary), config)
if err != nil {
_ = conn.Close(websocket.StatusInternalError,fmt.Sprintf("multiplex server: %s", err))
_ = conn.Close(websocket.StatusInternalError,fmtWebsocketCloseMsg("multiplex server: %s", err))
return
}
mux := drpcmux.New()
Expand All@@ -80,13 +80,13 @@ func (api *api) provisionerDaemonsListen(rw http.ResponseWriter, r *http.Request
Logger: api.Logger.Named(fmt.Sprintf("provisionerd-%s", daemon.Name)),
})
if err != nil {
_ = conn.Close(websocket.StatusInternalError,fmt.Sprintf("drpc register provisioner daemon: %s", err))
_ = conn.Close(websocket.StatusInternalError,fmtWebsocketCloseMsg("drpc register provisioner daemon: %s", err))
return
}
server := drpcserver.New(mux)
err = server.Serve(r.Context(), session)
if err != nil {
_ = conn.Close(websocket.StatusInternalError,fmt.Sprintf("serve: %s", err))
_ = conn.Close(websocket.StatusInternalError,fmtWebsocketCloseMsg("serve: %s", err))
return
}
_ = conn.Close(websocket.StatusGoingAway, "")
Expand Down
2 changes: 1 addition & 1 deletioncoderd/workspaceresources.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -108,7 +108,7 @@ func (api *api) workspaceResourceDial(rw http.ResponseWriter, r *http.Request) {
Pubsub: api.Pubsub,
})
if err != nil {
_ = conn.Close(websocket.StatusInternalError,fmt.Sprintf("serve: %s", err))
_ = conn.Close(websocket.StatusInternalError,fmtWebsocketCloseMsg("serve: %s", err))
return
}
}
Expand Down
40 changes: 40 additions & 0 deletionscoderd/ws.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
package coderd

import (
"fmt"
"strings"
)

const websocketCloseMaxLen = 123

// fmtWebsocketCloseMsg formats a websocket close message and ensures it is
// truncated to the maximum allowed length.
func fmtWebsocketCloseMsg(format string, vars ...any) string {
msg := fmt.Sprintf(format, vars...)

// Cap msg length at 123 bytes. nhooyr/websocket only allows close messages
// of this length.
if len(msg) > websocketCloseMaxLen {
return truncateString(msg, websocketCloseMaxLen)
}

return msg
}

// truncateString safely truncates a string to a maximum size of byteLen. It
// writes whole runes until a single rune would increase the string size above
// byteLen.
func truncateString(str string, byteLen int) string {
builder := strings.Builder{}
builder.Grow(byteLen)

for _, char := range str {
if builder.Len()+len(string(char)) > byteLen {
break
}

_, _ = builder.WriteRune(char)
}

return builder.String()
Copy link
Member

Choose a reason for hiding this comment

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

Could we do[]byte(str)[:websocketCloseMaxLen] instead?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

You can't safely slice strings, because characters can be more than 1 byte. This could cause us to slice in the middle of a single character.

Copy link
Member

Choose a reason for hiding this comment

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

Could we do[]rune(str)[:websocketCloseMaxLen] in that case?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

That would limit the rune count instead of the byte count

Copy link
Member

Choose a reason for hiding this comment

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

I left a brainded comment

Copy link
Member

Choose a reason for hiding this comment

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

You are correct sir

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

i made it simpler tho and got rid of the string builder

}
30 changes: 30 additions & 0 deletionscoderd/ws_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
// This file tests an internal function.
//nolint:testpackage
package coderd

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_websocketCloseMsg(t *testing.T) {
t.Parallel()

t.Run("TruncateSingleByteCharacters", func(t *testing.T) {
t.Parallel()

msg := strings.Repeat("d", 255)
trunc := fmtWebsocketCloseMsg(msg)
assert.LessOrEqual(t, len(trunc), 123)
})

t.Run("TruncateMultiByteCharacters", func(t *testing.T) {
t.Parallel()

msg := strings.Repeat("こんにちは", 10)
trunc := fmtWebsocketCloseMsg(msg)
assert.LessOrEqual(t, len(trunc), 123)
})
}
1 change: 1 addition & 0 deletionsprovisioner/terraform/serve.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,7 @@ import (
"cdr.dev/slog"

"github.com/cli/safeexec"

"github.com/coder/coder/provisionersdk"

"github.com/hashicorp/hc-install/product"
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp