Skip to content

Encode endpoint and content ID as single URL path segments - #23

Closed
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/fix-url-path-injection-a62e
Closed

cursor[bot] wants to merge 1 commit into
mainfrom
cursor/fix-url-path-injection-a62e

Conversation

@cursor

@cursor cursor Bot commented Nov 20, 2026

Copy link
Copy Markdown

Summary

makeRequest used to build every content API URL by splicing the endpoint and content ID into a raw string. Get, Create, Update and Delete also passed them through path.Join first, and the result was parsed as a full URL. Because of that:

  • A content ID such as ../other-endpoint/item was cleaned by path.Join. The request, with the client's API key attached, went to a different endpoint.
  • A # 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

  • makeRequest now takes endpoint and contentID as separate arguments. It builds the URL with net/url from a fixed https://{serviceDomain}.microcms.io/api/v1/ base and adds each identifier as exactly one segment, escaped with url.PathEscape. The query is set only from url.Values.
  • path.Join is no longer used on identifiers anywhere.
  • validatePathSegment rejects 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%2e or %252e%252e. Rejection happens before a request exists, so the API key is never sent for a rejected identifier.
  • An empty content ID still means "no ID segment", which keeps object-type Get/Update and POST create working.
  • If the parsed request's host, path, query or fragment differs from what was built, the request is refused. This covers a malformed serviceDomain.
  • List, Get, Create, Update and Delete were the only callers of makeRequest, 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:

  • Unchanged code: go test ./... fails. TestPathSegmentsThatClimbOrAreEmptyAreRejected, TestPercentEncodedDotSegmentsDoNotEscapeEndpoint and TestIdentifierCannotSwallowQuery fail because requests go to /api/v1/private-endpoint/secret-item and the query lands in the fragment.
  • With this fix: go test -count=1 ./... passes. All three PR Add failing tests for content URL injection #22 tests pass, along with every existing TestClient_* 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:

  • encoded dot segments: .%2e, %2E%2e, %252e%252e, %25252e%25252e
  • encoded and literal slashes: ..%2Fx, a%2Fb, ..\x, %5C, deep ../../.. chains
  • # and ? inside identifiers
  • service domains containing /, ?, #, @ 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.

Open in Web Open in Cursor 

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.
@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