Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e1286a

Browse files
fix: ipv6 handling for RemoteAddr
1 parent 6f9601b commit 9e1286a

File tree

5 files changed

+53
-38
lines changed

5 files changed

+53
-38
lines changed

‎functionurl_test.go‎

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import (
1111
"github.com/its-felix/aws-lambda-go-http-adapter/handler"
1212
"github.com/labstack/echo/v4"
1313
"io"
14+
"net"
1415
"net/http"
1516
"reflect"
1617
"strings"
1718
"testing"
1819
"time"
1920
)
2021

21-
func newFunctionURLRequest() events.LambdaFunctionURLRequest {
22+
func newFunctionURLRequest(sourceIpstring) events.LambdaFunctionURLRequest {
2223
return events.LambdaFunctionURLRequest{
2324
Version: "2.0",
2425
RawPath: "/example",
@@ -39,7 +40,7 @@ func newFunctionURLRequest() events.LambdaFunctionURLRequest {
3940
Method: "POST",
4041
Path: "/example",
4142
Protocol: "HTTP/1.1",
42-
SourceIP: "127.0.0.1",
43+
SourceIP: sourceIp,
4344
UserAgent: "Go-http-client/1.1",
4445
},
4546
},
@@ -150,7 +151,7 @@ func newFiberAdapter() handler.AdapterFunc {
150151
result := make(map[string]string)
151152
result["Method"] = ctx.Method()
152153
result["URL"] = ctx.Request().URI().String()
153-
result["RemoteAddr"] = ctx.IP()+":http" // fiber uses net.ResolveTCPAddr which resolves :http to :80
154+
result["RemoteAddr"] = net.JoinHostPort(ctx.IP(), "http") // fiber uses net.ResolveTCPAddr which resolves :http to :80
154155
result["Body"] = string(ctx.Body())
155156

156157
return ctx.JSON(result)
@@ -262,38 +263,47 @@ func TestFunctionURLPOST(t *testing.T) {
262263
}
263264

264265
func runTestFunctionURLPOST[T any](t *testing.T, h func(context.Context, events.LambdaFunctionURLRequest) (T, error), ex extractor[T]) {
265-
req := newFunctionURLRequest()
266-
res, err := h(context.Background(), req)
267-
if err != nil {
268-
t.Error(err)
266+
sourceIps := map[string][2]string{
267+
"ipv4": {"127.0.0.1", "127.0.0.1:http"},
268+
"ipv6": {"::1", "[::1]:http"},
269269
}
270270

271-
if ex.StatusCode(res) != http.StatusOK {
272-
t.Error("expected status to be 200")
273-
}
274-
275-
if ex.Headers(res)["Content-Type"] != "application/json" {
276-
t.Error("expected Content-Type to be application/json")
277-
}
278-
279-
if ex.IsBase64Encoded(res) {
280-
t.Error("expected body not to be base64 encoded")
281-
}
282-
283-
body := make(map[string]string)
284-
_ = json.Unmarshal([]byte(ex.Body(res)), &body)
285-
286-
expectedBody := map[string]string{
287-
"Method": "POST",
288-
"URL": "https://0dhg9709da0dhg9709da0dhg9709da.lambda-url.eu-central-1.on.aws/example?key=value",
289-
"RemoteAddr": "127.0.0.1:http",
290-
"Body": "hello world",
291-
}
292-
293-
if !reflect.DeepEqual(body, expectedBody) {
294-
t.Logf("expected: %v", expectedBody)
295-
t.Logf("actual: %v", body)
296-
t.Error("request/response didnt match")
271+
for name, inputPair := range sourceIps {
272+
t.Run(name, func(t *testing.T) {
273+
req := newFunctionURLRequest(inputPair[0])
274+
res, err := h(context.Background(), req)
275+
if err != nil {
276+
t.Error(err)
277+
}
278+
279+
if ex.StatusCode(res) != http.StatusOK {
280+
t.Error("expected status to be 200")
281+
}
282+
283+
if ex.Headers(res)["Content-Type"] != "application/json" {
284+
t.Error("expected Content-Type to be application/json")
285+
}
286+
287+
if ex.IsBase64Encoded(res) {
288+
t.Error("expected body not to be base64 encoded")
289+
}
290+
291+
body := make(map[string]string)
292+
_ = json.Unmarshal([]byte(ex.Body(res)), &body)
293+
294+
expectedBody := map[string]string{
295+
"Method": "POST",
296+
"URL": "https://0dhg9709da0dhg9709da0dhg9709da.lambda-url.eu-central-1.on.aws/example?key=value",
297+
"RemoteAddr": inputPair[1],
298+
"Body": "hello world",
299+
}
300+
301+
if !reflect.DeepEqual(body, expectedBody) {
302+
t.Logf("expected: %v", expectedBody)
303+
t.Logf("actual: %v", body)
304+
t.Error("request/response didnt match")
305+
}
306+
})
297307
}
298308
}
299309

@@ -328,7 +338,7 @@ func TestFunctionURLWithPanicAndRecover(t *testing.T) {
328338
}
329339

330340
func runTestFunctionURLPanicAndRecover[T any](t *testing.T, h func(context.Context, events.LambdaFunctionURLRequest) (T, error)) {
331-
req := newFunctionURLRequest()
341+
req := newFunctionURLRequest("127.0.0.1")
332342
_, err := h(context.Background(), req)
333343
if err == nil {
334344
t.Error("expected to receive an error")
@@ -362,7 +372,7 @@ func TestFunctionURLDelayed(t *testing.T) {
362372
}
363373

364374
func runTestFunctionURLDelayed[T any](t *testing.T, h func(context.Context, events.LambdaFunctionURLRequest) (T, error), ex extractor[T]) {
365-
req := newFunctionURLRequest()
375+
req := newFunctionURLRequest("127.0.0.1")
366376
res, err := h(context.Background(), req)
367377
if err != nil {
368378
t.Error(err)

‎handler/apigwv1.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func convertApiGwV1Request(ctx context.Context, event events.APIGatewayProxyRequ
5353
req.ProtoMajor, req.ProtoMinor = pMajor, pMinor
5454
}
5555

56-
req.RemoteAddr = event.RequestContext.Identity.SourceIP+":http"
56+
req.RemoteAddr = buildRemoteAddr(event.RequestContext.Identity.SourceIP)
5757
req.RequestURI = req.URL.RequestURI()
5858

5959
return req, nil

‎handler/apigwv2.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func convertApiGwV2Request(ctx context.Context, event events.APIGatewayV2HTTPReq
3737
req.ProtoMajor, req.ProtoMinor = pMajor, pMinor
3838
}
3939

40-
req.RemoteAddr = event.RequestContext.HTTP.SourceIP+":http"
40+
req.RemoteAddr = buildRemoteAddr(event.RequestContext.HTTP.SourceIP)
4141
req.RequestURI = req.URL.RequestURI()
4242

4343
return req, nil

‎handler/common.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handler
33
import (
44
"encoding/base64"
55
"io"
6+
"net"
67
"net/url"
78
"strings"
89
)
@@ -57,3 +58,7 @@ func getBody(body string, isB64 bool) io.Reader {
5758

5859
return b
5960
}
61+
62+
func buildRemoteAddr(sourceIp string) string {
63+
return net.JoinHostPort(sourceIp, "http")
64+
}

‎handler/functionurl.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func convertFunctionURLRequest(ctx context.Context, event events.LambdaFunctionU
3838
req.ProtoMajor, req.ProtoMinor = pMajor, pMinor
3939
}
4040

41-
req.RemoteAddr = event.RequestContext.HTTP.SourceIP+":http"
41+
req.RemoteAddr = buildRemoteAddr(event.RequestContext.HTTP.SourceIP)
4242
req.RequestURI = req.URL.RequestURI()
4343

4444
return req, nil

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /