diff --git a/.planning/phases/38-extensible-journal-embed-providers/38-REVIEW.md b/.planning/phases/38-extensible-journal-embed-providers/38-REVIEW.md new file mode 100644 index 00000000..c455e01a --- /dev/null +++ b/.planning/phases/38-extensible-journal-embed-providers/38-REVIEW.md @@ -0,0 +1,170 @@ +--- +phase: 38-extensible-journal-embed-providers +reviewed: 2026-11-28T13:08:00+01:00 +depth: standard +files_reviewed: 33 +files_reviewed_list: + - ../sm-journal-plugin/classes/embed_registry.go + - ../sm-journal-plugin/classes/format_html.go + - ../sm-journal-plugin/classes/blog_editor.go + - ../sm-journal-plugin/plugin.go + - ../sm-journal-plugin/config/config.yaml + - ../sm-journal-plugin/controllers/api/posts.go + - ../sm-journal-plugin/controllers/api/posts_helpers.go + - ../sm-journal-plugin/controllers/posts.go + - ../sm-journal-plugin/console/import_posts.go + - ../sm-journal-plugin/assets/src/blogeditor/embed.ts + - ../sm-journal-plugin/assets/src/blogeditor/extensions.ts + - ../sm-journal-plugin/assets/src/blogeditor/element.ts + - ../sm-journal-plugin/assets/src/blogeditor/preview.ts + - ../sm-journal-plugin/assets/src/blogeditor/labels.ts + - ../sm-journal-plugin/assets/src/blogeditor/blogeditor.css + - ../sm-journal-plugin/classes/embed_registry_test.go + - ../sm-journal-plugin/classes/editor_policy_test.go + - ../sm-journal-plugin/tests/blogeditor/embed.test.ts + - ../sm-journal-plugin/tests/blogeditor/additions.test.ts + - ../sm-journal-plugin/README.md + - ../vue-journal-layer/app/utils/embedRegistry.ts + - ../vue-journal-layer/app/plugins/journal-embeds.ts + - ../vue-journal-layer/app/composables/useJournalEmbedHydration.ts + - ../vue-journal-layer/app/utils/htmlGate.ts + - ../vue-journal-layer/app/utils/embeds.ts + - ../vue-journal-layer/app/components/Journal/PostBody.vue + - ../vue-journal-layer/app/components/Journal/CategoryHeader.vue + - ../vue-journal-layer/app/assets/css/journal-base.css + - ../vue-journal-layer/tests/embeds.test.ts + - ../vue-journal-layer/tests/htmlGate.test.ts + - ../vue-journal-layer/tests/embedHydration.test.ts + - ../vue-journal-layer/README.md + - scripts/check-phase38.sh +findings: + critical: 0 + warning: 4 + info: 3 + total: 7 +status: clean +fixed: 2026-11-28T13:13:00+01:00 +fix_scope: all +--- + +# Phase 38: Code Review Report + +**Reviewed:** 2026-11-28 +**Depth:** standard +**Files Reviewed:** 33 (across `sm-journal-plugin`, `vue-journal-layer`, and `summercms.go`) +**Status:** clean (0 Critical, 4 Warning, 3 Info — all 7 findings fixed and re-verified via `bash scripts/check-phase38.sh`) + +## Summary + +Phase 38 cleanly replaces hardcoded YouTube-only embed handling with a generic, app-extensible embed provider registry across `sm-journal-plugin` and `vue-journal-layer`. The core security boundaries are solid: + +- Pair-aware `(data-embed, data-id)` validation is enforced consistently in both Go (`bluemonday` + `enforceEmbedPairs`) and TypeScript (`sanitize-html` + `makeFilterAttributeValues`), stripping all four embed attributes (`data-embed`, `data-id`, `data-aspect`, `data-height`) on mismatch or orphan attributes. +- `data-aspect` and `data-height` regexes allow zero CSS injection vectors and are re-validated before inline style emission in `expandEmbeds`. +- Fallback `` and `
` children pass through UGC sanitization in both runtimes, and `EmbedRegistry` in Go uses `sync.RWMutex` cleanly with double-checked lazy policy caching. +- Built-in `youtube` SSR output remains byte-compatible, and all Journal write/load/import paths (`blog_editor.go`, `posts_helpers.go`, `import_posts.go`, `controllers/posts.go`) wire `EmbedRegistryFrom(app)`. + +No Critical vulnerabilities were found. Four Warnings should be addressed before verification/release: two in the client hydration runtime (`useNuxtApp()` called outside `setup()` and detached-DOM mounting that yields `0×0` layout dimensions for chart/viz providers), one in the Blog Editor adaptive form (custom `mode: "url"` providers cannot be inserted and `aspect`/`height` validation displays the wrong error label), and one cross-repo key validation parity gap (`constructor`/`prototype` accepted in Go/Blog Editor but rejected in `vue-journal-layer`). + +--- + +## Warnings + +### WR-01: `useNuxtApp()` called inside `onMounted` / `watch` callback instead of synchronously in `setup()` + +**File:** `../vue-journal-layer/app/composables/useJournalEmbedHydration.ts:205-206` +**Issue:** `useJournalEmbedHydration` calls `useNuxtApp()` inside `scan()`, which is invoked asynchronously from `onMounted()` (line 263) and `watch(htmlSource, ..., { flush: 'post' })` (line 267): + +```ts +const scan = () => { + ... + const nuxtApp = typeof useNuxtApp === 'function' ? useNuxtApp() : undefined + const appContext = (nuxtApp?.vueApp?._context as AppContext | undefined) ?? null +``` + +In Vue 3 and Nuxt 4, `getCurrentInstance()` is `null` inside `onMounted` and post-flush `watch` callbacks. Calling `useNuxtApp()` outside synchronous `setup()` violates Nuxt composable rules and throws `[nuxt] A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function` whenever Nuxt's global context singleton is unavailable (e.g., multi-app mounts, test harnesses, or async teardown boundaries). + +**Fix:** Capture `appContext` once synchronously at the top of `useJournalEmbedHydration()` using `getCurrentInstance()?.appContext` with a safe `tryUseNuxtApp` / `useNuxtApp` fallback: + +```ts +import { getCurrentInstance, ... } from 'vue' +... +const instance = getCurrentInstance() +const appContext: AppContext | null = + instance?.appContext ?? + (typeof useNuxtApp === 'function' ? ((useNuxtApp()?.vueApp?._context as AppContext | undefined) ?? null) : null) +``` + +--- + +### WR-02: `hydrateEmbedFigure` mounts Vue components and DOM `mount()` callbacks into a detached element (`clientWidth === 0`) + +**File:** `../vue-journal-layer/app/composables/useJournalEmbedHydration.ts:90-150` +**Issue:** `hydrateEmbedFigure` creates `host = doc.createElement('div')` (detached from the document), calls `render(vnode, host)` (line 111) or `await provider.mount(host, ctx)` (line 137), and only attaches `host` to `figure` afterwards via `replaceChildrenSafe(figure, host)` (lines 117, 143). + +As a result, during a Vue component's `onMounted` hook or a plain DOM provider's `mount(el, ctx)` callback, `host.isConnected` is `false`, `host.clientWidth` and `host.getBoundingClientRect().width` are `0`, and computed styles / `ResizeObserver` initial measurements fail. Interactive charts and canvas/SVG visualizations (the primary use case for Phase 38) routinely measure container width on mount to size their viewport. + +**Fix:** For Vue `component` providers, `await provider.component()` (the dynamic import) while `figure` still displays the SSR fallback. Once the module is loaded (or for `mount`), attach `host` into `figure` (e.g., hiding or stashing the fallback nodes, or replacing children after saving them) before calling `render(vnode, host)` / `provider.mount(host, ctx)`, and restore the original fallback children in the `catch` / abort block if mounting throws or aborts. This gives mounted widgets real layout dimensions while preserving error resilience. + +--- + +### WR-03: Custom `mode: "url"` providers cannot be inserted in Blog Editor, and `aspect`/`height` validation reports `embedIdInvalid` + +**File:** `../sm-journal-plugin/assets/src/blogeditor/embed.ts:107-117` and `../sm-journal-plugin/assets/src/blogeditor/element.ts:779-808` +**Issue:** Two UX/contract bugs in the Blog Editor adaptive embed form: +1. `EmbedRegistry.Register` in Go accepts `Mode: "url"` for any custom provider key (e.g. `vimeo`), and `resolveEmbedProviders` preserves `mode: 'url'`, but only attaches `idFromUrl` when `key === 'youtube'` (`embed.ts:116`). In `element.ts:779-788`, selecting a custom `mode: "url"` provider shows the URL input and calls `parseEmbedUrlWith([provider], ...)`, which always returns `null` because `!provider.idFromUrl`, permanently blocking insertion with `"This video URL is not supported. Paste a YouTube link."`. +2. In `element.ts:799-808`, when the author enters a valid `embedId` but an invalid `aspect` (e.g. `16:9`) or `height` (e.g. `420em`), the form displays `this.#labels.embedIdInvalid` (`"Enter a valid ID for this embed provider."`), pointing the author at the wrong field. + +**Fix:** +1. In `resolveEmbedProviders` (`embed.ts`), degrade `mode` to `'id'` whenever a provider has no `idFromUrl` parser (or reject `mode: "url"` for non-`youtube` keys in `EmbedRegistry.Register` if URL-parsing is YouTube-only in v1). +2. Add dedicated `embedAspectInvalid` and `embedHeightInvalid` labels in `labels.ts` (`en` and `pl`) and display them in `element.ts:800-808`. Rebuild `assets/dist/blogeditor.{js,css}`. + +--- + +### WR-04: Cross-repo reserved provider key parity gap (`constructor`, `prototype`) + +**File:** `../sm-journal-plugin/classes/embed_registry.go:26,99-102` and `../sm-journal-plugin/assets/src/blogeditor/embed.ts:45,95` +**Issue:** `vue-journal-layer/app/utils/embedRegistry.ts:17,127` rejects `RESERVED_PROVIDER_KEYS = new Set(['constructor', 'prototype'])` in addition to `EMBED_PROVIDER_KEY`. However, neither Go (`embed_registry.go`) nor the Blog Editor (`embed.ts`) rejects `constructor` or `prototype`, which both match `^[a-z][a-z0-9_-]{0,31}$`. A YAML config declaring `constructor` or `prototype` would boot cleanly in Go, appear in the Blog Editor, and then fail at runtime in `vue-journal-layer`. + +**Fix:** Reject `"constructor"` and `"prototype"` in `EmbedRegistry.Register` (`embed_registry.go`) and `resolveEmbedProviders` (`embed.ts`) so all three layers enforce the exact same provider key allow-list. + +--- + +## Info + +### IN-01: `app/plugins/journal-embeds.ts` re-normalizes providers on every SSR request against a module-level singleton + +**File:** `../vue-journal-layer/app/plugins/journal-embeds.ts:10-15` and `../vue-journal-layer/app/utils/embedRegistry.ts:223-240` +**Issue:** `journal-embeds.ts` calls `defineJournalEmbedProviders(appConfig.journal.embeds)` on every SSR request in Nitro, re-compiling provider `RegExp`s each time and mutating the process-wide `activeRegistry`. While deterministic for static `app.config.ts` definitions, a consuming app that overrides a provider in a later plugin will have its override temporarily overwritten at the start of every concurrent SSR request. +**Recommendation:** Track whether the current `appConfig.journal.embeds` object reference has already been synced (e.g., via a `WeakSet` or reference equality check) so static `app.config.ts` providers are compiled once per server process rather than per request. + +--- + +### IN-02: Blog Editor preview `.embed-card img` forces `aspect-ratio: 16/9` on custom fallback images + +**File:** `../sm-journal-plugin/assets/src/blogeditor/preview.ts:223` +**Issue:** `.embed-card img { display: block; width: 100%; height: auto; aspect-ratio: 16/9; object-fit: cover }` crops non-16/9 custom fallback charts (e.g., `4/3` or tall visualizations) in the Blog Editor preview pane, unlike `vue-journal-layer/app/assets/css/journal-base.css` which scopes `aspect-ratio: 16 / 9` to `.journal-embed--youtube` and `.journal-embed:has(> iframe)`. +**Recommendation:** Add a modifier class (e.g., `.embed-card--youtube`) for YouTube thumbnail cards and scope `aspect-ratio: 16/9; object-fit: cover` to `.embed-card--youtube img` so custom fallback images render at their natural aspect ratio. + +--- + +### IN-03: Minor stale YouTube-only wording in `README.md` feature summaries + +**File:** `../sm-journal-plugin/README.md:21,477` and `../vue-journal-layer/README.md:35` +**Issue:** While both repositories added thorough new Embeds / Embed providers sections, the top-level `## Features` HTML sanitizer bullets in both READMEs still describe `data-embed` as matching `^youtube$` only (with 5 rules), and `sm-journal-plugin/README.md:477` (`journal.Plugin.ConfigFS`) omits `golem15.journal.embed_providers`. +**Recommendation:** Update those three summary lines to reference the extensible embed registry and `data-aspect`/`data-height` rules. + +--- + +## Fix Resolution (`--fix --all`) + +All 7 findings (WR-01..WR-04 and IN-01..IN-03) were applied and verified with `bash scripts/check-phase38.sh` (all 5 stages PASS): + +| ID | Status | Summary of Fix | +|----|--------|----------------| +| **WR-01** | Fixed | Captured `appContext` synchronously in `useJournalEmbedHydration` via `getCurrentInstance()?.appContext` with `useNuxtApp()` fallback instead of calling `useNuxtApp()` inside `scan()`. | +| **WR-02** | Fixed | Attached `host` to `figure` before `render(vnode, host)` and `provider.mount(host, ctx)` so layout-measuring widgets see real dimensions, saving fallback children and restoring them on throw or abort; added `attachedDuringMount` assertion in `tests/embedHydration.test.ts`. | +| **WR-03** | Fixed | Degraded custom providers without `idFromUrl` to `'id'` mode in `resolveEmbedProviders`, added `embedAspectInvalid` / `embedHeightInvalid` (`en` and `pl`) labels in `labels.ts`, wired them in `element.ts`, added Vitest assertions in `tests/blogeditor/embed.test.ts`, and rebuilt `assets/dist/blogeditor.{js,css}`. | +| **WR-04** | Fixed | Rejected reserved keys `constructor` and `prototype` in `EmbedRegistry.Register` (`classes/embed_registry.go`) and `resolveEmbedProviders` (`assets/src/blogeditor/embed.ts`) with Go and Vitest coverage. | +| **IN-01** | Fixed | Guarded `defineJournalEmbedProviders(embeds)` in `app/plugins/journal-embeds.ts` with a `WeakSet` so static `app.config.ts` embed maps are synced once per process instead of per SSR request. | +| **IN-02** | Fixed | Scoped `aspect-ratio: 16/9; object-fit: cover` in `assets/src/blogeditor/preview.ts` to `.embed-card--youtube img` so custom fallback images render at their natural aspect ratio. | +| **IN-03** | Fixed | Updated top-level `Features` HTML sanitizer bullets in `sm-journal-plugin/README.md` and `vue-journal-layer/README.md` (and `ConfigFS` in `sm-journal-plugin/README.md`) to reflect the extensible embed registry and `data-aspect`/`data-height` rules. |