Add an in-browser chat, without putting a key in the browser
So someone can try a model without setting up a client, and — more often — tell whether a problem is the gateway or their code. The page carries a session, and the server turns that into the identity an API key would have produced, resolved from the person's project. The request then goes down the ordinary path by being handed to the same handler /v1 uses: admission, model allowlist, rate limits, budget, accounting. There is exactly one request path, so there is no second place for its rules to drift — and without that, a playground is a hole in every limit the rest of the gateway enforces. The allowlist test posts a forbidden model and expects 403 for that reason. The body is rebuilt rather than forwarded, so the page cannot set fields it has no business setting: a stream:false that would hang the reader, a max_tokens large enough to occupy a replica for an hour. One turn is capped at 512 tokens. The picker offers what is actually served and allowed, not what is catalogued, because offering a model with no replica produces a 503 the reader cannot interpret. Inline vanilla JavaScript, for the same reason the charts are hand-drawn SVG. It streams, because on CPU inference ten seconds is normal and a chat that hangs and then dumps a paragraph feels broken. Replies are written with textContent throughout: model output and user input have no business being parsed as markup. Its POSTs carry the CSRF token in a header, which a cross-site form cannot set at all. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
859f08da9b
commit
de21649ad6
10 files changed
+567
-8
No files matched your search
+151
-7
@@ -1,7 +1,9 @@
|
||||
package e2e
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
@@ -64,16 +66,18 @@ func (b *browser) post(path string, form url.Values) (int, string, string) {
|
||||
return resp.StatusCode, string(body), resp.Header.Get("Location")
|
||||
}
|
||||
|
||||
// csrf pulls the token the server put in the page's forms.
|
||||
// csrf pulls the token the server put in the page, from a form field or the
|
||||
// meta tag the JSON-posting pages use.
|
||||
func csrf(t *testing.T, body string) string {
|
||||
t.Helper()
|
||||
const marker = `name="csrf" value="`
|
||||
i := strings.Index(body, marker)
|
||||
if i < 0 {
|
||||
t.Fatal("no csrf token in the page")
|
||||
for _, marker := range []string{`name="csrf" value="`, `name="csrf" content="`} {
|
||||
if i := strings.Index(body, marker); i >= 0 {
|
||||
rest := body[i+len(marker):]
|
||||
return rest[:strings.IndexByte(rest, '"')]
|
||||
}
|
||||
}
|
||||
rest := body[i+len(marker):]
|
||||
return rest[:strings.IndexByte(rest, '"')]
|
||||
t.Fatal("no csrf token in the page")
|
||||
return ""
|
||||
}
|
||||
|
||||
func signupHarness(t *testing.T) (*harness, string) {
|
||||
@@ -468,3 +472,143 @@ func (fakeCluster) Nodes() []adminapi.NodeView {
|
||||
func (fakeCluster) Models() []adminapi.ModelView { return nil }
|
||||
func (fakeCluster) Replicas(string) []adminapi.ReplicaView { return nil }
|
||||
func (fakeCluster) Explain(string) adminapi.ExplainView { return adminapi.ExplainView{} }
|
||||
|
||||
func TestTheChatPageTalksToTheModelWithoutTheBrowserHoldingAKey(t *testing.T) {
|
||||
// The point of the page: someone tries a model without setting up a client,
|
||||
// and the browser never sees a credential. The request still goes down the
|
||||
// ordinary path, so every limit that applies to an API key applies here.
|
||||
path, _ := provision(t)
|
||||
h := start(t, 2, func(c *config.Server) {
|
||||
c.Auth = config.Auth{Enabled: true, Store: path, SignupOpen: true, CacheTTL: 50 * time.Millisecond}
|
||||
})
|
||||
b := newBrowser(t, h.router.URL)
|
||||
signUpAndApprove(t, b, path, "ayse@example.com")
|
||||
|
||||
code, page := b.get("/chat")
|
||||
if code != http.StatusOK {
|
||||
t.Fatalf("/chat returned %d", code)
|
||||
}
|
||||
if !strings.Contains(page, testModel) {
|
||||
t.Errorf("the model picker does not offer %s", testModel)
|
||||
}
|
||||
// Whatever else is on the page, a key is not.
|
||||
if strings.Contains(page, store.KeyPrefix) {
|
||||
t.Error("an API key was rendered into the chat page")
|
||||
}
|
||||
|
||||
token := csrf(t, page)
|
||||
body, _ := json.Marshal(map[string]any{
|
||||
"model": testModel,
|
||||
"messages": []map[string]string{{"role": "user", "content": "hello"}},
|
||||
})
|
||||
req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost,
|
||||
h.router.URL+"/chat/send", bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-CSRF-Token", token)
|
||||
resp, err := b.c.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
out, _ := io.ReadAll(resp.Body)
|
||||
t.Fatalf("chat send returned %d: %s", resp.StatusCode, out)
|
||||
}
|
||||
out, _ := io.ReadAll(resp.Body)
|
||||
if !strings.Contains(string(out), "data:") {
|
||||
t.Errorf("the reply did not stream: %.200s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheChatPageIsNotOpenToStrangers(t *testing.T) {
|
||||
path, _ := provision(t)
|
||||
h := start(t, 2, func(c *config.Server) {
|
||||
c.Auth = config.Auth{Enabled: true, Store: path, SignupOpen: true}
|
||||
})
|
||||
anon := newBrowser(t, h.router.URL)
|
||||
|
||||
if code, _ := anon.get("/chat"); code != http.StatusSeeOther {
|
||||
t.Errorf("an anonymous visit to /chat returned %d", code)
|
||||
}
|
||||
req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost,
|
||||
h.router.URL+"/chat/send", strings.NewReader(`{"model":"m","messages":[{}]}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := anon.c.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusUnauthorized {
|
||||
t.Errorf("an anonymous send returned %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatObeysTheModelAllowlist(t *testing.T) {
|
||||
// If the page could reach a model the key path refuses, it would be a hole
|
||||
// in the limits everything else enforces.
|
||||
path, _ := provision(t)
|
||||
h := start(t, 2, func(c *config.Server) {
|
||||
c.Auth = config.Auth{Enabled: true, Store: path, SignupOpen: true, CacheTTL: 50 * time.Millisecond}
|
||||
})
|
||||
b := newBrowser(t, h.router.URL)
|
||||
signUpAndApprove(t, b, path, "ayse@example.com")
|
||||
|
||||
withStore(t, path, func(s *store.SQLite) {
|
||||
if err := s.PutLimits(context.Background(), store.Limits{
|
||||
Scope: store.ScopeProject, ScopeID: "proj",
|
||||
AllowedModels: []string{"something-else"},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
_, page := b.get("/chat")
|
||||
if strings.Contains(page, `value="`+testModel+`"`) {
|
||||
t.Error("the picker offers a model the allowlist forbids")
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(map[string]any{
|
||||
"model": testModel, "messages": []map[string]string{{"role": "user", "content": "hi"}},
|
||||
})
|
||||
req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost,
|
||||
h.router.URL+"/chat/send", bytes.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-CSRF-Token", csrf(t, page))
|
||||
resp, err := b.c.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusForbidden {
|
||||
t.Errorf("asking for a forbidden model returned %d", resp.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatSendNeedsItsToken(t *testing.T) {
|
||||
// A JSON post carries the token in a header, which a cross-site form cannot
|
||||
// set at all.
|
||||
path, _ := provision(t)
|
||||
h := start(t, 2, func(c *config.Server) {
|
||||
c.Auth = config.Auth{Enabled: true, Store: path, SignupOpen: true}
|
||||
})
|
||||
b := newBrowser(t, h.router.URL)
|
||||
signUpAndApprove(t, b, path, "ayse@example.com")
|
||||
|
||||
for _, token := range []string{"", "forged"} {
|
||||
req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost,
|
||||
h.router.URL+"/chat/send", strings.NewReader(`{"model":"m","messages":[{}]}`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if token != "" {
|
||||
req.Header.Set("X-CSRF-Token", token)
|
||||
}
|
||||
resp, err := b.c.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusBadRequest {
|
||||
t.Errorf("a send with token %q returned %d", token, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user