From 3476e2fc13708965dabdde1ffb2cf108bc26b148 Mon Sep 17 00:00:00 2001 From: Ryan Devenney Date: Mon, 20 Feb 2023 15:44:22 -0500 Subject: [PATCH] fix cors * behavior 2338 --- middleware/cors/cors.go | 6 +++--- middleware/cors/cors_test.go | 2 +- test_dir/my_tester.go | 21 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 test_dir/my_tester.go diff --git a/middleware/cors/cors.go b/middleware/cors/cors.go index d7325d5b2f1..011ca25459e 100644 --- a/middleware/cors/cors.go +++ b/middleware/cors/cors.go @@ -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 } diff --git a/middleware/cors/cors_test.go b/middleware/cors/cors_test.go index 3600282331b..4343cc23134 100644 --- a/middleware/cors/cors_test.go +++ b/middleware/cors/cors_test.go @@ -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))) diff --git a/test_dir/my_tester.go b/test_dir/my_tester.go new file mode 100644 index 00000000000..0b4dd309479 --- /dev/null +++ b/test_dir/my_tester.go @@ -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!") +}