Skip to content

Fix audit-log hooks skipped at registration; add read-only observe mode for content:beforeSave - #2805

Closed
ascorbic wants to merge 5 commits into
mainfrom
claude/hopeful-pascal-35fa4b
Closed

ascorbic wants to merge 5 commits into
mainfrom
claude/hopeful-pascal-35fa4b

Conversation

@ascorbic

Copy link
Copy Markdown
Member

What does this PR do?

Fixes the audit-log plugin's hooks being skipped at registration on every scaffolded-site boot:

[hooks] Plugin "audit-log" declares content:beforeSave hook without content:write capability — skipping
[hooks] Plugin "audit-log" declares media:afterUpload hook without media:read capability — skipping

The manifest declared only content:read, but HOOK_REQUIRED_CAPABILITY requires content:write for content:beforeSave and media:read for media:afterUpload. Update audit entries lost their before/after diffs and media uploads were never audited.

Resolution of the design fork (over-grant content:write vs. teach core about read-only observation): the capability system's own design leans hard toward legible least-privilege (hooks.email-events:register exists precisely so "reads email events" is visible separately from "can send email"), and content:write hands a plugin the full ctx.content write API. Granting write access to an audit plugin whose manifest is documented as a read-only trust contract seemed wrong, so core gets the distinction:

  • observe: true on content:beforeSave registers the hook with content:read. It's enforced, not honor-system: the handler gets a structuredClone of the content (nested mutation can't leak), its return value is discarded, and its errors are logged without aborting the pipeline — an observer cannot alter or veto a save through any path. observe on any other hook, or combined with exclusive, is rejected at definePlugin, adaptSandboxEntry, and plugin-cli build time.
  • ContentHookEvent gains an optional id, set on updates. This turned out to be a second, hidden blocker: the update payload's content carries only field data (id is a reserved field slug), so the audit plugin's before-state cache could never be keyed — diffs were broken even with the hook registered. The id passed is resolvedItem?.id, never a raw slug.
  • audit-log adds media:read, registers beforeSave as an observer keyed by event.id, caches the prior field data so changes.before matches the shape of changes.after, and bounds the before-state cache (the consuming after-hook never fires for failed saves).

Verified live on the blog template: no [hooks] warnings on boot, and an authenticated post update produces an audit entry with both before and after.

An adversarial review pass (see disclosure) ran before pushing; its three blocking findings (shallow clone was mutable through nested references, an observer could veto saves via the default errorPolicy: "abort", and the before/after shapes mismatched) are fixed in the last commit, along with most minors.

Notes / consciously not done:

  • The isolate-sandbox dispatch (runSandboxedBeforeSave) applies hook results without any capability gating and ignores observe — pre-existing for that tier, unchanged here. Worth its own issue.
  • audit-log's emdash peer floor stays at >=0.15.0: on older cores the plugin degrades to exactly today's behavior (skip + warning), which seemed better than a hard install failure; the changeset spells it out.
  • On revision-enabled collections ctx.content.get reads the published record, so changes.before is the last published state rather than the previous draft — existing accessor semantics, noted here for reviewers.
  • The observer costs 1–2 extra reads per authenticated content update when audit-log is installed. Logged-out routes are untouched (hooks fire on writes only).

Closes # — no issue; direct maintainer instruction.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change) — full emdash suite plus new targeted tests; audit-log's new test package is wired into test:unit
  • pnpm format has been run (scoped to touched files to avoid unrelated repo drift)
  • I have added/updated tests for my changes (if applicable) — all written failing-first: observer registration/discard/clone/no-veto and event-id tests in core; a manifest↔code coupling test that loads the real audit-log plugin and manifest through adaptSandboxEntry + HookPipeline (fails with exactly the two projector warnings on the old code); a before/after diff-flow test against the real handlers
  • User-visible strings in the admin UI are wrapped for translation — n/a, no admin UI changes
  • I have added and reviewed the user-facing changeset (if this PR changes a published package) — emdash minor + @emdash-cms/plugin-cli patch, and @emdash-cms/plugin-audit-log patch
  • New features link to an approved Discussion — n/a: bug fix executed on direct maintainer instruction (the observe option is the fix mechanism)

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Fable 5 (implementation) with a Claude Opus adversarial-review pass before push

Screenshots / test output

packages/core plugin unit tests:  31 files, 658 passed
emdash full suite:                6172 passed, 9 skipped
@emdash-cms/plugin-audit-log:     2 passed
pnpm typecheck:                   clean
pnpm lint:                        clean
docs build:                       clean

Live blog-template check (dev server, authenticated update via API):

{"action":"update","before":{"title":"Audit test post"},"afterTitle":"Audit shape check"}

…vents

An observe: true hook config registers content:beforeSave with only
content:read; the pipeline hands it a shallow-cloned event and discards
its return value so it cannot mutate the draft. ContentHookEvent gains
an optional id, set on updates where the payload data carries no id.
The plugin-cli probe and dev pipeline forward the new flag.
The manifest declared only content:read, so core skipped
content:beforeSave (requires content:write) and media:afterUpload
(requires media:read) at registration — update audit entries lost
their before/after diffs and media uploads were never audited. Add
media:read, register beforeSave as an observe hook keyed by the new
event id, and cover the manifest/code coupling and the diff flow with
tests wired into test:unit.
Observers now get a structuredClone of the content so nested mutation
cannot leak into the save, and their errors are recorded but never
abort the pipeline — a content:read observer can no longer alter or
veto a save through any path. Reject observe on unsupported hooks and
observe+exclusive at definePlugin, adaptSandboxEntry, and plugin-cli
build time. Pass resolvedItem?.id (never a raw slug) as the beforeSave
event id and forward it through PluginManager. audit-log caches the
prior field data (matching the shape of changes.after), bounds its
before-state cache, and its test stubs the real ContentItem shape.
@changeset-bot

changeset-bot Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dca6498

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 18 packages
Name Type
@emdash-cms/plugin-audit-log Patch
emdash Minor
@emdash-cms/plugin-cli Patch
@emdash-cms/cloudflare Minor
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Minor
@emdash-cms/auth Minor
@emdash-cms/blocks Minor
@emdash-cms/gutenberg-to-portable-text Minor
@emdash-cms/x402 Minor
create-emdash Minor
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

Scope check

This PR changes 667 lines across 26 files. Large PRs are harder to review and more likely to be closed without review.

If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs.

See CONTRIBUTING.md for contribution guidelines.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
docs c85c1cf Aug 31 2026, 08:48 AM

@pkg-pr-new

pkg-pr-new Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/admin@2805

@emdash-cms/auth

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/auth@2805

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/auth-atproto@2805

@emdash-cms/blocks

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/blocks@2805

@emdash-cms/cloudflare

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/cloudflare@2805

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/contentful-to-portable-text@2805

emdash

npm i https://pkg.pr.new/emdash-cms/emdash@2805

create-emdash

npm i https://pkg.pr.new/emdash-cms/emdash/create-emdash@2805

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/gutenberg-to-portable-text@2805

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-cli@2805

@emdash-cms/plugin-types

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-types@2805

@emdash-cms/registry-client

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/registry-client@2805

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/registry-lexicons@2805

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/registry-moderation@2805

@emdash-cms/registry-verification

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/registry-verification@2805

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/sandbox-workerd@2805

@emdash-cms/x402

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/x402@2805

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-ai-moderation@2805

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-atproto@2805

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-audit-log@2805

@emdash-cms/plugin-color

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-color@2805

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-embeds@2805

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-field-kit@2805

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-forms@2805

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/emdash-cms/emdash/@emdash-cms/plugin-webhook-notifier@2805

commit: dca6498

@github-actions

Copy link
Copy Markdown
Contributor

Overlapping PRs

This PR modifies files that are also changed by other open PRs:

This may cause merge conflicts or duplicated work. A maintainer will coordinate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant