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
@@ -236,6 +236,27 @@ Tell them out of band and have them change it from their profile. Their sessions
|
||||
are closed — if they lost the password, the reason might be that someone else has
|
||||
it. Their API keys keep working; those are separate credentials.
|
||||
|
||||
## Trying a model in the browser
|
||||
|
||||
`/chat` is a playground for anyone signed in. It exists so somebody can see
|
||||
whether a model works without setting up a client, and — more often — so they
|
||||
can tell whether a problem is the gateway or their code.
|
||||
|
||||
**The browser never holds a key.** The page carries a session; the server turns
|
||||
that into the same identity an API key would produce, resolved from the person's
|
||||
project, and the request goes down the ordinary path: admission, model
|
||||
allowlist, rate limits, budget, accounting. Anything else would make the page a
|
||||
hole in every limit the rest of the gateway enforces.
|
||||
|
||||
Usage from the page is attributed to `session:<user-id>` rather than a key, so
|
||||
it is billed to the right project and visibly did not come from a credential
|
||||
somebody holds. One turn is capped at 512 tokens: a playground should not be the
|
||||
thing that ties up a replica for ten minutes, and anyone who wants a longer
|
||||
answer has an API key for that.
|
||||
|
||||
Nothing is stored. The conversation lives in the page and is gone when it is
|
||||
closed.
|
||||
|
||||
## Seeing the cluster
|
||||
|
||||
The dashboard — nodes, replicas, models, why something did not get placed, usage
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"slices"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/auth"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/version"
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/web"
|
||||
)
|
||||
|
||||
// registerChat mounts the in-browser chat for signed-in people.
|
||||
//
|
||||
// It exists so someone can try a model without setting up a client, and — more
|
||||
// often — so they can tell whether a problem is the gateway or their code.
|
||||
//
|
||||
// The browser never sees an API key. The page carries a session, the server
|
||||
// turns that into the same identity an API key would produce, and the request
|
||||
// goes down the ordinary path: admission, model allowlist, rate limits, budget,
|
||||
// accounting. Anything else would make this page a hole in every limit the rest
|
||||
// of the gateway enforces.
|
||||
func (r *Router) registerChat(mux *http.ServeMux, pages *web.AccountPages) {
|
||||
mux.HandleFunc("GET /chat", pages.Guard(func(w http.ResponseWriter, req *http.Request, user store.User) {
|
||||
web.Chat(user, r.modelsFor(req, user), version.Version)(w, req)
|
||||
}))
|
||||
mux.HandleFunc("POST /chat/send", pages.Guard(r.chatSend))
|
||||
}
|
||||
|
||||
// chatSend dispatches one turn on behalf of a signed-in person.
|
||||
func (r *Router) chatSend(w http.ResponseWriter, req *http.Request, user store.User) {
|
||||
principal, err := r.principalFor(req, user)
|
||||
if err != nil {
|
||||
http.Error(w, "your account has no project to bill this to", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(req.Body, r.cfg.MaxBodyBytes))
|
||||
if err != nil {
|
||||
http.Error(w, "could not read the request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var in struct {
|
||||
Model string `json:"model"`
|
||||
Messages []json.RawMessage `json:"messages"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &in); err != nil || in.Model == "" || len(in.Messages) == 0 {
|
||||
http.Error(w, "a model and at least one message are required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 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.
|
||||
upstream, err := json.Marshal(map[string]any{
|
||||
"model": in.Model,
|
||||
"messages": in.Messages,
|
||||
"stream": true,
|
||||
"max_tokens": chatMaxTokens,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, "could not build the request", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
next := req.Clone(auth.WithPrincipal(req.Context(), principal))
|
||||
next.Body = io.NopCloser(bytes.NewReader(upstream))
|
||||
next.ContentLength = int64(len(upstream))
|
||||
next.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// The same handler the API serves, so there is exactly one request path and
|
||||
// no second place for its rules to drift.
|
||||
r.handleInference("/v1/chat/completions")(w, next)
|
||||
}
|
||||
|
||||
// chatMaxTokens bounds one turn from the page.
|
||||
//
|
||||
// A playground should not be the thing that ties up a replica for ten minutes,
|
||||
// and someone who wants a longer answer has an API key for that.
|
||||
const chatMaxTokens = 512
|
||||
|
||||
// principalFor builds the identity a signed-in person acts under.
|
||||
//
|
||||
// The key id is synthetic and marked as a session, because there is no key: it
|
||||
// keeps usage rows attributable and rate limits per person, while making it
|
||||
// obvious in the usage table that these requests came from the page rather than
|
||||
// from a credential somebody holds.
|
||||
func (r *Router) principalFor(req *http.Request, user store.User) (auth.Principal, error) {
|
||||
if r.store == nil || user.ProjectID == "" {
|
||||
return auth.Principal{}, errors.New("no project")
|
||||
}
|
||||
eff, err := r.store.ResolveProject(req.Context(), user.ProjectID)
|
||||
if err != nil {
|
||||
return auth.Principal{}, err
|
||||
}
|
||||
return auth.Principal{
|
||||
KeyID: "session:" + user.ID,
|
||||
KeyPrefix: "session",
|
||||
ProjectID: eff.ProjectID,
|
||||
TeamID: eff.TeamID,
|
||||
OrgID: eff.OrgID,
|
||||
Band: eff.Band,
|
||||
RoutingProfile: eff.RoutingProfile,
|
||||
Limits: eff,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// modelsFor lists what this person may actually ask for.
|
||||
//
|
||||
// Served rather than catalogued: offering a model with no replica produces a
|
||||
// 503 the reader cannot interpret, and the allowlist is applied so the picker
|
||||
// never shows a model the request would refuse.
|
||||
func (r *Router) modelsFor(req *http.Request, user store.User) []string {
|
||||
principal, err := r.principalFor(req, user)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
seen := map[string]struct{}{}
|
||||
var out []string
|
||||
for _, s := range r.reg.Snapshots("") {
|
||||
for _, m := range s.Models {
|
||||
if _, dup := seen[m]; dup {
|
||||
continue
|
||||
}
|
||||
seen[m] = struct{}{}
|
||||
if r.permits(principal, m) {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
slices.Sort(out)
|
||||
return out
|
||||
}
|
||||
@@ -108,6 +108,7 @@ func (r *Router) Handler() http.Handler {
|
||||
// they are exempt below.
|
||||
pages := web.NewAccountPages(r.accounts, r.store, r.store, version.Version, r.log)
|
||||
pages.Register(mux)
|
||||
r.registerChat(mux, pages)
|
||||
|
||||
if r.clusterView != nil {
|
||||
// The cluster dashboard, reachable from a browser without exposing
|
||||
@@ -127,7 +128,8 @@ func (r *Router) Handler() http.Handler {
|
||||
// stays open because a load balancer has no key.
|
||||
return auth.Middleware(r.auth, r.log,
|
||||
"/health", "/", "/_static/", "/claim/",
|
||||
"/signup", "/login", "/logout", "/profile", "/profile/", "/admin", "/admin/")(mux)
|
||||
"/signup", "/login", "/logout", "/profile", "/profile/",
|
||||
"/admin", "/admin/", "/chat", "/chat/")(mux)
|
||||
}
|
||||
|
||||
// parsedRequest is what the router needs from a request. Everything else in the
|
||||
|
||||
@@ -256,3 +256,41 @@ func (s *SQLite) AllProjects(ctx context.Context) ([]ProjectPath, error) {
|
||||
type ProjectPath struct {
|
||||
ID, Path string
|
||||
}
|
||||
|
||||
// ResolveProject returns the limits an account inherits from its project.
|
||||
//
|
||||
// The same walk as Resolve, minus the key. A person signed in to the chat page
|
||||
// has no key in their hand — the key was shown once and is theirs to keep — so
|
||||
// their identity there is their project, and it must be resolved the same way
|
||||
// or the page would be a hole in the limits everything else obeys.
|
||||
func (s *SQLite) ResolveProject(ctx context.Context, projectID string) (Effective, error) {
|
||||
var e Effective
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
`SELECT p.id, t.id, o.id
|
||||
FROM projects p
|
||||
JOIN teams t ON t.id = p.team_id
|
||||
JOIN organizations o ON o.id = t.org_id
|
||||
WHERE p.id = ?`, projectID)
|
||||
if err := row.Scan(&e.ProjectID, &e.TeamID, &e.OrgID); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return Effective{}, ErrNotFound
|
||||
}
|
||||
return Effective{}, err
|
||||
}
|
||||
|
||||
// Widest first, so a narrower scope can only tighten what it inherits.
|
||||
for _, step := range []struct {
|
||||
scope Scope
|
||||
id string
|
||||
}{{ScopeOrg, e.OrgID}, {ScopeTeam, e.TeamID}, {ScopeProject, e.ProjectID}} {
|
||||
l, err := s.LimitsFor(ctx, step.scope, step.id)
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return Effective{}, err
|
||||
}
|
||||
e.tighten(l)
|
||||
}
|
||||
return e, nil
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
|
||||
)
|
||||
|
||||
type chatData struct {
|
||||
User store.User
|
||||
Models []string
|
||||
CSRF string
|
||||
Version string
|
||||
Now string
|
||||
}
|
||||
|
||||
// Chat renders the in-browser chat for one signed-in person.
|
||||
func Chat(user store.User, models []string, version string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
noStore(w)
|
||||
render(w, "chat", chatData{
|
||||
User: user, Models: models, CSRF: csrfFor(r),
|
||||
Version: version, Now: time.Now().UTC().Format(time.RFC3339),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Guard runs a handler only for a signed-in, approved account.
|
||||
//
|
||||
// Exported because the chat lives in the router — it needs the endpoint
|
||||
// registry and the request path — while the session machinery lives here. The
|
||||
// alternative was a second copy of the session check next to the one thing that
|
||||
// must never have two copies.
|
||||
//
|
||||
// Its POSTs carry the CSRF token in a header rather than a form field, because
|
||||
// they are JSON. A header is a stronger check than a field: a cross-site form
|
||||
// post cannot set one at all.
|
||||
func (p *AccountPages) Guard(
|
||||
next func(w http.ResponseWriter, r *http.Request, user store.User),
|
||||
) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
noStore(w)
|
||||
sess, user, ok := p.session(r)
|
||||
if !ok {
|
||||
if r.Method == http.MethodGet {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
} else {
|
||||
http.Error(w, "not signed in", http.StatusUnauthorized)
|
||||
}
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodGet {
|
||||
token := r.Header.Get("X-CSRF-Token")
|
||||
if token == "" {
|
||||
token = r.FormValue("csrf")
|
||||
}
|
||||
if !p.accounts.CheckCSRF(sess, token) {
|
||||
http.Error(w, "bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
next(w, r, user)
|
||||
}
|
||||
}
|
||||
@@ -53,6 +53,7 @@ Tell whoever you asked for access that you have signed up.</p>
|
||||
<h1>Your keys</h1>
|
||||
<p class="sub">{{.User.Name}} · {{.User.Email}} · project {{.User.ProjectID}}</p>
|
||||
<nav>
|
||||
<a href="/chat">chat</a>
|
||||
<a href="/">about this gateway</a>
|
||||
{{if .User.IsAdmin}}<a href="/admin">cluster</a>{{end}}
|
||||
<form method="post" action="/logout" class="inline">
|
||||
|
||||
@@ -86,3 +86,16 @@ select { font: inherit; padding: .35rem .4rem; border: 1px solid var(--line);
|
||||
font-size: .72rem; color: var(--muted); }
|
||||
.chart-row { display: grid; gap: 1rem; grid-template-columns: 1fr 1fr; }
|
||||
@media (max-width: 720px) { .chart-row { grid-template-columns: 1fr; } }
|
||||
.chat-bar { display: flex; align-items: center; gap: 1rem; margin-bottom: .75rem; }
|
||||
.inline-label { font-size: .85rem; font-weight: 600; margin: 0; }
|
||||
.inline-label select { margin-left: .4rem; font-weight: 400; }
|
||||
.chat-log { min-height: 16rem; max-height: 55vh; overflow-y: auto; padding: .5rem;
|
||||
background: var(--card); border: 1px solid var(--line); border-radius: 6px; }
|
||||
.bubble { white-space: pre-wrap; word-wrap: break-word; padding: .55rem .75rem;
|
||||
margin: .4rem 0; border-radius: 8px; max-width: 46rem; }
|
||||
.bubble.you { background: var(--accent); color: var(--bg); margin-left: auto; }
|
||||
.bubble.assistant { background: var(--bg); border: 1px solid var(--line); }
|
||||
.bubble.failed { color: var(--bad); border-color: var(--bad); }
|
||||
.chat-composer { display: flex; gap: .6rem; margin-top: .75rem; align-items: flex-end; }
|
||||
.chat-composer textarea { flex: 1; font: inherit; padding: .5rem .6rem; resize: vertical;
|
||||
border: 1px solid var(--line); border-radius: 6px; background: var(--card); color: var(--fg); }
|
||||
@@ -0,0 +1,132 @@
|
||||
{{define "chat"}}<!doctype html>
|
||||
<html lang="en"><head>
|
||||
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Chat · llmgw</title><link rel="stylesheet" href="/_static/base.css">
|
||||
<meta name="csrf" content="{{.CSRF}}">
|
||||
</head><body><div class="wrap">
|
||||
<header>
|
||||
<h1>Chat</h1>
|
||||
<p class="sub">{{.User.Email}} · project {{.User.ProjectID}}</p>
|
||||
<nav>
|
||||
<a href="/profile">your keys</a>
|
||||
{{if .User.IsAdmin}}<a href="/admin">cluster</a>{{end}}
|
||||
<a href="/">about this gateway</a>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
{{if .Models}}
|
||||
<div class="chat-bar">
|
||||
<label class="inline-label">Model
|
||||
<select id="model">{{range .Models}}<option value="{{.}}">{{.}}</option>{{end}}</select>
|
||||
</label>
|
||||
<button id="reset" type="button" class="link">New conversation</button>
|
||||
</div>
|
||||
|
||||
<div id="log" class="chat-log" aria-live="polite"></div>
|
||||
|
||||
<form id="composer" class="chat-composer">
|
||||
<textarea id="input" rows="3" placeholder="Ask something…" required></textarea>
|
||||
<button type="submit" id="send">Send</button>
|
||||
</form>
|
||||
<p class="note">Nothing here is stored. The conversation lives in this page and
|
||||
is gone when you close it — what is recorded is the same usage any request
|
||||
records: tokens, cost and latency.</p>
|
||||
{{else}}
|
||||
<p class="empty">No models are available to you right now.</p>
|
||||
<p class="note">Either nothing is placed yet, or your project's allowlist does
|
||||
not include anything that is. <code>/admin</code> shows the cluster if you have
|
||||
access to it.</p>
|
||||
{{end}}
|
||||
|
||||
<footer><span>llmgw {{.Version}}</span><span>{{.Now}}</span></footer>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Vanilla, inline, no build step — the same reason the charts are hand-drawn
|
||||
// SVG. It streams because a chat that hangs for ten seconds and then dumps a
|
||||
// paragraph feels broken, and on CPU inference ten seconds is normal.
|
||||
(function () {
|
||||
const log = document.getElementById("log");
|
||||
if (!log) return;
|
||||
const form = document.getElementById("composer");
|
||||
const input = document.getElementById("input");
|
||||
const send = document.getElementById("send");
|
||||
const model = document.getElementById("model");
|
||||
// From a meta tag rather than interpolated into the script, so the token
|
||||
// travels through the same HTML escaping as everything else on the page.
|
||||
const csrf = document.querySelector('meta[name="csrf"]').content;
|
||||
let messages = [];
|
||||
|
||||
document.getElementById("reset").onclick = () => { messages = []; log.textContent = ""; };
|
||||
|
||||
function bubble(role) {
|
||||
const el = document.createElement("div");
|
||||
el.className = "bubble " + role;
|
||||
// textContent throughout: the reply is model output and the prompt is user
|
||||
// input, and neither has any business being parsed as markup.
|
||||
log.appendChild(el);
|
||||
log.scrollTop = log.scrollHeight;
|
||||
return el;
|
||||
}
|
||||
|
||||
form.onsubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
const text = input.value.trim();
|
||||
if (!text) return;
|
||||
|
||||
input.value = "";
|
||||
send.disabled = true;
|
||||
bubble("you").textContent = text;
|
||||
messages.push({ role: "user", content: text });
|
||||
|
||||
const out = bubble("assistant");
|
||||
let reply = "";
|
||||
try {
|
||||
const resp = await fetch("/chat/send", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", "X-CSRF-Token": csrf },
|
||||
body: JSON.stringify({ model: model.value, messages: messages }),
|
||||
});
|
||||
if (!resp.ok) {
|
||||
out.textContent = "(" + resp.status + ") " + (await resp.text()).slice(0, 400);
|
||||
out.classList.add("failed");
|
||||
send.disabled = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = resp.body.getReader();
|
||||
const dec = new TextDecoder();
|
||||
let buf = "";
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
buf += dec.decode(value, { stream: true });
|
||||
// Server-sent events arrive in whatever chunks the network felt like;
|
||||
// only whole lines can be parsed.
|
||||
let nl;
|
||||
while ((nl = buf.indexOf("\n")) >= 0) {
|
||||
const line = buf.slice(0, nl).trim();
|
||||
buf = buf.slice(nl + 1);
|
||||
if (!line.startsWith("data:")) continue;
|
||||
const payload = line.slice(5).trim();
|
||||
if (payload === "[DONE]") continue;
|
||||
try {
|
||||
const chunk = JSON.parse(payload);
|
||||
const piece = chunk.choices?.[0]?.delta?.content;
|
||||
if (piece) { reply += piece; out.textContent = reply; log.scrollTop = log.scrollHeight; }
|
||||
} catch (_) { /* a partial frame; the next read completes it */ }
|
||||
}
|
||||
}
|
||||
if (reply) messages.push({ role: "assistant", content: reply });
|
||||
else { out.textContent = "(no content came back)"; out.classList.add("failed"); }
|
||||
} catch (err) {
|
||||
out.textContent = "(" + err + ")";
|
||||
out.classList.add("failed");
|
||||
} finally {
|
||||
send.disabled = false;
|
||||
input.focus();
|
||||
}
|
||||
};
|
||||
})();
|
||||
</script>
|
||||
</body></html>{{end}}
|
||||
@@ -1447,6 +1447,11 @@ The launch path is now built too:
|
||||
- [ ] External provider support (as a fallback)
|
||||
- [x] Read-only dashboard on the admin listener (nodes, replicas, models,
|
||||
placement failures, usage) and a public landing page
|
||||
- [x] `/chat`: an in-browser playground for signed-in people, dispatched through
|
||||
the ordinary request path so every limit still applies and the browser
|
||||
never holds a key
|
||||
- [x] `/admin/users`: approve, suspend and reinstate accounts from the browser;
|
||||
promoting, resetting passwords and issuing keys stay on the CLI
|
||||
- [x] The dashboard reachable over the web at `/admin`, behind a session and an
|
||||
admin flag, without exposing the listener that carries pprof
|
||||
- [ ] An admin UI that can *change* things, which needs roles this deliberately
|
||||
|
||||
+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