gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Source Site URLs Injected into Destination Editor via wp_sync_data After Data Sync (CRDT Collaborative Editing State Not Cleared)

Open gregswaney opened this issue 5 months ago • 4 comments

Description

After performing a data sync between environments (e.g. production → staging or staging → production), posts that were previously opened in Gutenberg's collaborative editing mode can have source site URLs injected into the block editor on the destination site.

These URLs bypass search-replace entirely because they are stored in binary/escaped format inside wp_sync_data database entries. When the affected post is subsequently opened and saved on the destination site, the old domain URLs are written into production content — causing 404 errors and broken pages.

Expected Behavior The destination site's content should contain only the post content as it was at the time of sync, with all URLs correctly replaced to reflect the destination domain. The CRDT collaborative editing state from the source environment should have no bearing on the destination site's content.

Actual Behavior The Gutenberg editor surfaces the wp_sync_data entry as a phantom autosave revision that appears newer than any real revision. This phantom revision contains hardcoded URLs from the source domain verbatim. When the post is saved, those source domain URLs are written into the destination site's content.

Source domain URLs are not caught by search-replace during the sync because they are stored in binary/escaped format within wp_sync_data meta — invisible to standard string-based search-replace routines.

Looking for potential suggested fixes from the Gutenberg side of things. While we're able to flush the tables before migration, I wouldn't just want to wipe tables by default on our end on a hunch.

Impact Any user who: -Runs the standalone Gutenberg plugin -Has collaboratively edited a post (or opened the same post in multiple sessions) -Has performed a data sync or migration between environments -Subsequently opens and saves the synced post in Gutenberg on the destination

This can silently introduce broken URLs into production content with no search-replace trace, making it difficult to detect and remediate.

Step-by-step reproduction instructions

-Install the standalone Gutenberg plugin (v23.3.2 confirmed affected) -Open a post in the Gutenberg block editor on the source site -Open the same post in a second browser session as a different user to trigger Gutenberg's CRDT collaborative editing state — confirmed active when the overlapping circles icon appears in the editor toolbar -Perform a data sync from the source environment to a destination/staging environment -On the destination site, open the synced post in the Gutenberg block editor -Observe: Gutenberg fetches the revision list and surfaces the wp_sync_data entry as a newer "autosave" phantom revision -Save the post

Screenshots, screen recording, code snippet

[email protected]:~/htdocs$ wp post list --post_type=wp_sync_storage --format=table +----+--------------+----------------------------------+---------------------+-------------+ | ID | post_title | post_name | post_date | post_status | +----+--------------+----------------------------------+---------------------+-------------+ | 13 | Sync Storage | 74e08c504aa8a88276be0c8927c3128a | 2026-06-11 13:59:47 | publish | | 11 | Sync Storage | 469b1b9d5a562c1488248bad3cc3fc25 | 2026-06-11 13:59:35 | publish | | 10 | Sync Storage | 10848375d272b7f31cc46e5889bbee53 | 2026-06-11 13:59:31 | publish | +----+--------------+----------------------------------+---------------------+-------------+

Environment info

Environment Gutenberg Plugin Version: 23.3.2 WordPress Version: 7.0 PHP Version: 8.5.7 Theme: Twenty Twenty-Five (block theme) Browser: Reproduced across multiple browsers

Please confirm that you have searched existing issues in the repo.

  • [x] Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • [x] Yes

Please confirm which theme type you used for testing.

  • [x] Block
  • [ ] Classic
  • [ ] Hybrid (e.g. classic with theme.json)
  • [ ] Not sure

gregswaney avatar Jun 11 '26 15:06 gregswaney

Hi @gregswaney,

I’ve been looking into this behavior and wanted to share what I think might be happening, along with a potential approach.

From what I can tell, the collaborative editing state is stored in two places as binary-encoded Yjs updates (encoded in base64):

  1. The wp_sync_update_data post meta on wp_sync_storage posts.
  2. The _crdt_document post meta on the actual post.

Because this data is stored in base64-encoded strings, standard wp search-replace during database migration doesn't detect or replace the URLs inside them. When the destination site loads, it pulls this stale CRDT state and surfaces the source URLs.

I think a potential fix from the Gutenberg side could be:

  • When a room's storage is first created, we store the originating site_url() in a separate meta key (e.g. _wp_sync_origin_url).
  • When updates are subsequently requested on the server side, we check if the stored origin matches the current site_url().
  • If there is a mismatch (indicating the database was migrated), the server would discard/purge the stale collaborative/sync state for that post so the editor can safely start fresh from the canonical post content.

This would lazy-purge the data when the post is opened on the new site, so we wouldn't need to manually wipe tables or hooks during the migration process itself.

I’m fairly new to this part of the codebase, so please let me know if this approach makes sense, or if there is a better way to handle this invalidation. I'd be happy to help put together a PR if the direction looks good!

vedant-ere avatar Jun 19 '26 13:06 vedant-ere

Thanks for the follow up! It sounds like you're on the right track with the proposed check and purge system, though I'm not a developer myself. One of our customers actually discovered this issue and did all the heavy lifting to track down the cause. From my end, I just confirmed the reported issue, reproduced it, and reported it here.

Regarding the proposed fix, I just wanted to add some things to consider:

Retroactive handling: Posts that already have wp_sync_update_data or _crdt_document entries but no _wp_sync_origin_url (i.e., everything created before this fix ships) need a defined fallback. Do they get treated as "safe" (no purge) or "unknown origin, purge on mismatch-detection failure"? If the former, existing migrated sites stay broken so you'll want to take current sites into consideration.

Both storage locations: You confirmed the two places the CRDT state lives: wp_sync_update_data (on wp_sync_storage posts) and _crdt_document (on the actual post). The proposal needs to explicitly confirm both are purged on an origin mismatch, not just one.

Timing / client-side race: The bug surfaces because the client fetches revision history and the phantom CRDT data is served as a newer autosave before any save action occurs. The origin check needs to fire server-side on the revision/autosave fetch endpoint, not just on save. If it only fires on write, there's still a window where the editor loads stale data and a distracted user could save it.

Edge cases that look like migrations but aren't: Domain mapping, multisite, local dev with a custom URL, or even a site URL change via Settings → General would all trigger a site_url() mismatch. Worth defining: should the purge be irreversible, or should the old CRDT state be flagged/quarantined so it can be recovered if the "mismatch" was a false positive?


Hope this helps!

gregswaney avatar Jun 22 '26 15:06 gregswaney

Thanks for the detailed breakdown @gregswaney! I've been working on a fix and wanted to check in before opening a PR.

Quick summary of what I've covered so far:

  • Retroactive handling: Data without a stored origin URL (created before this fix) is treated as stale and purged — so existing migrated sites don't stay broken.
  • Both storage locations: Both wp_sync_update_data (on wp_sync_storage posts) and _crdt_document (on the post itself) are purged on mismatch.
  • Timing: The check fires on the server-side read path, so stale data is never delivered to the client in the first place.

One open question: You mentioned that a site_url() mismatch could also be triggered by non-migration scenarios like changing the URL via Settings → General, domain mapping, or local dev setups. In my current implementation the purge is irreversible (hard delete).

Would a hard delete be acceptable here, given that the canonical post content in post_content is always preserved? Or would a softer quarantine approach would be preferrable where the stale state is moved to a separate meta key so it can be recovered if the mismatch was a false positive?

Happy to go either way just didn't want to finalize the PR without getting input on this.

vedant-ere avatar Jun 25 '26 13:06 vedant-ere

The hard delete is acceptable in my view, and here's the reasoning: the CRDT state in wp_sync_data / _crdt_document is fundamentally ephemeral. It's a collaborative editing session artifact, not canonical content. The source of truth has always been post_content, and that's preserved. If the CRDT state is stale or from the wrong environment, there's no meaningful "recovery" scenario where you'd want it back.

The false positive concern is real but I'd argue it's low risk in practice. A legitimate URL change via Settings → General is a deliberate, admin-level action, and at that point any in-flight collaborative editing sessions should be considered invalidated anyway. You'd want editors to reload and start fresh. Domain mapping and local dev are similar: if the domain has changed, the old CRDT state is useless or actively harmful.

A quarantine approach adds complexity without much benefit. You're storing data you'd almost certainly never restore, and it creates an ongoing maintenance surface (when does quarantined data get cleaned up?).

I'd consider going with the hard delete, but maybe log a warning (e.g. to error_log or a doing_it_wrong() call) when a purge fires, so developers in local/staging setups can see why their collaborative state was cleared. That gives the observability of a soft approach without the storage overhead.

Thanks for checking in before the PR!

gregswaney avatar Jun 25 '26 14:06 gregswaney