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

Conversation

eddeee888
Copy link
Contributor

@eddeee888 eddeee888 commented Jun 18, 2019

Problem

Currently, we are allowing clients to always connect to the server unless the message is invalid. Current implementation here

This is not safe as a malicious user can connect to the WebSocket and perform queries/mutations. An example of such message:

Screen Shot 2019-06-18 at 11 05 15 am

We would have to check in every query/mutation/subscription to make sure that a user can perform such action (related PR: #348).

Proposal

This PR adds a new function to handler config called websocketOnInitFunc. This can be used by the server to check the init payload to decide whether to establish the connection or reject it. If this function is not given, the connection is always established.

Example workflow

This is an example workflow, different apps may have different approaches:

  1. The client sends a WebSocket upgrade request with init payload to the server and a token in cookie
  2. The server receives the request, picks out the cookie and put it in context
  3. When the init function is called, it will use the init payload and compare with the cookie in context
    4a. If they are the same, create the connection
    4b. If they are not the same, close the connection and send back a invalid init payload message

Accepted connection:
Screen Shot 2019-06-18 at 10 53 02 am

Rejected connection:
Screen Shot 2019-06-18 at 10 53 18 am

I have:

  • Added tests covering the bug / feature (see testing)
  • Updated any relevant documentation (see docs) - None

@eddeee888 eddeee888 requested review from vektah and sinamt June 18, 2019 01:28
@@ -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 😬

handler/graphql.go Outdated Show resolved Hide resolved
handler/graphql.go Outdated Show resolved Hide resolved
handler/websocket.go Outdated Show resolved Hide resolved
@@ -30,6 +30,8 @@ type params struct {
Variables map[string]interface{} `json:"variables"`
}

type websocketInitFunc func(ctx context.Context, initPayload InitPayload) error
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the interface to return error instead of bool

@@ -94,6 +94,14 @@ func (c *wsConnection) init() bool {
}
}

if c.cfg.websocketInitFunc != nil {
if err := c.cfg.websocketInitFunc(c.ctx, c.initPayload); err != nil {
c.sendConnectionError(err.Error())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The server now has the option to return an error. The error body is then sent to the client

@eddeee888 eddeee888 merged commit 726a94f into master Jun 19, 2019
@eddeee888 eddeee888 deleted the ws-connection-param-check branch June 19, 2019 23:34
@vektah vektah added the v0.9.1 fixed in v0.9.1 label Jun 27, 2019
cgxxv pushed a commit to cgxxv/gqlgen that referenced this pull request Mar 25, 2022
…heck

[websocket] Add a config to reject initial connection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v0.9.1 fixed in v0.9.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants