Add WordCraft and DeckCraft office editors with protected server saves
This commit is contained in:
1 parent
bc9709e500
commit
6e36b6c4b7
20 files changed
+582
-40
No files matched your search
+138
-2
@@ -1,9 +1,12 @@
|
||||
package fbhttp
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
@@ -23,7 +26,7 @@ import (
|
||||
func installedTestManager(t *testing.T) *addons.Manager {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
for _, id := range []string{"gridcraft", "pdfcraft"} {
|
||||
for _, id := range []string{"wordcraft", "gridcraft", "deckcraft", "pdfcraft"} {
|
||||
dir := filepath.Join(root, id, "versions", "test-123")
|
||||
if err := os.MkdirAll(dir, 0750); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -124,7 +127,7 @@ func TestAddonAssetsProtectedFromFileAPI(t *testing.T) {
|
||||
}
|
||||
}
|
||||
func TestCraftBlankFormats(t *testing.T) {
|
||||
for id, ext := range map[string]string{"gridcraft": ".xlsx", "pdfcraft": ".pdf"} {
|
||||
for id, ext := range map[string]string{"wordcraft": ".docx", "gridcraft": ".xlsx", "deckcraft": ".pptx", "pdfcraft": ".pdf"} {
|
||||
data, err := blankCraftDocument(id)
|
||||
if err != nil || validateCraftDocument(ext, data) != nil {
|
||||
t.Fatalf("invalid %s template", id)
|
||||
@@ -134,6 +137,139 @@ func TestCraftBlankFormats(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOfficeCreateSaveAndFormatIsolation(t *testing.T) {
|
||||
for _, tool := range []string{"wordcraft", "deckcraft"} {
|
||||
t.Run(tool, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
key := []byte("office-test-key")
|
||||
perm := users.Permissions{Download: true, Modify: true, Create: true}
|
||||
store := scopedUserStorage(t, root, perm, key)
|
||||
token := signToken(t, perm, key)
|
||||
handler := handle(craftDocumentHandler(installedTestManager(t), nil), "", store, &settings.Server{})
|
||||
catalog, _ := addons.Find(tool)
|
||||
name := "/office" + catalog.Extension
|
||||
request := func(method, target string, body []byte, tag string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(method, "/api/craft/"+tool+"/document?path="+url.QueryEscape(target), bytes.NewReader(body))
|
||||
r = mux.SetURLVars(r, map[string]string{"addon": tool})
|
||||
r.Header.Set("If-Match", tag)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, authorizeTestRequest(r, token))
|
||||
return rec
|
||||
}
|
||||
if rec := request("POST", name, nil, ""); rec.Code != 204 {
|
||||
t.Fatalf("create %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec := request("POST", name, nil, ""); rec.Code != 409 {
|
||||
t.Fatalf("overwritten %d", rec.Code)
|
||||
}
|
||||
loaded := request("GET", name, nil, "")
|
||||
if loaded.Code != 200 {
|
||||
t.Fatalf("load %d", loaded.Code)
|
||||
}
|
||||
body, err := zip.NewReader(bytes.NewReader(loaded.Body.Bytes()), int64(loaded.Body.Len()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, part := range body.File {
|
||||
f, err := part.Open()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoder := xml.NewDecoder(f)
|
||||
for {
|
||||
_, err = decoder.Token()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
f.Close()
|
||||
if err != io.EOF {
|
||||
t.Fatalf("invalid XML %s: %v", part.Name, err)
|
||||
}
|
||||
}
|
||||
wrong, _ := blankCraftDocument("gridcraft")
|
||||
if rec := request("PUT", name, wrong, loaded.Header().Get("ETag")); rec.Code != 422 {
|
||||
t.Fatalf("wrong format accepted %d", rec.Code)
|
||||
}
|
||||
if rec := request("PUT", name, loaded.Body.Bytes(), loaded.Header().Get("ETag")); rec.Code != 204 {
|
||||
t.Fatalf("save %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec := request("PUT", name, loaded.Body.Bytes(), `"stale"`); rec.Code != 412 {
|
||||
t.Fatalf("stale overwrite %d", rec.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOfficeBridgeDelivery(t *testing.T) {
|
||||
manager := installedTestManager(t)
|
||||
root := t.TempDir()
|
||||
key := []byte("office-route-test")
|
||||
perm := users.Permissions{Download: true, Modify: true}
|
||||
store := scopedUserStorage(t, root, perm, key)
|
||||
handler := handle(addonStaticHandler(manager), "", store, &settings.Server{})
|
||||
for _, id := range []string{"wordcraft", "deckcraft"} {
|
||||
index := filepath.Join(manager.Root, id, "versions", "test-123", "index.html")
|
||||
original := []byte("<html><head></head><body>vendor</body></html>")
|
||||
if err := os.WriteFile(index, original, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"index.html", "filebrowser-bridge.js"} {
|
||||
r := httptest.NewRequest("GET", "/addons/"+id+"/test-123/"+name+"?host=parent", nil)
|
||||
r.Header.Set("Accept-Encoding", "gzip")
|
||||
r = mux.SetURLVars(r, map[string]string{"addon": id, "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-Encoding") != "" || rec.Header().Get("Cache-Control") != "no-store" {
|
||||
t.Fatalf("bridge %s: %d %v", name, rec.Code, rec.Header())
|
||||
}
|
||||
if name == "index.html" && !strings.Contains(rec.Body.String(), `src="./filebrowser-bridge.js"`) {
|
||||
t.Fatal("missing adapter")
|
||||
}
|
||||
if name != "index.html" && !bytes.Equal(rec.Body.Bytes(), officeBridge) {
|
||||
t.Fatal("wrong bridge")
|
||||
}
|
||||
}
|
||||
unchanged, _ := os.ReadFile(index)
|
||||
if !bytes.Equal(original, unchanged) {
|
||||
t.Fatal("modified checksum-verified bundle")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOfficeArchiveLimitsWithoutInflating(t *testing.T) {
|
||||
valid, err := blankCraftDocument("wordcraft")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Falsify the central-directory expanded size. Validation must reject it
|
||||
// using only the directory, without allocating or inflating that data.
|
||||
bomb := append([]byte(nil), valid...)
|
||||
offset := bytes.Index(bomb, []byte{'P', 'K', 1, 2})
|
||||
if offset < 0 {
|
||||
t.Fatal("missing ZIP directory")
|
||||
}
|
||||
binary.LittleEndian.PutUint32(bomb[offset+24:offset+28], 0x7fffffff)
|
||||
if validateCraftDocument(".docx", bomb) == nil {
|
||||
t.Fatal("accepted oversized Office part")
|
||||
}
|
||||
var unsafe bytes.Buffer
|
||||
writer := zip.NewWriter(&unsafe)
|
||||
for _, name := range []string{"[Content_Types].xml", "word/document.xml", "../outside.xml"} {
|
||||
f, err := writer.Create(name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Write([]byte("<xml/>"))
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if validateCraftDocument(".docx", unsafe.Bytes()) == nil {
|
||||
t.Fatal("accepted traversal")
|
||||
}
|
||||
}
|
||||
func TestAddonRoutesAuthMIMECompressionAndDisable(t *testing.T) {
|
||||
manager := installedTestManager(t)
|
||||
root := t.TempDir()
|
||||
|
||||
Reference in new issue
Block a user