Repository navigation
Reject endpoint names and content ids that are not a single path segment - #13
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
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
Microcms\Clientbuilt request paths by joining the endpoint name and the content id with/, and did no validation. Guzzle resolves the result againsthttps://{service}.microcms.io/api/v1/and removes dot segments. So a content id such as../inquiriespassed toget('blog', ...)sentGET /api/v1/inquirieswith the application'sX-MICROCMS-API-KEYheader. That reached a different content API.create,updateanddeleteused the same join, so writes and deletes could also be redirected. An endpoint value that is an absolute URL would also have replaced the base URI.Fix
list,get,create,updateanddeletenow build their path through one privatebuildPath()helper. Every endpoint name and content id, including theidtaken from acreate/updatebody, must match\A[A-Za-z0-9_-]+\z. Anything else throwsInvalidArgumentExceptionbefore a request is made. That includes/,\,.,%,?,#,:, whitespace, a trailing newline, empty ids, and non-scalar ids. Integer ids are still accepted, as before. This is the character set microCMS uses for endpoint names and content ids, so ordinary calls behave the same.These five methods are the only places in the repository that build request paths (
src/Client.php). All of them are covered.Tests
The tests that prove the fix are in #11. This PR does not include them.
vendor/bin/phpunit testsreported OK (52 tests, 83 assertions). That covers all 45 tests inContentPathTraversalTestplus the existingClientTest. Without the fix, 44 of the 45 failed.vendor/bin/phpunit testsreported OK (7 tests, 36 assertions) (the existingClientTest).vendor/bin/phpstan analyse: no errors.php-cs-fixer fix --dry-run src: no changes.list, and both the endpoint and the id positions ofget,create,updateanddelete). The inputs were..%2finquiries,%2e%2e,..\inquiries, a trailing newline, a NUL byte, a leading space,https://…,//host/x,host:80,?x=1,#frag,.,.., a full-width letter, and array, null, float and bool ids. Every one was rejected and no request was sent.