Vai al contenuto

La REST API di WordPress elenca pubblicamente i plugin che usa

Avviso Intero dominio Sicurezza

Istruzioni generali. Apri questo articolo dal tuo report PageCheck per vederlo compilato con i plugin che l’indice del tuo sito nomina e con quali di essi usano i browser dei tuoi visitatori.

Che cos’è

La REST API di WordPress permette agli script di leggere e modificare il sito tramite indirizzi sotto /wp-json/: la usano l’editor a blocchi, le app e molti plugin. Il suo indice elenca ogni «namespace» che il sito registra – un gruppo di indirizzi – e WordPress stesso ne possiede solo una manciata. Ogni altro namespace appartiene a un plugin.

Cosa non va

L’indice risponde a chiunque lo richieda, e i suoi namespace nominano i plugin del sito. Con quell’elenco un aggressore non tira a indovinare: la versione di ogni plugin viene poi letta dai suoi stessi file e confrontata con gli elenchi pubblici di falle di sicurezza note. La pagina in sé mostra solo i plugin che vi caricano file; l’indice nomina anche quelli che non lo fanno – plugin per SEO, moduli, backup, sicurezza e amministrazione.

Come correggere

L’API resta; solo la sua mappa e le parti che i visitatori non chiamano mai vengono nascoste a chi non è connesso come membro dello staff. Gli indirizzi nascosti devono rispondere «non trovato» (404), non «non autorizzato» (401): un 401 conferma comunque che il plugin c’è.

1. Scopri quali namespace chiamano le tue pagine. Apri il sito in una finestra di navigazione privata, apri gli strumenti per sviluppatori del browser (F12) → Rete, filtra per wp-json e usa il sito come un visitatore: invia un modulo, aggiungi qualcosa al carrello, apri il checkout, cerca. Ogni richiesta elencata nomina un namespace da mantenere.

I più comuni:

  • Contact Form 7 invia i suoi moduli tramite la REST API.
  • Carrello, checkout e mini carrello a blocchi di WooCommerce leggono la Store API (wc/store).
  • I contatori di visite come WP Statistics e WordPress Popular Posts registrano le visite tramite essa.
  • I plugin per community, donazioni e consenso (BuddyPress, GiveWP, Complianz) la usano nelle pagine.

2. Nascondi il resto ai visitatori. Salva questo come wp-content/mu-plugins/hide-rest-map.php (la cartella potrebbe dover essere creata; i file lì dentro sono sempre attivi). Metti nell’elenco i namespace del passo 1: ciascuno viene mantenuto con tutto ciò che contiene.

wp-content/mu-plugins/hide-rest-map.php

<?php
/* Hide the REST API's map from visitors who are not logged in. */
add_filter( 'rest_endpoints', function ( $endpoints ) {
	if ( current_user_can( 'edit_posts' ) ) {
		return $endpoints; // staff keep everything
	}
	// Namespaces your pages call. Everything else answers 404 to visitors.
	$keep = array( 'wc/store', 'contact-form-7/v1' );
	foreach ( array_keys( $endpoints ) as $route ) {
		$kept = false;
		foreach ( $keep as $namespace ) {
			if ( 0 === strpos( $route, '/' . $namespace . '/' ) ) {
				$kept = true;
				break;
			}
		}
		if ( ! $kept ) {
			unset( $endpoints[ $route ] );
		}
	}
	return $endpoints;
}, 5 );

// No pointer to the API in the pages' head and headers.
remove_action( 'wp_head', 'rest_output_link_wp_head' );
remove_action( 'template_redirect', 'rest_output_link_header', 11 );

Chi può modificare i contenuti mantiene l’intera API, quindi l’editor a blocchi, le schermate di amministrazione di WooCommerce e degli altri plugin e Salute del sito funzionano come prima. I plugin di sicurezza offrono lo stesso come impostazione («Disattiva la REST API per i visitatori» o simile); assicurati che accetti un elenco di eccezioni e risponda 404, non 401.

Sconsigliato: il frammento di codice diffuso che nega l’intera API ai visitatori tramite rest_authentication_errors. Rompe i moduli e il carrello a blocchi, e risponde comunque 401 per ogni indirizzo, il che conferma che ogni plugin esiste.

3. Verifica. Apri https://example.com/wp-json/ in una finestra privata: deve rispondere «non trovato» (404, rest_no_route). Poi invia un modulo e usa il carrello o qualsiasi altra cosa facciano le tue pagine, e ripeti questa verifica.

Hide my WordPress site's REST API map from visitors who are not logged in, without breaking the features that use the API. https://example.com/wp-json/ currently answers anyone and lists every namespace, i.e. every plugin.

Do this:
1. Find which namespaces the public pages call (forms, cart, checkout, view counters): grep the theme and plugins for wp-json / rest_url / apiFetch, and check the browser's network tab as a visitor.
2. Add a must-use plugin (wp-content/mu-plugins/hide-rest-map.php) with a rest_endpoints filter: when the user cannot edit_posts, unset every route except those under the kept namespaces (match '/<namespace>/' as a prefix of the route), so the index '/', every namespace index and the other routes answer 404 rest_no_route, never 401. Also remove_action wp_head rest_output_link_wp_head and template_redirect rest_output_link_header (priority 11).
3. Do not use rest_authentication_errors to refuse the whole API: it breaks visitor-facing features and answers 401, which confirms the plugins.
4. Verify: /wp-json/ and /wp-json/wp/v2 answer 404 for a visitor and still work for an editor; the kept features work in a private window; no <link rel="https://api.w.org/"> and no Link header on the pages.
Tell me exactly which file you added and which namespaces you kept.

Controlla il tuo sito per sapere se ha questo problema. Controlla un sito