fix: added a check for the size of JWT token#248
Conversation
Note that now all JWTs are limited to 1MB - which may or not be breaking. Note that Generic JWTs could possibly have whatever size they want but this change affects all decoding. Signed-off-by: Alberto Ricart <alberto@synadia.com>
|
Claude finished @aricart's task in 1m 32s —— View job PR Review: fix: added a check for the size of JWT token
SummaryThe change is a straightforward and reasonable security/resource protection measure. The logic is correct and the test covers the basic case. Two issues are worth addressing: Issue 1:
|
v2/decoder.go
Outdated
| // not valid or verification fails an error is returned. | ||
| func Decode(token string) (Claims, error) { | ||
| if len(token) > MaxTokenSize { | ||
| return nil, fmt.Errorf("token size %d exceeds maximum of %d bytes", len(token), MaxTokenSize) |
There was a problem hiding this comment.
Consider using a sentinel error instead of a formatted string. Callers may want to programmatically detect this specific error condition:
| return nil, fmt.Errorf("token size %d exceeds maximum of %d bytes", len(token), MaxTokenSize) | |
| if len(token) > MaxTokenSize { | |
| return nil, fmt.Errorf("token size %d exceeds maximum of %d bytes: %w", len(token), MaxTokenSize, ErrTokenTooLarge) | |
| } |
Where ErrTokenTooLarge = errors.New("token too large") is exported. This allows callers to do errors.Is(err, jwt.ErrTokenTooLarge) instead of string matching.
v2/decoder_test.go
Outdated
| if err == nil { | ||
| t.Fatal("expected error for oversized token") | ||
| } | ||
| if !strings.Contains(err.Error(), "exceeds maximum") { |
There was a problem hiding this comment.
Missing boundary test: the test only checks MaxTokenSize+1 (over limit) but doesn't verify that a token of exactly MaxTokenSize bytes is accepted. Consider adding:
// Exactly at the limit should not error on size (will fail on format)
token = strings.Repeat("a", MaxTokenSize)
_, err = Decode(token)
if err != nil && strings.Contains(err.Error(), "exceeds maximum") {
t.Fatal("token at exactly MaxTokenSize should not be rejected for size")
}|
DecodeGeneric has very specific uses that are application defined, so putting a limit is assume that we understand how it is used - this is not in any direct flow to the server etc. |
Signed-off-by: Alberto Ricart <alberto@synadia.com>
Note that now all JWTs are limited to 1MB - which may or not be breaking. Note that Generic JWTs could possibly have whatever size they want but this change affects all decoding.