Skip to content

wpcomsh: add /wp/v2/streaming-export REST endpoint#47838

Draft
adamziel wants to merge 31 commits intotrunkfrom
adamziel/wpcomsh-pr-1927
Draft

wpcomsh: add /wp/v2/streaming-export REST endpoint#47838
adamziel wants to merge 31 commits intotrunkfrom
adamziel/wpcomsh-pr-1927

Conversation

@adamziel
Copy link
Copy Markdown
Contributor

@adamziel adamziel commented Mar 30, 2026

Summary

Adds a /wp/v2/streaming-export REST API endpoint to wpcomsh. This is the Jetpack monorepo counterpart of wpcomsh#1927.

Instead of intercepting query parameters and manually validating OAuth tokens, this registers a standard WordPress REST route. Authorization uses is_super_admin(), so only network administrators (mapped from the WPCOM connected user by Jetpack's connection layer) can access the endpoint.

The endpoint returns {"authorization": "successful"} on success and a 403 otherwise.

The branch now also bumps wp-php-toolkit/streaming-exporter and wp-php-toolkit/streaming-importer to v0.1.34 so the wpcomsh endpoint picks up the Atomic duplicate-path fix in the exporter. That release suppresses duplicate /wordpress follow-up roots and recursive /srv/htdocs/srv/htdocs/... re-entry paths at the source.

Test plan

  • Hit /wp-json/wp/v2/streaming-export/ via the WPCOM REST API proxy as a connected super admin — should return 200
  • Hit it as a regular admin or unauthenticated — should return 403
  • Hit it directly on the site without going through WPCOM — should return 403 for non-super-admins

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 30, 2026

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack or WordPress.com Site Helper), and enable the adamziel/wpcomsh-pr-1927 branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack adamziel/wpcomsh-pr-1927

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 30, 2026

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • 🔴 Add testing instructions.
  • 🔴 Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


🔴 Action required: Please include detailed testing steps, explaining how to test your change, like so:

## Testing instructions:

* Go to '..'
*

🔴 Action required: We would recommend that you add a section to the PR description to specify whether this PR includes any changes to data or privacy, like so:

## Does this pull request change what data or activity we track or use?

My PR adds *x* and *y*.

Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

No scheduled milestone found for this plugin.

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.


Wpcomsh plugin:

  • Next scheduled release: Atomic deploys happen twice daily on weekdays (p9o2xV-2EN-p2)

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Mar 30, 2026
@jp-launch-control
Copy link
Copy Markdown

jp-launch-control bot commented Mar 30, 2026

Code Coverage Summary

Coverage changed in 2 files.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/modules/module-headings.php 219/1067 (20.52%) 0.03% 18 💔
projects/plugins/wpcomsh/wpcomsh.php 112/361 (31.02%) -0.09% 1 ❤️‍🩹

2 files are newly checked for coverage.

File Coverage
projects/plugins/wpcomsh/feature-plugins/site-export-api.php 40/76 (52.63%) 💚
projects/plugins/jetpack/modules/streaming-export.php 0/0 (—%) 🤷

Full summary · PHP report · JS report

Coverage check overridden by Covered by non-unit tests Use to ignore the Code coverage requirement check when E2Es or other non-unit tests cover the code .

