Skip to content

Fix API key leak via absolute endpoint URLs in Client - #12

Open
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/endpoint-url-ssrf-fix-3ea1
Open

cursor[bot] wants to merge 1 commit into
mainfrom
cursor/endpoint-url-ssrf-fix-3ea1

Conversation

@cursor

@cursor cursor Bot commented Nov 20, 2026

Copy link
Copy Markdown

Problem

Client::list, get, create, update and delete passed the endpoint string, joined with any content ID, directly to Guzzle as the request URI. The base URL was only supplied as base_uri. Guzzle treats a value with a scheme (https://…, HTTPS://…) as an absolute URI, and a value starting with // as a new host. In both cases the request went to a host chosen by whoever supplied the endpoint, and it still carried the X-MICROCMS-API-KEY header.

Fix (src/Client.php)

  • Single path segments only. Endpoints and content IDs must each be one path segment made of URL-unreserved characters (A-Z a-z 0-9 . _ ~ -), and . and .. are refused. This covers the endpoint, the $contentId argument to get, the $id argument to delete, and $body["id"] in create and update. Integer IDs are still accepted. Anything else throws InvalidArgumentException before Guzzle is called.
  • Service domain checked. The constructor requires serviceDomain to be a single host label, so it can't change the host either.
  • Client builds the URI itself. A new buildUri() builds the absolute https://{serviceDomain}.microcms.io/api/v1/{endpoint}[/{id}] URI as a GuzzleHttp\Psr7\Uri object. It checks the scheme, host, user info, port and path, and the same object is what gets sent. Caller strings are no longer passed to Guzzle as a raw URI, and the base_uri option has been removed. An injected Guzzle client's own base_uri can no longer redirect requests either.

src/Client.php contains every request the SDK makes, so no other call sites remain.

Behaviour change: endpoint or content ID values that contain /, ?, #, :, @, %, whitespace or non-ASCII characters are now rejected rather than sent. microCMS endpoint names and content IDs use only letters, digits, - and _.

Tests

The tests that prove the fix are in #10 (tests/EndpointUrlSecurityTest.php). They are not included in this PR.

Run Result
PR #10 tests, vendor/bin/phpunit tests, unpatched commit 71e4e90 48 of 48 EndpointUrlSecurityTest cases fail: the request reaches attacker.example with the API key header attached. ClientTest passes, 7 of 7.
PR #10 tests, vendor/bin/phpunit tests, with this fix applied OK, 55 tests (48 EndpointUrlSecurityTest and 7 ClientTest)
This branch, vendor/bin/phpunit tests (existing ClientTest) OK, 7 tests, 36 assertions
vendor/bin/phpstan analyse (level 8) No errors
vendor/bin/php-cs-fixer fix --dry-run No changes needed

I also ran an ad-hoc script with 117 bypass variants through all five methods, using an injected Guzzle client whose base_uri points to another host. The payloads went into both the endpoint and the content ID. They included backslashes, @ user info, ? and #, percent-encoded slashes, dot segments, leading whitespace, newlines and NUL bytes, full-width characters, an empty ID, and array, float and bool IDs. A separate check tried hostile service domains. No request reached a host other than svc.microcms.io, and every hostile service domain was rejected. Legitimate calls still produce the same URLs as before.

Open in Web Open in Cursor 

Endpoint and content id values were passed to Guzzle as the request URI,
so an absolute or protocol-relative value replaced the microCMS host and
received the X-MICROCMS-API-KEY header. The client now validates the
service domain as a host label, accepts each endpoint/content id only as a
single unreserved path segment, builds the absolute URI on
{serviceDomain}.microcms.io itself, checks the resulting host, and sends
that same URI object.
@YouheiNozaki YouheiNozaki self-assigned this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants