test(album,cabana,pier,surf,admin): add Phase 37 test suites and fail-closed check-phase37.sh gate
This commit is contained in:
174
modules/surf/media_test.go
Normal file
174
modules/surf/media_test.go
Normal file
@@ -0,0 +1,174 @@
|
||||
package surf
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/album"
|
||||
"git.golem15.com/golem15/summercms/modules/backpack"
|
||||
"git.golem15.com/golem15/summercms/modules/compass"
|
||||
"git.golem15.com/golem15/summercms/modules/lagoon/attach"
|
||||
"git.golem15.com/golem15/summercms/modules/party"
|
||||
"gocloud.dev/blob"
|
||||
"gocloud.dev/blob/memblob"
|
||||
)
|
||||
|
||||
func mediaConfig(t *testing.T, keys map[string]any) *compass.Config {
|
||||
t.Helper()
|
||||
cfg := writeHTTPConfig(t, "body_limits:\n default_bytes: 1024\n upload_bytes: 1024\n")
|
||||
for k, v := range keys {
|
||||
if err := cfg.Set(k, v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func mediaRoutes(t *testing.T, app *backpack.App, plugins []party.Plugin) []RouteInfo {
|
||||
t.Helper()
|
||||
rt, err := BuildRouter(app, plugins)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out []RouteInfo
|
||||
for _, info := range rt.Routes() {
|
||||
if info.PluginID == mediaPluginID {
|
||||
out = append(out, info)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestMediaMount(t *testing.T) {
|
||||
t.Run("off_registers_no_static_route", func(t *testing.T) {
|
||||
app := backpack.New(mediaConfig(t, map[string]any{"media.serve": false}))
|
||||
routes := mediaRoutes(t, app, nil)
|
||||
for _, rt := range routes {
|
||||
if rt.Method == http.MethodGet {
|
||||
t.Fatalf("unexpected static GET route when media.serve=false: %+v", rt)
|
||||
}
|
||||
}
|
||||
if _, ok := album.ServiceFrom(app); !ok {
|
||||
t.Fatal("expected *album.Service to be published on app")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("static_zero_db_serving_and_headers", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
bucket := memblob.OpenBucket(nil)
|
||||
defer bucket.Close()
|
||||
|
||||
if err := bucket.WriteAll(ctx, "media/abc123456789/hero.png", []byte("png-data"), attach.UploadWriterOptions("image/png")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := bucket.WriteAll(ctx, "media/abc123456789/spec.pdf", []byte("%PDF-1.4\n"), attach.UploadWriterOptions("application/pdf")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Intentionally do NOT publish *gorm.DB: proves zero-DB-hit serving.
|
||||
app := backpack.New(mediaConfig(t, map[string]any{"media.serve": true}))
|
||||
if err := app.Publish[*blob.Bucket](bucket); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h, err := Assemble(app, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Assemble: %v", err)
|
||||
}
|
||||
|
||||
// Raster image served inline with immutable Cache-Control, nosniff, CSP.
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/media/abc123456789/hero.png", nil))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("GET image status = %d, want 200", rec.Code)
|
||||
}
|
||||
if rec.Body.String() != "png-data" {
|
||||
t.Fatalf("body = %q, want png-data", rec.Body.String())
|
||||
}
|
||||
if got := rec.Header().Get("Cache-Control"); got != album.MediaCacheControl {
|
||||
t.Fatalf("Cache-Control = %q, want %q", got, album.MediaCacheControl)
|
||||
}
|
||||
if got := rec.Header().Get("X-Content-Type-Options"); got != "nosniff" {
|
||||
t.Fatalf("X-Content-Type-Options = %q", got)
|
||||
}
|
||||
if got := rec.Header().Get("Content-Security-Policy"); got != album.MediaContentSecurityPolicy {
|
||||
t.Fatalf("CSP = %q", got)
|
||||
}
|
||||
if got := rec.Header().Get("Content-Disposition"); got != "" {
|
||||
t.Fatalf("image Content-Disposition = %q, want empty", got)
|
||||
}
|
||||
|
||||
// PDF served with Content-Disposition: attachment.
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/media/abc123456789/spec.pdf", nil))
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("GET pdf status = %d, want 200", rec.Code)
|
||||
}
|
||||
if got := rec.Header().Get("Content-Disposition"); got != "attachment" {
|
||||
t.Fatalf("pdf Content-Disposition = %q, want attachment", got)
|
||||
}
|
||||
|
||||
// Traversal path rejected with 404.
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/media/abc123456789/..%2Fsecret.png", nil))
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("traversal status = %d, want 404", rec.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("prefix_collision_rejected", func(t *testing.T) {
|
||||
app := backpack.New(mediaConfig(t, map[string]any{"media.serve": true}))
|
||||
_, err := BuildRouter(app, []party.Plugin{uploadsPathPlugin{path: "/media/custom"}})
|
||||
if err == nil || !strings.Contains(err.Error(), "media prefix") {
|
||||
t.Fatalf("BuildRouter collision err = %v, want media prefix collision", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid_prefix_rejected", func(t *testing.T) {
|
||||
app := backpack.New(mediaConfig(t, map[string]any{
|
||||
"media.serve": true,
|
||||
"media.public_path_prefix": "https://cdn.example.com/media",
|
||||
}))
|
||||
if _, err := BuildRouter(app, nil); err == nil {
|
||||
t.Fatal("expected invalid media.public_path_prefix to fail BuildRouter")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("signed_upload_route_mounted_raw_without_admin_cookie", func(t *testing.T) {
|
||||
app := backpack.New(mediaConfig(t, map[string]any{"media.serve": true}))
|
||||
routes := mediaRoutes(t, app, nil)
|
||||
var sawPut, sawPost bool
|
||||
for _, rt := range routes {
|
||||
if rt.Pattern == "/media/upload/{token}" {
|
||||
if rt.Method == http.MethodPut {
|
||||
sawPut = true
|
||||
}
|
||||
if rt.Method == http.MethodPost {
|
||||
sawPost = true
|
||||
}
|
||||
for _, mw := range rt.Middleware {
|
||||
if mw == "backend" || mw == "csrf" {
|
||||
t.Fatalf("signed upload route must not require %q middleware: %+v", mw, rt.Middleware)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !sawPut || !sawPost {
|
||||
t.Fatalf("expected PUT and POST /media/upload/{token}, got routes: %+v", routes)
|
||||
}
|
||||
|
||||
// Calling PUT /media/upload/tok without any cookie/CSRF reaches the handler
|
||||
// (returns 503 because no DB is published, rather than 401/403 CSRF).
|
||||
h, err := Assemble(app, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodPut, "/media/upload/some-token", strings.NewReader("bytes")))
|
||||
if rec.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("PUT /media/upload/some-token without DB status = %d, want 503", rec.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user