Skip to content

fix(core): pass site context to sandboxed plugins#2081

Open
logelog wants to merge 2 commits into
emdash-cms:mainfrom
logelog:fix/sandbox-plugin-site-info
Open

fix(core): pass site context to sandboxed plugins#2081
logelog wants to merge 2 commits into
emdash-cms:mainfrom
logelog:fix/sandbox-plugin-site-info

Conversation

@logelog

@logelog logelog commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Forwards normalized site information to sandbox runners created for configured, marketplace, and registry-installed plugins. Sandboxed plugin contexts can now build absolute same-site URLs through ctx.url(path) when emdash:site_url is configured.

The runtime already resolved site information during cold start, but runner construction discarded it. The runner therefore generated plugin wrappers with an empty base URL.

Closes #1872

Type of change

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

Checklist

  • I have read CONTRIBUTING.md
  • pnpm --filter emdash typecheck passes
  • Type-aware lint passes for the changed source and test files
  • Targeted unit and integration tests pass
  • Formatting checks pass
  • I added regression coverage
  • No admin UI strings were added
  • A patch changeset is included

Validation

  • pnpm --filter emdash typecheck
  • pnpm --filter emdash build
  • pnpm --filter emdash check
  • vitest run tests/unit/plugins/sandbox-runner-options.test.ts tests/integration/runtime/create.test.ts
  • oxlint --type-aware --deny-warnings on changed source and test files
  • oxfmt --check on changed source and test files
  • prettier --check .changeset/sandbox-plugin-site-context.md

@changeset-bot

changeset-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cb9c38e

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

This PR includes changesets to release 16 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@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 Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@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

@logelog
logelog marked this pull request as ready for review July 16, 2026 22:13
@github-actions github-actions Bot added the review/needs-review No maintainer or bot review yet label Jul 16, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a focused, correct fix for #1872. The runtime was already reading site information during cold start, but the sandbox runner factory was being invoked without it, so the generated plugin wrapper baked an empty base URL into ctx.url(). The new createSandboxRunnerOptions helper reuses the same createSiteInfo normalization that trusted plugin contexts use, keeping sandboxed and trusted plugins consistent. The change threads siteInfo through all three runner construction sites (configured, marketplace, and registry plugins), adds a regression unit test for the helper and an integration test for the runtime wiring, and includes a patch changeset. No user-facing UI strings, migrations, SQL, or authorization changes are involved.

I checked the changed files, the adapter implementations (@emdash-cms/cloudflare, @emdash-cms/workerd), the virtual-module declarations, and the wrapper generators; the only remaining cleanup is a stale type cast in astro/middleware.ts that still describes the pre-refactor sandbox-runner signature.


Findings

  • [suggestion] packages/core/src/astro/middleware.ts:203-215

    buildDependencies still casts sandboxModule.createSandboxRunner to the old inline signature that only knows db and mediaStorage. Since RuntimeDependencies.createSandboxRunner is now SandboxRunnerFactory, this narrower cast is stale and misleading — it obscures the fact that the runtime will call the factory with siteInfo too. Please switch the cast to SandboxRunnerFactory | null and add SandboxRunnerFactory to the existing plugins/sandbox/types.js import (replacing the now-unused SandboxRunner import).

    Replace the cast on lines 203–215 with:

    		createSandboxRunner: sandboxModule.createSandboxRunner as SandboxRunnerFactory | null,
    

    And update the import on line 50 to:

    import type { SandboxRunnerFactory } from "../plugins/sandbox/types.js";
    

@github-actions github-actions Bot added review/needs-rereview Author pushed changes since the last review size/L and removed size/M review/needs-review No maintainer or bot review yet labels Jul 17, 2026
@ascorbic ascorbic added the bot:review Trigger an emdashbot code review on this PR label Jul 17, 2026

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of #2081. The previous finding is fully resolved: packages/core/src/astro/middleware.ts now imports SandboxRunnerFactory and casts createSandboxRunner to SandboxRunnerFactory | null, removing the stale inline signature.

I re-read the diff, the full changed files, and the Cloudflare/workerd adapter implementations. The fix is correct and complete:

  • The new createSandboxRunnerOptions helper reuses createSiteInfo from trusted plugin contexts, so sandboxed plugins get the same normalized ctx.site/ctx.url() behavior.
  • siteInfo is threaded through all three runner creation sites: configured sandboxed plugins (loadSandboxedPlugins), marketplace plugins, and registry plugins (both via loadInstalledSandboxedPlugins).
  • Both platform runners (CloudflareSandboxRunner and WorkerdSandboxRunner) already consumed options.siteInfo, so passing it from the runtime is sufficient.
  • Regression coverage includes a unit test for the helper and an integration test verifying the runtime calls the factory with normalized site info.
  • The patch changeset is user-facing and appropriate; no UI strings, SQL, migrations, or authorization changes are involved.

No new issues found. LGTM.

@emdashbot emdashbot Bot removed the bot:review Trigger an emdashbot code review on this PR label Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core cla: signed review/needs-rereview Author pushed changes since the last review size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sandboxed plugin ctx.url() returns a relative path — siteInfo not forwarded to the sandbox runner

2 participants