oficina / slides Public

Merge remote-tracking branch 'origin/main' into feat/assistant

Paul Campbell pushed by paul@keymail.dev e63a6d69a4b9981505594727c852d179e43f67bf parent dd917c9parent d047eef
4 files changed, +108 −23
  • AGENTS.md +10 −4
  • cmd/slides/main.go +5 −1
  • internal/slides/assets.go +59 −18
  • internal/slidestest/assets_test.go +34 −0
diff --git a/AGENTS.md b/AGENTS.md
index a36f7fd..84548c1 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -152,10 +152,16 @@ comments, and version history. Those are the triggers in spec §9.
`docs/api.md` is the reference for the API, written for a person and an
agent both.
-**The deployment has no object store bound today** (`carlos store create`
-is the fix, and nobody has run it). The upload endpoints answer
-`503` there and the editor's image field says so, server-rendered, rather
-than letting somebody pick a file and find out afterwards.
+**The deployment's object store is bound (2026-11-11, bucket
+`carlos-store-bds-slides`) and served through the app**, not presigned:
+`cmd/slides` wraps it in `slides.ThroughApp`, so the browser uploads to
+`PUT /d/{sqid}/uploads/{key}` and pictures and exports stream back from
+this app. The presigned path below is the design and still what a
+presigning store would take, but the page's CSP (`default-src 'self'`,
+`img-src 'self' data:`) refuses a browser PUT to the bucket and a picture
+from it, and the bucket has no CORS for this origin; going direct needs
+both changed. With no store bound at all the upload endpoints answer
+`503` and the editor's image field says so.
## Layout
diff --git a/cmd/slides/main.go b/cmd/slides/main.go
index c6b18bb..98a8083 100644
--- a/cmd/slides/main.go
+++ b/cmd/slides/main.go
@@ -158,7 +158,11 @@ func newBlobs(env, dbPath string, logger *slog.Logger) (blobs.Store, error) {
return nil, err
}
if s3 != nil {
- return s3, nil
+ // Through the app, not presigned: the browser can reach neither
+ // the bucket's PUT nor its GET under this app's CSP, and the
+ // bucket has no CORS for it (slides.ThroughApp says why).
+ logger.Info("slides: object store bound, served through the app", "bucket", os.Getenv("CARLOS_STORE_BUCKET"))
+ return slides.ThroughApp(s3), nil
}
if env != slides.EnvDevelopment {
// No bucket outside development is not a refusal here: nothing
diff --git a/internal/slides/assets.go b/internal/slides/assets.go
index b6f840f..cde07d9 100644
--- a/internal/slides/assets.go
+++ b/internal/slides/assets.go
@@ -142,6 +142,37 @@ type presigner interface {
PresignGet(hash string, ttl time.Duration) (string, error)
}
+// ThroughApp is a bound object store with its presigning hidden, so
+// every byte goes through this app: an upload is PUT to
+// /d/{sqid}/uploads/{key} (putUpload), a picture is streamed from
+// serveAsset, and an export from serveExport — the paths development's
+// blobs.Dir always took.
+//
+// Production binds the platform's bucket through it (cmd/slides). The
+// presigned design (spec §2.7) has the browser PUT to the bucket and
+// load pictures from it, and neither can work as deployed: the page's
+// CSP is default-src 'self' and img-src 'self' data: (serving.go), so
+// the browser refuses both, and the platform's bucket answers no CORS
+// for this origin. Found when the store was first bound, 2026-11-11 —
+// the path had never run. Going direct again needs the bucket's origin
+// in connect-src and img-src and a CORS rule on the bucket; until then
+// this is the path that works. The cost is the bytes passing through
+// one instance. On the way out a picture is streamed (serveAsset). On
+// the way in rastrillo's blobs.S3.Put holds the body in memory (twice,
+// as of v0.27), and registration reads it again to sniff and promote,
+// so one 20 MiB upload can take ~60 MiB for a moment — fine for one
+// team, and a rastrillo streaming Put is the fix when it is not.
+func ThroughApp(s blobs.Store) blobs.Store {
+ if s == nil {
+ return nil
+ }
+ return throughApp{s}
+}
+
+// throughApp embeds only the blobs.Store interface, so a type assertion
+// to presigner fails on it whatever the store underneath can do.
+type throughApp struct{ blobs.Store }
+
// presignTTL is how long a minted URL lives. An hour for a GET, because
// that is the life of one person reading a deck; the same for a PUT,
// because a 20 MiB file on a bad connection is not a five-minute job.
@@ -310,12 +341,10 @@ func (a *app) signUpload(w http.ResponseWriter, r *http.Request) {
}
out["url"] = url
} else {
- // blobs.Dir has nothing to sign, so development uploads the
- // same way through this app. Production with a real bucket
- // never reaches this branch and never proxies a byte — see
- // putUpload's own refusal. ObjectKey stays empty: a
- // content-addressed store picks the key itself, and putUpload
- // records which one.
+ // A store that does not presign — blobs.Dir in development, and
+ // production's bucket behind ThroughApp — takes the bytes
+ // through this app. ObjectKey stays empty: a content-addressed
+ // store picks the key itself, and putUpload records which one.
out["url"] = "/d/" + d.Sqid + "/uploads/" + key
}
if err := a.guarded(r.Context(), a.deckGuard(r.Context(), u, d), func(db *gorm.DB) error { return db.Create(&up).Error }); err != nil {
@@ -330,15 +359,13 @@ func (a *app) signUpload(w http.ResponseWriter, r *http.Request) {
// --- PUT /d/{sqid}/uploads/{ref} ---
-// putUpload is the development-only receiving end of the URL signUpload
-// hands back when the bound store cannot presign — blobs.Dir, a
-// directory beside the database.
+// putUpload is the receiving end of the URL signUpload hands back when
+// the bound store does not presign — blobs.Dir in development, and the
+// platform's bucket behind ThroughApp in production.
//
-// It refuses outright on a store that CAN presign, rather than quietly
-// working: the whole point of the presigned path is that 20 MiB does
-// not travel through an app process that has one small instance's
-// memory, and an endpoint that silently accepted it anyway would be a
-// performance cliff nobody could see in the code.
+// It refuses outright on a store that DOES presign: that store's uploads
+// go to the bucket, and an endpoint that also accepted them here would
+// be a second path nobody chose.
//
// It takes a staging key, not a ref, and resolves the receipt for it
// before it reads a byte (codex 1). Without that this endpoint was its
@@ -793,12 +820,19 @@ func (a *app) serveAsset(w http.ResponseWriter, r *http.Request, d Deck) {
return
}
defer rc.Close()
- raw, err := io.ReadAll(io.LimitReader(rc, maxAssetBytes+1))
- if err != nil {
+ // Streamed, not read whole: the bucket is served through this app
+ // (ThroughApp), and a deck of large photos opened by a few readers
+ // at once would otherwise hold 20 MiB per picture per request. The
+ // sniff needs only the head (sniffImage reads 1 KiB), which is read
+ // first and sent ahead of the rest.
+ head := make([]byte, 1024)
+ n, err := io.ReadFull(rc, head)
+ if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) && !errors.Is(err, io.EOF) {
a.fail(w, r, "read an asset", err)
return
}
- kind := sniffImage(raw)
+ head = head[:n]
+ kind := sniffImage(head)
if kind == "" {
a.notFound(w, r)
return
@@ -808,7 +842,14 @@ func (a *app) serveAsset(w http.ResponseWriter, r *http.Request, d Deck) {
// is honest. Private rather than public — the address is the deck's
// capability, not the object's.
w.Header().Set("Cache-Control", "private, max-age=31536000, immutable")
- http.ServeContent(w, r, ref, time.Time{}, bytes.NewReader(raw))
+ // The ref is the content hash, so it is an honest strong validator.
+ w.Header().Set("ETag", `"`+ref+`"`)
+ if match := r.Header.Get("If-None-Match"); match != "" && strings.Contains(match, `"`+ref+`"`) {
+ w.WriteHeader(http.StatusNotModified)
+ return
+ }
+ w.Write(head)
+ io.Copy(w, io.LimitReader(rc, maxAssetBytes-int64(len(head))))
}
// assetHeaders is the header set every served asset carries.
diff --git a/internal/slidestest/assets_test.go b/internal/slidestest/assets_test.go
index 3fcab8b..678e019 100644
--- a/internal/slidestest/assets_test.go
+++ b/internal/slidestest/assets_test.go
@@ -311,6 +311,40 @@ func TestUploadAndRegisterWithDirStore(t *testing.T) {
}
}
+// TestBucketThroughTheAppTakesTheInAppPath: production binds its bucket
+// through slides.ThroughApp (cmd/slides), because the page's CSP lets
+// the browser reach neither the bucket's PUT nor its GET. A store that
+// CAN presign, bound that way, must sign nothing: the upload goes to
+// this app, the picture is streamed back by it, and the bytes land in
+// the bucket underneath.
+func TestBucketThroughTheAppTakesTheInAppPath(t *testing.T) {
+ bucket := &presigningStore{Store: dirStore(t)}
+ a := newAppWithStore(t, slides.ThroughApp(bucket))
+ cookies := signedIn(t, a, allowedAddress)
+ token := mintAgent(t, a, cookies, "claude")
+ sqid := apiDeck(t, a, token, "Pictures")
+ deck := "/d/" + sqid
+
+ record := upload(t, a, deck, cookies, "chart.png", "image/png", onePixelPNG, "A bar chart")
+ registerInDeck(t, a, token, sqid, record)
+ ref := record["ref"].(string)
+ rec := mustGet(t, a, deck+"/assets/"+ref, http.StatusOK, cookies)
+ if !bytes.Equal(rec.Body.Bytes(), onePixelPNG) {
+ t.Errorf("GET %s/assets/%s returned %d bytes, want the picture streamed back by the app", deck, ref, rec.Body.Len())
+ }
+ if loc := rec.Header().Get("Location"); loc != "" {
+ t.Errorf("the picture redirected to %q; through the app it is served, not sent to the bucket", loc)
+ }
+ if len(bucket.minted) != 0 {
+ t.Errorf("the bucket signed %v; through the app it signs nothing", bucket.minted)
+ }
+ if rc, err := bucket.Store.Get(context.Background(), ref); err != nil {
+ t.Errorf("the picture is not in the bucket underneath: %v", err)
+ } else {
+ rc.Close()
+ }
+}
+
// TestUploadLimits: the four types and the 20 MiB ceiling, refused
// before a URL is ever minted.
func TestUploadLimits(t *testing.T) {