Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Unverified
Commit
8aa5e754
authored
Jul 27, 2026
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: rename trusted_proxies package to middleware and update function calls
parent
b8bb3f40
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
10 additions
and
10 deletions
+10
-10
main.go
+1
-1
middleware/trusted_proxies.go
+2
-2
middleware/trusted_proxies_test.go
+7
-7
No files found.
main.go
View file @
8aa5e754
...
...
@@ -172,7 +172,7 @@ func main() {
// Initialize HTTP server
server
:=
gin
.
New
()
if
err
:=
c
onfigureTrustedProxies
(
server
);
err
!=
nil
{
if
err
:=
middleware
.
C
onfigureTrustedProxies
(
server
);
err
!=
nil
{
common
.
FatalLog
(
"failed to configure trusted proxies: "
+
err
.
Error
())
return
}
...
...
trusted_proxies.go
→
middleware/
trusted_proxies.go
View file @
8aa5e754
package
m
ain
package
m
iddleware
import
(
"errors"
...
...
@@ -19,7 +19,7 @@ var defaultTrustedProxyCIDRs = []string{
"fc00::/7"
,
}
func
c
onfigureTrustedProxies
(
engine
*
gin
.
Engine
)
error
{
func
C
onfigureTrustedProxies
(
engine
*
gin
.
Engine
)
error
{
rawTrustedProxies
:=
strings
.
TrimSpace
(
os
.
Getenv
(
"TRUSTED_PROXIES"
))
if
rawTrustedProxies
==
""
{
log
.
Print
(
"WARNING: TRUSTED_PROXIES is unset or blank; trusting loopback, RFC 1918, and IPv6 ULA proxy addresses for compatibility. Set TRUSTED_PROXIES=none to trust no proxies, or configure explicit proxy IPs/CIDRs to replace these defaults."
)
...
...
trusted_proxies_test.go
→
middleware/
trusted_proxies_test.go
View file @
8aa5e754
package
m
ain
package
m
iddleware
import
(
"net/http"
...
...
@@ -33,7 +33,7 @@ func TestConfigureTrustedProxiesDefaultsToLoopbackAndPrivateNetworks(t *testing.
gin
.
SetMode
(
gin
.
TestMode
)
t
.
Setenv
(
"TRUSTED_PROXIES"
,
""
)
router
:=
newClientIPRouter
()
require
.
NoError
(
t
,
c
onfigureTrustedProxies
(
router
))
require
.
NoError
(
t
,
C
onfigureTrustedProxies
(
router
))
testCases
:=
[]
struct
{
name
string
...
...
@@ -59,7 +59,7 @@ func TestConfigureTrustedProxiesDefaultRejectsPublicPeerHeaders(t *testing.T) {
gin
.
SetMode
(
gin
.
TestMode
)
t
.
Setenv
(
"TRUSTED_PROXIES"
,
"
\t
"
)
router
:=
newClientIPRouter
()
require
.
NoError
(
t
,
c
onfigureTrustedProxies
(
router
))
require
.
NoError
(
t
,
C
onfigureTrustedProxies
(
router
))
clientIP
:=
requestClientIP
(
router
,
"198.51.100.10:12345"
,
"203.0.113.10"
)
assert
.
Equal
(
t
,
"198.51.100.10"
,
clientIP
,
"a public peer must not make a spoofed X-Forwarded-For authoritative"
)
...
...
@@ -69,7 +69,7 @@ func TestConfigureTrustedProxiesDefaultStopsAtPublicClientInForwardedChain(t *te
gin
.
SetMode
(
gin
.
TestMode
)
t
.
Setenv
(
"TRUSTED_PROXIES"
,
""
)
router
:=
newClientIPRouter
()
require
.
NoError
(
t
,
c
onfigureTrustedProxies
(
router
))
require
.
NoError
(
t
,
C
onfigureTrustedProxies
(
router
))
clientIP
:=
requestClientIP
(
router
,
"172.20.0.2:12345"
,
"192.0.2.99, 203.0.113.10"
)
assert
.
Equal
(
t
,
"203.0.113.10"
,
clientIP
,
"the first public hop from the trusted proxy must win over a client-supplied prefix"
)
...
...
@@ -79,7 +79,7 @@ func TestConfigureTrustedProxiesNoneDisablesForwardedHeaders(t *testing.T) {
gin
.
SetMode
(
gin
.
TestMode
)
t
.
Setenv
(
"TRUSTED_PROXIES"
,
" NoNe "
)
router
:=
newClientIPRouter
()
require
.
NoError
(
t
,
c
onfigureTrustedProxies
(
router
))
require
.
NoError
(
t
,
C
onfigureTrustedProxies
(
router
))
clientIP
:=
requestClientIP
(
router
,
"127.0.0.1:12345"
,
"203.0.113.10"
)
assert
.
Equal
(
t
,
"127.0.0.1"
,
clientIP
)
...
...
@@ -89,7 +89,7 @@ func TestConfigureTrustedProxiesAcceptsTrimmedIPsAndCIDRs(t *testing.T) {
gin
.
SetMode
(
gin
.
TestMode
)
t
.
Setenv
(
"TRUSTED_PROXIES"
,
" 192.0.2.0/24, 198.51.100.30 "
)
router
:=
newClientIPRouter
()
require
.
NoError
(
t
,
c
onfigureTrustedProxies
(
router
))
require
.
NoError
(
t
,
C
onfigureTrustedProxies
(
router
))
trustedClientIP
:=
requestClientIP
(
router
,
"192.0.2.10:12345"
,
"203.0.113.20"
)
assert
.
Equal
(
t
,
"203.0.113.20"
,
trustedClientIP
)
...
...
@@ -121,7 +121,7 @@ func TestConfigureTrustedProxiesRejectsInvalidConfiguration(t *testing.T) {
t
.
Run
(
testCase
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Setenv
(
"TRUSTED_PROXIES"
,
testCase
.
value
)
router
:=
newClientIPRouter
()
assert
.
Error
(
t
,
c
onfigureTrustedProxies
(
router
))
assert
.
Error
(
t
,
C
onfigureTrustedProxies
(
router
))
})
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment