Skip to content

Commit ded0446

Browse files
James Fargherproglottis
authored andcommitted
sshd: Return error when proxy policy is misconfigured
MustStrictWhiteListPolicy panics when configured incorrectly. So here we use the error returning version instead.
1 parent 95e4909 commit ded0446

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

internal/sshd/sshd.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,14 @@ func (s *Server) listen(ctx context.Context) error {
9595
}
9696

9797
if s.Config.Server.ProxyProtocol {
98+
policy, err := s.proxyPolicy()
99+
if err != nil {
100+
return fmt.Errorf("invalid policy configuration: %w", err)
101+
}
102+
98103
sshListener = &proxyproto.Listener{
99104
Listener: sshListener,
100-
Policy: s.requirePolicy(),
105+
Policy: policy,
101106
ReadHeaderTimeout: time.Duration(s.Config.Server.ProxyHeaderTimeout),
102107
}
103108

@@ -200,22 +205,22 @@ func (s *Server) handleConn(ctx context.Context, nconn net.Conn) {
200205
})
201206
}
202207

203-
func (s *Server) requirePolicy() proxyproto.PolicyFunc {
208+
func (s *Server) proxyPolicy() (proxyproto.PolicyFunc, error) {
204209
if len(s.Config.Server.ProxyAllowed) > 0 {
205-
return proxyproto.MustStrictWhiteListPolicy(s.Config.Server.ProxyAllowed)
210+
return proxyproto.StrictWhiteListPolicy(s.Config.Server.ProxyAllowed)
206211
}
207212

208213
// Set the Policy value based on config
209214
// Values are taken from https://github.com/pires/go-proxyproto/blob/195fedcfbfc1be163f3a0d507fac1709e9d81fed/policy.go#L20
210215
switch strings.ToLower(s.Config.Server.ProxyPolicy) {
211216
case "require":
212-
return staticProxyPolicy(proxyproto.REQUIRE)
217+
return staticProxyPolicy(proxyproto.REQUIRE), nil
213218
case "ignore":
214-
return staticProxyPolicy(proxyproto.IGNORE)
219+
return staticProxyPolicy(proxyproto.IGNORE), nil
215220
case "reject":
216-
return staticProxyPolicy(proxyproto.REJECT)
221+
return staticProxyPolicy(proxyproto.REJECT), nil
217222
default:
218-
return staticProxyPolicy(proxyproto.USE)
223+
return staticProxyPolicy(proxyproto.USE), nil
219224
}
220225
}
221226

0 commit comments

Comments
 (0)