-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ Add caddy client_ip variable parsing (#184)
* feat: ✨ Add caddy client_ip variable parsing * fix: 🐛 panic when client_ip ctx var is not set * feat: ✅ separate to an utils function and add tests * fix: 🚨 linting * feat: ✅ Add integration test * fix: 🚨 linting * fix: 💡 Add comment about trusted_proxies in Caddyfile --------- Co-authored-by: Juan Pablo Tosso <[email protected]>
- Loading branch information
1 parent
c29c4b5
commit 0959a59
Showing
5 changed files
with
115 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2023 The OWASP Coraza contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package coraza | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/caddyserver/caddy/v2/modules/caddyhttp" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParsegClientAddress(t *testing.T) { | ||
|
||
remoteIp := "127.0.0.1" | ||
remotePort := 9090 | ||
clientIp := "127.0.0.2" | ||
clientPort := 8080 | ||
|
||
req, _ := http.NewRequest("GET", "/", nil) | ||
|
||
req.RemoteAddr = fmt.Sprintf("%v:%v", remoteIp, remotePort) | ||
ip, port := getClientAddress(req) | ||
require.Equal(t, remoteIp, ip) | ||
require.Equal(t, remotePort, port) | ||
|
||
req.RemoteAddr = remoteIp | ||
ip, port = getClientAddress(req) | ||
require.Equal(t, remoteIp, ip) | ||
require.Equal(t, 0, port) | ||
|
||
req = req.WithContext(context.WithValue(req.Context(), caddyhttp.VarsCtxKey, make(map[string]any))) | ||
req.RemoteAddr = fmt.Sprintf("%v:%v", remoteIp, remotePort) | ||
|
||
ip, port = getClientAddress(req) | ||
require.Equal(t, remoteIp, ip) | ||
require.Equal(t, remotePort, port) | ||
|
||
caddyhttp.SetVar(req.Context(), caddyhttp.ClientIPVarKey, fmt.Sprintf("%v:%v", clientIp, clientPort)) | ||
ip, port = getClientAddress(req) | ||
require.Equal(t, clientIp, ip) | ||
require.Equal(t, clientPort, port) | ||
|
||
caddyhttp.SetVar(req.Context(), caddyhttp.ClientIPVarKey, clientIp) | ||
ip, port = getClientAddress(req) | ||
require.Equal(t, clientIp, ip) | ||
require.Equal(t, 0, port) | ||
} |