Skip to content

fix(collab): back-fill attachmentId from file URL on HTML→JSON writes (#392) - #36

Merged
PMQ9 merged 3 commits into
wiki-v2from
fix/392-attachment-id-backfill
Sep 18, 2026
Merged

PMQ9 merged 3 commits into
wiki-v2from
fix/392-attachment-id-backfill

Conversation

@PMQ9

@PMQ9 PMQ9 commented Sep 18, 2026

Copy link
Copy Markdown

What

htmlToJson now back-fills a node's attachmentId from its same-origin file URL when the HTML author omitted data-attachment-id.

Every attachment node type (image/video/audio/pdf/attachment/excalidraw/drawio) parses attachmentId only from a data-attachment-id attribute. So an agent authoring the natural <img src="/api/files/<uuid>/x.png"> (or <video src>/<audio src>) over the /v1 API produced a node with attachmentId: null — it renders (the view reads src alone) but the page↔attachment linkage is silently lost, orphaning the file to getAttachmentIds, which export / share / duplicate all key on.

A single backfillAttachmentIds post-pass in htmlToJson (beside the existing addUniqueIdsToDoc) recovers the id from attrs.src (or attrs.url for the attachment node) for every attachment node type. An explicit data-attachment-id still wins; the URL match is anchored to a leading slash (an external https://evil.example/api/files/<uuid>/… never matches) and gated by the version-agnostic isValidUUID (Docmost mints uuidv7, so a v4-only regex would silently no-op). The inherited img[src]:not([src^="data:"]) tag gate is untouched — inline data: images still drop.

Covers the server /v1 + markdown-import path. This is the wiki-v2 super-repo's issue docmost#392; it pairs with a super-repo PR that adds a wiki_upload_attachment MCP tool (docmost#368) which authors correct JSON directly.

Testing

apps/server/src/editor-compat/attachment-id-backfill.spec.ts exercises the real htmlToJson path: image/video/audio backfill (Class A), the /files/, /public/, and ?query URL variants, explicit-id-wins, external-URL and anchored-slash-spoof rejection, non-UUID rejection, the data: drop, and the attachment-node (Class B) safety net.

pnpm --filter docmost-server exec jest src/editor-compat   # 22 passed (12 new + 10 existing typography)

Co-Authored-By: Claude Opus 4.8 [email protected]

PMQ9 and others added 3 commits September 18, 2026 09:40
…docmost#392)

Every attachment node type (image/video/audio/pdf/attachment/excalidraw/drawio)
parses `attachmentId` only from a `data-attachment-id` attribute, so an agent
authoring the natural `<img src="/api/files/<uuid>/x.png">` (or `<video>`/`<audio>`)
over the /v1 API produced a node with `attachmentId: null` — it rendered (the view
reads `src` alone) but the page↔attachment linkage was silently lost, orphaning the
file to `getAttachmentIds` (export / share / duplicate all key on it).

`htmlToJson` now back-fills `attachmentId` from the node's same-origin file URL
(`attrs.src`, or `attrs.url` for the attachment node) after `generateJSON`, covering
all attachment node types in one place. An explicit `data-attachment-id` still wins;
the URL match is anchored to a leading slash (an external URL that merely contains
`/api/files/<uuid>` never matches) and gated by the version-agnostic `isValidUUID`
(Docmost mints uuidv7). The inherited `img[src]:not([src^="data:"])` tag gate is
untouched, so inline `data:` images still drop.

Covers the server /v1 + markdown-import path (what docmost#392 reports). Spec lives under
src/editor-compat/ so the docmost-authz jest glob runs it.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…tor-compat (review)

Address the PR review (wiki-v2#396): the ~40 lines of new attachmentId-backfill
logic lived inline in the upstream-owned collaboration.util.ts. Per the fork model
(new logic in fork-owned/boundary-excluded dirs; upstream files get minimal wiring —
mirrors the #59/#61 typography seam), extract the regex + attachmentIdFromFileUrl +
backfillAttachmentIds into apps/server/src/editor-compat/attachment-id-backfill.ts,
leaving collaboration.util.ts with just an import + the one call in htmlToJson. No
behavior change; the editor-compat spec (which exercises the real htmlToJson path)
stays green and now also pins that isValidUUID is load-bearing (a 36-char [0-9a-f-]
non-UUID must not backfill). Documents the doubled-walk as an accepted tradeoff.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
… seam (docmost#392)

CI caught it: moving the docmost#392 backfill logic into fork-owned editor-compat/ (review) made
the upstream-owned collaboration.util.ts import CCC code, which the fork import-boundary
spec (rule 3) flags unless the seam is documented. Add collaboration/collaboration.util.ts
to SEAM_ALLOWLIST (it now imports editor-compat/attachment-id-backfill), cross-referenced
to UPSTREAM_MODIFICATIONS #61. Also satisfies the rule-3 hygiene check (the file genuinely
imports CCC code now).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@PMQ9
PMQ9 force-pushed the fix/392-attachment-id-backfill branch from c9070f2 to 5828dde Compare September 18, 2026 14:42
@PMQ9
PMQ9 merged commit 1c8cfce into wiki-v2 Sep 18, 2026
@PMQ9
PMQ9 deleted the fix/392-attachment-id-backfill branch September 18, 2026 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant