Skip to content

Commit

Permalink
fix cors * behavior 2338
Browse files Browse the repository at this point in the history
  • Loading branch information
ryand67 committed Feb 20, 2023
1 parent 497eb02 commit 3476e2f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
6 changes: 3 additions & 3 deletions middleware/cors/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ func New(config ...Config) fiber.Handler {

// Check allowed origins
for _, o := range allowOrigins {
if o == "*" && cfg.AllowCredentials {
allowOrigin = origin
if o == "*" {
allowOrigin = "*"
break
}
if o == "*" || o == origin {
if o == origin {
allowOrigin = o
break
}
Expand Down
2 changes: 1 addition & 1 deletion middleware/cors/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Test_CORS_Wildcard(t *testing.T) {
handler(ctx)

// Check result
utils.AssertEqual(t, "localhost", string(ctx.Response.Header.Peek(fiber.HeaderAccessControlAllowOrigin)))
utils.AssertEqual(t, "*", string(ctx.Response.Header.Peek(fiber.HeaderAccessControlAllowOrigin)))
utils.AssertEqual(t, "true", string(ctx.Response.Header.Peek(fiber.HeaderAccessControlAllowCredentials)))
utils.AssertEqual(t, "3600", string(ctx.Response.Header.Peek(fiber.HeaderAccessControlMaxAge)))
utils.AssertEqual(t, "Authentication", string(ctx.Response.Header.Peek(fiber.HeaderAccessControlAllowHeaders)))
Expand Down
21 changes: 21 additions & 0 deletions test_dir/my_tester.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"log"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
)

func main() {
app := fiber.New()
app.Use(cors.New(cors.Config{}))
app.Get("/hello", hello)
if err := app.Listen(":8081"); err != nil {
log.Fatal(err)
}
}

func hello(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
}

0 comments on commit 3476e2f

Please sign in to comment.