Repository navigation
Fix API key leak via absolute endpoint URLs in Client - #12
Open
cursor[bot] wants to merge 1 commit into
Open
cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Client::list,get,create,updateanddeletepassed the endpoint string, joined with any content ID, directly to Guzzle as the request URI. The base URL was only supplied asbase_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 theX-MICROCMS-API-KEYheader.Fix (
src/Client.php)A-Z a-z 0-9 . _ ~ -), and.and..are refused. This covers the endpoint, the$contentIdargument toget, the$idargument todelete, and$body["id"]increateandupdate. Integer IDs are still accepted. Anything else throwsInvalidArgumentExceptionbefore Guzzle is called.serviceDomainto be a single host label, so it can't change the host either.buildUri()builds the absolutehttps://{serviceDomain}.microcms.io/api/v1/{endpoint}[/{id}]URI as aGuzzleHttp\Psr7\Uriobject. 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 thebase_urioption has been removed. An injected Guzzle client's ownbase_urican no longer redirect requests either.src/Client.phpcontains 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.vendor/bin/phpunit tests, unpatched commit 71e4e90EndpointUrlSecurityTestcases fail: the request reachesattacker.examplewith the API key header attached.ClientTestpasses, 7 of 7.vendor/bin/phpunit tests, with this fix appliedEndpointUrlSecurityTestand 7ClientTest)vendor/bin/phpunit tests(existingClientTest)vendor/bin/phpstan analyse(level 8)vendor/bin/php-cs-fixer fix --dry-runI also ran an ad-hoc script with 117 bypass variants through all five methods, using an injected Guzzle client whose
base_uripoints 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 thansvc.microcms.io, and every hostile service domain was rejected. Legitimate calls still produce the same URLs as before.