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

[websocket] Add a config to reject initial connection #750

Merged
merged 3 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 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
9 changes: 9 additions & 0 deletions handler/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Config struct {
tracer graphql.Tracer
complexityLimit int
complexityLimitFunc graphql.ComplexityLimitFunc
websocketOnInitFunc func(ctx context.Context, initPayload InitPayload) bool
eddeee888 marked this conversation as resolved.
Show resolved Hide resolved
disableIntrospection bool
connectionKeepAlivePingInterval time.Duration
uploadMaxMemory int64
Expand Down Expand Up @@ -250,6 +251,14 @@ func (tw *tracerWrapper) EndOperationExecution(ctx context.Context) {
tw.tracer1.EndOperationExecution(ctx)
}

// WebsocketOnInitFunc is called when the server receives connection init message from the client.
// This can be used to check initial payload to see whether to accept the websocket connection.
func WebsocketOnInitFunc(websocketOnInitFunc func(ctx context.Context, initPayload InitPayload) bool) Option {
Sonna marked this conversation as resolved.
Show resolved Hide resolved
return func(cfg *Config) {
cfg.websocketOnInitFunc = websocketOnInitFunc
}
}

// CacheSize sets the maximum size of the query cache.
// If size is less than or equal to 0, the cache is disabled.
func CacheSize(size int) Option {
Expand Down
11 changes: 10 additions & 1 deletion handler/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

"github.com/99designs/gqlgen/graphql"
"github.com/gorilla/websocket"
"github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru"
Copy link
Contributor

Choose a reason for hiding this comment

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

🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My IDE linter does this automatically 😬

"github.com/vektah/gqlparser"
"github.com/vektah/gqlparser/ast"
"github.com/vektah/gqlparser/gqlerror"
Expand Down Expand Up @@ -94,6 +94,15 @@ func (c *wsConnection) init() bool {
}
}

if c.cfg.websocketOnInitFunc != nil {
isValidPayload := c.cfg.websocketOnInitFunc(c.ctx, c.initPayload)
if !isValidPayload {
eddeee888 marked this conversation as resolved.
Show resolved Hide resolved
c.sendConnectionError("invalid init payload")
c.close(websocket.CloseNormalClosure, "terminated")
return false
}
}

c.write(&operationMessage{Type: connectionAckMsg})
case connectionTerminateMsg:
c.close(websocket.CloseNormalClosure, "terminated")
Expand Down
50 changes: 50 additions & 0 deletions handler/websocket_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"context"
"encoding/json"
"net/http/httptest"
"strings"
Expand Down Expand Up @@ -158,6 +159,55 @@ func TestWebsocketWithKeepAlive(t *testing.T) {
})
}

func TestWebsocketOnInitFunc(t *testing.T) {
next := make(chan struct{})

t.Run("accept connection if WebsocketOnInitFunc is NOT provided", func(t *testing.T) {
h := GraphQL(&executableSchemaStub{next})
srv := httptest.NewServer(h)
defer srv.Close()

c := wsConnect(srv.URL)
defer c.Close()

require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionInitMsg}))

require.Equal(t, connectionAckMsg, readOp(c).Type)
})

t.Run("accept connection if WebsocketOnInitFunc is provided and is accepting connection", func(t *testing.T) {
h := GraphQL(&executableSchemaStub{next}, WebsocketOnInitFunc(func(ctx context.Context, initPayload InitPayload) bool {
return true
}))
srv := httptest.NewServer(h)
defer srv.Close()

c := wsConnect(srv.URL)
defer c.Close()

require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionInitMsg}))

require.Equal(t, connectionAckMsg, readOp(c).Type)
})

t.Run("reject connection if WebsocketOnInitFunc is provided and is accepting connection", func(t *testing.T) {
h := GraphQL(&executableSchemaStub{next}, WebsocketOnInitFunc(func(ctx context.Context, initPayload InitPayload) bool {
return false
}))
srv := httptest.NewServer(h)
defer srv.Close()

c := wsConnect(srv.URL)
defer c.Close()

require.NoError(t, c.WriteJSON(&operationMessage{Type: connectionInitMsg}))

msg := readOp(c)
require.Equal(t, connectionErrorMsg, msg.Type)
require.Equal(t, `{"message":"invalid init payload"}`, string(msg.Payload))
})
}

func wsConnect(url string) *websocket.Conn {
c, _, err := websocket.DefaultDialer.Dial(strings.Replace(url, "http://", "ws://", -1), nil)
if err != nil {
Expand Down