diff --git a/ext/autossl/autossl.go b/ext/autossl/autossl.go index 26f71d8..241e7ba 100644 --- a/ext/autossl/autossl.go +++ b/ext/autossl/autossl.go @@ -55,18 +55,25 @@ func New(opts ...Option) *AutoSSL { // - httpSrv: A pointer to the HTTP server to be configured. // - httpsSrv: A pointer to the HTTPS server to be configured. func (autossl *AutoSSL) Configure(httpSrv *http.Server, httpsSrv *http.Server) { - httpSrv.Handler = autossl.Manager.HTTPHandler(httpSrv.Handler) + if httpSrv != nil && httpsSrv != nil { + httpSrv.Handler = autossl.Manager.HTTPHandler(httpSrv.Handler) - if httpSrv.ReadHeaderTimeout == 0 { - httpSrv.ReadHeaderTimeout = 3 * time.Second // Potential slowloris attack - } + if httpSrv.ReadHeaderTimeout == 0 { + httpSrv.ReadHeaderTimeout = 3 * time.Second // prevent Potential slowloris attack + } - if httpsSrv.TLSConfig == nil { - httpsSrv.TLSConfig = &tls.Config{ - MinVersion: tls.VersionTLS12, - MaxVersion: 0, + if httpsSrv.ReadHeaderTimeout == 0 { + httpsSrv.ReadHeaderTimeout = 3 * time.Second // prevent Potential slowloris attack } + + if httpsSrv.TLSConfig == nil { + httpsSrv.TLSConfig = &tls.Config{ + MinVersion: tls.VersionTLS12, + MaxVersion: 0, + } + } + + httpsSrv.TLSConfig.GetCertificate = autossl.Manager.GetCertificate } - httpsSrv.TLSConfig.GetCertificate = autossl.Manager.GetCertificate } diff --git a/ext/autossl/autossl_test.go b/ext/autossl/autossl_test.go index d70bc02..8e67028 100644 --- a/ext/autossl/autossl_test.go +++ b/ext/autossl/autossl_test.go @@ -24,8 +24,8 @@ func TestConfigure(t *testing.T) { as := New() require.NotNil(t, as) - httpSrv := &http.Server{} - httpsSrv := &http.Server{} + httpSrv := &http.Server{} // skipcp: GO-S2112 + httpsSrv := &http.Server{} // skipcq: GSC-G402 as.Configure(httpSrv, httpsSrv) @@ -33,6 +33,7 @@ func TestConfigure(t *testing.T) { require.NotNil(t, httpsSrv.TLSConfig) require.Equal(t, 3*time.Second, httpSrv.ReadHeaderTimeout) + require.Equal(t, 3*time.Second, httpsSrv.ReadHeaderTimeout) require.Equal(t, uint16(tls.VersionTLS12), httpsSrv.TLSConfig.MinVersion) require.Equal(t, uint16(0), httpsSrv.TLSConfig.MaxVersion) @@ -43,6 +44,7 @@ func TestConfigure(t *testing.T) { ReadHeaderTimeout: 1 * time.Second, } httpsSrv = &http.Server{ + ReadHeaderTimeout: 1 * time.Second, TLSConfig: &tls.Config{ MinVersion: tls.VersionTLS10, // skipcq: GSC-G402 MaxVersion: tls.VersionTLS13, @@ -54,6 +56,7 @@ func TestConfigure(t *testing.T) { require.NotNil(t, httpsSrv.TLSConfig) require.Equal(t, 1*time.Second, httpSrv.ReadHeaderTimeout) + require.Equal(t, 1*time.Second, httpsSrv.ReadHeaderTimeout) require.Equal(t, uint16(tls.VersionTLS10), httpsSrv.TLSConfig.MinVersion) require.Equal(t, uint16(tls.VersionTLS13), httpsSrv.TLSConfig.MaxVersion)