detached 01d3159

fix(proxy): handle bare IPs in isTrustedSource RemoteAddr

When r.RemoteAddr lacks a port (bare IP from Unix sockets or certain
proxy setups), net.SplitHostPort fails. Previously this returned false,
silently treating trusted proxies as untrusted. Now falls back to using
RemoteAddr directly, stripping IPv6 brackets if present.
commit
01d3159d6bbd55c8d486abad50c4ec1ace6f9c80
author
brice · · 2026-04-03 18:17 UTC
parent
f9f7402
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
2 files · +24 -1
M internal/adapters/in/http/proxy/forwarder.go +3 -1
@@ -219,7 +219,9 @@ func isTrustedSource(r *http.Request, trustedNets []*net.IPNet) bool {
219219 }
220220 host, _, err := net.SplitHostPort(r.RemoteAddr)
221221 if err != nil {
222- return false
222+ // RemoteAddr may be a bare IP without a port (e.g. from Unix sockets
223+ // or certain proxy setups). Fall back to using it directly.
224+ host = strings.TrimSuffix(strings.TrimPrefix(r.RemoteAddr, "["), "]")
223225 }
224226 return middleware.IsTrustedProxy(host, trustedNets)
225227 }
@@ -219,7 +219,9 @@ func isTrustedSource(r *http.Request, trustedNets []*net.IPNet) bool {
219 }219 }
220 host, _, err := net.SplitHostPort(r.RemoteAddr)220 host, _, err := net.SplitHostPort(r.RemoteAddr)
221 if err != nil {221 if err != nil {
222- return false222+ // RemoteAddr may be a bare IP without a port (e.g. from Unix sockets
223+ // or certain proxy setups). Fall back to using it directly.
224+ host = strings.TrimSuffix(strings.TrimPrefix(r.RemoteAddr, "["), "]")
223 }225 }
224 return middleware.IsTrustedProxy(host, trustedNets)226 return middleware.IsTrustedProxy(host, trustedNets)
225 }227 }
M internal/adapters/in/http/proxy/forwarder_test.go +21 -0
@@ -238,6 +238,27 @@ func TestNewReverseProxy_XForwardedProto(t *testing.T) {
238238 wantProto: "http",
239239 nets: nil,
240240 },
241+ {
242+ name: "bare IPv4 no port trusted source preserves proto",
243+ remoteAddr: "10.0.0.1",
244+ incomingProto: "https",
245+ wantProto: "https",
246+ nets: trustedNets,
247+ },
248+ {
249+ name: "bare IPv6 with brackets trusted source preserves proto",
250+ remoteAddr: "[::1]",
251+ incomingProto: "https",
252+ wantProto: "https",
253+ nets: middleware.ParseTrustedProxies([]string{"::1/128"}),
254+ },
255+ {
256+ name: "bare IPv4 no port untrusted source strips proto",
257+ remoteAddr: "1.2.3.4",
258+ incomingProto: "https",
259+ wantProto: "http",
260+ nets: trustedNets,
261+ },
241262 }
242263
243264 for _, tt := range tests {
@@ -238,6 +238,27 @@ func TestNewReverseProxy_XForwardedProto(t *testing.T) {
238 wantProto: "http",238 wantProto: "http",
239 nets: nil,239 nets: nil,
240 },240 },
241+ {
242+ name: "bare IPv4 no port trusted source preserves proto",
243+ remoteAddr: "10.0.0.1",
244+ incomingProto: "https",
245+ wantProto: "https",
246+ nets: trustedNets,
247+ },
248+ {
249+ name: "bare IPv6 with brackets trusted source preserves proto",
250+ remoteAddr: "[::1]",
251+ incomingProto: "https",
252+ wantProto: "https",
253+ nets: middleware.ParseTrustedProxies([]string{"::1/128"}),
254+ },
255+ {
256+ name: "bare IPv4 no port untrusted source strips proto",
257+ remoteAddr: "1.2.3.4",
258+ incomingProto: "https",
259+ wantProto: "http",
260+ nets: trustedNets,
261+ },
241 }262 }
242 263
243 for _, tt := range tests {264 for _, tt := range tests {

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close