0 && ob_end_clean() ) { continue; } header_remove( 'Location' ); header( 'Content-Type: text/html; charset=utf-8' ); header( 'X-Robots-Tag: noindex' ); header( 'HTTP/1.1 500 Internal Server Error', true, 500 ); // ESCAPED THROUGH `filter_var`, WHICH IS `htmlspecialchars( ENT_QUOTES )` UNDER // A NAME A TOOL RECOGNISES. This used to escape through a `$safe` closure, and // PHPCS cannot see through a closure held in a variable: it read every // interpolation below as raw and `WordPress.Security.EscapeOutput` reported // five ERRORs on output that was already escaped. Plugin Check runs that // sniff, so the false positive was a blocked submission form (wp-qw0s1). // // `esc_html()` is the usual answer and is the wrong one HERE, for the reason // three lines up in the docblock: this runs inside a shutdown handler after a // fatal, where a WordPress call is a second fatal and a blank page instead of // the evidence. `FILTER_SANITIZE_FULL_SPECIAL_CHARS` is documented as // equivalent to `htmlspecialchars()` with `ENT_QUOTES`, `filter_var` is on // WPCS's own escaping list, and neither reaches WordPress -- so the escaping // is the same escaping, and it is now visible to the sniff and to a reviewer. // A `phpcs:ignore` would have silenced this too and is a thing a reviewer has // to take on trust. printf( 'Pielyt could not start' . '
' . '

Pielyt could not be switched on.

' . '

PHP stopped inside this plugin. WordPress would otherwise have said only' . ' “it triggered a fatal error”, which names nothing, so here is what it was:

' . '
%s
' . '

Nothing has been activated and nothing on this site was changed.' . ' Sending the three lines above to Pielyt is enough to identify the cause.

' . '

PHP %s · WordPress %s

', filter_var( sprintf( "%s\nin %s\non line %d", (string) $error['message'], (string) $error['file'], (int) $error['line'] ), FILTER_SANITIZE_FULL_SPECIAL_CHARS ), filter_var( PHP_VERSION, FILTER_SANITIZE_FULL_SPECIAL_CHARS ), filter_var( isset( $GLOBALS['wp_version'] ) ? (string) $GLOBALS['wp_version'] : 'unknown', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ); } // ONLY DURING AN ACTIVATION, and `WP_SANDBOX_SCRAPING` is how that is known: // `plugin_sandbox_scrape()` defines it immediately before including the plugin // it is about to switch on. An ordinary request never has it, so an ordinary // request never registers this. Registered here, at the top, because the point // is to be in place before anything below can fail -- including the autoloader // require, whose failure modes `pielyt_blocker()` catches, and the ones it // cannot, such as a parse error in a file this build autoloads on a PHP version // older than the one it was built on. if ( defined( 'WP_SANDBOX_SCRAPING' ) && WP_SANDBOX_SCRAPING ) { register_shutdown_function( 'pielyt_report_activation_fatal' ); } /** * Requirements are hard, and the plugin refuses to activate without them, * naming the one that failed. * * `ext-sodium` is not negotiable: verification is * `sodium_crypto_sign_verify_detached`, and a plugin that activated without it * would be a plugin that could not check a signature. * * @return string The failing requirement, or an empty string. */ function pielyt_unmet_requirement(): string { if ( version_compare( PHP_VERSION, '7.4', '<' ) ) { /* translators: %s: the PHP version this site is running. */ return sprintf( __( 'PHP 7.4 or newer (this site runs %s)', 'pielyt' ), PHP_VERSION ); } if ( version_compare( (string) get_bloginfo( 'version' ), '6.9', '<' ) ) { /* translators: %s: the WordPress version this site is running. */ return sprintf( __( 'WordPress 6.9 or newer (this site runs %s)', 'pielyt' ), get_bloginfo( 'version' ) ); } if ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) ) { return __( 'the PHP sodium extension, which this plugin uses to check signatures', 'pielyt' ); } return ''; } /** * Everything that has to be true before this file may name one of its own * classes: the site's requirements, and then the plugin's own code. * * THE ONE STATEMENT HERE THAT COULD KILL A REQUEST used to be * `require_once __DIR__ . '/vendor/autoload_packages.php'`, written twice and * guarded neither time, and it can fail in three ways that have nothing to do * with each other: * * The file is not there. `plugin/vendor/` is gitignored — it is a build * output, staged into the zip by `scripts/build-plugin.mjs` — so a plugin * directory assembled from a source checkout has no vendor tree at all, and * neither does an extraction that stopped early. `require` on a missing file * is an uncatchable fatal, which is why the readability check below is a * check and not a courtesy. * * It is there and the bundled autoloader THROWS. * `Plugin_Locator::find_current_plugin()` raises RuntimeException the moment * `@is_file( /vendor/composer/jetpack_autoload_classmap.php )` is * false — which is what a directory this site's PHP cannot traverse looks * like from the inside. Reproduced on a real WordPress; the trace runs * through `plugin_sandbox_scrape()` and out of `activate_plugin()`. * * It is there, it returns, and it registers nothing. The autoloader stops * without a word when another plugin's copy has already claimed to be the * latest, and then the next line — `\Pielyt\WordPress\Upgrade::register()` — * is a class that does not exist. * * WHY IT IS WORTH CATCHING, given the plugin cannot run either way. Because of * what the site owner is shown. `activate_plugin()` emits * `Location: plugins.php?error=true` BEFORE it includes us, and a fatal leaves * that header standing, so WordPress renders "Plugin could not be activated * because it triggered a fatal error" — which names nothing, suggests nothing, * and is what wp-i8zoh was reported as. A `wp_die()` from the activation hook * answers 500 with the message in the body instead, and a browser renders a 500 * rather than following its Location; measured on WordPress 6.9.4, not assumed. * So the difference between fatalling and refusing here is the difference * between a dead end and a sentence naming the cause. * * @return string What stops this plugin running, or an empty string. */ function pielyt_blocker(): string { static $blocker = null; if ( null !== $blocker ) { return $blocker; } // The environment first, and it short-circuits: an unsupported PHP must be // reported as an unsupported PHP, not as whatever the autoloader does on it. $blocker = pielyt_unmet_requirement(); if ( '' !== $blocker ) { return $blocker; } $autoloader = __DIR__ . '/vendor/autoload_packages.php'; if ( ! is_readable( $autoloader ) ) { $blocker = __( 'its own vendor/ directory, which is not in this copy of the plugin', 'pielyt' ); return $blocker; } try { require_once $autoloader; } catch ( \Throwable $e ) { /* translators: %s: the message the bundled autoloader raised. */ $blocker = sprintf( __( 'to load its own classes, and the bundled autoloader refused: %s', 'pielyt' ), $e->getMessage() ); return $blocker; } // Asked of the autoloader rather than assumed of it -- but a backstop, not a // live failure mode, and the difference is worth stating because the obvious // reading of this branch is wrong. It LOOKS like it is for the shared- // autoloader race: another plugin's newer copy of the Jetpack autoloader // winning the latest-version handoff before this plugin was in // `active_plugins`. That race cannot strand a plugin whose classmap is // readable, and that was measured rather than reasoned (wp-i8zoh). // `Autoloader::init()` appends the current plugin to `$all_plugins` BEFORE the // guard runs, so a list that newly contains us differs from the cached one, // `should_stop_init()` calls `reset_autoloader()`, and the latest-version // global is nulled -- so the "somebody else already won" early return cannot // fire on the request that first sees us. The only route to it is a cached // list that already contains us, and whoever won THAT negotiation has already // read our manifest. Verified against Jetpack 16.1.3, whose bundled autoloader // is 5.0.22 to our 3.1.3: it wins the handoff, and it carries all forty of our // classes out of our own directory, on activation and on every request after. // (Measured before ours moved to 6.0.3, which is now the newer copy and the // one that wins that handoff; the mechanism above does not depend on who wins.) // So do not build a fallback autoloader for this. What is left here is the // case with no mechanism attached -- a require that returns having registered // nothing -- and it costs one class_exists() on a path already refusing. if ( ! class_exists( 'Pielyt\WordPress\Upgrade' ) ) { $blocker = __( 'to load its own classes, and the bundled autoloader registered none of them', 'pielyt' ); return $blocker; } $blocker = ''; return $blocker; } register_activation_hook( __FILE__, /** * @param bool $network_wide Core passes this; every build before wp-2s5b * declared no parameter and therefore ignored it. */ function ( $network_wide = false ) { // A NETWORK ACTIVATION IS REFUSED, AND A SINGLE-SITE ACTIVATION INSIDE A // NETWORK IS NOT (wp-2s5b). Everything this plugin stores is per-site and // WordPress runs this hook once, in one site's context, so a network // activation would leave every other site with no replay store. `wp_die()` // alone: core fires this hook before it records the network activation, so // there is nothing to undo, and `deactivate_plugins()` here would switch off // this site's own per-site activation. `is_multisite()` beside the flag, // because on a single site core passes the flag through and activates // per-site anyway. Advisory: a `$silent` activation skips this hook, which // is what `pielyt_network_activation_notice()` covers. if ( $network_wide && is_multisite() ) { wp_die( esc_html__( 'Pielyt could not be activated for the whole network. It is a single-site plugin: it keeps its settings, its agent roles and its records in each site\'s own tables, so a network activation would set up one site of this network and leave every other one unable to run anything — and removing Pielyt afterwards would leave its rows behind on all of them. Activate Pielyt from the Plugins screen of each site you want to connect instead; that works normally on a network. If you selected other plugins alongside Pielyt, the ones after it in the list were not switched on.', 'pielyt' ) ); } $missing = pielyt_blocker(); if ( '' !== $missing ) { deactivate_plugins( plugin_basename( __FILE__ ) ); /* translators: %s: the requirement this site does not meet. */ wp_die( esc_html( sprintf( __( 'Pielyt could not be switched on. It needs %s.', 'pielyt' ), $missing ) ) ); } // THE STORE FIRST, AND CHECKED, AND THAT ORDER IS THE POINT (wp-xhfau). // A fresh install runs no upgrade step, so `maybeUpgrade()` is what // checks this table: it stamps `CURRENT` only once the table exists, and // an unstamped site refuses every grant (`JtiStore::consume()`). // // A FAILURE HERE DOES NOT REFUSE THE ACTIVATION, and does not need a flag // of its own either. `wp_die()` on a database that is briefly unhappy // would leave the owner with a plugin they cannot switch on and no idea // why. What the failure must not do is pass SILENTLY, and it no longer // can: `maybeUpgrade()` below will not stamp a fresh install as being at // this build's schema unless the store is there, and the settings screen // asks the database itself on every render, so the notice appears without // a stored boolean and goes when the table does arrive. \Pielyt\WordPress\JtiStore::install(); // The notice must not outlive its own remedy (wp-6rltv). \Pielyt\WordPress\Fault::unlatchStorageRefusal(); // Then the stamp, so a first activation is recorded at the current // schema, and a site that has been running the plugin since before there // was a schema version walks the steps here rather than on whichever // request happens to arrive next. \Pielyt\WordPress\Upgrade::maybeUpgrade(); \Pielyt\WordPress\SyntheticUsers::registerRole(); if ( ! wp_next_scheduled( 'pielyt_purge_jti' ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', 'pielyt_purge_jti' ); } } ); register_deactivation_hook( __FILE__, function () { // Deactivation is reversible and leaves no orphans: the pairing survives // so reactivating does not force the owner through the handshake again, // and the cron event goes away because a deactivated plugin's cron event // is an orphan by definition. The literal is held to // `JtiStore::PURGE_HOOK` by `CronHookNamesTest`; it is a literal because // this callback can run on a site whose requirements check returned // early, before the autoloader was required. wp_clear_scheduled_hook( 'pielyt_purge_jti' ); } ); if ( '' !== pielyt_blocker() ) { add_action( 'admin_notices', function () { // Only someone who can deactivate the plugin can act on this. if ( ! current_user_can( 'activate_plugins' ) ) { return; } printf( '

%s

', esc_html( /* translators: %s: the requirement this site does not meet. */ sprintf( __( 'Pielyt cannot run on this site. It needs %s.', 'pielyt' ), pielyt_blocker() ) ) ); } ); return; } // On `plugins_loaded` and not on activation alone, because a site can arrive at // new plugin code without ever running an activation hook — an unzip over the // directory, a deploy, `wp plugin update`. The check itself is one autoloaded // option read on every request that finds nothing to do; see `Upgrade`. \Pielyt\WordPress\Upgrade::register(); add_action( 'rest_api_init', array( \Pielyt\WordPress\Routes::class, 'register' ) ); add_action( 'rest_api_init', array( \Pielyt\WordPress\Authors::class, 'register' ) ); add_action( 'rest_api_init', array( \Pielyt\WordPress\HookOwners::class, 'register' ) ); // Pairing v2 section 4.2: the callback, Finish and Cancel are admin-post actions, // which WordPress runs only for a logged-in user. \Pielyt\WordPress\Pairing::register(); // Section 6.3: synthetic users can never authenticate, from the first request. \Pielyt\WordPress\SyntheticUsers::registerHooks(); // Section 6.3: the permanent capability filter, at `plugins_loaded` and as late // on it as an action can run, so that a same-priority `user_has_cap` callback a // plugin registers while loading still runs before the one that replaces. add_action( 'plugins_loaded', array( \Pielyt\WordPress\Capabilities::class, 'register' ), PHP_INT_MAX ); add_action( 'pielyt_purge_jti', array( \Pielyt\WordPress\JtiStore::class, 'purge' ) ); \Pielyt\WordPress\Admin::register(); // The activity screen, section 6.1's site-to-gateway direction from this side. // Registered beside `Admin` rather than inside it because it is a second screen // with its own menu entry, and a screen that reaches the network on render has no // business being a private method of the one that does not. \Pielyt\WordPress\Activity::register(); /** * The network refusal, one step over: ALREADY network-activated. * * The activation hook guards the moment somebody presses Network Activate. It * cannot reach a site that pressed it before this build existed, because an * update runs no activation hook at all — the same gap `Upgrade` exists for. * Without this, the population the refusal was written for is the one * population it never reaches. * * WHAT THEY SEE OTHERWISE: every site of the network except one refusing every * grant with `jti_store_unavailable`, and nothing anywhere saying why. See the * activation hook for how that state is reached. * * TO THE CAPABILITY THAT CAN ACT ON IT: the whole instruction here is "deactivate this for the network and activate it per * site", and only a super admin can do either. On both hooks, because * `admin_notices` does not fire in Network Admin and `network_admin_notices` * fires nowhere else — and the super admin reading the Plugins screen where * they would fix it is in the second. * * `function_exists()` because `is_plugin_active_for_network()` is wp-admin's, * and a notice callback that fatals is a white screen rather than a warning. */ function pielyt_network_activation_notice(): void { if ( ! is_multisite() || ! current_user_can( 'manage_network_plugins' ) ) { return; } if ( ! function_exists( 'is_plugin_active_for_network' ) || ! is_plugin_active_for_network( plugin_basename( __FILE__ ) ) ) { return; } printf( '

%s

', esc_html__( 'Pielyt is activated for this whole network, and it is a single-site plugin. It keeps its settings, its agent roles and its records in each site\'s own tables, so only one site of this network is set up and the rest cannot run anything — and removing Pielyt would leave its rows behind on all of them. Deactivate Pielyt for the network, then activate it from the Plugins screen of each site you want to connect.', 'pielyt' ) ); } add_action( 'admin_notices', 'pielyt_network_activation_notice' ); add_action( 'network_admin_notices', 'pielyt_network_activation_notice' ); /** * RESERVES THE BARE NAME, NOT THE PREFIX, and the difference is worth stating * because this comment used to claim the prefix. Core's check is * `in_array( strtolower( $user_login ), $illegal )` — an equality test — so this * reserves the login `pielyt` and nothing else. A site with open registration * can still have `pielyt-agent-...` taken by a stranger. * * What that costs is bounded and is not an escalation: the squatter carries no * identity tag and no trust-row entry names them, so every grant naming them is * refused, and `SyntheticUsers::identityFor()` refuses to adopt the login rather * than handing an agent identity to somebody's account. It is a denial of * provisioning, which is why the filter stays rather than being deleted as * useless. Reserving the prefix properly needs a `validate_username` filter that * `SyntheticUsers::identityFor()` is exempt from — see the bead, not this file. */ add_filter( 'illegal_user_logins', function ( $logins ) { $logins = is_array( $logins ) ? $logins : array(); $logins[] = 'pielyt'; return $logins; } ); /** Agent users get no mail. Their address is unroutable by construction. */ add_filter( 'send_password_change_email', function ( $send, $user ) { if ( ! is_array( $user ) || ! isset( $user['user_login'] ) ) { return $send; } $login = (string) $user['user_login']; foreach ( \Pielyt\WordPress\SyntheticUsers::LOGIN_PREFIXES as $prefix ) { if ( 0 === strpos( $login, $prefix ) ) { return false; } } return $send; }, 10, 2 );