Files
adminandClaude Opus 5.5 3802cc9114 Be our own edge: headers, Open Graph and the console from the backend, no Caddy
The platform is dropping Caddy (Atrium's backend becomes the front door and
forwards /galena/* with the prefix stripped), and the standalone stack drops
it too, so the backend now answers for everything Caddy added.

- app/edge.py: SecurityHeaders, pure ASGI and outermost, stamps every
  response (errors included): the console CSP character for character as
  Caddy sent it, with frame-ancestors from FRAME_ANCESTORS; X-Frame-Options
  DENY or SAMEORIGIN to match; nosniff, Referrer-Policy same-origin, COOP,
  no-cache on the console. The ReaderJS viewer keeps _READER_CSP and
  SAMEORIGIN regardless of the setting. /raw keeps its own sandbox CSP.
- FRAME_ANCESTORS is 'none' (default) or 'self'; anything else refuses to
  start. GALENA_FRAME_ANCESTORS now refuses to start and names the new knob,
  so there is one switch.
- ConsoleFiles: the try_files fallback, plus the Open Graph tags Caddy's
  `templates` rendered, now from GALENA_PUBLIC_URL or scheme://Host +
  root_path (so the /galena prefix is right), HTML-escaped, ETag/304.
- CompressibleGZip skips /raw downloads and images; BodySizeLimit caps a
  request at 100 MB (MAX_FILE_BYTES stays the stricter content limit).
- ProblemDetailErrorMiddleware is pure ASGI, so small answers stay small.
- Dockerfile: frontend as a named build context; uvicorn with
  --proxy-headers, --forwarded-allow-ips and --no-server-header.
- Standalone compose publishes the backend on :80; Caddyfile deleted;
  compose-smoke asserts the new headers and a rendered og:url.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 16:06:40 +02:00

9.2 KiB
Raw Permalink Blame History

Editing office documents (DOC / DOCX / ODT / RTF) — options & decision

This records the deep analysis behind how Galena edits office documents, and the options that were considered and rejected, so the trade-offs aren't re-litigated from scratch later.

Decision

  • Primary (shipped, in core): option #2 — in-app editing of DOCX & ODT via the Markdown editor. On open, the document body is extracted to Markdown (python-docx for DOCX, odfdo for ODT) and opened in the existing Slate editor; on save, it's regenerated from Markdown and funnelled through the normal write chokepoint (changes.apply_change) as a redaction/draft. All deps are permissive (MIT / Apache-2.0 / BSD-3), so it stays in the permissive core.
  • RTF (shipped, in core): client-side text-level editor. .rtf is plain-text markup, so Galena ships a tiny in-browser reader/writer (rtfEditor in app.js) — no server-side RTF dependency. It opens .rtf in a full-screen editor and saves clean RTF through the multipart /upload chokepoint (so policy/encryption/audit/redaction all apply). Text-level and lossy, like DOCX/ODT. Ported from the 0BSD RTF Editor plugin and folded into core under Apache-2.0 — a permissive, zero-server-dep path the license gate allows.
  • Legacy DOC (shipped, in core): client-side text-level editor. The old Word 97–2003 binary OLE2 .doc now edits in-browser too, via JSDoc (Alpaq92/JSDoc, 0BSD) — a pure-JS, dependency-free reader/writer built clean-room from [MS-CFB]/[MS-DOC] (no GPL catdoc/antiword). Vendored into frontend/static/jsdoc/; docEditor opens the .doc, edits its text, and writes a real .doc back through /upload. Text-level/lossy like RTF; unreadable variants (Word 6/95, encrypted, corrupt → docToText returns null) fall back to the handoff. This supersedes the earlier "no permissive .doc reader" finding.
  • Fallback (shipped, in core): option #1 — "edit locally" handoff. Any document that can't be round-tripped — legacy DOC, passphrase-encrypted files, or a DOCX/ODT/RTF that fails to parse — shows a card to Download, edit in your own app, and Upload edited version (which overwrites the file in place, again through apply_change). This is also offered alongside the editor for DOCX/ODT/RTF when full fidelity matters.

The honest trade-off

Editing via Markdown is body-level and lossy: headings, paragraphs, bold/italic, bullet/number lists and (DOCX) simple tables survive; images, footnotes, comments, custom styles, headers/footers and tracked changes are dropped on save. The editor therefore shows a "formatting may be simplified" banner and a one-click Download-the-original. For full fidelity, use the handoff.

No option is both permissive-core-eligible and high-fidelity — that combination does not exist. Every faithful editor is copyleft (Collabora MPL, ONLYOFFICE/SuperDoc AGPL) or proprietary (Apryse, Syncfusion). And keeping the real office binary breaks Galena's git-native UX (a zipped DOCX/ODT diffs wholesale, so Changes/Authorship/merge/search degrade — see the binary-blob note below). The ranking optimizes for Galena's priorities: permissive core → git-native text UX → low infra.

Full ranking (best → worse for Galena)

# Option Formats truly editable License → placement Fidelity Effort Git fit
★ Client-side RTF reader/writer (text-level, in app.js) RTF 0BSD → core Low (text-level) Low text
1 Native handoff (download → edit → re-upload) DOC·DOCX·ODT·RTF (lossless) none → core Perfect Low binary
2 python-docx + odfdo behind the Slate editor DOCX·ODT (lossy); RTF/DOC read-only MIT/Apache → core Low High text (extracted)
3 Collabora Online (WOPI sidecar) DOC·DOCX·ODT·RTF MPL-2.0 → operator add-on Highest Very high binary
4 ONLYOFFICE Docs (iframe+JWT sidecar) DOC·DOCX·ODT·RTF AGPL-3.0 → add-on High Very high binary
5 SuperDoc (client-side, plugin) DOCX only AGPL-3.0 → plugin High High binary
6 LibreOffice-WASM (ZetaOffice, plugin) DOC·DOCX·ODT·RTF MPL+LGPL → plugin Highest, but beta Very high binary
7 Pandoc (server convert ↔ Markdown) DOCX·ODT·RTF; no DOC GPL → out-of-process Low (double-lossy) Very high text
8 Apryse WebViewer (commercial, client WASM) DOCX only Proprietary (~$10k+/yr) High Very high binary
9 TipTap + DOCX converters DOCX only Paid+cloud or AGPL High Very high binary
10 LibreOffice headless (soffice --convert-to) all (convert-only, not an editor) MPL+bundled (L)GPL → sidecar High convert / lossy edit Very high binary

Considered and cut: Syncfusion Document Editor (proprietary + needs a second .NET/Java service — worst stack clash) and CKEditor 5 (GPL core + paid/cloud converters that egress document bytes, and it's HTML round-trip, not true DOCX round-trip).

Why each rank (one line)

  1. Handoff — already ~95% built (binary download card + multipart upload through apply_change); zero deps/infra; lossless for all four formats because the user's own app does the editing.
  2. python-docx + odfdo — the only path that's both permissive-core-eligible and can revive text-native features; reuses the existing editor; lossy and DOCX/ODT-only. (chosen)
  3. Collabora — best fidelity + co-editing; MPL (most permissive heavyweight); but a ~1GB sidecar and a full WOPI host in the backend; the pattern Nextcloud/Seafile/etc. all use.
  4. ONLYOFFICE — marginally better Microsoft-format fidelity than Collabora, but AGPL-3.0 (network copyleft) and heavier (~4GB).
  5. SuperDoc — cleanest in-browser true editor (no server), but AGPL and DOCX-only; needs a registerEditor plugin seam Galena lacks.
  6. LibreOffice-WASM — real LibreOffice client-side for all four formats, but beta/unstable, ~1GB runtime, and the export-bytes-out step is unimplemented today. Watch, don't ship.
  7. Pandoc — cleanest git story if Markdown becomes canonical, but GPL (can't even be a plugin), no .doc, doubly-lossy; strictly a worse-licensed #2.
  8. Apryse — genuine client-side WASM DOCX editing, but proprietary (~$10k+/yr) and DOCX-only.
  9. TipTap + converters — MIT editor, but converters are paid+cloud or AGPL; effectively DOCX-only and needs a full editor-bundle swap. Dominated by #5.
  10. LibreOffice headless — reference converter, not an editor; heavy, copyleft-bundling, server attack surface. Its real value is office→PDF preview, not editing.

Cross-cutting constraints (why the ranking is what it is)

  • License is the dominant axis. Core ships only permissive deps and must pass backend/scripts/license_gate.py (denies AGPL/SSPL always, GPL-without-LGPL). Only zero-dependency patterns (the handoff, and the client-side text-level RTF editor — 0BSD, no server dep) and genuinely permissive libs (python-docx MIT, odfdo Apache-2.0, striprtf BSD-3) can touch core. Everything high-fidelity is copyleft or proprietary → operator- installed plugin/sidecar only (the GPL-2.0 DjVu Viewer is the precedent). (Trap: avoid odfpy — its tri-license string slips the gate via the "LGPL" substring but embeds GPL; use odfdo.)
  • Git-model fit splits the field. Storing the real office binary is binary-blob: a zipped DOCX/ODT decodes as non-UTF-8, so Changes returns "binary" (re-zipping diffs wholesale), Authorship is unavailable, the textarea-based Resolve-discrepancies is meaningless, git merge conflicts wholesale, and search skips it. Only convert-to-text strategies make those features meaningful — and only by tracking text. Galena keeps the original DOCX/ODT as the blob (so these features stay limited for office files, as documented) and gives editing via extraction.
  • Infra fit. Galena is single-instance FastAPI + Postgres (the backend serves its own console), no runtime build, one committed editor bundle. Anything adding a container or foreign runtime (Collabora ~1GB, ONLYOFFICE ~4GB, Syncfusion .NET, …) is operator-add-on-only. Option #2 runs in-process with permissive Python libs.
  • Write chokepoint. The JSON save route is UTF-8-text-only; office saves use a binary-capable route (POST /projects/{id}/office-file, and the multipart /upload for the handoff) that still funnels through changes.apply_change — preserving review policy, encryption, audit, and redaction/draft.
  • Encryption wall. Passphrase-mode blobs can't be decrypted server-side, so they can't be extracted for editing — they fall back to the handoff (the user supplies the passphrase to their own app).

If higher fidelity is ever needed

Add Collabora Online over WOPI as an optional, operator-installed sidecar (rank 3) — the same two-tier split (Markdown editor in core, office suite as an external WOPI server) that the whole self-hosted-drive market uses. This is a deliberate, documented future step, not core.