Skip to content

Commit 8963661

Browse files
Copilotlpcox
andauthored
Fix GraphQL forwarding path rewrite for github.com proxy requests
Agent-Logs-Url: https://github.com/github/gh-aw-mcpg/sessions/704c283d-fc05-4f02-bd25-3574eec4f332 Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
1 parent f100954 commit 8963661

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

internal/proxy/handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func TestServeHTTP_GraphQLPreservesQueryString(t *testing.T) {
195195
wantPath string
196196
}{
197197
{name: "graphql path", path: "/graphql?foo=bar", wantPath: "/graphql?foo=bar"},
198-
{name: "ghes api graphql path", path: "/api/graphql?foo=bar", wantPath: "/api/graphql?foo=bar"},
198+
{name: "ghes api graphql path", path: "/api/graphql?foo=bar", wantPath: "/graphql?foo=bar"},
199199
{name: "gh host prefixed graphql path", path: "/api/v3/graphql?foo=bar", wantPath: "/graphql?foo=bar"},
200200
}
201201

internal/proxy/proxy.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,12 @@ func (r *restBackendCaller) CallTool(ctx context.Context, toolName string, args
344344
func (s *Server) forwardToGitHub(ctx context.Context, method, path string, body io.Reader, contentType string, clientAuth string) (*http.Response, error) {
345345
url := s.githubAPIURL + path
346346
pathOnly, query, hasQuery := strings.Cut(path, "?")
347-
if strings.HasSuffix(s.githubAPIURL, "/api/v3") && IsGraphQLPath(pathOnly) {
348-
url = strings.TrimSuffix(s.githubAPIURL, "/api/v3") + "/api/graphql"
347+
if IsGraphQLPath(pathOnly) {
348+
graphqlURL := s.githubAPIURL + "/graphql"
349+
if strings.HasSuffix(s.githubAPIURL, "/api/v3") {
350+
graphqlURL = strings.TrimSuffix(s.githubAPIURL, "/api/v3") + "/api/graphql"
351+
}
352+
url = graphqlURL
349353
if hasQuery {
350354
url += "?" + query
351355
}

internal/proxy/proxy_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,3 +887,46 @@ func TestForwardToGitHub_RewritesGraphQLPathForGHESAPIBase(t *testing.T) {
887887
})
888888
}
889889
}
890+
891+
func TestForwardToGitHub_RewritesGraphQLPathForDotComAPIBase(t *testing.T) {
892+
var receivedPath string
893+
var receivedQuery string
894+
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
895+
receivedPath = r.URL.Path
896+
receivedQuery = r.URL.RawQuery
897+
w.WriteHeader(http.StatusOK)
898+
}))
899+
defer upstream.Close()
900+
901+
s := &Server{
902+
githubAPIURL: upstream.URL,
903+
httpClient: upstream.Client(),
904+
}
905+
906+
tests := []struct {
907+
name string
908+
path string
909+
wantPath string
910+
wantQuery string
911+
}{
912+
{name: "plain graphql endpoint", path: "/graphql", wantPath: "/graphql"},
913+
{name: "ghes api graphql endpoint", path: "/api/graphql", wantPath: "/graphql"},
914+
{name: "gh host prefixed graphql endpoint", path: "/api/v3/graphql", wantPath: "/graphql"},
915+
{name: "graphql endpoint with query string", path: "/api/graphql?foo=bar", wantPath: "/graphql", wantQuery: "foo=bar"},
916+
}
917+
918+
for _, tt := range tests {
919+
t.Run(tt.name, func(t *testing.T) {
920+
receivedPath = ""
921+
receivedQuery = ""
922+
923+
resp, err := s.forwardToGitHub(context.Background(), http.MethodPost, tt.path, nil, "application/json", "")
924+
require.NoError(t, err)
925+
require.NotNil(t, resp)
926+
defer resp.Body.Close()
927+
928+
assert.Equal(t, tt.wantPath, receivedPath)
929+
assert.Equal(t, tt.wantQuery, receivedQuery)
930+
})
931+
}
932+
}

0 commit comments

Comments
 (0)