test(19.3-05): build tags, S3 tag guard, DiskExtension and public_url
- manifest_tags_test.go: build.tags load, trim, dedupe and validation errors naming build.tags; SaveManifest keeps tags and plugin order; goBuildArgs puts one -tags before -o - s3_test.go: s3:// refused without summercms_s3 naming the tag, opens with it (both build modes, any scheme case) - diskext_test.go: RESEARCH probe rows and table consistency - url_test.go: public_url base joining, File.URL and thumbnails, malformed values refused naming the key - store_test.go: Postgres regression table through Store - gate: attach names in both tag modes, Store regression in docker
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"regexp"
|
||||
@@ -343,3 +344,83 @@ func TestStoreDiskExtensionSmoke(t *testing.T) {
|
||||
t.Fatalf("HTML body named note.txt: disk name %q, file_name %q", note.DiskName, note.FileName)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStoreDiskExtension (plan 05 of phase 19.3, D-15) runs the stored-XSS
|
||||
// regression table through Store on Postgres: the disk name and the URL end
|
||||
// in the extension DiskExtension picks for the sniffed bytes, file_name keeps
|
||||
// the client's name and content_type the sniffed type.
|
||||
func TestStoreDiskExtension(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("requires testcontainers postgres")
|
||||
}
|
||||
ctx := t.Context()
|
||||
gdb := attachGorm(t)
|
||||
if err := lagoon.Migrate(gdb, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bucket := memblob.OpenBucket(nil)
|
||||
t.Cleanup(func() { _ = bucket.Close() })
|
||||
lim := attach.Limits{Extensions: []string{"html", "png", "jpg", "jpeg", "docx", "txt", "ics", "svg", "js", "pdf", "mkv"}}
|
||||
|
||||
var jpegBody bytes.Buffer
|
||||
if err := jpeg.Encode(&jpegBody, image.NewRGBA(image.Rect(0, 0, 4, 4)), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var docxBody bytes.Buffer
|
||||
zw := zip.NewWriter(&docxBody)
|
||||
w, err := zw.Create("word/document.xml")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := w.Write([]byte("<w:document/>")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := zw.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
webm := []byte("\x1a\x45\xdf\xa3\x9f\x42\x86\x81\x01\x42\xf7\x81\x01\x42\xf2\x81\x04\x42\xf3\x81\x08\x42\x82\x84webm\x42\x87\x81\x04\x42\x85\x81\x02")
|
||||
|
||||
for _, tc := range []struct {
|
||||
name, fileName string
|
||||
body []byte
|
||||
ext, ct string
|
||||
}{
|
||||
{"png named html", "x.html", smokePNG(t), "png", "image/png"},
|
||||
{"jpeg named png", "photo.png", jpegBody.Bytes(), "jpg", "image/jpeg"},
|
||||
{"jpeg named jpeg", "photo.jpeg", jpegBody.Bytes(), "jpeg", "image/jpeg"},
|
||||
{"docx under zip", "report.docx", docxBody.Bytes(), "docx", "application/zip"},
|
||||
{"ics under text", "event.ics", []byte("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nEND:VCALENDAR\r\n"), "ics", "text/plain"},
|
||||
{"html named txt", "note.txt", []byte("<html><body><script>alert(1)</script></body></html>"), "txt", "text/html"},
|
||||
{"html named html", "page.html", []byte("<!DOCTYPE html><html><script>alert(1)</script></html>"), "txt", "text/html"},
|
||||
{"svg under xml", "logo.svg", []byte(`<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>`), "txt", "text/xml"},
|
||||
{"js under text", "app.js", []byte("console.log(document.cookie)\n"), "txt", "text/plain"},
|
||||
// A generic sniff keeps the content_type rule (from the extension).
|
||||
{"js under octet-stream", "app.js", []byte{0x00, 0x01, 0x02, 'a', 'l', 'e', 'r', 't'}, "bin", "text/javascript"},
|
||||
{"pdf named txt", "doc.txt", []byte("%PDF-1.4\n%\xe2\xe3\xcf\xd3\n1 0 obj\n<<>>\nendobj\n"), "pdf", "application/pdf"},
|
||||
{"mkv under webm", "clip.mkv", webm, "mkv", "video/webm"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
f, err := attach.Store(ctx, gdb, bucket, attach.Upload{FileName: tc.fileName, Body: bytes.NewReader(tc.body), Public: true}, lim)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasSuffix(f.DiskName, "."+tc.ext) || !strings.HasSuffix(f.URL(), "/"+f.DiskName) || !strings.HasSuffix(f.URL(), "."+tc.ext) {
|
||||
t.Fatalf("disk name %q URL %q, want .%s", f.DiskName, f.URL(), tc.ext)
|
||||
}
|
||||
if f.FileName != tc.fileName || !strings.HasPrefix(f.ContentType, tc.ct) {
|
||||
t.Fatalf("file_name %q content_type %q, want %q %q", f.FileName, f.ContentType, tc.fileName, tc.ct)
|
||||
}
|
||||
var stored attach.File
|
||||
if err := gdb.First(&stored, f.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stored.DiskName != f.DiskName || stored.FileName != tc.fileName {
|
||||
t.Fatalf("stored row %+v", stored)
|
||||
}
|
||||
got, err := bucket.ReadAll(ctx, attach.BlobKey(f.DiskName))
|
||||
if err != nil || !bytes.Equal(got, tc.body) {
|
||||
t.Fatalf("blob under %s: %v", attach.BlobKey(f.DiskName), err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user