fix(cabana): serve public image thumbnails on the admin thumb routes
The record and relation-child thumb routes now also serve the thumbnail of a public JPEG, PNG, GIF or WebP file, under the same record or pending-session scope and D-10 headers as a protected one, so the admin can preview a public image whose public URL does not load. The download routes still answer 404 for every public file, and a public non-image thumb stays 404. TestPublicImageThumb covers owner, foreign and hidden records, image types, a public row under a protected field, another admin's pending upload and the relation-child route.
This commit is contained in:
@@ -59,9 +59,11 @@ func manualPath(gadget, file uint, rest string) string {
|
||||
}
|
||||
|
||||
// TestProtectedFileScope: the protected download and thumb routes answer
|
||||
// 404 for another gadget's file, for another admin's pending file on id 0
|
||||
// and for any is_public=true row; the caption, remove and reorder routes
|
||||
// answer 404 for another gadget's file id and change nothing (D-10, D-15).
|
||||
// 404 for another gadget's file and for another admin's pending file on
|
||||
// id 0; the download route (not the thumb route, see TestPublicImageThumb)
|
||||
// answers 404 for any is_public=true row; the caption, remove and reorder
|
||||
// routes answer 404 for another gadget's file id and change nothing (D-10,
|
||||
// D-15).
|
||||
func TestProtectedFileScope(t *testing.T) {
|
||||
env := newDeferredEnv(t)
|
||||
g1 := env.gadget(t, "g1-"+env.stamp, false)
|
||||
@@ -97,13 +99,12 @@ func TestProtectedFileScope(t *testing.T) {
|
||||
|
||||
t.Run("public rows", func(t *testing.T) {
|
||||
// A public photo, and a public row attached under the protected
|
||||
// field, are never served by the protected routes.
|
||||
// field, are never streamed by the download route (their thumbs are
|
||||
// covered by TestPublicImageThumb).
|
||||
mixed := env.storeFile(t, dfGadgetMorph, g2, "manual", "public.png", conformPNG(t), true)
|
||||
for _, path := range []string{
|
||||
dfPath(g2, fmt.Sprintf("/files/photos/%d/download", p2.ID)),
|
||||
dfPath(g2, fmt.Sprintf("/files/photos/%d/thumb", p2.ID)),
|
||||
manualPath(g2, mixed.ID, "/download"),
|
||||
manualPath(g2, mixed.ID, "/thumb"),
|
||||
} {
|
||||
want(t, path, env.a.do(t, http.MethodGet, path, nil, nil), http.StatusNotFound)
|
||||
}
|
||||
@@ -128,6 +129,94 @@ func TestProtectedFileScope(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// TestPublicImageThumb: the admin thumb routes (record and relation child)
|
||||
// also serve the thumbnail of a public JPEG, PNG, GIF or WebP file, under
|
||||
// the same scope and D-10 headers as a protected one, so the admin can
|
||||
// preview a public image whose public URL does not load. The download
|
||||
// routes still refuse every public file.
|
||||
func TestPublicImageThumb(t *testing.T) {
|
||||
env := newDeferredEnv(t)
|
||||
g := env.gadget(t, "g-"+env.stamp, false)
|
||||
photoPath := func(gadget, file uint, rest string) string {
|
||||
return dfPath(gadget, fmt.Sprintf("/files/photos/%d%s", file, rest))
|
||||
}
|
||||
imageThumb := func(t *testing.T, what string, rec *httptest.ResponseRecorder) {
|
||||
t.Helper()
|
||||
want(t, what, rec, http.StatusOK)
|
||||
securityHeaders(t, what, rec)
|
||||
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "image/") {
|
||||
t.Fatalf("%s content type %q", what, ct)
|
||||
}
|
||||
if rec.Body.Len() == 0 {
|
||||
t.Fatalf("%s empty body", what)
|
||||
}
|
||||
}
|
||||
pub := env.storeFile(t, dfGadgetMorph, g, "photos", "pub.png", conformPNG(t), true)
|
||||
|
||||
t.Run("owner", func(t *testing.T) {
|
||||
imageThumb(t, "owner thumb", env.a.do(t, http.MethodGet, photoPath(g, pub.ID, "/thumb"), nil, nil))
|
||||
want(t, "owner download", env.a.do(t, http.MethodGet, photoPath(g, pub.ID, "/download"), nil, nil), http.StatusNotFound)
|
||||
})
|
||||
|
||||
t.Run("foreign and hidden records", func(t *testing.T) {
|
||||
g2 := env.gadget(t, "g2-"+env.stamp, false)
|
||||
hidden := env.gadget(t, "hidden-"+env.stamp, true)
|
||||
p2 := env.storeFile(t, dfGadgetMorph, g2, "photos", "g2.png", conformPNG(t), true)
|
||||
ph := env.storeFile(t, dfGadgetMorph, hidden, "photos", "hidden.png", conformPNG(t), true)
|
||||
before := env.state(t)
|
||||
want(t, "G2's public photo through G", env.a.do(t, http.MethodGet, photoPath(g, p2.ID, "/thumb"), nil, nil), http.StatusNotFound)
|
||||
want(t, "hidden gadget's public photo", env.a.do(t, http.MethodGet, photoPath(hidden, ph.ID, "/thumb"), nil, nil), http.StatusNotFound)
|
||||
env.unchanged(t, "foreign public thumbs", before)
|
||||
})
|
||||
|
||||
t.Run("image types", func(t *testing.T) {
|
||||
svg := []byte(`<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>`)
|
||||
txt := env.storeFile(t, dfGadgetMorph, g, "photos", "notes.txt", []byte("plain text\n"), true)
|
||||
logo := env.storeFile(t, dfGadgetMorph, g, "photos", "logo.svg", svg, true)
|
||||
before := env.state(t)
|
||||
for _, f := range []uint{txt.ID, logo.ID} {
|
||||
want(t, fmt.Sprintf("public non-image %d thumb", f), env.a.do(t, http.MethodGet, photoPath(g, f, "/thumb"), nil, nil), http.StatusNotFound)
|
||||
}
|
||||
env.unchanged(t, "public non-image thumbs", before)
|
||||
for name, data := range map[string][]byte{"anim.gif": dfGIF(t), "pic.webp": dfWebP, "photo.jpg": dfJPEG(t)} {
|
||||
f := env.storeFile(t, dfGadgetMorph, g, "photos", name, data, true)
|
||||
imageThumb(t, name+" thumb", env.a.do(t, http.MethodGet, photoPath(g, f.ID, "/thumb"), nil, nil))
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("public row under the protected field", func(t *testing.T) {
|
||||
mixed := env.storeFile(t, dfGadgetMorph, g, "manual", "public.png", conformPNG(t), true)
|
||||
imageThumb(t, "mixed thumb", env.a.do(t, http.MethodGet, manualPath(g, mixed.ID, "/thumb"), nil, nil))
|
||||
want(t, "mixed download", env.a.do(t, http.MethodGet, manualPath(g, mixed.ID, "/download"), nil, nil), http.StatusNotFound)
|
||||
})
|
||||
|
||||
t.Run("pending upload of another admin", func(t *testing.T) {
|
||||
key := newSessionKey(t)
|
||||
up := env.a.upload(t, dfPath(0, "/files/photos"), "pending.png", conformPNG(t), sk(key))
|
||||
want(t, "A uploads a public photo to id 0", up, http.StatusCreated)
|
||||
id := dataID(t, up.Body.Bytes())
|
||||
imageThumb(t, "A's pending photo thumb", env.a.do(t, http.MethodGet, photoPath(0, id, "/thumb"), nil, sk(key)))
|
||||
before := env.state(t)
|
||||
want(t, "B's thumb of A's pending photo", env.b.do(t, http.MethodGet, photoPath(0, id, "/thumb"), nil, sk(key)), http.StatusNotFound)
|
||||
want(t, "A's pending photo download", env.a.do(t, http.MethodGet, photoPath(0, id, "/download"), nil, sk(key)), http.StatusNotFound)
|
||||
env.unchanged(t, "B's thumb with A's key", before)
|
||||
})
|
||||
|
||||
t.Run("relation child", func(t *testing.T) {
|
||||
g2 := env.gadget(t, "g2c-"+env.stamp, false)
|
||||
p := env.part(t, g, "p-pub")
|
||||
f := env.storeFile(t, dfPartMorph, p, "images", "part.png", conformPNG(t), true)
|
||||
childPath := func(gadget uint, rest string) string {
|
||||
return dfPath(gadget, fmt.Sprintf("/relations/parts/records/%d/files/images/%d%s", p, f.ID, rest))
|
||||
}
|
||||
imageThumb(t, "child thumb", env.a.do(t, http.MethodGet, childPath(g, "/thumb"), nil, ck(newSessionKey(t))))
|
||||
before := env.state(t)
|
||||
want(t, "child download", env.a.do(t, http.MethodGet, childPath(g, "/download"), nil, ck(newSessionKey(t))), http.StatusNotFound)
|
||||
want(t, "child thumb through G2", env.a.do(t, http.MethodGet, childPath(g2, "/thumb"), nil, ck(newSessionKey(t))), http.StatusNotFound)
|
||||
env.unchanged(t, "refused child file requests", before)
|
||||
})
|
||||
}
|
||||
|
||||
// TestProtectedFileHeaders: only jpeg, png, gif and webp are served inline
|
||||
// with their own type; SVG, HTML and text download as an octet-stream
|
||||
// attachment. Every response carries nosniff, private no-store and the
|
||||
|
||||
Reference in New Issue
Block a user