Skip to content

Agent Mesh

add timeout to oidc discovery

Antonio Ojeaab84116d2d14
diff --git a/internal/console/server.go b/internal/console/server.go
index 90d76afb..495c27de 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -66,7 +66,10 @@ func NewServer(cfg Config) (*Server, error) {
}
if info.OidcIssuer != "" && info.ClientId != "" {
- provider, err = discoverProviderWithRetry(context.Background(), info.OidcIssuer, oidcDiscoveryMaxAttempts, oidcDiscoveryBaseDelay, oidcDiscoveryMaxDelay)
+ // Bound the discovery client so an unresponsive issuer can't hang startup.
+ oidcClient := &http.Client{Timeout: 10 * time.Second}
+ discoveryCtx := oidc.ClientContext(context.Background(), oidcClient)
+ provider, err = discoverProviderWithRetry(discoveryCtx, info.OidcIssuer, oidcDiscoveryMaxAttempts, oidcDiscoveryBaseDelay, oidcDiscoveryMaxDelay)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed OIDC discovery on %s: %v\n", info.OidcIssuer, err)
} else if provider.Endpoint().AuthURL != "" && provider.Endpoint().TokenURL != "" {
@@ -267,7 +270,9 @@ func (s *Server) HandleCallback(w http.ResponseWriter, r *http.Request) {
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
}
- oauth2Token, err := oauth2Config.Exchange(r.Context(), code,
+ exchangeCtx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
+ defer cancel()
+ oauth2Token, err := oauth2Config.Exchange(exchangeCtx, code,
oauth2.SetAuthURLParam("code_verifier", verifierCookie.Value),
)
if err != nil {
diff --git a/internal/console/server_test.go b/internal/console/server_test.go
index 583b9483..8eb5e6cf 100644
--- a/internal/console/server_test.go
+++ b/internal/console/server_test.go
@@ -5,12 +5,14 @@ import (
"crypto/rand"
"crypto/rsa"
"encoding/json"
+ "net"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
"time"
+ "github.com/coreos/go-oidc/v3/oidc"
"github.com/google/sam/api"
"google.golang.org/protobuf/proto"
)
@@ -204,4 +206,40 @@ func TestDiscoverProviderWithRetry(t *testing.T) {
t.Fatal("expected discovery to fail after exhausting retries")
}
})
+
+ // A hung connection (issuer accepts but never responds) must not block
+ // discovery forever: the client attached via oidc.ClientContext needs its
+ // own timeout, since neither the retry loop nor context.Background() bound
+ // a single attempt's duration on their own.
+ t.Run("does not hang on an unresponsive issuer", func(t *testing.T) {
+ ln, err := net.Listen("tcp", "127.0.0.1:0")
+ if err != nil {
+ t.Fatalf("failed to listen: %v", err)
+ }
+ defer func() { _ = ln.Close() }()
+ go func() {
+ for {
+ conn, err := ln.Accept()
+ if err != nil {
+ return
+ }
+ _ = conn // accepted but deliberately never responds, to simulate a hang
+ }
+ }()
+
+ client := &http.Client{Timeout: 50 * time.Millisecond}
+ ctx := oidc.ClientContext(context.Background(), client)
+
+ done := make(chan struct{})
+ go func() {
+ defer close(done)
+ _, _ = discoverProviderWithRetry(ctx, "http://"+ln.Addr().String(), 2, time.Millisecond, 5*time.Millisecond)
+ }()
+
+ select {
+ case <-done:
+ case <-time.After(2 * time.Second):
+ t.Fatal("discoverProviderWithRetry hung on an unresponsive issuer instead of timing out")
+ }
+ })
}