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

Validate JWT header field #144

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var (
// ErrUnsupportedAlg indicates that given algorithm is not supported.
ErrUnsupportedAlg = errors.New("algorithm is not supported")

// ErrNotJWTType indicates that JWT token type is not JWT.
ErrNotJWTType = errors.New("token of not JWT type")

// ErrInvalidFormat indicates that token format is not valid.
ErrInvalidFormat = errors.New("token format is not valid")

Expand Down
3 changes: 3 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func parse(token []byte) (*Token, error) {
header: header,
claims: claims,
}
if !constTimeEqual(tk.header.Type, "JWT") {
return nil, ErrNotJWTType
}
return tk, nil
}

Expand Down
12 changes: 11 additions & 1 deletion parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ func TestParseAnotherAlgorithm(t *testing.T) {
}
}

func TestParseWrongType(t *testing.T) {
tokenHS256 := `eyJhbGciOiJIUzI1NiIsInR5cCI6IkJPTUJPTSJ9.eyJqdGkiOiJqdXN0IGFuIGlkIiwiYXVkIjoiYXVkaWVuY2UifQ.t5oEdZGp0Qbth7lo5fZlV_o4-r9gMoYBSktXbarjWoo`
verifier := mustVerifier(NewVerifierHS(HS256, []byte("key")))

_, err := Parse([]byte(tokenHS256), verifier)
if err == nil {
t.Fatal()
}
}

func TestParseMalformed(t *testing.T) {
f := func(got string) {
t.Helper()
Expand All @@ -81,7 +91,7 @@ func TestParseMalformed(t *testing.T) {
f(`eyJ.xyz`)
f(`eyJ!.x!yz.e30`)
f(`eyJ.xyz.xyz`)
f(`eyJhIjoxMjN9.x!yz.e30`) // `e30` is JSON `{}` in base64
f(`eyJhIjoxMjN9.x!yz.e30`) // `e30` is JSON `{}` in base64.
f(`eyJhIjoxMjN9.e30.x!yz`)
}

Expand Down
Loading