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

ttyd sends 1006 control code on non-zero exit, violating WebSocket spec #1395

Open
xozzslip opened this issue Aug 25, 2024 · 1 comment
Open
Labels

Comments

@xozzslip
Copy link

When running ttyd, if a command exits with a code other than 0, ttyd sends a WebSocket control code 1006. However, according to the WebSocket specification, code 1006 is a reserved value and should not be sent by the server explicitly. This behavior causes issues when running ttyd behind a proxy that uses the Gorilla WebSocket library, as it triggers an error when the proxy detects that the server has sent a 1006 code.

RFC 6455: The WebSocket Protocol
1006 is a reserved value and MUST NOT be set as a status code in a
Close control frame by an endpoint. It is designated for use in
applications expecting a status code to indicate that the
connection was closed abnormally, e.g., without sending or
receiving a Close control frame.

@xozzslip xozzslip added the bug label Aug 25, 2024
@xozzslip
Copy link
Author

I use the 1011 code instead of 1006 for non-zero exit codes. Binary is here if anyone needs it: https://github.com/xozzslip/ttyd/releases/tag/1.7.7

diff --git a/src/protocol.c b/src/protocol.c
index 53e65d4..d9d0865 100644
--- a/src/protocol.c
+++ b/src/protocol.c
@@ -86,7 +86,7 @@ static void process_read_cb(pty_process *process, pty_buf_t *buf, bool eof) {
   }

   if (eof && !process_running(process))
-    ctx->pss->lws_close_status = process->exit_code == 0 ? 1000 : 1006;
+    ctx->pss->lws_close_status = process->exit_code == 0 ? 1000 : 1011;
   else
     ctx->pss->pty_buf = buf;
   lws_callback_on_writable(ctx->pss->wsi);
@@ -101,7 +101,7 @@ static void process_exit_cb(pty_process *process) {

   lwsl_notice("process exited with code %d, pid: %d\n", process->exit_code, process->pid);
   ctx->pss->process = NULL;
-  ctx->pss->lws_close_status = process->exit_code == 0 ? 1000 : 1006;
+  ctx->pss->lws_close_status = process->exit_code == 0 ? 1000 : 1011;
   lws_callback_on_writable(ctx->pss->wsi);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant