Skip to content

Commit

Permalink
fix(autossl): fixed deepsource warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi committed Jan 5, 2025
1 parent ba34d59 commit 3cd2f80
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
25 changes: 16 additions & 9 deletions ext/autossl/autossl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 5 additions & 2 deletions ext/autossl/autossl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ 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)

require.NotNil(t, httpSrv.Handler)
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)
Expand All @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit 3cd2f80

Please sign in to comment.