Skip to content

[FIXED] Preserve websocket auth errors during connect#2058

Open
officialasishkumar wants to merge 1 commit intonats-io:mainfrom
officialasishkumar:fix-ws-connect-auth-errors
Open

[FIXED] Preserve websocket auth errors during connect#2058
officialasishkumar wants to merge 1 commit intonats-io:mainfrom
officialasishkumar:fix-ws-connect-auth-errors

Conversation

@officialasishkumar
Copy link
Copy Markdown

  • coalesce CONNECT and PING into a single direct websocket write so auth failures are not exposed as write-side transport errors
  • preserve buffered protocol bytes when websocketReader and natsReader receive data alongside EOF, so initial -ERR lines are surfaced correctly during connect
  • add regression coverage for websocket auth-failure connect and reader EOF edge cases

Resolves #2024

Signed-off-by: Asish Kumar officialasishkumar@gmail.com

Coalesce CONNECT and PING into a single direct write so websocket connect handshakes are not split across frames during authentication failures.

Preserve buffered protocol data when websocket and NATS readers receive data alongside EOF so initial -ERR lines are surfaced as ErrAuthorization instead of transport-level broken pipe or EOF errors.

Add regression coverage for the reader edge cases and the public websocket auth-failure connect path.

Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
@synadia-claude-reviewer
Copy link
Copy Markdown

Claude Code is working…

I'll analyze this and get back to you.

View job run

@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 85.399% (-0.1%) from 85.504% — officialasishkumar:fix-ws-connect-auth-errors into nats-io:main

wallyqs added a commit to wallyqs/nats.go that referenced this pull request Apr 9, 2026
Add TestWSMockServerAuthFailureWithEOF which reproduces the exact
condition from issue nats-io#2024: a websocket server sends -ERR 'Authorization
Violation' + close frame and immediately closes the TCP connection, so
the error data and EOF may arrive on the same underlying read. Without
the fixes in PR nats-io#2058, this test fails with "Expected ErrAuthorization,
got: EOF".

Also adds nats-server test dependency to go.mod so all tests compile.

https://claude.ai/code/session_01364JzLu2QseCg45NmNVa31
wallyqs added a commit to wallyqs/nats.go that referenced this pull request Apr 9, 2026
Add TestWSExpiredJWTAuthReturnsAuthError which sets up a full JWT auth
infrastructure (operator trust + account resolver) on a websocket server
and connects with an already-expired user JWT. This directly reproduces
the scenario from issue nats-io#2024 where expired JWT tokens over websocket
returned EOF instead of a proper auth error.

Without the fixes in PR nats-io#2058, this test fails with:
  Expected auth error (ErrAuthorization or ErrAuthExpired), got: EOF

https://claude.ai/code/session_01364JzLu2QseCg45NmNVa31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EOF returned instead of real error when connecting with websockets and an expired jwt token

2 participants