Skip to content

Reject credentials without a JWT marker instead of looping at EOF - #1020

Open
AayushDani wants to merge 2 commits into
nats-io:mainfrom
AayushDani:fix/reject-missing-jwt-marker
Open

AayushDani wants to merge 2 commits into
nats-io:mainfrom
AayushDani:fix/reject-missing-jwt-marker

Conversation

@AayushDani

Copy link
Copy Markdown

The legacy client's JWT credential reader loops forever at EOF when a credential file or RawCredentials value has no BEGIN NATS USER JWT marker. Because this reader runs synchronously, the loop can block the asyncio thread; an asyncio timeout cannot interrupt it.

Raise the existing InvalidUserCredentialsError on EOF. Regression tests cover empty and seed-only synthetic input through string paths, Path, and RawCredentials, using a bounded subprocess so the old behavior cannot hang the test runner. Valid JWT blocks after a preamble remain covered. This change leaves reconnect/retry policy unchanged.

Validation on Python 3.12.14/macOS arm64 with NATS Server 2.14.6:

  • Full legacy suite: 339 passed, 9 skipped, 54 subtests passed.
  • Authentication suite: 16 passed.
  • Repository Ruff, formatting, and spelling checks passed.
  • A synthetic TCP handshake reproduction blocks on the base revision despite an asyncio timeout and raises the expected error with the patch.

The other Python/OS combinations and modern-client runtime tests remain for upstream CI.

AI assistance: OpenAI Codex implemented and tested this change.

@caspervonb
caspervonb self-requested a review September 14, 2026 07:29
@caspervonb caspervonb added the nats-legacy Legacy asyncio NATS client label Sep 14, 2026

@caspervonb caspervonb left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm, draft tho?

@AayushDani
AayushDani marked this pull request as ready for review September 16, 2026 19:11
@AayushDani
AayushDani force-pushed the fix/reject-missing-jwt-marker branch from df5558e to 699fdb2 Compare September 16, 2026 19:11
@AayushDani

Copy link
Copy Markdown
Author

lgtm, draft tho?

Marked ready for review. @caspervonb can you please merge the PR?

@AayushDani
AayushDani force-pushed the fix/reject-missing-jwt-marker branch 2 times, most recently from ce16b65 to 2822a91 Compare September 17, 2026 01:19
@wallyqs
wallyqs force-pushed the fix/reject-missing-jwt-marker branch from 2822a91 to be2afae Compare September 30, 2026 06:11
@wallyqs
wallyqs force-pushed the fix/reject-missing-jwt-marker branch from be2afae to 5c450d9 Compare October 15, 2026 08:35
@caspervonb

caspervonb commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

PTAL @claude

@synadia-claude-reviewer

synadia-claude-reviewer Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Claude finished @caspervonb's task in 1m 28s —— View job


Review

  • Gather context (diff, relevant source/tests, error class usage)
  • Analyze fix in nats/src/nats/aio/client.py
  • Analyze regression tests in nats/tests/test_client_nkeys.py
  • Post feedback

Verdict: looks correct and safe to merge.

nats/src/nats/aio/client.py:743-745 — The fix is minimal and correct. f.readline() returns b"" at EOF, which bytearray(b"") preserves as falsy, so if not line: raise errors.InvalidUserCredentialsError cleanly breaks the previous infinite loop (the old code checked b"BEGIN NATS USER JWT" in line forever without ever testing for EOF). Raising the class without parens matches the existing style used throughout this file (e.g. errors.BadSubjectError, errors.ConnectionClosedError).

One minor style nit, not a blocker: the new code raises errors.InvalidUserCredentialsError directly, while the neighboring _read_creds_user_nkey (line 722) raises the deprecated alias ErrInvalidUserCredentials imported from .errors. Both resolve to the same exception type (ErrInvalidUserCredentials subclasses nats.errors.InvalidUserCredentialsError), so behavior is identical — just a slight inconsistency in which name is used within the same file. Not worth blocking on.

nats/tests/test_client_nkeys.py — Good regression coverage:

  • Empty and seed-only-without-JWT-marker inputs are tested across RawCredentials, str path, and Path inputs.
  • Running the EOF-triggering case in a bounded subprocess (5s timeout) is a sound way to prove the old code actually hangs, without risking the test runner itself hanging on a regression.
  • A valid-JWT-after-preamble case is covered in-process (no hang risk there), confirming the fix doesn't affect the happy path.
  • Imports (NATS, RawCredentials, subprocess, sys) are all available/added correctly; the subprocess script correctly imports Client/RawCredentials/InvalidUserCredentialsError and asserts the expected exception type.

No functional issues found. This is a clean, narrowly-scoped fix for a real synchronous-hang bug, consistent with the repo's legacy-client conventions (Apache header file, unittest/unittest-adjacent pytest test style, ruff-compatible formatting by inspection). I was not able to execute ruff/pytest in this review session (read-only tooling), but the diff is small enough to verify by inspection and matches surrounding style conventions.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nats-legacy Legacy asyncio NATS client

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants