Hide the mirror's bookkeeping, bound GGUF array nesting, and forget old serve jobs and downloads

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-04 10:56:16 +02:00
1 parent fdd4dcf841
commit 7e57863edd
10 files changed
+151 -3

No files matched your search

+5
View File
@@ -196,6 +196,11 @@ APIs change between minors without a deprecation period.
pages' "only while visible" refresh no longer needs an evaluated filter.
Pages now carry their headers even when answered with an error status.
- **KaTeX 0.16.47** (from 0.16.11), past the advisories fixed in 0.16.21.
- **Smaller hardening.** The mirror no longer serves its bookkeeping files
(a file's `.source` URL and `.sha256`) on `:8081`. A GGUF header nesting
arrays more than 4 deep is refused rather than recursed into. Serve and
update jobs keep the last 100 finished, and the mirror forgets finished
downloads after a week, instead of remembering every one until restart.
### Everything that can be changed on a running cluster, from the console
+8
View File
@@ -388,6 +388,14 @@ func readOnly(h http.Handler) http.Handler {
http.Error(w, "the mirror is read-only", http.StatusMethodNotAllowed)
return
}
// The mirror's own bookkeeping — a file's checksum, the URL it came
// from — sits beside it under a dot, and is nobody's to download.
for _, seg := range strings.Split(r.URL.Path, "/") {
if strings.HasPrefix(seg, ".") {
http.NotFound(w, r)
return
}
}
h.ServeHTTP(w, r)
})
}
+21
View File
@@ -72,3 +72,24 @@ func TestAVersionAnswersHeadAsWellAsGet(t *testing.T) {
t.Errorf("expected a manifest, got %q", got)
}
}
func TestTheMirrorDoesNotServeItsBookkeeping(t *testing.T) {
root := t.TempDir()
dir := filepath.Join(root, "llama", "v1")
_ = os.MkdirAll(dir, 0o755)
_ = os.WriteFile(filepath.Join(dir, "m.gguf"), []byte("x"), 0o644)
_ = os.WriteFile(filepath.Join(dir, ".m.gguf.source"), []byte("https://example.com/m.gguf\n"), 0o644)
h := readOnly(withManifest(root, http.FileServer(http.Dir(root))))
for path, want := range map[string]int{
"/llama/v1/m.gguf": http.StatusOK,
"/llama/v1/.m.gguf.source": http.StatusNotFound,
"/llama/.hidden/x": http.StatusNotFound,
} {
w := httptest.NewRecorder()
h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
if w.Code != want {
t.Errorf("%s: %d, expected %d", path, w.Code, want)
}
}
}
+26 -2
View File
@@ -234,13 +234,37 @@ func (s *Server) Start(req Request) (Job, error) {
s.seq++
j := &Job{ID: fmt.Sprintf("serve-%d-%d", time.Now().Unix(), s.seq), Request: req,
State: Running, Started: time.Now()}
s.jobs[j.ID] = j
s.keepJob(j)
s.mu.Unlock()
go s.run(j)
return s.snapshot(j), nil
}
// Jobs is every job since the control plane started, the newest first.
// keptJobs is how many finished jobs are remembered: enough for the console's
// list, and not one more for every model ever served.
const keptJobs = 100
// keepJob records a job, and forgets the oldest finished ones beyond
// keptJobs. Called with s.mu held.
func (s *Server) keepJob(j *Job) {
s.jobs[j.ID] = j
var done []*Job
for _, o := range s.jobs {
if o.State != Running {
done = append(done, o)
}
}
if len(done) <= keptJobs {
return
}
slices.SortFunc(done, func(a, b *Job) int { return a.Started.Compare(b.Started) })
for _, o := range done[:len(done)-keptJobs] {
delete(s.jobs, o.ID)
}
}
// Jobs is the jobs since the control plane started — every one running, and
// the last keptJobs finished — the newest first.
func (s *Server) Jobs() []Job {
s.mu.Lock()
all := make([]*Job, 0, len(s.jobs))
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/binary"
"fmt"
"net/http"
"net/http/httptest"
"slices"
@@ -375,3 +376,24 @@ func TestAModelThatFitsBusyCardsIsAddedToWaitNotRefused(t *testing.T) {
t.Errorf("%+v", last)
}
}
func TestOnlyTheLastFinishedJobsAreRemembered(t *testing.T) {
s := New(Deps{})
start := time.Now().Add(-time.Hour)
s.mu.Lock()
running := &Job{ID: "running", State: Running, Started: start}
s.keepJob(running)
for i := range keptJobs + 5 {
s.keepJob(&Job{ID: fmt.Sprintf("done-%d", i), State: "done", Started: start.Add(time.Duration(i+1) * time.Second)})
}
s.mu.Unlock()
if n := len(s.Jobs()); n != keptJobs+1 {
t.Errorf("%d jobs remembered, expected %d finished and the running one", n, keptJobs)
}
if _, ok := s.Get("running"); !ok {
t.Error("the running job, the oldest, was forgotten")
}
if _, ok := s.Get("done-0"); ok {
t.Error("the oldest finished job is still remembered")
}
}
+1 -1
View File
@@ -436,7 +436,7 @@ func (s *Server) StartUpdate(name, by string) (Job, error) {
s.seq++
j := &Job{ID: fmt.Sprintf("update-%d-%d", time.Now().Unix(), s.seq), Kind: "update", Model: name,
Request: Request{Name: name, By: by}, State: Running, Started: time.Now()}
s.jobs[j.ID] = j
s.keepJob(j)
s.mu.Unlock()
go s.runUpdate(j)
return s.snapshot(j), nil
+12
View File
@@ -163,6 +163,10 @@ func Read(r io.Reader) (Header, error) {
}
return nil
}
// depth is how many arrays the value being read is inside. Real files
// nest none; a crafted one nesting thousands deep made a recursion as
// deep, a few bytes per level.
depth := 0
var value func(t uint32) (string, error)
value = func(t uint32) (string, error) {
num = 0
@@ -195,6 +199,11 @@ func Read(r io.Reader) (Header, error) {
if n > math.MaxInt32 {
return "", fmt.Errorf("a %d-element array in a GGUF header", n)
}
if depth >= maxArrayDepth {
return "", fmt.Errorf("arrays nested more than %d deep in a GGUF header", maxArrayDepth)
}
depth++
defer func() { depth-- }()
for i := uint64(0); i < n; i++ {
if _, err := value(et); err != nil {
return "", err
@@ -296,6 +305,9 @@ func FromURL(ctx context.Context, client *http.Client, url string) (Header, erro
return Read(io.LimitReader(resp.Body, headerBytes))
}
// maxArrayDepth is how deep arrays may nest in a header.
const maxArrayDepth = 4
// IsAdapter reports whether the file is a LoRA adapter rather than a model.
func (h Header) IsAdapter() bool { return h.Type == "adapter" }
+20
View File
@@ -143,3 +143,23 @@ func TestTheKVCacheIsSizedFromTheModelsShape(t *testing.T) {
t.Error("a header with no shape produced a figure")
}
}
func TestArraysNestedDeepAreRefused(t *testing.T) {
var b bytes.Buffer
w := func(v any) { _ = binary.Write(&b, binary.LittleEndian, v) }
b.WriteString("GGUF")
w(uint32(3))
w(uint64(0))
w(uint64(1))
w(uint64(len("deep")))
b.WriteString("deep")
w(uint32(tArray))
for range 10_000 {
w(uint32(tArray))
w(uint64(1))
}
_, err := Read(bytes.NewReader(b.Bytes()))
if err == nil || !strings.Contains(err.Error(), "nested") {
t.Errorf("ten thousand nested arrays: %v", err)
}
}
+14
View File
@@ -128,6 +128,7 @@ func (m *Manager) Start(source string, e Entry) (Status, error) {
State: Fetching, Source: source, StartedAt: time.Now(),
}
m.running[key] = st
m.forgetOld(time.Now())
out := *st
m.mu.Unlock()
@@ -308,6 +309,19 @@ func (m *Manager) All() ([]Status, error) {
return out, nil
}
// keptFinished is how long a finished download's status is remembered.
const keptFinished = 7 * 24 * time.Hour
// forgetOld drops downloads finished more than keptFinished ago. Called with
// m.mu held.
func (m *Manager) forgetOld(now time.Time) {
for key, st := range m.running {
if st.State != Fetching && !st.FinishedAt.IsZero() && now.Sub(st.FinishedAt) > keptFinished {
delete(m.running, key)
}
}
}
// Forget drops a remembered download, so a failed one can be retried.
//
// Without it a fetch that failed once would refuse every later attempt at the
+22
View File
@@ -0,0 +1,22 @@
package mirror
import (
"testing"
"time"
)
func TestAFinishedDownloadIsForgottenAfterAWeek(t *testing.T) {
m := NewManager(t.TempDir(), discardLog())
now := time.Now()
m.mu.Lock()
m.running["old"] = &Status{State: Present, FinishedAt: now.Add(-keptFinished - time.Hour)}
m.running["recent"] = &Status{State: Failed, FinishedAt: now.Add(-time.Hour)}
m.running["going"] = &Status{State: Fetching}
m.forgetOld(now)
_, old := m.running["old"]
n := len(m.running)
m.mu.Unlock()
if old || n != 2 {
t.Errorf("after a week: old remembered=%v, %d left", old, n)
}
}