Skip to content

Commit e3b2780

Browse files
committed
webdav: ignore port for unix domain sockets
Fixes #2151 Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
1 parent e44ff48 commit e3b2780

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

internal/webdavd/internal_test.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Ap157PUHTriSnxyMF2Sb3EhX/rQkmbnbCqqygHC14iBy8MrKzLG00X6BelZV5n0D
270270
RKjnkiEZeG4+G91Xu7+HmcBLwV86k5I+tXK9O1Okomr6Zry8oqVcxU5TB6VRS+rA
271271
ubwF00Drdvk2+kDZfxIM137nBiy7wgCJi2Ksm5ihN3dUF6Q0oNPl
272272
-----END RSA PRIVATE KEY-----`
273+
osWindows = "windows"
273274
)
274275

275276
// MockOsFs mockable OsFs
@@ -523,7 +524,7 @@ func TestResolvePathErrors(t *testing.T) {
523524
assert.EqualError(t, err, common.ErrGenericFailure.Error())
524525
}
525526

526-
if runtime.GOOS != "windows" {
527+
if runtime.GOOS != osWindows {
527528
user.HomeDir = filepath.Clean(os.TempDir())
528529
connection.User = user
529530
fs := vfs.NewOsFs("connID", connection.User.HomeDir, "", nil)
@@ -1740,3 +1741,68 @@ func TestGetCacheExpirationTime(t *testing.T) {
17401741
c.ExpirationTime = 1
17411742
assert.False(t, c.getExpirationTime().IsZero())
17421743
}
1744+
1745+
func TestBindingGetAddress(t *testing.T) {
1746+
tests := []struct {
1747+
name string
1748+
binding Binding
1749+
want string
1750+
}{
1751+
{
1752+
name: "IP address with port",
1753+
binding: Binding{Address: "127.0.0.1", Port: 8080},
1754+
want: "127.0.0.1:8080",
1755+
},
1756+
{
1757+
name: "Unix socket path (no port)",
1758+
binding: Binding{Address: "/tmp/app.sock", Port: 0},
1759+
want: "/tmp/app.sock",
1760+
},
1761+
}
1762+
1763+
for _, tt := range tests {
1764+
t.Run(tt.name, func(t *testing.T) {
1765+
if got := tt.binding.GetAddress(); got != tt.want {
1766+
t.Errorf("GetAddress() = %v, want %v", got, tt.want)
1767+
}
1768+
})
1769+
}
1770+
}
1771+
1772+
func TestBindingIsValid(t *testing.T) {
1773+
tests := []struct {
1774+
name string
1775+
binding Binding
1776+
want bool
1777+
}{
1778+
{
1779+
name: "Valid: Positive port",
1780+
binding: Binding{Address: "127.0.0.1", Port: 10080},
1781+
want: true,
1782+
},
1783+
{
1784+
name: "Valid: Absolute path on Unix (non-Windows)",
1785+
binding: Binding{Address: "/var/run/app.sock", Port: 0},
1786+
// This test outcome is dynamic based on the OS
1787+
want: runtime.GOOS != osWindows,
1788+
},
1789+
{
1790+
name: "Invalid: Port 0 and relative path",
1791+
binding: Binding{Address: "relative/path", Port: 0},
1792+
want: false,
1793+
},
1794+
{
1795+
name: "Invalid: Empty address and port 0",
1796+
binding: Binding{Address: "", Port: 0},
1797+
want: false,
1798+
},
1799+
}
1800+
1801+
for _, tt := range tests {
1802+
t.Run(tt.name, func(t *testing.T) {
1803+
if got := tt.binding.IsValid(); got != tt.want {
1804+
t.Errorf("IsValid() = %v, want %v", got, tt.want)
1805+
}
1806+
})
1807+
}
1808+
}

internal/webdavd/webdavd.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net/http"
2222
"os"
2323
"path/filepath"
24+
"runtime"
2425
"time"
2526

2627
"github.com/go-chi/chi/v5/middleware"
@@ -176,12 +177,21 @@ func (b *Binding) isMutualTLSEnabled() bool {
176177

177178
// GetAddress returns the binding address
178179
func (b *Binding) GetAddress() string {
179-
return fmt.Sprintf("%s:%d", b.Address, b.Port)
180+
if b.Port > 0 {
181+
return fmt.Sprintf("%s:%d", b.Address, b.Port)
182+
}
183+
return b.Address
180184
}
181185

182-
// IsValid returns true if the binding port is > 0
186+
// IsValid returns true if the binding is valid
183187
func (b *Binding) IsValid() bool {
184-
return b.Port > 0
188+
if b.Port > 0 {
189+
return true
190+
}
191+
if filepath.IsAbs(b.Address) && runtime.GOOS != "windows" {
192+
return true
193+
}
194+
return false
185195
}
186196

187197
func (b *Binding) listenerWrapper() func(net.Listener) (net.Listener, error) {

0 commit comments

Comments
 (0)