diff --git a/.serena/memories/audit-log.md b/.serena/memories/audit-log.md index 73f4e72610..3a81d6e03c 100644 --- a/.serena/memories/audit-log.md +++ b/.serena/memories/audit-log.md @@ -13,6 +13,7 @@ ## Projection contracts - Emit the entity and scope IDs receivers need instead of assuming access to production tables. A create needs the new entity's ID; moves need source and destination team IDs; deletes need affected IDs before rows disappear. For bulk paths that bypass per-entity emission, use the create-event helpers in `app.loggers.audit`. An existing membership conflict is not a creation event; keep the actor separate from the member. +- Preserve `:file-id` and the resolved `:team-id` on `file-gc` trim events. `delete-object-with-cascade` carries flat `:team-ids` alongside the nested `:cascade` map so receivers can route the event without joining production tables. - An `update-file` event can describe a successful, partial or rejected save. Only saved changes have `:file-change-id`; it points to the revision, not the previous-state snapshot. Read it with `::db/remove-deleted false` because it already has a future deletion date. Even then GC may have removed it. - Preserve explicit business props that receivers depend on, including direct versus team organization invitations. `backend-tests.rpc-audit-test` and `backend-tests.rpc-team-test` cover emission and attribution. Assert exact matching event counts so duplicate emissions cannot pass. diff --git a/.serena/memories/backend/core.md b/.serena/memories/backend/core.md index 67244b03e4..29070ee4d1 100644 --- a/.serena/memories/backend/core.md +++ b/.serena/memories/backend/core.md @@ -6,7 +6,7 @@ JVM Clojure backend. Start in `app.main` for Integrant wiring, `app.rpc.commands - RPC wrapper order, HTTP middleware and session identity: `mem:backend/http-rpc`. - Environment coercion and PostgreSQL driver settings: `mem:backend/config`. -- Media validation and historical topic links: `mem:backend/subtleties`. +- Local and remote media processing, upload validation and SVG handling: `mem:backend/media`. - Metric recording, HTTP and JVM sampling, and log-derived counters: `mem:backend/metrics`. - Transaction ownership, after-commit callbacks and advisory locks: `mem:backend/db-transactions`. - RPC concurrency limit declarations, acquisition order and queue diagnosis: `mem:backend/rpc-concurrency`. diff --git a/.serena/memories/backend/media.md b/.serena/memories/backend/media.md new file mode 100644 index 0000000000..2d2ad809b2 --- /dev/null +++ b/.serena/memories/backend/media.md @@ -0,0 +1,7 @@ +# Backend media + +- Start at `app.media/run`: ImageMagick and FontForge belong to the local path; `:remote-media-processing` sends supported operations through `app.media.remote`. Check the active flag before diagnosing a tool or output difference. The HTTP service contract lives in `mem:media-processor/core`; object lifecycle and buckets live in `mem:backend/storage`. +- Validate the upload path before MIME sniffing or size checks: those checks read the file. Keep unreadable paths distinct from files that exceed a size limit. For the outer multipart boundary see `mem:backend/http-rpc`. +- Keep SVG sanitization at final storage boundaries. Uploads, URL downloads, thumbnails and organization logos that store SVG use `app.media.svg/sanitize-svg-upload`; binfile imports use `app.binfile.common/sanitize-imported-svg`. Backend sanitization is a blocklist with namespace checks and escaped serialization. Check size before calling helpers that load the whole file; the upload limit does not bound the size of the sanitized output. Profile picture imports do not accept SVG. +- `app.media/download-image` checks status, declared length and MIME before writing a temp file, and checks the actual byte count afterward. This download path is separate from processing via the remote service. +- The per-team media byte quota is disabled on `upload-file-media-object` because summing storage objects on every upload was too costly. Do not restore the per-upload sum; keep the team lookup for audit attribution. The upload-session quota is separate. diff --git a/.serena/memories/backend/subtleties.md b/.serena/memories/backend/subtleties.md index e72f462ccd..8f3e38a469 100644 --- a/.serena/memories/backend/subtleties.md +++ b/.serena/memories/backend/subtleties.md @@ -1,5 +1,7 @@ # Backend Subtleties +Historical index. Start at `mem:backend/core` for current guidance, or follow the topic links below. + ## RPC exposure and wrappers - Command registration, validation and wrapper order: `mem:backend/http-rpc`. @@ -36,15 +38,9 @@ ## Storage and media -- The `media-storage-bytes-per-team` quota check on `upload-file-media-object` is TEMPORARILY DISABLED (2026-10-19, kept commented in `app.rpc.commands.media` with its `sql:get-team-id-for-file` lookup still active for the audit props): the quote performs a full `SUM` over `storage_object` per upload and was too slow in production. The `upload-sessions-per-profile` quota is unaffected and stays active. Re-enable only with a cheaper approximation. -- `file-gc` trims emit `file-gc` with the cleaned `:file-id` and its `:team-id`; `delete-object-with-cascade` carries flat `:team-ids` next to the nested `:cascade` map. A consumer can key on those props without joining production tables. -- Storage abstraction, backend configuration, logical buckets, object lifecycle, deduplication, access rules, and garbage collection: `mem:backend/storage`. -- SVG validation strips DOCTYPE and uses secure SAX parsing. Basic SVG info falls back to 100x100 dimensions when width/height/viewBox are missing. -- The SVG sanitizer (`app.media.svg`) is a **blocklist**, not an allowlist: it drops dangerous elements and attributes by name and escapes every text and attribute value when it writes the tree back, so a payload hidden in a value cannot become live markup again. The allowlist rework is [#12190](https://github.com/penpot/penpot/issues/12190) and lands in media-processor, not here. `sanitize-svg` also tracks the namespace bindings through the recursion and drops any element that is not in the SVG namespace, so a document whose root is not an `svg` element in the SVG namespace is rejected and a child that rebinds `xmlns` to XHTML (`iframe`/`srcdoc`/`meta`) is dropped with its subtree; it compares the scheme of an URL the way the browser does (dropping tab/CR/LF/spaces), so `java script:` does not slip through the `javascript:` filter. The paths that store an uploaded SVG as its final object go through `app.media.svg/sanitize-svg-upload` (media upload, the URL download, both thumbnail writers and the organization logo); the three binfile imports go through `app.binfile.common/sanitize-imported-svg`, which calls `sanitize-svg` directly. The profile picture is not an SVG: `import-profile-picture` ignores an `image/svg+xml` download, because the avatar is always rendered as an image. The caller checks the size before calling it: the helper reads the whole file, and the size limit applies to the upload, not to the sanitized copy, which can be up to six times bigger. -- Upload handlers run the `media.v` checks in a fixed order: `check-upload-path`, then `check-media-type`, then `check-media-size` (or `check-font-size` for fonts). The path check gates every later read: `check-media-type` sniffs the bytes at `:path` and `check-media-size` reads `fs/size`, so a path the client named outside `allowed-upload-prefixes` must be rejected before either touches the disk. `check-media-size` leans on that and answers a file it cannot read with the path code (`:params-validation`), not `:media-max-file-size-reached`. Keep the order when touching or adding a handler; two branches that reorder these lines conflict. -- Raster metadata is shell-derived with ImageMagick `identify`, verifies detected MIME against the supplied MIME, and swaps dimensions for EXIF orientations 6/8. -- Remote image download requires 2xx status, `content-length`, a known MIME, and size under the configured maximum before writing the temp file; mismatched byte count is an internal error. -- Font processing shells out to FontForge and WOFF conversion tools and can derive TTF/OTF/WOFF variants from uploaded fonts. +- Local and remote processing, upload validation and SVG handling: `mem:backend/media`. +- Buckets, object lifecycle and cleanup: `mem:backend/storage`. +- File trim and cascade event fields: `mem:audit-log`. ## File data persistence