193 lines
6.1 KiB
Go
193 lines
6.1 KiB
Go
package album_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/album"
|
|
"git.golem15.com/golem15/summercms/modules/lagoon/attach"
|
|
"gocloud.dev/blob"
|
|
"gocloud.dev/blob/memblob"
|
|
)
|
|
|
|
func makeTestPNG(t *testing.T, w, h int) image.Image {
|
|
t.Helper()
|
|
img := image.NewRGBA(image.Rect(0, 0, w, h))
|
|
for y := 0; y < h; y++ {
|
|
for x := 0; x < w; x++ {
|
|
img.Set(x, y, color.RGBA{R: uint8(x % 256), G: uint8(y % 256), B: 180, A: 255})
|
|
}
|
|
}
|
|
return img
|
|
}
|
|
|
|
func encodeTestPNG(t *testing.T, img image.Image) []byte {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
if err := png.Encode(&buf, img); err != nil {
|
|
t.Fatalf("encode png: %v", err)
|
|
}
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func TestStaticHandlerHeadersAndTraversal(t *testing.T) {
|
|
ctx := context.Background()
|
|
bucket := memblob.OpenBucket(nil)
|
|
defer bucket.Close()
|
|
|
|
// Write an inline raster image and a non-inline PDF directly into memblob.
|
|
pngBytes := encodeTestPNG(t, makeTestPNG(t, 16, 16))
|
|
imgKey := "media/abc123456789/hero-photo.png"
|
|
if err := bucket.WriteAll(ctx, imgKey, pngBytes, attach.UploadWriterOptions("image/png")); err != nil {
|
|
t.Fatalf("write png: %v", err)
|
|
}
|
|
pdfKey := "media/abc123456789/brochure.pdf"
|
|
if err := bucket.WriteAll(ctx, pdfKey, []byte("%PDF-1.4\n"), attach.UploadWriterOptions("application/pdf")); err != nil {
|
|
t.Fatalf("write pdf: %v", err)
|
|
}
|
|
|
|
h := album.StaticHandler(bucket, "/media")
|
|
|
|
// 1. Valid raster image serves inline with immutable Cache-Control, nosniff, and sandboxing CSP.
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/media/abc123456789/hero-photo.png", nil))
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("image status = %d, want 200", rec.Code)
|
|
}
|
|
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, want nosniff", got)
|
|
}
|
|
if got := rec.Header().Get("Content-Security-Policy"); got != album.MediaContentSecurityPolicy {
|
|
t.Fatalf("CSP = %q, want %q", got, album.MediaContentSecurityPolicy)
|
|
}
|
|
if got := rec.Header().Get("Content-Disposition"); got != "" {
|
|
t.Fatalf("inline image Content-Disposition = %q, want empty", got)
|
|
}
|
|
|
|
// 2. Non-inline PDF always gets Content-Disposition: attachment.
|
|
rec = httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/media/abc123456789/brochure.pdf", nil))
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("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)
|
|
}
|
|
|
|
// 3. Traversal and malformed segments are rejected with 404.
|
|
badPaths := []string{
|
|
"/media/../etc/passwd",
|
|
"/media/abc123456789/../secret.png",
|
|
"/media/short/hero.png",
|
|
"/media/ABC123456789/hero.png",
|
|
"/media/abc123456789/sub/hero.png",
|
|
"/media/abc123456789/.hidden.png",
|
|
"/media/abc123456789/noext",
|
|
}
|
|
for _, p := range badPaths {
|
|
rec = httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, p, nil))
|
|
if rec.Code != http.StatusNotFound {
|
|
t.Fatalf("path %q status = %d, want 404", p, rec.Code)
|
|
}
|
|
if got := rec.Header().Get("X-Content-Type-Options"); got != "nosniff" {
|
|
t.Fatalf("path %q missing nosniff header", p)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGenerateWebPVariantsNoUpscale(t *testing.T) {
|
|
ctx := context.Background()
|
|
bucket := memblob.OpenBucket(nil)
|
|
defer bucket.Close()
|
|
|
|
// 1200x800 PNG should generate 480w and 960w, and must NOT upscale to 1600w.
|
|
img := makeTestPNG(t, 1200, 800)
|
|
variants, keys, err := album.GenerateWebPVariants(ctx, nil, bucket, "abc123456789", "banner", img, "png")
|
|
if err != nil {
|
|
t.Fatalf("GenerateWebPVariants: %v", err)
|
|
}
|
|
if len(variants) != 2 || len(keys) != 2 {
|
|
t.Fatalf("got %d variants (%d keys), want 2 (480w, 960w)", len(variants), len(keys))
|
|
}
|
|
if variants[0].Width != 480 || variants[0].Height != 320 {
|
|
t.Fatalf("variant[0] = %dx%d, want 480x320", variants[0].Width, variants[0].Height)
|
|
}
|
|
if variants[1].Width != 960 || variants[1].Height != 640 {
|
|
t.Fatalf("variant[1] = %dx%d, want 960x640", variants[1].Width, variants[1].Height)
|
|
}
|
|
for _, key := range keys {
|
|
exists, err := bucket.Exists(ctx, key)
|
|
if err != nil || !exists {
|
|
t.Fatalf("expected variant blob %q to exist (err=%v)", key, err)
|
|
}
|
|
}
|
|
|
|
item := album.Item{
|
|
ShortID: "abc123456789",
|
|
BlobKey: "media/abc123456789/banner.png",
|
|
Variants: album.Variants(variants),
|
|
}
|
|
srcset := item.Srcset(nil)
|
|
if !strings.Contains(srcset, "/media/abc123456789/banner-480w.webp 480w") ||
|
|
!strings.Contains(srcset, "/media/abc123456789/banner-960w.webp 960w") {
|
|
t.Fatalf("unexpected srcset: %q", srcset)
|
|
}
|
|
}
|
|
|
|
func TestSlugAndShortIDGeneration(t *testing.T) {
|
|
id, err := album.GenerateShortID()
|
|
if err != nil {
|
|
t.Fatalf("GenerateShortID: %v", err)
|
|
}
|
|
if len(id) != 12 {
|
|
t.Fatalf("short_id length = %d, want 12", len(id))
|
|
}
|
|
if _, ok := album.ParsePublicMediaPath(id + "/hello-world.webp"); !ok {
|
|
t.Fatalf("ParsePublicMediaPath rejected generated short_id %q", id)
|
|
}
|
|
|
|
cases := map[string]string{
|
|
"My Hero Photo (2026)!.PNG": "my-hero-photo-2026-png",
|
|
" ___ ": "file",
|
|
"summer--beach_day": "summer-beach-day",
|
|
}
|
|
for in, want := range cases {
|
|
if got := album.SanitizeSlug(in); got != want {
|
|
t.Errorf("SanitizeSlug(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDisallowedTypesRejectedBeforeDB(t *testing.T) {
|
|
ctx := context.Background()
|
|
bucket := memblob.OpenBucket(nil)
|
|
defer bucket.Close()
|
|
|
|
// Verify ParsePublicMediaPath and attach allow-lists reject SVG/HTML/JS.
|
|
for _, ext := range []string{"svg", "html", "htm", "js", "mjs"} {
|
|
for _, allowed := range attach.DefaultFileExtensions {
|
|
if allowed == ext {
|
|
t.Fatalf("extension %q must not be in DefaultFileExtensions", ext)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Verify DeleteBlobs handles missing keys cleanly.
|
|
if err := album.DeleteBlobs(ctx, bucket, []string{"media/abc123456789/missing.png"}); err != nil {
|
|
t.Fatalf("DeleteBlobs on missing key: %v", err)
|
|
}
|
|
}
|
|
|
|
var _ *blob.Bucket
|