Skip to content

Commit 63d22fa

Browse files
authored
fix(publisher): record repository.id at init so renames stay resolvable (#1570)
Towards #1484 ## Background #1484 audited the `repository.url` of the 398 highest-graded remote servers and found 38 (9.5%) pointing at repositories that have since been renamed or transferred. No hard 404s — GitHub's REST API follows the redirect — but GraphQL consumers resolve them as nonexistent (`Could not resolve to a Repository`), so the source link is broken for a real class of tooling, and it stays broken once the redirect eventually lapses. ## The gap this closes `pkg/model/types.go` already defines the field that solves this: ```go ID string `json:"id,omitempty" doc:"Repository identifier from the hosting service (e.g., GitHub repo ID). Owned and determined by the source forge. Should remain stable across repository renames and may be used to detect repository resurrection attacks …"` ``` Nothing populates it. `mcp-publisher init` builds the repository block from `detectRepoURL()` and writes `url` and `source` only, so in practice every entry's sole pointer at its source is a URL — and a URL is not a stable pointer. This PR resolves the ID at init time and writes it alongside the URL. A rename after that point no longer loses the entry: the ID still identifies the repository, and it also gives the "deleted and recreated" detection the schema doc already describes something to compare against. ## Scope and deliberate limits - **One request, at scaffolding time.** Not in the publish path, so it adds no latency or GitHub rate-limit exposure to publishing. - **Best effort.** `init` is otherwise fully offline. Being off the network, rate limited, or pointed at a private repo leaves the field empty rather than failing the command. `GITHUB_TOKEN` is used when set, mostly so a rate-limited developer still gets an ID. - **GitHub only.** `repository.source` also allows `gitlab`, and `detectRepoURL` can produce a plain `git` source. Those return empty; GitLab's equivalent lookup is easy to add if you want it, but I did not want to guess at the ID semantics for a forge the audit did not cover. - Host matching is exact (`github.com`, optionally `www.`) so a lookalike host cannot get us to fetch an ID that belongs to somebody else. Covered by a test. ## What this does **not** do Two parts of #1484 are maintainer calls, not code I should pick unilaterally, and I have written them up on the issue rather than guessing here: 1. **Backfilling the 38 existing entries.** The registry's data is in the database, not this repo — `CLAUDE.md` is explicit that `data/seed.json` is local dev seed data and must not be used to publish. So a data fix cannot arrive as a PR to this repository at all; it needs an operator-run migration, and someone has to decide whether rewriting a publisher's `repository.url` without them re-publishing is acceptable. 2. **Publish-time behaviour on a redirecting URL** — reject, warn, or accept and record. `ValidateServerJSON` is currently pure and offline; the only network validation lives in `ValidatePublishRequest` behind `cfg.EnableRegistryValidation`, and it returns a bare `error` with no channel for a warning even though `ValidationIssueSeverityWarning` exists elsewhere. Wiring a warning through is a real design decision about the publish contract. ## Test `cmd/publisher/commands/init_internal_test.go`: - `TestParseGitHubOwnerRepo` — the URL shapes `detectRepoURL` actually produces (plain, `www.`, trailing slash, `.git`), plus the ones that must **not** be treated as GitHub: gitlab, a `github.com.evil.example` lookalike, a deep `/tree/main/...` path, an owner with no repo. - `TestDetectRepoID` — against an `httptest` stub: records the numeric id and requests `/repos/{owner}/{repo}`; and stays empty on 404 / non-GitHub source / empty URL, so a failed lookup can never fail `init`. ``` $ go test ./cmd/publisher/... ok github.com/modelcontextprotocol/registry/cmd/publisher/auth 2.363s ok github.com/modelcontextprotocol/registry/cmd/publisher/commands 4.838s $ go build ./... # clean $ gofmt -l cmd/ # clean ``` `golangci-lint` was not available in my environment, so that gate is unverified locally. --------- Signed-off-by: Kush Zingade <[email protected]>
1 parent 020fed3 commit 63d22fa

2 files changed

Lines changed: 197 additions & 0 deletions

File tree

‎cmd/publisher/commands/init.go‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import (
55
"encoding/json"
66
"errors"
77
"fmt"
8+
"net/http"
9+
"net/url"
810
"os"
911
"os/exec"
1012
"path/filepath"
13+
"strconv"
1114
"strings"
1215
"time"
1316

@@ -64,6 +67,16 @@ func InitCommand() error {
6467
model.CurrentSchemaURL, name, description, version, repoURL, repoSource, subfolder,
6568
packageType, packageIdentifier, version, envVars,
6669
)
70+
if server.Repository != nil {
71+
id, err := detectRepoID(repoSource, repoURL)
72+
if err != nil {
73+
// An empty `id` on a GitHub source is indistinguishable from a
74+
// non-GitHub entry once serialized, so report the gap while there
75+
// is still someone around to read it.
76+
_, _ = fmt.Fprintf(os.Stderr, "warning: could not resolve repository.id (%v); publishing without it\n", err)
77+
}
78+
server.Repository.ID = id
79+
}
6780

6881
// Write to file
6982
jsonData, err := json.MarshalIndent(server, "", " ")
@@ -247,6 +260,90 @@ func buildGitHubServerName(repoURL, subfolder string) string {
247260
return fmt.Sprintf("io.github.%s/%s", owner, repo)
248261
}
249262

263+
// githubAPIBaseURL is a variable so the tests can point it at a stub server.
264+
var githubAPIBaseURL = "https://api.github.com"
265+
266+
// parseGitHubOwnerRepo pulls owner/repo out of a github.com web URL, which is
267+
// the only shape `detectRepoURL` produces for GitHub (it rewrites the SSH form
268+
// and strips `.git`). Anything else — a different host, a bare path, a URL with
269+
// extra segments — returns ok=false rather than a guess.
270+
func parseGitHubOwnerRepo(repoURL string) (owner, repo string, ok bool) {
271+
u, err := url.Parse(repoURL)
272+
if err != nil {
273+
return "", "", false
274+
}
275+
if strings.TrimPrefix(strings.ToLower(u.Hostname()), "www.") != "github.com" {
276+
return "", "", false
277+
}
278+
parts := strings.Split(strings.Trim(u.Path, "/"), "/")
279+
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
280+
return "", "", false
281+
}
282+
return parts[0], strings.TrimSuffix(parts[1], ".git"), true
283+
}
284+
285+
// detectRepoID resolves the forge's own identifier for the repository, which
286+
// `repository.id` exists to carry.
287+
//
288+
// A URL is not a stable pointer: renaming or transferring a GitHub repo leaves
289+
// the registered `repository.url` resolving only through GitHub's redirect,
290+
// which the REST API follows but GraphQL does not, so the source link rots for
291+
// consumers. The numeric ID does not move (#1484). Recording it at init time
292+
// costs one request and makes the entry re-resolvable later even if the URL has
293+
// gone stale.
294+
//
295+
// Deliberately best effort: `init` is otherwise fully offline, so being off the
296+
// network, rate limited, or pointed at a private repo leaves the field empty
297+
// instead of failing the command. `GITHUB_TOKEN` is used when present, mostly
298+
// so a rate-limited developer still gets the ID.
299+
//
300+
// The returned error is why a GitHub lookup came back without an ID, for the
301+
// caller to warn about; a non-GitHub source has nothing to resolve, so it
302+
// returns an empty ID and no error.
303+
func detectRepoID(repoSource, repoURL string) (string, error) {
304+
if repoSource != MethodGitHub {
305+
return "", nil
306+
}
307+
owner, repo, ok := parseGitHubOwnerRepo(repoURL)
308+
if !ok {
309+
return "", fmt.Errorf("%q is not a github.com repository URL", repoURL)
310+
}
311+
312+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
313+
defer cancel()
314+
req, err := http.NewRequestWithContext(
315+
ctx, http.MethodGet,
316+
fmt.Sprintf("%s/repos/%s/%s", githubAPIBaseURL, owner, repo), nil,
317+
)
318+
if err != nil {
319+
return "", err
320+
}
321+
req.Header.Set("Accept", "application/vnd.github+json")
322+
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
323+
req.Header.Set("Authorization", "Bearer "+token)
324+
}
325+
326+
resp, err := http.DefaultClient.Do(req)
327+
if err != nil {
328+
return "", err
329+
}
330+
defer resp.Body.Close()
331+
if resp.StatusCode != http.StatusOK {
332+
return "", fmt.Errorf("GET /repos/%s/%s returned %s", owner, repo, resp.Status)
333+
}
334+
335+
var body struct {
336+
ID int64 `json:"id"`
337+
}
338+
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
339+
return "", err
340+
}
341+
if body.ID == 0 {
342+
return "", errors.New("response carried no repository id")
343+
}
344+
return strconv.FormatInt(body.ID, 10), nil
345+
}
346+
250347
func detectDescription() string {
251348
// Try to get from package.json
252349
if data, err := os.ReadFile("package.json"); err == nil {
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package commands
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"strconv"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestParseGitHubOwnerRepo(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
repoURL string
17+
owner string
18+
repo string
19+
ok bool
20+
}{
21+
{name: "plain https URL", repoURL: "https://github.com/acme/weather", owner: "acme", repo: "weather", ok: true},
22+
{name: "www host", repoURL: "https://www.github.com/acme/weather", owner: "acme", repo: "weather", ok: true},
23+
{name: "trailing slash", repoURL: "https://github.com/acme/weather/", owner: "acme", repo: "weather", ok: true},
24+
{name: "dot-git suffix", repoURL: "https://github.com/acme/weather.git", owner: "acme", repo: "weather", ok: true},
25+
{name: "gitlab is not github", repoURL: "https://gitlab.com/acme/weather"},
26+
// A lookalike host must not be treated as GitHub — the ID we would fetch
27+
// would belong to whatever that host decided to return.
28+
{name: "lookalike host", repoURL: "https://github.com.evil.example/acme/weather"},
29+
{name: "deep path is not a repo root", repoURL: "https://github.com/acme/weather/tree/main/src"},
30+
{name: "owner only", repoURL: "https://github.com/acme"},
31+
{name: "empty", repoURL: ""},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
owner, repo, ok := parseGitHubOwnerRepo(tt.repoURL)
37+
assert.Equal(t, tt.ok, ok)
38+
assert.Equal(t, tt.owner, owner)
39+
assert.Equal(t, tt.repo, repo)
40+
})
41+
}
42+
}
43+
44+
func TestDetectRepoID(t *testing.T) {
45+
t.Run("records the numeric id GitHub reports", func(t *testing.T) {
46+
var gotPath string
47+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
48+
gotPath = r.URL.Path
49+
_, _ = w.Write([]byte(`{"id": 123456789, "full_name": "acme/weather"}`))
50+
}))
51+
defer srv.Close()
52+
t.Setenv("GITHUB_TOKEN", "")
53+
54+
originalBaseURL := githubAPIBaseURL
55+
githubAPIBaseURL = srv.URL
56+
defer func() { githubAPIBaseURL = originalBaseURL }()
57+
58+
id, err := detectRepoID(MethodGitHub, "https://github.com/acme/weather")
59+
require.NoError(t, err)
60+
assert.Equal(t, "123456789", id)
61+
assert.Equal(t, "/repos/acme/weather", gotPath)
62+
})
63+
64+
t.Run("stays empty rather than failing init when the lookup does not work", func(t *testing.T) {
65+
// A failed lookup leaves the field empty, but it reports why, so init can
66+
// say so on stderr instead of silently producing a GitHub entry that
67+
// serializes exactly like a non-GitHub one.
68+
for _, status := range []int{http.StatusNotFound, http.StatusForbidden} {
69+
t.Run(http.StatusText(status), func(t *testing.T) {
70+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
71+
w.WriteHeader(status)
72+
}))
73+
defer srv.Close()
74+
t.Setenv("GITHUB_TOKEN", "")
75+
76+
originalBaseURL := githubAPIBaseURL
77+
githubAPIBaseURL = srv.URL
78+
defer func() { githubAPIBaseURL = originalBaseURL }()
79+
80+
// 404 is private or missing, 403 is rate limited.
81+
id, err := detectRepoID(MethodGitHub, "https://github.com/acme/weather")
82+
assert.Empty(t, id)
83+
require.Error(t, err)
84+
assert.Contains(t, err.Error(), strconv.Itoa(status))
85+
})
86+
}
87+
88+
// Non-GitHub sources are never looked up, so no request is made at all —
89+
// and the empty ID is correct there, so there is nothing to report.
90+
id, err := detectRepoID("gitlab", "https://gitlab.com/acme/weather")
91+
assert.Empty(t, id)
92+
assert.NoError(t, err)
93+
94+
// A GitHub source we cannot even build a request for is still a GitHub
95+
// entry that ended up without its ID.
96+
id, err = detectRepoID(MethodGitHub, "")
97+
assert.Empty(t, id)
98+
assert.Error(t, err)
99+
})
100+
}

0 commit comments

Comments
 (0)