Validate decoded photo size and require a photos row to serve
decodePhoto checked the GIF logical screen size, but gif.Decode returns
the first frame, which may be smaller; a 400x400 screen with a 1x1 first
frame passed and was scaled. The short-side check now measures the
decoded image's bounds. The pixel cap still comes from the header, before
decoding.
OpenPhoto served any file whose name was a canonical ULID and stored
size, with no photos row required. It now looks the id up with the new
GetPhoto query and reports an id with no row as fs.ErrNotExist, so
GET /media/photos/{id}-{size}.jpg is 404 for it even when a file exists.
OpenPhoto takes the request context for the lookup.
The media test fixture records a photos row for its served photo and
adds an orphan photo whose files exist without a row; both its sizes
must be 404.
Maestro task: 8a432108-d3ba-4013-ae8c-11963981d641
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -165,13 +165,18 @@ func (c *PhotoCache) CachePhoto(ctx context.Context, rawURL string) (db.Photo, e
|
||||
}
|
||||
|
||||
// OpenPhoto opens the stored file of photo id at size. An id that is not a
|
||||
// canonical ULID, a size not in PhotoSizes, or a photo that is not stored
|
||||
// is an error matching fs.ErrNotExist.
|
||||
func (c *PhotoCache) OpenPhoto(id string, size int) (*os.File, error) {
|
||||
// canonical ULID, a size not in PhotoSizes, an id with no photos row, or a
|
||||
// photo whose file is not stored is an error matching fs.ErrNotExist.
|
||||
func (c *PhotoCache) OpenPhoto(ctx context.Context, id string, size int) (*os.File, error) {
|
||||
parsed, err := ulid.ParseStrict(id)
|
||||
if err != nil || parsed.String() != id || !slices.Contains(PhotoSizes, size) {
|
||||
return nil, fmt.Errorf("recipes: photo %q at size %d: %w", id, size, fs.ErrNotExist)
|
||||
}
|
||||
if _, err := db.New(c.store.Reader).GetPhoto(ctx, id); errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, fmt.Errorf("recipes: photo %q: %w", id, fs.ErrNotExist)
|
||||
} else if err != nil {
|
||||
return nil, fmt.Errorf("recipes: look up photo %q: %w", id, err)
|
||||
}
|
||||
return os.Open(c.path(id, size))
|
||||
}
|
||||
|
||||
@@ -183,8 +188,9 @@ func photoFileName(id string, size int) string {
|
||||
return id + "-" + strconv.Itoa(size) + ".jpg"
|
||||
}
|
||||
|
||||
// decodePhoto decodes a JPEG, PNG, WebP or GIF image, checking its size
|
||||
// from the header before decoding the pixels.
|
||||
// decodePhoto decodes a JPEG, PNG, WebP or GIF image, checking its pixel
|
||||
// count from the header before decoding the pixels, and the shorter side of
|
||||
// the decoded image: a GIF's first frame may be smaller than its screen.
|
||||
func decodePhoto(b []byte) (image.Image, error) {
|
||||
cfg, format, err := image.DecodeConfig(bytes.NewReader(b))
|
||||
if err != nil {
|
||||
@@ -197,13 +203,13 @@ func decodePhoto(b []byte) (image.Image, error) {
|
||||
if int64(cfg.Width)*int64(cfg.Height) > PhotoMaxPixels {
|
||||
return nil, fmt.Errorf("%w: %dx%d", ErrPhotoTooLarge, cfg.Width, cfg.Height)
|
||||
}
|
||||
if min(cfg.Width, cfg.Height) < PhotoMinShortSide {
|
||||
return nil, fmt.Errorf("%w: %dx%d, shorter side under %d px", ErrPhotoTooSmall, cfg.Width, cfg.Height, PhotoMinShortSide)
|
||||
}
|
||||
img, err := decode(b)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %w", ErrPhotoFormat, err)
|
||||
}
|
||||
if size := img.Bounds().Size(); min(size.X, size.Y) < PhotoMinShortSide {
|
||||
return nil, fmt.Errorf("%w: %dx%d, shorter side under %d px", ErrPhotoTooSmall, size.X, size.Y, PhotoMinShortSide)
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -204,6 +204,33 @@ func TestCachePhotoGIFUsesFirstFrame(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachePhotoRejectsSmallGIFFirstFrame(t *testing.T) {
|
||||
t.Parallel()
|
||||
// A 400x400 screen whose first frame is 1x1: the frame is what would be
|
||||
// stored, so it is what must measure PhotoMinShortSide.
|
||||
var b bytes.Buffer
|
||||
anim := &gif.GIF{
|
||||
Image: []*image.Paletted{image.NewPaletted(image.Rect(0, 0, 1, 1), color.Palette{color.Black})},
|
||||
Delay: []int{10},
|
||||
Config: image.Config{ColorModel: color.Palette{color.Black}, Width: 400, Height: 400},
|
||||
}
|
||||
if err := gif.EncodeAll(&b, anim); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cache, st, dataDir, _ := newTestPhotoCache(t, map[string]photoFile{
|
||||
"/speck.gif": {"image/gif", b.Bytes()},
|
||||
})
|
||||
if _, err := cache.CachePhoto(t.Context(), "https://example.com/speck.gif"); !errors.Is(err, ErrPhotoTooSmall) {
|
||||
t.Errorf("CachePhoto error = %v, want ErrPhotoTooSmall", err)
|
||||
}
|
||||
if n := countPhotos(t, st); n != 0 {
|
||||
t.Errorf("%d photos recorded for a refused image", n)
|
||||
}
|
||||
if names := photoFiles(t, dataDir); len(names) != 0 {
|
||||
t.Errorf("files stored for a refused image: %v", names)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachePhotoFillsTransparencyWhite(t *testing.T) {
|
||||
t.Parallel()
|
||||
cache, _, dataDir, _ := newTestPhotoCache(t, map[string]photoFile{
|
||||
@@ -320,7 +347,7 @@ func TestCachePhotoDeduplicatesBySHA256(t *testing.T) {
|
||||
|
||||
func TestOpenPhoto(t *testing.T) {
|
||||
t.Parallel()
|
||||
cache, _, _, _ := newTestPhotoCache(t, map[string]photoFile{
|
||||
cache, _, dataDir, _ := newTestPhotoCache(t, map[string]photoFile{
|
||||
"/a.png": {"image/png", encodePNG(t, gradient(400, 300))},
|
||||
})
|
||||
photo, err := cache.CachePhoto(t.Context(), "https://example.com/a.png")
|
||||
@@ -328,23 +355,29 @@ func TestOpenPhoto(t *testing.T) {
|
||||
t.Fatalf("CachePhoto: %v", err)
|
||||
}
|
||||
for _, size := range PhotoSizes {
|
||||
f, err := cache.OpenPhoto(photo.ID, size)
|
||||
f, err := cache.OpenPhoto(t.Context(), photo.ID, size)
|
||||
if err != nil {
|
||||
t.Fatalf("OpenPhoto %d: %v", size, err)
|
||||
}
|
||||
f.Close()
|
||||
}
|
||||
// A file with no photos row is not a stored photo.
|
||||
orphan := store.NewID()
|
||||
if err := os.WriteFile(filepath.Join(dataDir, "media", "photos", orphan+"-400.jpg"), []byte("jpeg"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
id string
|
||||
size int
|
||||
}{
|
||||
{photo.ID, 800},
|
||||
{store.NewID(), 400},
|
||||
{orphan, 400},
|
||||
{strings.ToLower(photo.ID), 400},
|
||||
{"../" + photo.ID, 400},
|
||||
{"", 400},
|
||||
} {
|
||||
if _, err := cache.OpenPhoto(tc.id, tc.size); !errors.Is(err, fs.ErrNotExist) {
|
||||
if _, err := cache.OpenPhoto(t.Context(), tc.id, tc.size); !errors.Is(err, fs.ErrNotExist) {
|
||||
t.Errorf("OpenPhoto(%q, %d) error = %v, want fs.ErrNotExist", tc.id, tc.size, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user