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

reverseproxy: Mask the WS close message when we're the client #5199

Merged
merged 5 commits into from
Nov 14, 2022

Conversation

francislavoie
Copy link
Member

While testing a NodeJS app of mine, I noticed that since our changes to the proxy to send a WebSocket close frame, when Caddy would close first, we would see RangeError: Invalid WebSocket frame: MASK must be set in our logs from https://github.com/websockets/ws.

After some investigation, it looks like when we're the "client" (i.e. the upstream half of the proxy, whereas for the downstream half we act as the "server"), we need to mask the message according to https://www.rfc-editor.org/rfc/rfc6455#section-5.3.

I copied the maskKey and maskBytes code from https://github.com/websockets/ws as noted in code comments.

Needs some testing to make sure this still works correctly.

@francislavoie francislavoie added the bug 🐞 Something isn't working label Nov 11, 2022
@francislavoie francislavoie added this to the v2.6.3 milestone Nov 11, 2022
@francislavoie
Copy link
Member Author

Tested it with my NodeJS app, seems to work! I see both the client and server closing the connection when I stop Caddy, and the error went away on the server.

@francislavoie francislavoie marked this pull request as ready for review November 11, 2022 06:00
@climba03003
Copy link

climba03003 commented Nov 11, 2022

I just notice the same on my server. It crashed the whole application and require hard restart to resume the upstream.

@francislavoie
Copy link
Member Author

francislavoie commented Nov 11, 2022

@climba03003 could you try out a build with this PR to confirm the fix works for you too?

What made you find this issue, is this just an insane coincidence?? I only just discovered this problem a few hours ago.

@climba03003
Copy link

climba03003 commented Nov 11, 2022

What made you find this issue, is this just an insane coincidence??

I am migrating my company from nginx to caddy in this few days. I notice it from one of my reporter that the application is down without any reason (e.g. no restart of server, no code changes).
So, I search a bit today and see this fix.

could you try out a build with this PR to confirm the fix works for you too?

I am using container for the application, it will be a bit hard to use a custom build.

@francislavoie
Copy link
Member Author

I am using container for the application, it will be a bit hard to use a custom build.

No, it should be super easy, actually.

FROM caddy:2.6.2-builder AS builder

# TODO: Temporarily using a commit from https://github.com/caddyserver/caddy/pull/5199
# until that pull request is merged.
RUN xcaddy build 775e352b6feb6f85f2066a8933d3e0e9751b32a0

FROM caddy:2.6.2-alpine
COPY --from=builder /usr/bin/caddy /usr/bin/caddy

@climba03003
Copy link

Good to know that, I will report back after the reporter test.

@climba03003
Copy link

Seems it is not fixed in my end. I might need to patch the outside caddy as well.

stateDiagram
  [*] --> Caddy(2.6.2)
  state Container {
    Caddy(775e352b6feb6f85f2066a8933d3e0e9751b32a0) --> Node.JS
    Node.JS --> [*]
  }
  Caddy(2.6.2) --> Container
Loading

@francislavoie
Copy link
Member Author

Probably. It depends which one is reloaded or restarted, as that's the one the close frame will be sent from.

@climba03003
Copy link

After running of 3 days, it does solve the issue. Thanks.

@francislavoie
Copy link
Member Author

Thanks!

We'll merge as soon as Matt has a chance to give this a look.

Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for figuring this out, Francis! 😁

@mholt mholt merged commit ee7c92e into master Nov 14, 2022
@mholt mholt deleted the websocket-masking branch November 14, 2022 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants