Repository navigation
fix: reject microCMS endpoints and content ids that are not a single path segment - #44
Closed
cursor[bot] wants to merge 1 commit into
Closed
cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
…path segment useMicroCMSGetList, useMicroCMSGetListDetail and useMicroCMSGetObject concatenated caller-supplied endpoint and contentId into the request path, so values such as '..\other-api?limit=100' made the server send its API key to a different content API. Validate every segment against the characters microCMS allows and encode it before calling useFetch.
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
useMicroCMSGetListDetailbuilt its request path as`${endpoint}/${contentId}`, anduseMicroCMSGetListanduseMicroCMSGetObjectpassedendpointthrough as is.useFetchthen joined that path tohttps://{serviceDomain}.microcms.io/api/v1and sent theX-MICROCMS-API-KEYheader. Pages commonly pass a route param as the content id. Because of that, a visitor could use a value like..\other-api?limit=100(a backslash or dot segment, optionally with a query) to make the server fetch a different content API with the service key. The response then ended up in the Nuxt payload.Fix
src/runtime/composables/useMicroCMSGet.ts: the shareduseMicroCMSGethelper now takes path segments instead of a ready-made URL string. Before callinguseFetch, it checks that every segment is a string matching^[A-Za-z0-9_-]+$and URL-encodes it. If any segment fails the check, it throws and no request is sent. All three exported composables go through this helper, so they are all covered. Nothing else in the repository builds microCMS request URLs.The allowlist matches what microCMS accepts:
a-z,A-Z,0-9,-and_(see the microCMS docs, "コンテンツIDの設定").-and_(see "APIの作成・管理").So no valid id or endpoint is rejected. The README now documents the constraint.
Behaviour changes for callers:
endpointorcontentIdnow throws anError, which Nuxt shows as a 500 during SSR. Before, the request was sent to whatever path the value resolved to.contentId, which used to fetch the list endpoint by accident, is also rejected.Tests
The tests that prove the fix are in #43 (#43). They are not part of this PR.
node --test test/content-id-traversal.test.mjsfrom #43/api/v1/other-api,/api/v1/other-api?limit=100,/api/other-apiand/api/v1/blogs/hello-world?limit=100npm run linton this branchnuxt-module-build buildon this branchnuxi build playgroundon this branchThe extra variants were:
%252e%252e%255cand%252F#,&, tab, NUL and space inside the id../and∕..;/,@host//hostandhttps://hostas the endpointLegitimate ids such as
hello-worldand endpoints such asblogsandbannerstill fetch/api/v1/blogs/hello-world,/api/v1/blogsand/api/v1/banner.The repository has no other test suite for this code.