Integrate graphics, RAW, audio and CAD Craft tools with protected saves
This commit is contained in:
1 parent
6e36b6c4b7
commit
852140c3a7
24 files changed
+600
-74
No files matched your search
@@ -0,0 +1,153 @@
|
||||
package fbhttp
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/binary"
|
||||
"image"
|
||||
"image/png"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/filebrowser/filebrowser/v2/settings"
|
||||
"github.com/filebrowser/filebrowser/v2/users"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func creativeZIP(t *testing.T, names ...string) []byte {
|
||||
t.Helper()
|
||||
var data bytes.Buffer
|
||||
writer := zip.NewWriter(&data)
|
||||
for _, name := range names {
|
||||
file, err := writer.Create(name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
file.Write([]byte(`{"format":"test"}`))
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return data.Bytes()
|
||||
}
|
||||
|
||||
func TestCreativeFormatsAreBoundedAndTypeChecked(t *testing.T) {
|
||||
var picture bytes.Buffer
|
||||
if err := png.Encode(&picture, image.NewRGBA(image.Rect(0, 0, 2, 2))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for ext, data := range map[string][]byte{
|
||||
".png": picture.Bytes(), ".svg": []byte(`<svg xmlns="http://www.w3.org/2000/svg"/>`),
|
||||
".vectorcraft": []byte(`{"format":"vectorcraft","version":3,"document":{}}`),
|
||||
".dxf": []byte("0\nSECTION\n2\nHEADER\n0\nENDSEC\n0\nEOF\n"),
|
||||
".pcraft": creativeZIP(t, "manifest.json"), ".figcraft": creativeZIP(t, "document.json"),
|
||||
".designcraft": creativeZIP(t, "document.json"), ".idml": creativeZIP(t, "designmap.xml"),
|
||||
".scraftz": creativeZIP(t, "Session.scraft", "Audio Files/recording.wav"),
|
||||
} {
|
||||
if err := validateCraftDocument(ext, data); err != nil {
|
||||
t.Fatalf("%s: %v", ext, err)
|
||||
}
|
||||
}
|
||||
for ext, data := range map[string][]byte{
|
||||
".jpg": picture.Bytes(), ".svg": []byte(`<!DOCTYPE svg><svg/>`),
|
||||
".vectorcraft": []byte(`{"format":"wrong"}`), ".dxf": []byte("not a drawing"),
|
||||
".pcraft": creativeZIP(t, "manifest.json", "../outside"),
|
||||
".figcraft": creativeZIP(t, "missing.json"), ".scraftz": creativeZIP(t, "not-a-session.json"),
|
||||
} {
|
||||
if validateCraftDocument(ext, data) == nil {
|
||||
t.Fatalf("accepted invalid %s", ext)
|
||||
}
|
||||
}
|
||||
bomb := creativeZIP(t, "manifest.json")
|
||||
at := bytes.Index(bomb, []byte{'P', 'K', 1, 2})
|
||||
binary.LittleEndian.PutUint32(bomb[at+24:at+28], 0x7fffffff)
|
||||
if validateCraftDocument(".pcraft", bomb) == nil {
|
||||
t.Fatal("accepted archive bomb")
|
||||
}
|
||||
var compressed bytes.Buffer
|
||||
writer := gzip.NewWriter(&compressed)
|
||||
io.CopyN(writer, zeroReader{}, maxCraftDocument+1)
|
||||
writer.Close()
|
||||
if validateCraftDocument(".vectorcraft", compressed.Bytes()) == nil {
|
||||
t.Fatal("accepted compressed JSON bomb")
|
||||
}
|
||||
}
|
||||
|
||||
type zeroReader struct{}
|
||||
|
||||
func (zeroReader) Read(data []byte) (int, error) { clear(data); return len(data), nil }
|
||||
|
||||
func TestCreativeSaveAndImportOnlyOriginals(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
manager := installedTestManager(t)
|
||||
key := []byte("creative-test")
|
||||
perm := users.Permissions{Download: true, Modify: true, Create: true}
|
||||
store := scopedUserStorage(t, root, perm, key)
|
||||
token := signToken(t, perm, key)
|
||||
handler := handle(craftDocumentHandler(manager, nil), "", store, &settings.Server{})
|
||||
request := func(method, id, name string, body []byte, tag string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(method, "/api/craft/"+id+"/document?path="+url.QueryEscape(name), bytes.NewReader(body))
|
||||
r = mux.SetURLVars(r, map[string]string{"addon": id})
|
||||
r.Header.Set("If-Match", tag)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, authorizeTestRequest(r, token))
|
||||
return rec
|
||||
}
|
||||
var picture bytes.Buffer
|
||||
png.Encode(&picture, image.NewRGBA(image.Rect(0, 0, 2, 2)))
|
||||
os.WriteFile(filepath.Join(root, "photo.png"), picture.Bytes(), 0600)
|
||||
loaded := request("GET", "photocraft", "/photo.png", nil, "")
|
||||
if loaded.Code != 200 {
|
||||
t.Fatalf("load: %d %s", loaded.Code, loaded.Body.String())
|
||||
}
|
||||
if rec := request("PUT", "photocraft", "/photo.png", picture.Bytes(), loaded.Header().Get("ETag")); rec.Code != 204 {
|
||||
t.Fatalf("save: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec := request("PUT", "photocraft", "/photo.png", []byte("broken"), loaded.Header().Get("ETag")); rec.Code != 422 {
|
||||
t.Fatalf("corrupt write: %d", rec.Code)
|
||||
}
|
||||
if data, _ := os.ReadFile(filepath.Join(root, "photo.png")); !bytes.Equal(data, picture.Bytes()) {
|
||||
t.Fatal("original damaged")
|
||||
}
|
||||
for _, input := range []struct {
|
||||
id, name string
|
||||
data []byte
|
||||
}{
|
||||
{"lightcraft", "/raw.dng", []byte{'I', 'I', 42, 0, 0, 0, 0, 0}},
|
||||
{"cadcraft", "/drawing.dwg", []byte("AC1027drawing")},
|
||||
} {
|
||||
os.WriteFile(filepath.Join(root, input.name[1:]), input.data, 0600)
|
||||
loaded := request("GET", input.id, input.name, nil, "")
|
||||
if loaded.Code != 200 {
|
||||
t.Fatalf("import %s: %d", input.id, loaded.Code)
|
||||
}
|
||||
if rec := request("PUT", input.id, input.name, input.data, loaded.Header().Get("ETag")); rec.Code != 415 {
|
||||
t.Fatalf("import-only overwrite %s: %d", input.id, rec.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPhotoCraftWASMPartsAreServedWithBinaryMIME(t *testing.T) {
|
||||
manager := installedTestManager(t)
|
||||
name := "app.part-aa"
|
||||
data := []byte("compressed WASM part")
|
||||
if err := os.WriteFile(filepath.Join(manager.Root, "photocraft", "versions", "test-123", name), data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key := []byte("part-test")
|
||||
perm := users.Permissions{Download: true, Modify: true}
|
||||
store := scopedUserStorage(t, t.TempDir(), perm, key)
|
||||
handler := handle(addonStaticHandler(manager), "", store, &settings.Server{})
|
||||
r := httptest.NewRequest("GET", "/addons/photocraft/test-123/"+name, nil)
|
||||
r = mux.SetURLVars(r, map[string]string{"addon": "photocraft", "build": "test-123", "asset": name})
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, authorizeTestRequest(r, signToken(t, perm, key)))
|
||||
if rec.Code != 200 || rec.Header().Get("Content-Type") != "application/octet-stream" || !bytes.Equal(rec.Body.Bytes(), data) {
|
||||
t.Fatalf("WASM part: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user