Skip to content

fix(redirects): reject enabling a redirect that closes a loop - #3692

Merged
ascorbic merged 1 commit into
mainfrom
fix/redirect-enable-loop
Oct 1, 2026
Merged

ascorbic merged 1 commit into
mainfrom
fix/redirect-enable-loop

Conversation

@ascorbic

@ascorbic ascorbic commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Enabling a disabled redirect can close a redirect loop. The redirect handler only ran the loop check when the source or destination changed, and the database guards from migration 081 fire on the same condition, so a plain enabled: false → true update skipped both.

  • The update handler now runs the loop check whenever a rule becomes enabled.
  • Migration 090 adds a trigger (SQLite and PostgreSQL) that rejects the same write from any writer: the condition is enabled 0 → 1 with a non-empty destination. The trigger excludes the row itself and takes the same advisory lock as the 081 guards.
  • Loops that already exist can still be disabled or edited.

The transfer loop test now drops the new trigger on its origin database, so it can model a site whose loop predates the guard.

This is the first of three stacked PRs from work on #2288. The others are #3693 (stable pattern precedence) and #3694 (redirect artifact cache), stacked on this branch.

Closes # (no issue; found while working on #2288)

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)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). n/a: no admin UI changes.
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to an approved Discussion: n/a, this is a bug fix.
  • I have included screenshots below if this PR changes the UI. n/a: no UI changes.

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 5.5 (Claude Code)

Screenshots / test output

Not applicable (no UI change).

  • Tests: the core suite passes on SQLite with the whole stack applied (747 files). The redirect and database integration tests also pass on PostgreSQL.
  • Regression tests:
    • A handler test checks that enabling a rule that closes a loop is rejected.
    • redirect-enable-loop-guard-migration.test.ts checks that the 090 trigger rejects the same write made directly against the database.

🤖 Generated with Claude Code

@changeset-bot

changeset-bot Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d8b7764

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

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

@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 the right fix for the stated bug: enabling a disabled redirect can close a redirect loop, and neither the handler nor the 081 database guards checked that path. The PR correctly tightens the handler’s loop-check condition and adds a defensive migration-level trigger (090) on both PostgreSQL and SQLite that rejects the same write from any writer.

What I checked:

  • Handler logic (packages/core/src/api/handlers/redirects.ts): the loop check now fires when a redirect becomes enabled, even if source/destination are unchanged. The condition (willBeEnabled && (source changed || destination changed || !existing.enabled)) correctly captures "the edge becomes active", covers terminal-status skips, and preserves the ability to disable redirects.
  • Migration 090: the trigger is idempotent (CREATE OR REPLACE FUNCTION + DROP TRIGGER IF EXISTS on Postgres, CREATE TRIGGER IF NOT EXISTS on SQLite), uses the same advisory lock as 081, excludes the row being updated, and only fires on a real enabled 0 → 1 transition with a non-empty destination. The SELECT RAISE(ABORT, ...) WHERE EXISTS(...) idiom matches the established D1-safe pattern from 081.
  • Migration registration: 090 is imported and registered in runner.ts immediately after 089, with the expected naming.
  • Tests: the handler test now asserts that enabling a loop-closer is rejected and leaves the row disabled; the new migration test verifies the 090 trigger rejects a direct SQL enable; the transfer test drops the new trigger on the origin to model a pre-090 loop and keeps testing the large-package import path. All tests look like they exercise real behavior, not implementation details.
  • Changeset: the entry leads with the user-facing behavior, names the affected surfaces (admin, API, plugins), notes the protection for writers that bypass the API, and reassures that existing loops remain editable/disablable. It meets the changeset README standard for a patch entry.
  • AGENTS.md conventions: no logged-out hot path is affected, no user-facing strings changed, no migrations are edited in-place, SQL is not interpolated, and comments explain intent without narrator/justification prose.

I found no logic bugs, regressions, or convention violations. The implementation is clean and the regression coverage is appropriate.

@github-actions github-actions Bot added the query-count changed PR diff modifies query-count snapshot files label Sep 30, 2026
@pkg-pr-new

pkg-pr-new Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/auth-atproto

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

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

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

emdash

npm i https://pkg.pr.new/emdash@3692

create-emdash

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

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

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

@emdash-cms/plugin-cli

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

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@3692

@emdash-cms/plugin-types

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

@emdash-cms/registry-client

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

@emdash-cms/registry-lexicons

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

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@3692

@emdash-cms/registry-moderation

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

@emdash-cms/registry-verification

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

@emdash-cms/sandbox-workerd

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-field-kit

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: d8b7764

@github-actions github-actions Bot added review/approved Approved; no new commits since area/core size/M labels Sep 30, 2026
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 30, 2026 •

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 ea78e47 Sep 30 2026, 03:30 PM

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://fix-redirect-enable-loop.try.emdashcms.com, https://fix-redirect-enable-loop-emdash-playground.emdash-cms.workers.dev (commit d8b7764)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://8964640f.try.emdashcms.com, https://8964640f-emdash-playground.emdash-cms.workers.dev d8b7764 2026-10-01T10:40:41.758Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://5add2ea1.try.emdashcms.com, https://5add2ea1-emdash-playground.emdash-cms.workers.dev ea78e47 2026-09-30T15:33:35.935Z Visit the dashboard ↗
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://1146a8af.try.emdashcms.com, https://1146a8af-emdash-playground.emdash-cms.workers.dev 468159d 2026-09-30T13:59:29.052Z Visit the dashboard ↗

github-actions Bot pushed a commit that referenced this pull request Sep 30, 2026
@ascorbic
ascorbic force-pushed the fix/redirect-enable-loop branch from 468159d to ea78e47 Compare September 30, 2026 15:07
@github-actions github-actions Bot added review/needs-rereview New commits since the last review overlap and removed review/approved Approved; no new commits since query-count changed PR diff modifies query-count snapshot files labels Sep 30, 2026
@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.

Enabling a disabled redirect skipped the loop check in the handler, and
the 081 database guards only fire when source or destination changes, so
a plain enable could close a loop. The handler now checks loops whenever
a rule becomes enabled, and migration 090 adds a trigger on both dialects
that rejects the same write from any writer.

The transfer loop test now removes the new trigger on its origin to model
a site whose loop predates the guard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ascorbic
ascorbic force-pushed the fix/redirect-enable-loop branch from ea78e47 to d8b7764 Compare October 1, 2026 10:38
@ascorbic
ascorbic merged commit 2a0cb93 into main Oct 1, 2026
41 checks passed
@ascorbic
ascorbic deleted the fix/redirect-enable-loop branch October 1, 2026 15:24
@emdashbot emdashbot Bot mentioned this pull request Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core review/needs-rereview New commits since the last review size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant