WordPress REST API 403 From the Cloud? Use ?rest_route= Instead

スポンサーリンク
WordPress REST API 403 From the Cloud? Use ?rest_route= Instead Automation
スポンサーリンク

Claude Code handles my blog from drafting articles to posting them to WordPress (I automated it from draft to WordPress post (in Japanese)). Until now it ran on my home PC. When I tried moving the blog work to the cloud version (Claude Code on the web), every WordPress REST API call came back 403.

Short answer: it wasn’t the credentials. The server was blocking the path /wp-json/wp/v2/, while the same API was reachable through URLs of the form ?rest_route=. Here are the steps I used to narrow it down, with the results.

What happened

I gave the cloud session the same WordPress user name plus application password as my home PC, as environment variables. Then I tried a read-only request to check the connection. With or without credentials, the response wasn’t a WordPress JSON error but a bare Apache 403 page:

<title>403 Forbidden</title>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>

The same credentials post fine from my home PC, so a wrong password seemed unlikely. My guess was that the server or a WAF stopped the request before it reached WordPress.

Narrowing it down

I let Claude Code do the narrowing, telling it to stick to read-only GET requests: no creating, updating, deleting. The curl it actually used looked like this (WP_URL, WP_USER, WP_APP_PASSWORD are environment variables):

curl -sS -o /dev/null -w "%{http_code}\n" -u "$WP_USER:$WP_APP_PASSWORD" "$WP_URL/wp-json/wp/v2/users/me?context=edit"

Changing one thing at a time (the URL, the credentials) gave these results:

Request Result
Home page (/) 200
/wp-json/ (with credentials, also without) 200
/wp-json/wp/v2/posts (no credentials) 403
/wp-json/wp/v2/posts (valid credentials) 403
/wp-json/wp/v2/posts (bogus credentials) 403
/wp-json/wp/v2/users/me (valid credentials) 403
/?rest_route=/wp/v2/users/me (valid credentials) 200
/?rest_route=/wp/v2/users/me (bogus credentials) 401

Three takeaways:

  • Everything under /wp-json/wp/v2/ returned the same 403 with valid, missing, even bogus credentials. It was blocked before anyone looked at the credentials
  • /wp-json/ itself returned 200. The REST API wasn’t disabled entirely; only paths under /wp/v2/ were blocked
  • With ?rest_route=, valid credentials gave 200, bogus ones 401. That 401 comes from WordPress itself (rest_not_logged_in), so the request reached WordPress, which checked the credentials properly

What is ?rest_route=?

WordPress accepts two URL forms for the same REST API call:

  • https://(your site)/wp-json/wp/v2/posts
  • https://(your site)/?rest_route=/wp/v2/posts

The usual /wp-json/ form works when pretty permalinks are enabled; ?rest_route= doesn’t depend on permalinks. It’s the same API, so path-based restrictions don’t catch it. Join any other parameters with & instead of ?:

curl -sS -u "$WP_USER:$WP_APP_PASSWORD" "$WP_URL/?rest_route=/wp/v2/posts&per_page=5&context=edit"

That returned the list of posts, drafts included, as an administrator.

Why only requests from the cloud were blocked

From my home PC, /wp-json/wp/v2/ works fine for posting, so the difference is where the request comes from. Cloud sessions connect from a data center outside my country.

My blog is hosted on ConoHa WING, a Japanese host. It has a “foreign access restriction” feature that blocks access from outside Japan, switchable per item: dashboard, XML-RPC API, REST API, wlwmanifest.xml, Abilities API. All are on by default (ConoHa WING support, in Japanese).

In the control panel, all five were on for my site. From the cloud, wp-login.php, wp-admin, xmlrpc.php also returned the same 403, so the dashboard, XML-RPC restrictions were active too. The /wp-json/wp/v2/ block came from the REST API item of this restriction.

Your host’s feature will have a different name, but many hosts, security plugins, WAFs offer country-based blocking. If only requests from a cloud service get 403, check for something like it.

Caution: ?rest_route= gets around the block

Put the other way round, this host’s REST API restriction doesn’t apply to the ?rest_route= form. Reading published posts is fine since anyone can see them anyway. But when I checked, the user list (/?rest_route=/wp/v2/users) was also visible from abroad without authentication, and it includes the names used to log in.

The login page is blocked from abroad, while REST authentication uses a random application password, so this isn’t an immediate danger. Still, there’s no need to show it, so I stopped returning the user list to visitors who aren’t logged in. I registered this code with the Code Snippets plugin:

add_filter( 'rest_endpoints', function ( $endpoints ) {
	if ( ! is_user_logged_in() ) {
		unset( $endpoints['/wp/v2/users'] );
		unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
	}
	return $endpoints;
} );

After that, an unauthenticated /?rest_route=/wp/v2/users returns 404 (rest_no_route). Requests authenticated with an application password count as logged in, so users/me etc. from my own posting scripts still return 200.

Login names can also be found from URLs like /?author=1 (it redirects to /author/(name)/), so I closed that by turning on “username leak protection” in SiteGuard WP Plugin. Since then, it redirects to the home page.

Note that the ?rest_route= form works for writes too. I confirmed that updating an existing post from the cloud (a form-urlencoded POST) goes through.

Summary

  • If the WordPress REST API returns 403 from the cloud, don’t blame the credentials right away. If bogus credentials get the same 403, something in front of WordPress is blocking it
  • If /wp-json/ returns 200 but everything under /wp-json/wp/v2/ returns 403, it’s a path-based restriction
  • The ?rest_route=/wp/v2/... form reaches the same API (writes work too)
  • A path-based block doesn’t cover ?rest_route=, so hide the user list separately

コメント

Copied title and URL