Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 004ea1c

Browse files
committed
fix(fiber): Fix fiber returning 500 internal server error
fde5929 caused a regression in the fiber adapter logic as we previously would try to get the source IP from nil, which internally would just return an empty string. Since above commit, the RemoteAddr is not nil anymore, but is lacking the required port that is needed in order to parse it as a *TCPAddr This commit makes sure the port section is always added if it does not exist. Furthermore, it adds a test that prevents this from happening again and we now have logging in place.
1 parent 06305ac commit 004ea1c

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

fiber/adapter.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ package fiberadapter
55

66
import (
77
"context"
8-
"io/ioutil"
8+
"fmt"
9+
"io"
10+
"log"
911
"net"
1012
"net/http"
13+
"strings"
1114

1215
"github.com/aws/aws-lambda-go/events"
1316
"github.com/gofiber/fiber/v2"
@@ -103,7 +106,7 @@ func (f *FiberLambda) adaptor(w http.ResponseWriter, r *http.Request) {
103106
defer fasthttp.ReleaseRequest(req)
104107

105108
// Convert net/http -> fasthttp request
106-
body, err := ioutil.ReadAll(r.Body)
109+
body, err := io.ReadAll(r.Body)
107110
if err != nil {
108111
http.Error(w, utils.StatusMessage(fiber.StatusInternalServerError), fiber.StatusInternalServerError)
109112
return
@@ -129,8 +132,16 @@ func (f *FiberLambda) adaptor(w http.ResponseWriter, r *http.Request) {
129132
}
130133
}
131134

132-
remoteAddr, err := net.ResolveTCPAddr("tcp", r.RemoteAddr)
135+
// We need to make sure the net.ResolveTCPAddr call works as it expects a port
136+
addrWithPort := r.RemoteAddr
137+
if !strings.Contains(r.RemoteAddr, ":") {
138+
addrWithPort = r.RemoteAddr + ":80" // assuming a default port
139+
}
140+
141+
remoteAddr, err := net.ResolveTCPAddr("tcp", addrWithPort)
133142
if err != nil {
143+
fmt.Printf("could not resolve TCP address for addr %s\n", r.RemoteAddr)
144+
log.Println(err)
134145
http.Error(w, utils.StatusMessage(fiber.StatusInternalServerError), fiber.StatusInternalServerError)
135146
return
136147
}

fiber/fiberlambda_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,39 @@ var _ = Describe("FiberLambda tests", func() {
3939
})
4040
})
4141

42+
Context("RemoteAddr handling", func() {
43+
It("Properly parses the IP address", func() {
44+
app := fiber.New()
45+
app.Get("/ping", func(c *fiber.Ctx) error {
46+
// make sure the ip address is actually set properly
47+
Expect(c.Context().RemoteAddr().String()).To(Equal("8.8.8.8:80"))
48+
return c.SendString("pong")
49+
})
50+
51+
adapter := fiberadaptor.New(app)
52+
53+
req := events.APIGatewayProxyRequest{
54+
Path: "/ping",
55+
HTTPMethod: "GET",
56+
RequestContext: events.APIGatewayProxyRequestContext{
57+
Identity: events.APIGatewayRequestIdentity{
58+
SourceIP: "8.8.8.8",
59+
},
60+
},
61+
}
62+
63+
resp, err := adapter.ProxyWithContext(context.Background(), req)
64+
65+
Expect(err).To(BeNil())
66+
Expect(resp.StatusCode).To(Equal(200))
67+
68+
resp, err = adapter.Proxy(req)
69+
70+
Expect(err).To(BeNil())
71+
Expect(resp.StatusCode).To(Equal(200))
72+
})
73+
})
74+
4275
Context("Request header", func() {
4376
It("Check pass canonical header to fiber", func() {
4477
app := fiber.New()

0 commit comments

Comments
 (0)