Harden JWT decode path#59
Conversation
Add issuer prefix validation on decode to match the encode-side check already in DoEncode, aligning with the Go nats-io/jwt library's Decode function. Add a 1MB MaxTokenSize cap on JWT input to prevent memory amplification. Wrap raw framework exceptions (JsonException, FormatException, etc.) in NatsJwtException so callers get the documented exception type. Validate seed in FormatUserConfig via KeyPair.FromSeed instead of a 2-char prefix check to prevent newline injection in creds files. Add Go cross-compat tests for operator, account, and user claims in both directions, plus a test confirming both .NET and Go reject wrong-prefix issuers.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #59 +/- ##
==========================================
- Coverage 85.61% 85.44% -0.17%
==========================================
Files 51 51
Lines 723 756 +33
Branches 90 97 +7
==========================================
+ Hits 619 646 +27
- Misses 77 81 +4
- Partials 27 29 +2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Add tests for null/oversized JWT, malformed base64, invalid JSON, null/invalid seed, wrong seed prefix, and malformed JWT format in FormatUserConfig. Covers the new error paths added in the decode hardening to restore code coverage.
aricart
left a comment
There was a problem hiding this comment.
Not sure why full validations in the end .net should be about generating.
| throw new NatsJwtException("JWT is null"); | ||
| } | ||
|
|
||
| if (seed == null) |
There was a problem hiding this comment.
fixed. now seed is optional.
Make seed parameter optional so bearer token users can format a creds file with just the JWT block, without requiring a signing key.
|
@aricart here's the full list of checks added in this PR so you can flag any you think are unnecessary: FormatUserConfig:
DecodeClaims (all Decode methods):*
NatsJwtException:
Most of these are basic input sanity, not full Go-style validation. Let me know if any of these are out of scope. |
Hardens the decode path and FormatUserConfig with input validation that was missing compared to the Go nats-io/jwt library.