From 13d7445a36ea85e7a67e3491cc632143020c70ee Mon Sep 17 00:00:00 2001 From: OpenWaygate <27287694+OpenWaygate@users.noreply.github.com> Date: Sun, 12 Apr 2026 11:28:10 +0800 Subject: [PATCH] :bug: Fix OAuth callback handler blocking forever --- pkg/auth/auth.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/auth/auth.go b/pkg/auth/auth.go index d202628..eba7c82 100644 --- a/pkg/auth/auth.go +++ b/pkg/auth/auth.go @@ -160,12 +160,13 @@ func (s *svc) startWebServer(redirectURL string) (chan string, error) { return nil, fmt.Errorf("%s: %w", listenFailed, err) } - codeCh := make(chan string) + codeCh := make(chan string, 1) go func() { _ = http.Serve( listener, http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { + http.NotFound(w, r) return } state := r.FormValue("state") @@ -173,13 +174,21 @@ func (s *svc) startWebServer(redirectURL string) (chan string, error) { slog.Error( stateMatchFailed, "actual", state, "expected", s.state, ) + http.Error(w, stateMatchFailed, http.StatusBadRequest) return } code := r.FormValue("code") - codeCh <- code - _ = listener.Close() - w.Header().Set("Content-Type", "text/plain") - _, _ = fmt.Fprintf(w, receivedCodeHint, code) + select { + case codeCh <- code: + w.Header().Set("Content-Type", "text/plain") + _, _ = fmt.Fprintf(w, receivedCodeHint, code) + _ = listener.Close() + default: + http.Error( + w, "authorization code already received", + http.StatusConflict, + ) + } }, ), )