Repository navigation
Encode endpoint and content ID as single URL path segments - #23
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
Build request URLs with net/url instead of splicing identifiers into a raw string. Endpoint and content ID are each percent-encoded as one path segment, and empty, '.', '..', slash- or backslash-containing values (including their percent-decoded forms) are rejected before any request is sent. The query is attached only via url.Values, so '#' or '?' in an identifier can no longer drop or replace it.
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.
Summary
makeRequestused to build every content API URL by splicing the endpoint and content ID into a raw string.Get,Create,UpdateandDeletealso passed them throughpath.Joinfirst, and the result was parsed as a full URL. Because of that:../other-endpoint/itemwas cleaned bypath.Join. The request, with the client's API key attached, went to a different endpoint.#or?in an endpoint or content ID turned the SDK's query string (fields,filters,limit,depth,draftKey,status) into a fragment, or replaced it outright.Fix
makeRequestnow takesendpointandcontentIDas separate arguments. It builds the URL withnet/urlfrom a fixedhttps://{serviceDomain}.microcms.io/api/v1/base and adds each identifier as exactly one segment, escaped withurl.PathEscape. The query is set only fromurl.Values.path.Joinis no longer used on identifiers anywhere.validatePathSegmentrejects an empty endpoint, an empty segment,.,.., and any value containing/or\. It applies the same check to every percent-decoded form of the value, such as%2e%2eor%252e%252e. Rejection happens before a request exists, so the API key is never sent for a rejected identifier.Get/UpdateandPOSTcreate working.serviceDomain.List,Get,Create,UpdateandDeletewere the only callers ofmakeRequest, and there is no other URL construction in the package, so no other call sites remain open.Tests
The tests that prove this fix are in #22. They are not included here.
With the PR #22 tests checked out:
go test ./...fails.TestPathSegmentsThatClimbOrAreEmptyAreRejected,TestPercentEncodedDotSegmentsDoNotEscapeEndpointandTestIdentifierCannotSwallowQueryfail because requests go to/api/v1/private-endpoint/secret-itemand the query lands in the fragment.go test -count=1 ./...passes. All three PR Add failing tests for content URL injection #22 tests pass, along with every existingTestClient_*test.On this branch, which holds the fix only:
go vet ./...passes.gofmt -l .reports nothing.go test -count=1 -v ./...passes:TestClient_List,TestClient_List_querystring,TestClient_Get,TestClient_Get_querystring,TestClient_Create_Post,TestClient_Create_Put,TestClient_Create_Draft,TestClient_Update,TestClient_Delete.I also retried the technique with a throwaway test that is not committed. It covered all five operations with these inputs:
.%2e,%2E%2e,%252e%252e,%25252e%25252e..%2Fx,a%2Fb,..\x,%5C, deep../../..chains#and?inside identifiers/,?,#,@or:Every climbing input was rejected with no request sent.
#and?stayed percent-encoded in the path with the query intact. Every malformed service domain was refused.