@adamziel adamziel self-assigned this Mar 30, 2026
@adamziel adamziel marked this pull request as draft March 30, 2026 19:38
@adamziel adamziel changed the title wpcomsh: add site-export-api OAuth validation endpoint wpcomsh: add /wp/v2/streaming-export REST endpoint Mar 30, 2026
@github-actions github-actions bot added [Status] In Progress [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ labels Mar 30, 2026
@adamziel adamziel force-pushed the adamziel/wpcomsh-pr-1927 branch 2 times, most recently from 5546260 to 37efb90 Compare March 31, 2026 15:47
@adamziel adamziel added the Covered by non-unit tests Use to ignore the Code coverage requirement check when E2Es or other non-unit tests cover the code label Apr 4, 2026
adamziel added 14 commits April 9, 2026 11:13
Adds a feature plugin that responds to `?site-export-api` in the URL.
When a valid WordPress.com OAuth Bearer token is provided, it returns
a JSON authorization confirmation. This is the authentication handshake
for an upcoming site export API integration.

Reproduces Automattic/wpcomsh#1927 in the Jetpack monorepo.
…EST route

Instead of intercepting ?site-export-api requests and manually validating
OAuth tokens against the WPCOM /me endpoint, register a proper WordPress
REST API route at /wp/v2/streaming-export. Authorization uses is_super_admin()
so only network administrators (mapped from the WPCOM connected user) can
access the endpoint.
The /wp/v2/streaming-export REST route now only registers when the
"streaming-export" Jetpack module is active. Admins can toggle it
from wp-admin → Jetpack → Modules. Off by default.
The /wp/v2/streaming-export route now serves the full streaming export
API from the streaming-exporter package. Super admins can use it to
run preflight checks, index files, fetch files, index databases, and
stream SQL chunks – everything a migration client needs to pull a
complete site copy.
Replace the hand-rolled endpoint dispatch with the library's own
_site_export_handle_api_request(), which handles config parsing,
cursor decoding, error handling, and endpoint routing. The wpcomsh
integration supplies a custom authenticate callback (is_super_admin)
so requests through the WPCOM REST API proxy work without HMAC.

The ?site-export-api query parameter triggers the export handler on
parse_request, gated behind the streaming-export Jetpack module.
The rotate-secret REST route remains for secret management.
- Pin streaming-site-migration to ^0.1.19 (CI rejects dev-trunk)
- Add production-include lines in .gitattributes for all transitive
  wp-php-toolkit/* dependencies
- Fix Jetpack changelog type: "added" is not valid, use "other"
- Add JSON_FORCE_OBJECT flag to wp_send_json() call (PHPCS requirement)
The streaming-importer package bundles its own WP_Error and other
WordPress class stubs for standalone usage. When Phan sees both
these stubs and the canonical wordpress-stubs, it reports 216
PhanRedefinedClassReference errors. Excluding the files from
analysis fixes the conflict.
…porter packages

Instead of depending on the monolith wp-php-toolkit/streaming-site-migration
package (hosted on a personal fork), depend directly on the released
wp-php-toolkit/streaming-exporter and wp-php-toolkit/streaming-importer
packages from packagist.

The request-handling and HMAC auth logic from streaming-site-migration's
wordpress-plugin/lib.php is inlined into site-export-api.php so the
monolith is no longer needed.
Verifies that the ?site-export-api handler and the rotate-secret REST
route only activate when the streaming-export Jetpack module is enabled.
Also covers permission enforcement (super-admin only) and secret
rotation/storage.
PHPCS: sanitize $_SERVER reads with wp_unslash/sanitize_text_field,
add phpcs:disable for $_FILES nonce verification (auth is via HMAC),
replace short ternary, add @throws tag, fix single-line array.

Tests: reset REST server between tests so route registration works
correctly, skip module-inactive tests on WP.com where modules are
always active, guard against missing array keys.
PHPCS: add missing @throws InvalidArgumentException tag.

Tests: skip route-dependent tests when Jetpack class is not loaded
(WP Cloud test site doesn't load the Jetpack megaclass).
Add Jetpack.Functions.JsonEncodeFlags.Missing and WordPress.PHP.IniSet.Risky
to phpcs:ignore comments. These json_encode calls are in error/exception
handlers where adding flags would change the error response format, and
ini_set is needed to prevent HTML error output from corrupting JSON responses.
adamziel added 15 commits April 9, 2026 11:13
The Gridicon bulk import resolves to a module namespace object in
Vitest, causing "Element type is invalid" in the annotation popover
close button. Switch to the individual icon import that the rest of
the monorepo uses.

Remove the stale SiteExportApiTest.php phan baseline entry — the
underlying issues were already fixed in earlier commits.
… functions

Cover secret management, HMAC verification, header helpers, permission
callbacks, and the rotate-secret callback. Mark the three exit()-calling
functions with @codeCoverageIgnore since they cannot be unit-tested
without process isolation.
Add assertNotNull() before assertStringContainsString() in HMAC
verification tests since _site_export_verify_hmac() returns ?string
and phan requires a non-nullable argument.
…nches

Add test for content hash mismatch detection in HMAC verification.
Mark the $_FILES upload branch and getallheaders() inner loop with
codeCoverageIgnore since they require real PHP upload state and HTTP
headers respectively, neither of which can be simulated in unit tests.
Replace the inlined HMAC verification code with the new
Site_Export_HMAC_Server class that ships in streaming-exporter v0.1.23.
This removes ~100 lines of duplicated auth logic (verify_hmac,
get_header) and the corresponding test helpers, replacing them with
direct calls to the package's verify() method.
Replace the ~170 lines of inlined request handling (error handlers,
config parsing, cursor decoding, budget creation, endpoint switch) with
Site_Export_HTTP_Server::handle_request() from streaming-exporter
v0.1.24. The wpcomsh integration now only handles the concerns specific
to the WordPress environment: CORS headers, HMAC authentication,
SECRET_KEY bridging, and loading the exporter runtime.
Separate the $server assignment from the $headers group with a blank
line so PHPCS doesn't try to align them with the longer
$headers['X-Auth-Timestamp'] key.
Remove the Access-Control-Allow-Credentials header since HMAC auth
uses custom headers, not cookies, and a wildcard origin with credentials
is invalid per the Fetch spec. Also update the "no secret configured"
error message to point to the actual rotate-secret REST endpoint
instead of a non-existent admin page.
@adamziel adamziel force-pushed the adamziel/wpcomsh-pr-1927 branch from 68aae0f to fce8203 Compare April 9, 2026 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Covered by non-unit tests Use to ignore the Code coverage requirement check when E2Es or other non-unit tests cover the code [JS Package] Charts [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Plugin] Wpcomsh RNA [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant