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

jwt: enforce the presence of a space between the authentication scheme and token #1190

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
70 changes: 50 additions & 20 deletions jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,61 @@ func TestJwtFromHeader(t *testing.T) {
}
}()

for _, test := range hamac {
// Arrange
app := fiber.New()
t.Run("regular", func(t *testing.T) {
for _, test := range hamac {
// Arrange
app := fiber.New()

app.Use(jwtware.New(jwtware.Config{
SigningKey: jwtware.SigningKey{
JWTAlg: test.SigningMethod,
Key: []byte(defaultSigningKey),
},
}))

app.Get("/ok", func(c *fiber.Ctx) error {
return c.SendString("OK")
})

req := httptest.NewRequest("GET", "/ok", nil)
req.Header.Add("Authorization", "Bearer "+test.Token)

// Act
resp, err := app.Test(req)

// Assert
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
}
})

app.Use(jwtware.New(jwtware.Config{
SigningKey: jwtware.SigningKey{
JWTAlg: test.SigningMethod,
Key: []byte(defaultSigningKey),
},
}))
t.Run("malformed header", func(t *testing.T) {
for _, test := range hamac {
// Arrange
app := fiber.New()

app.Get("/ok", func(c *fiber.Ctx) error {
return c.SendString("OK")
})
app.Use(jwtware.New(jwtware.Config{
SigningKey: jwtware.SigningKey{
JWTAlg: test.SigningMethod,
Key: []byte(defaultSigningKey),
},
}))

req := httptest.NewRequest("GET", "/ok", nil)
req.Header.Add("Authorization", "Bearer "+test.Token)
app.Get("/ok", func(c *fiber.Ctx) error {
return c.SendString("OK")
})

// Act
resp, err := app.Test(req)
req := httptest.NewRequest("GET", "/ok", nil)
req.Header.Add("Authorization", "Bearer"+test.Token)

// Assert
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 200, resp.StatusCode)
}
// Act
resp, err := app.Test(req)

// Assert
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 400, resp.StatusCode)
}
})
}

func TestJwtFromCookie(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions jwt/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type jwtExtractor func(c *fiber.Ctx) (string, error)

// jwtFromHeader returns a function that extracts token from the request header.
func jwtFromHeader(header string, authScheme string) func(c *fiber.Ctx) (string, error) {
// Enforce the presence of a space between the authentication scheme and the token
authScheme = authScheme + " "
return func(c *fiber.Ctx) (string, error) {
auth := c.Get(header)
l := len(authScheme)
Expand Down
Loading