Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

provider: a relay's 403 in markup says its words, not a bare "403 Forbidden"

yetone3b2794cadd9e
diff --git a/internal/gateway/zcode_blocked_test.go b/internal/gateway/zcode_blocked_test.go
index 6ea45a9a3..0cef940e6 100644
--- a/internal/gateway/zcode_blocked_test.go
+++ b/internal/gateway/zcode_blocked_test.go
@@ -189,6 +189,71 @@ func TestRelayRefusalSaysWhatItWas(t *testing.T) {
}
}
+// A custom Chat relay that serves only some agents' clients, sent
+// WorkBuddy's request with the agent's own User-Agent passed on
+// (maralica on Discord, "magpie-grok"): it is asked for its own model id,
+// never one magpie named, with WorkBuddy's User-Agent as sent, and
+// whatever reason its 403 gives reaches the agent: plain text, JSON, a
+// page with no title, a page's fragment, an XML error.
+func TestChatRelayRefusalSaysWhy(t *testing.T) {
+ const ua = "WorkBuddy/5.9.2 WorkBuddy/5.9.2 CLI/2.165.0"
+ for _, c := range []struct {
+ name, ct, body, want string
+ }{
+ {"text", "text/plain", "only Claude Code and Codex CLI are allowed", "magpie-grok: 403 Forbidden: only Claude Code and Codex CLI are allowed"},
+ {"json", "application/json", `{"error":{"message":"client WorkBuddy is not allowed","type":"forbidden"}}`, "magpie-grok: client WorkBuddy is not allowed"},
+ {"untitled page", "text/html", "<html><body><h1>403</h1><p>only Claude Code &amp; Codex CLI are allowed</p></body></html>", "magpie-grok: 403 Forbidden: a web page, not an API reply: “403 only Claude Code & Codex CLI are allowed”"},
+ {"fragment", "text/html", "<h1>Forbidden</h1>\n<p>client not allowed</p>", "magpie-grok: 403 Forbidden: Forbidden client not allowed"},
+ {"xml", "application/xml", `<?xml version="1.0"?><Error><Code>ClientNotAllowed</Code><Message>client not allowed</Message></Error>`, "magpie-grok: 403 Forbidden: ClientNotAllowed client not allowed"},
+ {"empty", "", "", "magpie-grok: 403 Forbidden: " + provider.NoDetails},
+ } {
+ for _, model := range []string{"magpie-grok/grok-4.7", "group/g"} {
+ t.Run(c.name+" "+model, func(t *testing.T) {
+ fresh(t)
+ var asked, sentUA string
+ relay := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ b, _ := io.ReadAll(r.Body)
+ var v struct {
+ Model string `json:"model"`
+ }
+ json.Unmarshal(b, &v)
+ asked, sentUA = v.Model, r.Header.Get("User-Agent")
+ if c.ct != "" {
+ w.Header().Set("Content-Type", c.ct)
+ }
+ w.WriteHeader(http.StatusForbidden)
+ io.WriteString(w, c.body)
+ }))
+ t.Cleanup(relay.Close)
+ if err := provider.Save(provider.Provider{ID: "magpie-grok", Name: "magpie-grok", Key: "k", Models: []string{"grok-4.7"}, Chat: relay.URL + "/v1", PassUserAgent: true}); err != nil {
+ t.Fatal(err)
+ }
+ if err := provider.SaveGroup(provider.Group{Name: "G", Members: []string{"magpie-grok/grok-4.7"}, Routing: provider.Ordered}); err != nil {
+ t.Fatal(err)
+ }
+ rec := httptest.NewRecorder()
+ req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"`+model+`","stream":true,"messages":[{"role":"user","content":"hi"}]}`))
+ req.Header.Set("User-Agent", ua)
+ New().Handler().ServeHTTP(rec, req)
+ if asked != "grok-4.7" || sentUA != ua {
+ t.Fatalf("relay asked for %q as %q", asked, sentUA)
+ }
+ var v struct {
+ Error struct {
+ Message string `json:"message"`
+ } `json:"error"`
+ }
+ if rec.Code != http.StatusForbidden || json.Unmarshal(rec.Body.Bytes(), &v) != nil {
+ t.Fatalf("%d %s", rec.Code, rec.Body)
+ }
+ if v.Error.Message != c.want {
+ t.Fatalf("said %q, want %q", v.Error.Message, c.want)
+ }
+ })
+ }
+ }
+}
+
// The ZCode plugin's answer to the Start Plan's 405 code 3012 (0.1.27 on,
// plugins#348) is an account ZCode's risk control blocked: it rests two
// hours as restricted, the account rather than one model, and is lifted
diff --git a/internal/provider/decide_test.go b/internal/provider/decide_test.go
index 7d8981f72..dc84addbd 100644
--- a/internal/provider/decide_test.go
+++ b/internal/provider/decide_test.go
@@ -515,11 +515,18 @@ func TestAPIErrorShapes(t *testing.T) {
if !strings.HasPrefix(got, `400 Bad Request: {"code":400`) || !strings.HasSuffix(got, "…") || len([]rune(got)) > 320 {
t.Fatal(got)
}
- if got := APIError([]byte("<html><body>bad</body></html>"), "400 Bad Request"); got != "400 Bad Request: a web page, not an API reply" {
+ // a page with no title by its words, other markup by its words too;
+ // markup with none says nothing more than the status
+ if got := APIError([]byte("<html><body>bad</body></html>"), "400 Bad Request"); got != "400 Bad Request: a web page, not an API reply: “bad”" {
t.Fatal(got)
}
- // other markup still says nothing worth showing
- if got := APIError([]byte("<Error><Code>AccessDenied</Code></Error>"), "403 Forbidden"); got != "403 Forbidden" {
+ if got := APIError([]byte("<html><body><script>var x=1</script></body></html>"), "400 Bad Request"); got != "400 Bad Request: a web page, not an API reply" {
+ t.Fatal(got)
+ }
+ if got := APIError([]byte(`<?xml version="1.0"?><Error><Code>AccessDenied</Code></Error>`), "403 Forbidden"); got != "403 Forbidden: AccessDenied" {
+ t.Fatal(got)
+ }
+ if got := APIError([]byte("<Error/>"), "403 Forbidden"); got != "403 Forbidden" {
t.Fatal(got)
}
// Command Code's refusal of a Go plan's key says where it works (#969)
diff --git a/internal/provider/test.go b/internal/provider/test.go
index 9b5296c2c..336d44aa4 100644
--- a/internal/provider/test.go
+++ b/internal/provider/test.go
@@ -925,10 +925,13 @@ func apiError(b []byte, fallback string) string {
return fallback
}
// a body in no shape known is shown as it is, cut short, so what the
- // vendor said isn't lost; other markup says nothing worth showing
+ // vendor said isn't lost; other markup (an XML error, a page's
+ // fragment) by its words, its tags taken out
s := strings.Join(strings.Fields(string(b)), " ")
if strings.HasPrefix(s, "<") {
- return fallback
+ if s = MarkupText(b); s == "" {
+ return fallback
+ }
}
if r := []rune(s); len(r) > 300 {
s = string(r[:300]) + "…"
@@ -1001,5 +1004,29 @@ func WebPage(b []byte) (string, bool) {
if title := PageTitle(t); title != "" {
return "a web page, not an API reply: “" + title + "”", true
}
+ // one with no title by its words, which may say why (a relay's "only
+ // … clients are allowed"), cut as a title is
+ if said := MarkupText(t); said != "" {
+ if r := []rune(said); len(r) > 120 {
+ said = string(r[:120]) + "…"
+ }
+ return "a web page, not an API reply: “" + said + "”", true
+ }
return "a web page, not an API reply", true
}
+
+// unshown is markup whose words aren't a reader's: a script, a style
+// sheet, a comment, an XML declaration.
+var unshown = regexp.MustCompile(`(?is)<script[^>]*>.*?</script\s*>|<style[^>]*>.*?</style\s*>|<!--.*?-->|<\?.*?\?>|<!doctype[^>]*>`)
+
+// MarkupText is the words markup holds, as plain words: scripts, styles
+// and comments out, then every tag, entities read, whitespace folded, cut
+// to 300 runes. "" when it holds none.
+func MarkupText(b []byte) string {
+ s := unshown.ReplaceAllString(string(b), " ")
+ s = strings.Join(strings.Fields(html.UnescapeString(tagRe.ReplaceAllString(s, " "))), " ")
+ if r := []rune(s); len(r) > 300 {
+ s = string(r[:300]) + "…"
+ }
+ return s
+}