Skip to content

Potential leak when applications don't read from the connection #93

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

Closed
propertyscott opened this issue Jun 5, 2019 · 8 comments · Fixed by #94
Closed

Potential leak when applications don't read from the connection #93

propertyscott opened this issue Jun 5, 2019 · 8 comments · Fixed by #94
Milestone

Comments

@propertyscott
Copy link

The readme says:

nhooyr/websocket also responds to pings, pongs and close frames in a separate goroutine so that your application doesn't always need to read from the connection unless it expects a data message.

If an adversary sends a data message to an application that's not expecting a data message, then the read loop blocks forever.

@propertyscott
Copy link
Author

A failed write unblocks the goroutine, but it might be some time before the application writes to the connection.

@nhooyr
Copy link
Contributor

nhooyr commented Jun 5, 2019

🤔

I don't think there's anything we can do here, its just normal connection behaviour. If the application isn't reading data messages from the connection, we can't read it for the application, we have to block until the application reads or the connection is closed (whether explicitly, via a failed write or a failed ping/pong).

All applications that are not reading from the connection should have a timeout anyway on how long the connection will be kept up, otherwise they're buggy. I guess this should be made more clear as right now it sort of sounds like you can just rely on c.Context() to tell if you 100% if the connection is alive or not.

What would you have it do?

Most I can think of is setting a timeout on the readLoop waiting for the application code to read. Maybe 1 minute would be reasonable? I had considered this when writing but it seemed too magical but I suppose a little magic is better than risking a security issue for the users of this package.

Or maybe we should ping/pong in the background by default? Maybe every 2 minutes? Its very cheap so I can't imagine any user of the package objecting.

@propertyscott
Copy link
Author

The issue can be viewed as a documentation problem. Require applications to receive messages, even when the application is not interested. Add a helper function to do this.

@nhooyr nhooyr changed the title goroutine leak Potential goroutine leak when applications don't read from the connection Jun 5, 2019
@nhooyr nhooyr changed the title Potential goroutine leak when applications don't read from the connection Potential leak when applications don't read from the connection Jun 5, 2019
@propertyscott
Copy link
Author

This is something that we noticed while evaluating the package. Our application always reads the connection, so this has no impact on us should we decide to use the package.

@nhooyr
Copy link
Contributor

nhooyr commented Jun 5, 2019

Sounds good, thanks for the report.

@nhooyr
Copy link
Contributor

nhooyr commented Jun 6, 2019

The issue can be viewed as a documentation problem. Require applications to receive messages, even when the application is not interested. Add a helper function to do this.

It makes the a little API fragile to require that as if you miss that single line, you introduce a vulnerability into your code. I think it's best the readLoop time out after 2 minutes if the application doesn't read the incoming message. It's nice to not even have to think about it. Does that seem reasonable to you as well?

Regarding the ping solution I proposed, we can't ping for package users because we can't read pongs until the package user reads the data message.

@propertyscott
Copy link
Author

I do not think that the package should try to hide the detail that the connection must be read.

We ended our evaluation of the package. Thank you for responding to my issues.

@nhooyr
Copy link
Contributor

nhooyr commented Jun 6, 2019

I agree, will fix this tomorrow night.

Thank you for the high quality issue reports.

nhooyr added a commit that referenced this issue Jun 7, 2019
@nhooyr nhooyr mentioned this issue Jun 7, 2019
@nhooyr nhooyr added this to the v1.2.0 milestone Jun 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants