From 50debb65e199cc03f11e2838f28b3209a14b4714 Mon Sep 17 00:00:00 2001 From: qiansc Date: Sun, 29 Nov 2026 08:11:38 +0000 Subject: [PATCH] feat: robots.txt keeps crawlers to the landing pages Code views, search results, repository pages and the APIs are per-commit and endless for a crawler. Served at /robots.txt and /sourcegraph/robots.txt without authentication. Docs: the portal's client address and robots rules. Co-Authored-By: Claude Opus 5.5 --- docs/deployment.md | 4 +++- internal/httpserver/robots.go | 14 ++++++++++++++ internal/httpserver/robots_test.go | 20 ++++++++++++++++++++ internal/httpserver/runtime.go | 6 ++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 internal/httpserver/robots.go create mode 100644 internal/httpserver/robots_test.go diff --git a/docs/deployment.md b/docs/deployment.md index 78e0bbc..456412d 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -20,7 +20,9 @@ Never let two instances serve the same repository or share a data directory: ind `fly.toml` deploys one Source Graph Machine in `sin`, with 1 GB memory, auto-stop disabled, and a volume named `sourcegraph_data`. The starter volume should be at least 20 GB. Index memory and disk needs depend on repository size; observe real indexing before changing capacity. Do not scale this App to multiple Machines: Machines of one App would share repositories. For sharding, deploy one App per shard behind a routing access layer. -The portal is a separate App. For a unified domain it reverse-proxies the entire `/sourcegraph/` prefix, preserving query strings, cookies and authorization. Keep the configured external origin fixed; never trust arbitrary forwarded hosts. The portal must not cache auth/API/MCP responses. Static assets may be cached independently. +The portal is a separate App. For a unified domain it reverse-proxies the entire `/sourcegraph/` prefix, preserving query strings, cookies and authorization. Keep the configured external origin fixed; never trust arbitrary forwarded hosts. The portal must not cache auth/API/MCP responses. Static assets may be cached independently. The portal forwards the client address as a single `X-Forwarded-For` hop (from Fly's `Fly-Client-IP`); `SOURCEGRAPH_TRUSTED_PROXIES` (comma-separated CIDRs, `fdaa::/16` in `fly.toml`) tells Source Graph to trust it, so anonymous limits and the query error log see each client. + +The service answers `/robots.txt` and `/sourcegraph/robots.txt`: crawlers may read the landing pages, not code views, search results, repository pages or the APIs. A portal on the same domain should serve the same rules at its own `/robots.txt`. ```sh fly apps create context4ai-sourcegraph diff --git a/internal/httpserver/robots.go b/internal/httpserver/robots.go new file mode 100644 index 0000000..68f91eb --- /dev/null +++ b/internal/httpserver/robots.go @@ -0,0 +1,14 @@ +package httpserver + +// RobotsTxt keeps crawlers to the landing pages: code views, search results +// and the APIs they call are per-commit and endless for a crawler. +const RobotsTxt = `User-agent: * +Disallow: /sourcegraph/code +Disallow: /sourcegraph/? +Disallow: /sourcegraph/v1/ +Disallow: /sourcegraph/api/ +Disallow: /sourcegraph/auth/ +Disallow: /sourcegraph/mcp +Disallow: /sourcegraph/repo? +Allow: / +` diff --git a/internal/httpserver/robots_test.go b/internal/httpserver/robots_test.go new file mode 100644 index 0000000..a386547 --- /dev/null +++ b/internal/httpserver/robots_test.go @@ -0,0 +1,20 @@ +package httpserver + +import ( + "net/http/httptest" + "strings" + "testing" +) + +// robots.txt is public at the host root and under the prefix. +func TestRobotsTxt(t *testing.T) { + admin := hexID("admin")[:24] + s := newSite(t, map[string]string{"admin": admin}, admin) + for _, path := range []string{"/robots.txt", "/sourcegraph/robots.txt"} { + w := httptest.NewRecorder() + s.handler.ServeHTTP(w, httptest.NewRequest("GET", path, nil)) + if w.Code != 200 || !strings.Contains(w.Body.String(), "Disallow: /sourcegraph/code") || !strings.HasPrefix(w.Header().Get("Content-Type"), "text/plain") { + t.Fatalf("%s: %d %q", path, w.Code, w.Body.String()) + } + } +} diff --git a/internal/httpserver/runtime.go b/internal/httpserver/runtime.go index dc85d19..d245a81 100644 --- a/internal/httpserver/runtime.go +++ b/internal/httpserver/runtime.go @@ -245,6 +245,12 @@ func runtime(c config.Config, log *slog.Logger, s *service.Service, token string browserAuth.ServeHTTP(w, r) return } + if (r.URL.Path == "/robots.txt" || r.URL.Path == Prefix+"/robots.txt") && (r.Method == "GET" || r.Method == "HEAD") { + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.Header().Set("Cache-Control", "public, max-age=86400") + _, _ = io.WriteString(w, RobotsTxt) + return + } if r.URL.Path == Prefix+"/healthz" { fallback.ServeHTTP(w, r) return