Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(auth): bound Google OAuth token exchange and follow the request context (#11235)

Thejeshdbc4b9e0fa77
diff --git a/server/internal/handler/auth.go b/server/internal/handler/auth.go
index 12515866fc..f385365916 100644
--- a/server/internal/handler/auth.go
+++ b/server/internal/handler/auth.go
@@ -561,11 +561,15 @@ func writeGoogleLoginActionableError(w http.ResponseWriter, err error) bool {
return true
}
+// googleOAuthClient bounds each Google OAuth call. The API server sets no
+// WriteTimeout, so an unanswered call would otherwise hold the handler open.
+var googleOAuthClient = &http.Client{Timeout: 15 * time.Second}
+
func (h *Handler) googleHTTPClient() *http.Client {
if h.googleOAuthHTTPClient != nil {
return h.googleOAuthHTTPClient
}
- return http.DefaultClient
+ return googleOAuthClient
}
func (h *Handler) GoogleLogin(w http.ResponseWriter, r *http.Request) {
@@ -593,13 +597,21 @@ func (h *Handler) GoogleLogin(w http.ResponseWriter, r *http.Request) {
}
// Exchange authorization code for tokens.
- tokenResp, err := h.googleHTTPClient().PostForm("https://oauth2.googleapis.com/token", url.Values{
+ tokenForm := url.Values{
"code": {req.Code},
"client_id": {clientID},
"client_secret": {clientSecret},
"redirect_uri": {redirectURI},
"grant_type": {"authorization_code"},
- })
+ }
+ tokenReq, err := http.NewRequestWithContext(r.Context(), http.MethodPost, "https://oauth2.googleapis.com/token", strings.NewReader(tokenForm.Encode()))
+ if err != nil {
+ slog.Error("failed to create google token request", "error", err)
+ writeError(w, http.StatusInternalServerError, "internal error")
+ return
+ }
+ tokenReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
+ tokenResp, err := h.googleHTTPClient().Do(tokenReq)
if err != nil {
slog.Error("google oauth token exchange failed", "error", err)
writeError(w, http.StatusBadGateway, "failed to exchange code with Google")
diff --git a/server/internal/handler/auth_google_timeout_test.go b/server/internal/handler/auth_google_timeout_test.go
new file mode 100644
index 0000000000..27cb7e152e
--- /dev/null
+++ b/server/internal/handler/auth_google_timeout_test.go
@@ -0,0 +1,63 @@
+package handler
+
+import (
+ "context"
+ "errors"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "time"
+)
+
+func TestGoogleOAuthDefaultClientHasTimeout(t *testing.T) {
+ h := newTestHandler(Config{})
+ if got := h.googleHTTPClient().Timeout; got <= 0 {
+ t.Fatalf("production Google OAuth client timeout = %s, want a bound", got)
+ }
+}
+
+func TestGoogleLoginTokenExchangeStopsWhenRequestIsCancelled(t *testing.T) {
+ t.Setenv("GOOGLE_CLIENT_ID", "test-client")
+ t.Setenv("GOOGLE_CLIENT_SECRET", "test-secret")
+
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ exchangeCancelled := false
+ h := newTestHandler(Config{})
+ h.googleOAuthHTTPClient = &http.Client{Transport: googleRoundTripper(func(req *http.Request) (*http.Response, error) {
+ if req.URL.Host != "oauth2.googleapis.com" {
+ t.Fatalf("unexpected request to %s", req.URL)
+ }
+ if got := req.Header.Get("Content-Type"); got != "application/x-www-form-urlencoded" {
+ t.Errorf("token request Content-Type = %q", got)
+ }
+ if err := req.ParseForm(); err != nil {
+ t.Errorf("parse token request form: %v", err)
+ }
+ if req.PostForm.Get("code") != "test-code" || req.PostForm.Get("grant_type") != "authorization_code" {
+ t.Errorf("token request form = %v", req.PostForm)
+ }
+ // Simulate a Google endpoint that accepts the connection but never
+ // answers, and a browser that gives up on the login request.
+ cancel()
+ select {
+ case <-req.Context().Done():
+ exchangeCancelled = true
+ return nil, req.Context().Err()
+ case <-time.After(5 * time.Second):
+ return nil, errors.New("token exchange ignored the request context")
+ }
+ })}
+
+ req := httptest.NewRequest(http.MethodPost, "/auth/google", strings.NewReader(`{"code":"test-code"}`)).WithContext(ctx)
+ rec := httptest.NewRecorder()
+ h.GoogleLogin(rec, req)
+
+ if !exchangeCancelled {
+ t.Fatal("token exchange kept running after the inbound request was cancelled")
+ }
+ if rec.Code != http.StatusBadGateway {
+ t.Fatalf("status = %d, want %d", rec.Code, http.StatusBadGateway)
+ }
+}