Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions gnmi_server/clientCertAuth.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,20 @@ func TryDownload(url string) bool {
glog.Infof("Download CRL start: %s", url)
resp, err := http.Get(url)

if resp != nil {
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}

if err != nil || resp.StatusCode != http.StatusOK {
if err != nil {

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the err != nil early-return path, resp (and resp.Body) may still be non-nil for some net/http failure modes (e.g., redirect-related errors). Returning without closing resp.Body can leak resources. Consider closing the body when resp != nil before returning (guarding resp.Body != nil).

Suggested change
if err != nil {
if err != nil {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}

Copilot uses AI. Check for mistakes.
glog.Infof("Download CRL: %s failed: %v", url, err)
return false
Comment on lines 160 to +169

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http.Get uses the default client without a timeout; a slow/non-responsive CRL endpoint can block the auth path indefinitely (potential DoS when CRL checking is enabled). Consider using an http.Client with a reasonable timeout or a context with deadline for the request.

Copilot uses AI. Check for mistakes.
}

if resp.StatusCode != http.StatusOK {
glog.Infof("Download CRL: %s failed: HTTP %d", url, resp.StatusCode)
return false
}

crlContent, err := io.ReadAll(resp.Body)
if err != nil {
glog.Infof("Download CRL: %s failed: %v", url, err)
Expand Down
26 changes: 26 additions & 0 deletions gnmi_server/crl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/sonic-net/sonic-gnmi/common_utils"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
Expand Down Expand Up @@ -248,3 +250,27 @@ func TestTryDownload(t *testing.T) {
t.Errorf("Download should failed: %v", downloaded)
}
}

func TestTryDownloadRedirectError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/redirect", http.StatusFound)
}))
defer server.Close()

downloaded := TryDownload(server.URL)
if downloaded {
t.Errorf("Download should fail on redirect loop")
}
}

func TestTryDownloadNonOKResponse(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not found", http.StatusNotFound)
}))
defer server.Close()

downloaded := TryDownload(server.URL)
if downloaded {
t.Errorf("Download should fail on non-OK response")
}
}
Loading