Skip to content

Commit 026975d

Browse files
James Fargherproglottis
authored andcommitted
sshd: Add ProxyAllowed setting to limit PROXY protocol IP addresses
Changelog: added
1 parent 85830ef commit 026975d

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type ServerConfig struct {
2727
Listen string `yaml:"listen,omitempty"`
2828
ProxyProtocol bool `yaml:"proxy_protocol,omitempty"`
2929
ProxyPolicy string `yaml:"proxy_policy,omitempty"`
30+
ProxyAllowed []string `yaml:"proxy_allowed,omitempty"`
3031
WebListen string `yaml:"web_listen,omitempty"`
3132
ConcurrentSessionsLimit int64 `yaml:"concurrent_sessions_limit,omitempty"`
3233
ClientAliveInterval YamlDuration `yaml:"client_alive_interval,omitempty"`

internal/sshd/sshd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ func (s *Server) handleConn(ctx context.Context, nconn net.Conn) {
201201
}
202202

203203
func (s *Server) requirePolicy() proxyproto.PolicyFunc {
204+
if len(s.Config.Server.ProxyAllowed) > 0 {
205+
return proxyproto.MustStrictWhiteListPolicy(s.Config.Server.ProxyAllowed)
206+
}
207+
204208
// Set the Policy value based on config
205209
// Values are taken from https://github.com/pires/go-proxyproto/blob/195fedcfbfc1be163f3a0d507fac1709e9d81fed/policy.go#L20
206210
switch strings.ToLower(s.Config.Server.ProxyPolicy) {

internal/sshd/sshd_test.go

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestListenAndServe(t *testing.T) {
5050
verifyStatus(t, s, StatusClosed)
5151
}
5252

53-
func TestListenAndServeRejectsPlainConnectionsWhenProxyProtocolEnabled(t *testing.T) {
53+
func TestListenAndServe_proxyProtocolEnabled(t *testing.T) {
5454
target, err := net.ResolveTCPAddr("tcp", serverUrl)
5555
require.NoError(t, err)
5656

@@ -70,10 +70,11 @@ func TestListenAndServeRejectsPlainConnectionsWhenProxyProtocolEnabled(t *testin
7070
}()
7171

7272
testCases := []struct {
73-
desc string
74-
proxyPolicy string
75-
header *proxyproto.Header
76-
isRejected bool
73+
desc string
74+
proxyPolicy string
75+
proxyAllowed []string
76+
header *proxyproto.Header
77+
isRejected bool
7778
}{
7879
{
7980
desc: "USE (default) without a header",
@@ -123,11 +124,65 @@ func TestListenAndServeRejectsPlainConnectionsWhenProxyProtocolEnabled(t *testin
123124
header: header,
124125
isRejected: false,
125126
},
127+
{
128+
desc: "Allow-listed IP with a header",
129+
proxyAllowed: []string{"127.0.0.1"},
130+
header: header,
131+
isRejected: false,
132+
},
133+
{
134+
desc: "Allow-listed IP without a header",
135+
proxyAllowed: []string{"127.0.0.1"},
136+
header: nil,
137+
isRejected: false,
138+
},
139+
{
140+
desc: "Allow-listed range with a header",
141+
proxyAllowed: []string{"127.0.0.0/24"},
142+
header: header,
143+
isRejected: false,
144+
},
145+
{
146+
desc: "Allow-listed range without a header",
147+
proxyAllowed: []string{"127.0.0.0/24"},
148+
header: nil,
149+
isRejected: false,
150+
},
151+
{
152+
desc: "Not allow-listed IP with a header",
153+
proxyAllowed: []string{"192.168.1.1"},
154+
header: header,
155+
isRejected: true,
156+
},
157+
{
158+
desc: "Not allow-listed IP without a header",
159+
proxyAllowed: []string{"192.168.1.1"},
160+
header: nil,
161+
isRejected: false,
162+
},
163+
{
164+
desc: "Not allow-listed range with a header",
165+
proxyAllowed: []string{"192.168.1.0/24"},
166+
header: header,
167+
isRejected: true,
168+
},
169+
{
170+
desc: "Not allow-listed range without a header",
171+
proxyAllowed: []string{"192.168.1.0/24"},
172+
header: nil,
173+
isRejected: false,
174+
},
126175
}
127176

128177
for _, tc := range testCases {
129178
t.Run(tc.desc, func(t *testing.T) {
130-
setupServerWithConfig(t, &config.Config{Server: config.ServerConfig{ProxyProtocol: true, ProxyPolicy: tc.proxyPolicy}})
179+
setupServerWithConfig(t, &config.Config{
180+
Server: config.ServerConfig{
181+
ProxyProtocol: true,
182+
ProxyPolicy: tc.proxyPolicy,
183+
ProxyAllowed: tc.proxyAllowed,
184+
},
185+
})
131186

132187
conn, err := net.DialTCP("tcp", nil, target)
133188
require.NoError(t, err)

0 commit comments

Comments
 (0)