gofiber / fiber

⚡️ Express inspired web framework written in Go
https://gofiber.io
MIT License
33.66k stars 1.65k forks source link

🐛 no cors headers while using proxy and cors #1037

Closed jney closed 3 years ago

jney commented 3 years ago

Fiber version

v2.2.0

Issue description

cors headers not added using both middlewares proxy and cors

Code snippet

package main

import (
    "fmt"
    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/fiber/v2/middleware/cors"
    "github.com/gofiber/fiber/v2/middleware/proxy"
)

func main() {
    app := fiber.New()
    app.Use(cors.New(cors.Config{AllowCredentials: true}))

    app.Use(proxy.Balancer(proxy.Config{
        Servers: []string{fmt.Sprintf("http://localhost:%s", cfg.ProxyPort)},
        ModifyRequest: func(c *fiber.Ctx) error {
            if c.Method() == fiber.MethodOptions {
                return c.SendStatus(fiber.StatusNoContent)
            }
            return nil
        },
        ModifyResponse: func(c *fiber.Ctx) error {

            return nil
        },
    }))

    log.Fatal(app.Listen(":8888"))
}
K0enM commented 3 years ago

A hacky way to fix this is doing this:


ModifyResponse: cors.New(cors.Config{AllowCredentials: true})
Fenny commented 3 years ago

I tested your example, and it seems to work well for me. I'm closing this issue, feel free to re-open 👍

pejeio commented 11 months ago

@K0enM @Fenny Looks like I have the same issue. Do you have an example using the proxy.Do function?

pejeio commented 11 months ago

@K0enM @Fenny Looks like I have the same issue. Do you have an example using the proxy.Do function?

Just found a way. This is example when you use proxy.Do:

err = proxy.Do(c, "http://localhost")
if err != nil {
    return err
}
c.Response().Header.Set("Access-Control-Allow-Origin", "http://host.com")
c.Response().Header.Set("Access-Control-Allow-Credentials", "true")
return nil
gaby commented 11 months ago

@pejeio Can you open a new issue, so that we can look at it. Thanks!

My guess is the Proxy middleware is removing the headers.

guvenaltunsoyy commented 1 month ago

Is there any development here to not remove those header during proxy?