Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

fix(auth): restrict SSO return_url to same-site hosts (#76)

Robert0622165912891d97
diff --git a/internal/httpapi/auth.go b/internal/httpapi/auth.go
index dfda57f5..7585a913 100644
--- a/internal/httpapi/auth.go
+++ b/internal/httpapi/auth.go
@@ -100,13 +100,10 @@ func getHeaderCaseInsensitive(r *http.Request, key string) string {
return ""
}
+// getReturnURL 计算 SSO 登录完成后的回跳地址。return_url / Referer 均由客户端
+// 控制,只接受指向本站(请求 Host 或 AppDomain 及其子域)的 http(s) 地址,
+// 否则回落到站点根路径,防止登录后被带到外部钓鱼站点(Open Redirect)。
func (h *authHandlers) getReturnURL(r *http.Request) string {
- if raw := strings.TrimSpace(r.URL.Query().Get("return_url")); raw != "" {
- return raw
- }
- if ref := strings.TrimSpace(r.Header.Get("Referer")); ref != "" {
- return ref
- }
proto := "http"
if r.TLS != nil || strings.EqualFold(r.Header.Get("X-Forwarded-Proto"), "https") {
proto = "https"
@@ -115,7 +112,52 @@ func (h *authHandlers) getReturnURL(r *http.Request) string {
if host == "" {
host = h.getAuthClient().Config().AppDomain
}
- return fmt.Sprintf("%s://%s/", proto, host)
+ origin := fmt.Sprintf("%s://%s", proto, host)
+ for _, raw := range []string{r.URL.Query().Get("return_url"), r.Header.Get("Referer")} {
+ if safe, ok := h.safeReturnURL(r, origin, raw); ok {
+ return safe
+ }
+ }
+ return origin + "/"
+}
+
+func (h *authHandlers) safeReturnURL(r *http.Request, origin, raw string) (string, bool) {
+ raw = strings.TrimSpace(raw)
+ if raw == "" || strings.ContainsAny(raw, "\\\r\n\t") {
+ return "", false
+ }
+ // 站内相对路径:必须以单个 "/" 开头,"//host" 是协议相对的外部地址。
+ if strings.HasPrefix(raw, "/") && !strings.HasPrefix(raw, "//") {
+ raw = origin + raw
+ }
+ u, err := url.Parse(raw)
+ if err != nil || u.User != nil || u.Opaque != "" || (u.Scheme != "http" && u.Scheme != "https") {
+ return "", false
+ }
+ if !h.allowedReturnHost(r, u.Hostname()) {
+ return "", false
+ }
+ return u.String(), true
+}
+
+func (h *authHandlers) allowedReturnHost(r *http.Request, host string) bool {
+ host = bareHostname(host)
+ if host == "" {
+ return false
+ }
+ if reqHost := bareHostname(r.Host); reqHost != "" && host == reqHost {
+ return true
+ }
+ appDomain := strings.TrimPrefix(bareHostname(h.getAuthClient().Config().AppDomain), ".")
+ return appDomain != "" && (host == appDomain || strings.HasSuffix(host, "."+appDomain))
+}
+
+func bareHostname(hostport string) string {
+ hostport = strings.TrimSpace(hostport)
+ if hn, _, err := net.SplitHostPort(hostport); err == nil {
+ hostport = hn
+ }
+ return strings.ToLower(strings.TrimSuffix(strings.Trim(hostport, "[]"), "."))
}
func (h *authHandlers) authCurrent(c *gin.Context) {
diff --git a/internal/httpapi/auth_binary_cases_test.go b/internal/httpapi/auth_binary_cases_test.go
index db5f0a8c..1e2c1534 100644
--- a/internal/httpapi/auth_binary_cases_test.go
+++ b/internal/httpapi/auth_binary_cases_test.go
@@ -248,15 +248,20 @@ func TestAuthCurrentGatewayAndPreviewBranches(t *testing.T) {
t.Fatalf("preview current = %d: %s", rec.Code, rec.Body.String())
}
- request := httptest.NewRequest(http.MethodGet, "/auth?return_url=https://return.test/path", nil)
- if got := h.getReturnURL(request); got != "https://return.test/path" {
+ request := httptest.NewRequest(http.MethodGet, "/auth?return_url=https://example.com/path", nil)
+ if got := h.getReturnURL(request); got != "https://example.com/path" {
t.Fatalf("return URL = %q", got)
}
request = httptest.NewRequest(http.MethodGet, "/auth", nil)
- request.Header.Set("Referer", "https://referer.test/")
- if got := h.getReturnURL(request); got != "https://referer.test/" {
+ request.Header.Set("Referer", "https://example.com/ref")
+ if got := h.getReturnURL(request); got != "https://example.com/ref" {
t.Fatalf("referer URL = %q", got)
}
+ request = httptest.NewRequest(http.MethodGet, "/auth?return_url=https://return.test/path", nil)
+ request.Header.Set("Referer", "https://referer.test/")
+ if got := h.getReturnURL(request); got != "http://example.com/" {
+ t.Fatalf("foreign return URL must fall back to site root, got %q", got)
+ }
}
func TestUsersNilAndMalformedBranches(t *testing.T) {
diff --git a/internal/httpapi/auth_return_url_test.go b/internal/httpapi/auth_return_url_test.go
new file mode 100644
index 00000000..2e445201
--- /dev/null
+++ b/internal/httpapi/auth_return_url_test.go
@@ -0,0 +1,69 @@
+package httpapi
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strings"
+ "testing"
+
+ "agentflow/internal/auth"
+)
+
+func TestGetReturnURLRejectsForeignHosts(t *testing.T) {
+ h := &authHandlers{deps: &deps{authClient: auth.NewClient(auth.Config{AppDomain: "loopforge.example.com"})}}
+
+ cases := []struct {
+ returnURL string
+ want string
+ }{
+ {"https://lf.test/#/dashboard", "https://lf.test/#/dashboard"},
+ {"https://LF.test:8443/x?y=1", "https://LF.test:8443/x?y=1"},
+ {"/#/settings", "http://lf.test/#/settings"},
+ {"https://loopforge.example.com/a", "https://loopforge.example.com/a"},
+ {"https://studio.loopforge.example.com/a", "https://studio.loopforge.example.com/a"},
+ {"https://attacker.example/sso-landing", "http://lf.test/"},
+ {"http://www.qq.com_521_qq_diao_yu_wangzhan_789.com", "http://lf.test/"},
+ {"https://lf.test.attacker.example/", "http://lf.test/"},
+ {"https://evilloopforge.example.com/", "http://lf.test/"},
+ {"https://[email protected]/", "http://lf.test/"},
+ {"//attacker.example/", "http://lf.test/"},
+ {"/\\attacker.example/", "http://lf.test/"},
+ {"https:\\\\attacker.example", "http://lf.test/"},
+ {"https:attacker.example", "http://lf.test/"},
+ {"javascript:alert(1)", "http://lf.test/"},
+ {"data:text/html,x", "http://lf.test/"},
+ }
+ for _, tc := range cases {
+ r := httptest.NewRequest(http.MethodGet, "http://lf.test/api/auth/current?return_url="+url.QueryEscape(tc.returnURL), nil)
+ if got := h.getReturnURL(r); got != tc.want {
+ t.Errorf("return_url=%q: got %q, want %q", tc.returnURL, got, tc.want)
+ }
+ }
+
+ r := httptest.NewRequest(http.MethodGet, "http://lf.test/api/auth/current", nil)
+ r.Header.Set("Referer", "https://attacker.example/")
+ if got := h.getReturnURL(r); got != "http://lf.test/" {
+ t.Errorf("foreign Referer must be ignored, got %q", got)
+ }
+}
+
+func TestAuthCurrentLoginURLDoesNotEmbedForeignReturnURL(t *testing.T) {
+ authClient := auth.NewClient(auth.Config{
+ PaaSID: "app",
+ AppToken: "token",
+ LoginURL: "https://passport.example.com/signin",
+ AppDomain: "loopforge.example.com",
+ })
+ handler, _ := newTestAuthServer(t, authClient)
+
+ req := httptest.NewRequest(http.MethodGet, "http://loopforge.example.com/api/auth/current?return_url=https://attacker.example/sso-landing", nil)
+ rec := httptest.NewRecorder()
+ handler.ServeHTTP(rec, req)
+ if rec.Code != http.StatusOK {
+ t.Fatalf("expected 200, got %d", rec.Code)
+ }
+ if body := rec.Body.String(); strings.Contains(body, "attacker.example") {
+ t.Fatalf("loginUrl must not carry a foreign return URL: %s", body)
+ }
+}