diff --git a/NOTICE b/NOTICE index 1fe523a90b..e06b3989f8 100644 --- a/NOTICE +++ b/NOTICE @@ -3535,6 +3535,32 @@ Source: https://github.com/tuna-os/ccleft/blob/b243120bfda2/LICENSE ----------------------------------------------------------------------------- +Package: github.com/vektah/gqlparser/v2 +Version: v2.5.30 +License: MIT +Source: https://github.com/vektah/gqlparser/blob/v2.5.30/LICENSE + +Copyright (c) 2018 Adam Scarr + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +----------------------------------------------------------------------------- + Package: github.com/xo/terminfo Version: v0.0.0-20220910002029-abceb7e1c41e License: MIT diff --git a/changelog.d/security-19058-graphql-repo-scope.md b/changelog.d/security-19058-graphql-repo-scope.md new file mode 100644 index 0000000000..7dc6ae1ea0 --- /dev/null +++ b/changelog.d/security-19058-graphql-repo-scope.md @@ -0,0 +1 @@ +- Apply per-agent repository scope to attributable GitHub GraphQL mutations, including node-ID writes resolved from earlier GitHub responses, with the same denial as REST. Queries stay unrestricted; unresolved mutations retain existing gates for compatibility with normal `gh` flows ([#19058](https://github.com/hivecommons/hive/issues/19058)). diff --git a/src/crux/go.mod b/src/crux/go.mod index 932ed89431..82af7a3168 100644 --- a/src/crux/go.mod +++ b/src/crux/go.mod @@ -8,6 +8,7 @@ require ( github.com/nats-io/nats-server/v2 v2.15.3 github.com/nats-io/nats.go v1.55.0 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e + github.com/vektah/gqlparser/v2 v2.5.30 go.opentelemetry.io/otel v1.47.0 go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.47.0 go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.47.0 diff --git a/src/crux/go.sum b/src/crux/go.sum index afff97d236..74f29ea7ff 100644 --- a/src/crux/go.sum +++ b/src/crux/go.sum @@ -51,6 +51,8 @@ github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= +github.com/vektah/gqlparser/v2 v2.5.30 h1:EqLwGAFLIzt1wpx1IPpY67DwUujF1OfzgEyDsLrN6kE= +github.com/vektah/gqlparser/v2 v2.5.30/go.mod h1:D1/VCZtV3LPnQrcPBeR/q5jkSQIPti0uYCP/RI0gIeo= go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64= go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y= go.opentelemetry.io/otel v1.47.0 h1:j7ALJ/zgkS7Z6aeJW09p8VC9804bC+PpeTfCD4XPnOM= diff --git a/src/crux/proxy/autonomy_enforcement_test.go b/src/crux/proxy/autonomy_enforcement_test.go index f874460f6a..d07bb00269 100644 --- a/src/crux/proxy/autonomy_enforcement_test.go +++ b/src/crux/proxy/autonomy_enforcement_test.go @@ -8,7 +8,7 @@ import ( func TestAutonomyDemotedRepoPRWriteRefused(t *testing.T) { hiveMode := agent.DefaultAgentMode("scanner", 5) - SetAutonomyRepoLevel("hivecommons/hive", 4) + setAutonomyRepoLevelForTest(t, "hivecommons/hive", 4) modeAfterDemotion := autonomyModeForRepo("scanner", "hivecommons/hive", hiveMode) if AllowedByMode(modeAfterDemotion, "POST", "/repos/hivecommons/hive/git/refs") { t.Fatal("demoted L4 scanner must not be allowed to create PR branches") @@ -21,7 +21,7 @@ func TestAutonomyDemotedRepoPRWriteRefused(t *testing.T) { func TestAutonomyDemotedRepoGraphQLPRWriteRefused(t *testing.T) { hiveMode := agent.DefaultAgentMode("scanner", 5) - SetAutonomyRepoLevel("hivecommons/hive", 4) + setAutonomyRepoLevelForTest(t, "hivecommons/hive", 4) body := []byte(`{"query":"mutation { createPullRequest(input: {repositoryId: \"R\"}) { pullRequest { id } } }"}`) allowed, mutation := GraphQLAllowedCaps(autonomyGraphQLMode("scanner", body, hiveMode), agent.AgentCapabilities{}, body) if !mutation { diff --git a/src/crux/proxy/github_proxy.go b/src/crux/proxy/github_proxy.go index 008dfdd63c..5875dedfc4 100644 --- a/src/crux/proxy/github_proxy.go +++ b/src/crux/proxy/github_proxy.go @@ -73,6 +73,7 @@ var ( // GitHubProxy is an HTTP CONNECT proxy that performs MITM TLS // inspection on GitHub API traffic and enforces ACMM mode rules. type GitHubProxy struct { + graphQLRepos graphQLRepoCache // localInferencePaths records CLI telemetry paths already logged once by // localInferenceResponse, so per-path DEBUG lines do not repeat. localInferencePaths sync.Map diff --git a/src/crux/proxy/github_proxy_mitm.go b/src/crux/proxy/github_proxy_mitm.go index 362263b0e2..ccbed3db67 100644 --- a/src/crux/proxy/github_proxy_mitm.go +++ b/src/crux/proxy/github_proxy_mitm.go @@ -73,6 +73,7 @@ func (p *GitHubProxy) proxyHTTPHost(client net.Conn, upstream net.Conn, host str // as paused even when the agent is also out of scope for it. blockJSON := false graphQLMutation := false + var graphQLBody []byte // A direct write from a lane under write_surface.enforce (#9772) is // refused and audited as agent_write_refused. Checked after pause and // scope so their more specific reasons win when both apply. @@ -155,7 +156,12 @@ func (p *GitHubProxy) proxyHTTPHost(client net.Conn, upstream net.Conn, host str p.logTimeout("proxy GraphQL request body read timed out", readErr, "agent", agentName, "path", req.URL.Path) return } - if reason, refused := p.graphQLAutoMergeRefusal(body); agentName != internalCallerName && refused { + graphQLBody = body + if reason, refused := p.graphQLAgentRepoScopeRefusal(agentName, body); agentName != internalCallerName && refused { + blocked = true + blockReason = reason + blockCode = DenyReasonAgentRepoScope + } else if reason, refused := p.graphQLAutoMergeRefusal(body); agentName != internalCallerName && refused { blocked = true blockReason = reason blockCode = DenyReasonRepoAutoMergeOff @@ -415,6 +421,15 @@ func (p *GitHubProxy) proxyHTTPHost(client net.Conn, upstream net.Conn, host str // longer than responseBodyStallTimeout errors out and lets the // handler's deferred Closes reclaim the sockets. resp.Body = &stallBoundedBody{body: resp.Body, conn: upstream, idle: p.bodyStallTimeoutDuration()} + if graphQLBody != nil && resp.StatusCode == http.StatusOK { + resp.Body = &graphQLRepoCapture{ + ReadCloser: resp.Body, + length: resp.ContentLength, + observe: func(body []byte) { + p.observeGraphQLRepoResponse(agentName, graphQLBody, body, resp.Header.Get("Content-Encoding")) + }, + } + } // Rolling per-Write deadline (see stallBoundedWriter): a response that // keeps flowing is never cut for being long, while a client that stops diff --git a/src/crux/proxy/graphql_repo_capture_test.go b/src/crux/proxy/graphql_repo_capture_test.go new file mode 100644 index 0000000000..1429eadec1 --- /dev/null +++ b/src/crux/proxy/graphql_repo_capture_test.go @@ -0,0 +1,70 @@ +package proxy + +import ( + "bytes" + "compress/gzip" + "io" + "strings" + "testing" +) + +func TestGraphQLRepoCapturePublishesBeforeFinalBytes(t *testing.T) { + for _, length := range []int64{3, -1} { + observed := false + capture := &graphQLRepoCapture{ + ReadCloser: io.NopCloser(strings.NewReader("abc")), + length: length, + observe: func(body []byte) { + if string(body) != "abc" { + t.Errorf("observed %q", body) + } + observed = true + }, + } + if length == 3 { + // HTTP relays with Content-Length may never perform an EOF read. + body := make([]byte, 3) + if _, err := io.ReadFull(capture, body); err != nil { + t.Fatal(err) + } + } else { + if _, err := io.Copy(io.Discard, capture); err != nil { + t.Fatal(err) + } + } + if !observed { + t.Fatalf("length=%d: response completed before cache publication", length) + } + } +} + +func TestGraphQLRepoResponseBadEncodingAndGzipLimit(t *testing.T) { + lookup := scopeGraphQLBody(`query { repository(owner:"acme",name:"widgets"){id} }`, nil) + for _, tc := range []struct { + encoding string + body []byte + }{ + {"gzip", []byte("broken")}, + {"br", []byte(`{"data":{"repository":{"id":"R"}}}`)}, + {"gzip", gzipScopeResponse(t, []byte(strings.Repeat(" ", graphQLRepoResponseLimit)+`{"data":{"repository":{"id":"R"}}}`))}, + } { + p := newTestProxy() + p.observeGraphQLRepoResponse("schema", lookup, tc.body, tc.encoding) + if p.graphQLRepos.get("schema", "R") != "" { + t.Fatal("invalid/oversized response seeded cache") + } + } +} + +func gzipScopeResponse(t *testing.T, body []byte) []byte { + t.Helper() + var buf bytes.Buffer + writer := gzip.NewWriter(&buf) + if _, err := writer.Write(body); err != nil { + t.Fatal(err) + } + if err := writer.Close(); err != nil { + t.Fatal(err) + } + return buf.Bytes() +} diff --git a/src/crux/proxy/graphql_repo_scope.go b/src/crux/proxy/graphql_repo_scope.go new file mode 100644 index 0000000000..2b3f08fad5 --- /dev/null +++ b/src/crux/proxy/graphql_repo_scope.go @@ -0,0 +1,346 @@ +package proxy + +import ( + "bytes" + "compress/gzip" + "encoding/json" + "io" + "strings" + "sync" + + "github.com/vektah/gqlparser/v2/ast" + "github.com/vektah/gqlparser/v2/parser" +) + +// Attribution is best-effort, not a new deny-by-default GraphQL allowlist. +// Unknown IDs (including cold-cache gh api calls) retain the existing mode and +// write-surface gates. Known out-of-scope targets always get the REST refusal. +// Entries come only from upstream responses, never from caller-supplied IDs +// paired with a claimed repo. Bound the cache across agents and connections. +const graphQLRepoCacheLimit = 8192 +const graphQLRepoResponseLimit = 4 << 20 + +type graphQLRepoCache struct { + mu sync.Mutex + repos map[graphQLRepoKey]string +} + +type graphQLRepoKey struct{ agent, id string } + +func (c *graphQLRepoCache) get(agent, id string) string { + c.mu.Lock() + defer c.mu.Unlock() + return c.repos[graphQLRepoKey{agent, id}] +} + +func (c *graphQLRepoCache) put(agent, id, repo string) { + if id == "" || repo == "" { + return + } + c.mu.Lock() + defer c.mu.Unlock() + if c.repos == nil || len(c.repos) >= graphQLRepoCacheLimit { + c.repos = make(map[graphQLRepoKey]string) + } + c.repos[graphQLRepoKey{agent, id}] = repo +} + +type scopedGraphQL struct { + doc *ast.QueryDocument + op *ast.OperationDefinition + vars map[string]any +} + +// Respect operationName, variable defaults, aliases and fragments. Looking for +// repo-looking strings in the raw JSON would mistake comment bodies for targets +// and would inspect operations that GitHub never executes. +func parseScopedGraphQL(body []byte) *scopedGraphQL { + var req graphQLRequest + if json.Unmarshal(body, &req) != nil { + return nil + } + doc, err := parser.ParseQuery(&ast.Source{Input: req.Query}) + if err != nil { + return nil + } + var op *ast.OperationDefinition + if req.OperationName != "" { + op = doc.Operations.ForName(req.OperationName) + } else if len(doc.Operations) == 1 { + op = doc.Operations[0] + } + if op == nil { + return nil + } + vars, _ := req.Variables.(map[string]any) + if vars == nil { + vars = make(map[string]any) + } + for _, def := range op.VariableDefinitions { + if _, exists := vars[def.Variable]; !exists && def.DefaultValue != nil { + vars[def.Variable], _ = def.DefaultValue.Value(nil) + } + } + return &scopedGraphQL{doc, op, vars} +} + +// fields expands fragments at one selection level. Visit each named fragment +// only once: a depth bound alone would still permit exponential expansion of +// repeated spreads. Schema validation remains GitHub's job. +func (g *scopedGraphQL) fields(set ast.SelectionSet, depth int) []*ast.Field { + var fields []*ast.Field + seen := make(map[string]bool) + var expand func(ast.SelectionSet, int) + expand = func(set ast.SelectionSet, depth int) { + if depth > 32 { + return + } + for _, selection := range set { + switch s := selection.(type) { + case *ast.Field: + fields = append(fields, s) + case *ast.InlineFragment: + expand(s.SelectionSet, depth+1) + case *ast.FragmentSpread: + if f := g.doc.Fragments.ForName(s.Name); f != nil && !seen[s.Name] { + seen[s.Name] = true + expand(f.SelectionSet, depth+1) + } + } + } + } + expand(set, depth) + return fields +} + +func (g *scopedGraphQL) args(f *ast.Field) map[string]any { + args := make(map[string]any) + for _, arg := range f.Arguments { + args[arg.Name], _ = arg.Value.Value(g.vars) + } + return args +} + +func graphQLRepoName(v any) string { + m, _ := v.(map[string]any) + if m == nil { + return "" + } + for _, key := range []string{"repositoryNameWithOwner", "nameWithOwner", "repository", "repo"} { + if s, ok := m[key].(string); ok && validGraphQLRepo(s) { + return s + } + } + owner, _ := m["owner"].(string) + if obj, ok := m["owner"].(map[string]any); ok { + owner, _ = obj["login"].(string) + } + name, _ := m["name"].(string) + if validGraphQLRepo(owner + "/" + name) { + return owner + "/" + name + } + return "" +} + +func validGraphQLRepo(s string) bool { + parts := strings.Split(s, "/") + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { + return false + } + for _, c := range s { + if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || strings.ContainsRune("-._/", c)) { + return false + } + } + return true +} + +// Check every target, not the first repo encountered in a map or document. +// Only input argument values are targets; response selections and free-form +// strings such as a title/body must not be interpreted as mutation input. +func (p *GitHubProxy) graphQLAgentRepoScopeRefusal(agent string, body []byte) (string, bool) { + if agent == "" || agent == internalCallerName { + return "", false + } + g := parseScopedGraphQL(body) + if g == nil || g.op.Operation != ast.Mutation { + return "", false + } + repos := make(map[string]bool) + var inputs func(any) + inputs = func(v any) { + switch v := v.(type) { + case map[string]any: + if repo := graphQLRepoName(v); repo != "" { + repos[repo] = true + } + for key, child := range v { + // Do not treat assignee/team/project IDs as repository targets. + switch key { + case "id", "repositoryId", "issueId", "pullRequestId", "subjectId", "commentId", "pullRequestReviewId", "reviewId", "discussionId", "replyToId", "threadId", "labelableId", "assignableId", "labelId", "refId", "branchProtectionRuleId", "pullRequestReviewThreadId", "pullRequestReviewCommentId", "issueCommentId", "discussionCommentId", "subIssueId", "blockedByIssueId", "blockingIssueId": + if id, ok := child.(string); ok { + if repo := p.graphQLRepos.get(agent, id); repo != "" { + repos[repo] = true + } + } + } + inputs(child) + } + case []any: + for _, child := range v { + inputs(child) + } + } + } + for _, field := range g.fields(g.op.SelectionSet, 0) { + inputs(g.args(field)) + } + for repo := range repos { + if reason, refused := p.agentRepoScopeRefusal(agent, "POST", "/repos/"+repo); refused { + return reason, true + } + } + return "", false +} + +// Keep response streaming and deadlines unchanged. Capture only a bounded copy +// of a successful GraphQL response. Learn before releasing its final bytes, +// so a lookup followed immediately by a write on another connection cannot +// race cache publication. Both wire and decompressed sizes are bounded. +type graphQLRepoCapture struct { + io.ReadCloser + buf bytes.Buffer + tooLarge bool + length int64 + read int64 + observe func([]byte) +} + +func (b *graphQLRepoCapture) Read(dst []byte) (int, error) { + n, err := b.ReadCloser.Read(dst) + if !b.tooLarge { + if b.buf.Len()+n > graphQLRepoResponseLimit { + b.tooLarge = true + b.buf.Reset() + } else { + _, _ = b.buf.Write(dst[:n]) + } + } + b.read += int64(n) + if !b.tooLarge && b.observe != nil && (err == io.EOF || b.length >= 0 && b.read == b.length) { + b.observe(b.buf.Bytes()) + b.observe = nil + } + return n, err +} + +func (p *GitHubProxy) observeGraphQLRepoResponse(agent string, request, response []byte, encoding string) { + switch strings.ToLower(strings.TrimSpace(encoding)) { + case "", "identity": + case "gzip": + reader, err := gzip.NewReader(bytes.NewReader(response)) + if err != nil { + return + } + decoded, err := io.ReadAll(io.LimitReader(reader, graphQLRepoResponseLimit+1)) + _ = reader.Close() + if err != nil || len(decoded) > graphQLRepoResponseLimit { + return + } + response = decoded + default: + return + } + p.observeGraphQLRepos(agent, request, response) +} + +// Match response keys through the parsed selection, so aliasing body to id or +// repository cannot seed the cache. Repository selection arguments establish +// context even for gh lookups which select only repository.id and issue.id. +func (p *GitHubProxy) observeGraphQLRepos(agent string, request, response []byte) { + g := parseScopedGraphQL(request) + if g == nil { + return + } + var payload struct { + Data map[string]any `json:"data"` + } + if json.Unmarshal(response, &payload) != nil { + return + } + canonical := func(set ast.SelectionSet, obj map[string]any) map[string]any { + result := make(map[string]any) + for _, f := range g.fields(set, 0) { + switch f.Name { + case "nameWithOwner", "name": + result[f.Name] = obj[f.Alias] + case "owner": + if owner, ok := obj[f.Alias].(map[string]any); ok { + for _, field := range g.fields(f.SelectionSet, 0) { + if field.Name == "login" { + result["owner"] = owner[field.Alias] + } + } + } + } + } + return result + } + remaining := 16384 // bound work even for overlapping/recursive selections + var walk func(ast.SelectionSet, any, string, int) + walk = func(set ast.SelectionSet, value any, repo string, depth int) { + if depth > 64 || remaining <= 0 { + return + } + remaining-- + if list, ok := value.([]any); ok { + for _, child := range list { + walk(set, child, repo, depth+1) + } + return + } + obj, ok := value.(map[string]any) + if !ok { + return + } + fields := g.fields(set, 0) + for _, f := range fields { + if f.Name == "nameWithOwner" { + if own, ok := obj[f.Alias].(string); ok && validGraphQLRepo(own) { + repo = own // a Repository returned through node/nodes + } + } + } + // A node selected outside repository{}, e.g. search/nodes, can + // identify itself with its repository sub-selection. + for _, f := range fields { + if f.Name == "repository" { + if child, ok := obj[f.Alias].(map[string]any); ok { + if own := graphQLRepoName(canonical(f.SelectionSet, child)); own != "" { + repo = own + } + } + } + } + for _, f := range fields { + child := obj[f.Alias] + if f.Name == "id" { + id, _ := child.(string) + p.graphQLRepos.put(agent, id, repo) + } + childRepo := repo + if f.Name == "repository" { + if r := graphQLRepoName(g.args(f)); r != "" { + childRepo = r + } + if obj, ok := child.(map[string]any); ok { + if r := graphQLRepoName(canonical(f.SelectionSet, obj)); r != "" { + childRepo = r + } + } + } + walk(f.SelectionSet, child, childRepo, depth+1) + } + } + walk(g.op.SelectionSet, payload.Data, "", 0) +} diff --git a/src/crux/proxy/graphql_repo_scope_test.go b/src/crux/proxy/graphql_repo_scope_test.go new file mode 100644 index 0000000000..29ff9543d1 --- /dev/null +++ b/src/crux/proxy/graphql_repo_scope_test.go @@ -0,0 +1,269 @@ +package proxy + +import ( + "bufio" + "bytes" + "compress/gzip" + "encoding/json" + "fmt" + "io" + "net" + "net/http" + "os" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/crux/agent" +) + +// Each exchange uses a fresh connection, as gh does between lookup and write. +// Wait for BOTH fake upstream and proxy to exit, including on assertion failure. +func graphQLScopeExchange(t *testing.T, p *GitHubProxy, request, reply []byte) (status int, headers http.Header, response string, reached bool) { + t.Helper() + // Go's HTTP transport (and gh) normally negotiates gzip. Keep that wire + // shape in the integration tests, not just an uncompressed mock response. + reply = gzipScopeResponse(t, reply) + client, proxyClient := net.Pipe() + upstream, proxyUpstream := net.Pipe() + for _, conn := range []net.Conn{client, proxyClient, upstream, proxyUpstream} { + _ = conn.SetDeadline(time.Now().Add(5 * time.Second)) + } + proxyDone := make(chan struct{}) + go func() { + defer close(proxyDone) + defer proxyClient.Close() + defer proxyUpstream.Close() + p.proxyHTTP(proxyClient, proxyUpstream, "schema", agent.ModeIssuesPRsMerge, agent.AgentCapabilities{}) + }() + forwarded := make(chan bool, 1) + go func() { + req, err := http.ReadRequest(bufio.NewReader(upstream)) + if err != nil { + forwarded <- false + return + } + body, err := io.ReadAll(req.Body) + _ = req.Body.Close() + if err != nil || !bytes.Equal(body, request) { + forwarded <- false + return + } + _, _ = fmt.Fprintf(upstream, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Encoding: gzip\r\nContent-Length: %d\r\n\r\n%s", len(reply), reply) + forwarded <- true + }() + defer func() { + _ = client.Close() + _ = upstream.Close() + <-proxyDone + reached = <-forwarded + }() + _, err := fmt.Fprintf(client, "POST /graphql HTTP/1.1\r\nHost: api.github.com\r\nAccept-Encoding: gzip\r\nContent-Length: %d\r\n\r\n%s", len(request), request) + if err != nil { + t.Fatal(err) + } + resp, err := http.ReadResponse(bufio.NewReader(client), nil) + if err != nil { + t.Fatal(err) + } + var responseReader io.Reader = resp.Body + if resp.Header.Get("Content-Encoding") == "gzip" { + decoded, err := gzip.NewReader(resp.Body) + if err != nil { + t.Fatal(err) + } + defer decoded.Close() + responseReader = decoded + } + var body []byte + if resp.StatusCode == http.StatusForbidden { + // Proxy denials are a single line with no Content-Length. The + // keep-alive proxy waits for another request, so waiting for EOF + // here deadlocks until the pipe deadline instead of reading a denial. + line, readErr := bufio.NewReader(responseReader).ReadString('\n') + body, err = []byte(line), readErr + _ = client.Close() + } else { + body, err = io.ReadAll(responseReader) + } + _ = resp.Body.Close() + if err != nil { + t.Fatal(err) + } + // Deferred teardown waits for response observation and sets reached + // before the caller can begin the next exchange. + return resp.StatusCode, resp.Header, string(body), false +} + +func scopeGraphQLBody(query string, variables any) []byte { + b, _ := json.Marshal(map[string]any{"query": query, "variables": variables}) + return b +} + +func TestGraphQLRepoScopeGHFlows(t *testing.T) { + // Scope is independent of autonomy. Other package tests register global + // overrides, which demote unattributed PR creates/merges to advisory. + // Snapshot and clear them so these flows exercise only repository scope; + // restore them afterwards, and do not run this test in parallel. + saved := map[any]any{} + autonomyRepoLevels.Range(func(k, v any) bool { + saved[k] = v + autonomyRepoLevels.Delete(k) + return true + }) + t.Cleanup(func() { + for k, v := range saved { + autonomyRepoLevels.Store(k, v) + } + }) + data, err := os.ReadFile("testdata/graphql-gh-repo-scope.json") + if err != nil { + t.Fatal(err) + } + var flows []struct { + Command string + Lookup, Response, Write json.RawMessage + } + if err := json.Unmarshal(data, &flows); err != nil { + t.Fatal(err) + } + for _, flow := range flows { + for _, inScope := range []bool{true, false} { + t.Run(fmt.Sprintf("%s/inScope=%v", flow.Command, inScope), func(t *testing.T) { + p := newTestProxy() + p.SetAgentRepoScopeFunc(func(_, repo string) bool { return repo == "acme/widgets" && inScope }) + // Cross-repo reads stay unrestricted, even when no repo is served. + status, _, body, reached := graphQLScopeExchange(t, p, flow.Lookup, flow.Response) + if status != 200 || !reached || body != string(flow.Response) { + t.Fatalf("lookup = %d, reached=%v, body=%s", status, reached, body) + } + status, headers, body, reached := graphQLScopeExchange(t, p, flow.Write, []byte(`{"data":{}}`)) + if inScope { + if status != 200 || !reached { + t.Fatalf("in-scope write = %d, reached=%v: %s", status, reached, body) + } + } else { + if status != 403 || reached { + t.Fatalf("out-of-scope write = %d, reached=%v: %s", status, reached, body) + } + if headers.Get(ProxyDenialHeader) != DenyReasonAgentRepoScope || headers.Get("X-Hive-Proxy-Blocked") != "true" || !strings.Contains(body, "NOT scoped to acme/widgets") { + t.Fatalf("not a REST-shaped scope denial: headers=%v body=%s", headers, body) + } + reason, _ := p.agentRepoScopeRefusal("schema", "POST", "/repos/acme/widgets") + wantBody := ProxyDenialBody("schema", agent.ModeIssuesPRsMerge, "POST", reason, DenyReasonAgentRepoScope) + if body != wantBody || headers.Get("Content-Type") != "text/plain" { + t.Fatalf("denial differs from REST: %s", body) + } + if p.AgentViolations("schema") != 1 { + t.Fatal("scope denial was not audited") + } + } + }) + } + t.Run(flow.Command+"/cold-cache", func(t *testing.T) { + p := newTestProxy() + p.SetAgentRepoScopeFunc(func(string, string) bool { return false }) + status, _, body, reached := graphQLScopeExchange(t, p, flow.Write, []byte(`{"data":{}}`)) + if status != 200 || !reached { + t.Fatalf("unresolved write broke existing behavior: %d %s", status, body) + } + }) + } +} + +func TestGraphQLRepoScopeArgumentsAndOperations(t *testing.T) { + p := newTestProxy() + p.SetAgentRepoScopeFunc(func(_, repo string) bool { return repo == "acme/open" }) + p.graphQLRepos.put("schema", "I_closed", "acme/closed") + for _, tc := range []struct { + name, query string + vars any + refused bool + }{ + {"inline input", `mutation { createCommitOnBranch(input:{branch:{repositoryNameWithOwner:"acme/closed",branchName:"main"},message:{headline:"hi"},expectedHeadOid:"abc"}){clientMutationId} }`, nil, true}, + {"inline cached node", `mutation { addComment(input:{subjectId:"I_closed",body:"hi"}){clientMutationId} }`, nil, true}, + {"variable input", `mutation($input:CreateCommitOnBranchInput!){createCommitOnBranch(input:$input){clientMutationId}}`, map[string]any{"input": map[string]any{"branch": map[string]any{"repositoryNameWithOwner": "acme/closed"}}}, true}, + {"repository selection", `mutation { repository(name:"closed",owner:"acme"){id} }`, nil, true}, + {"in scope", `mutation { repository(owner:"acme",name:"open"){id} }`, nil, false}, + {"all targets", `mutation { a:addComment(input:{subjectId:"I_unknown",body:"hi"}){clientMutationId} b:addComment(input:{subjectId:"I_closed",body:"hi"}){clientMutationId} }`, nil, true}, + {"fragment", `mutation { ...Write } fragment Write on Mutation { addComment(input:{subjectId:"I_closed",body:"hi"}){clientMutationId} }`, nil, true}, + {"default variable", `mutation($id:ID!="I_closed"){addComment(input:{subjectId:$id,body:"hi"}){clientMutationId}}`, nil, true}, + {"read", `query { repository(owner:"acme",name:"closed"){id} }`, nil, false}, + {"body is not a target", `mutation { addComment(input:{subjectId:"I_unknown",body:"acme/closed"}){clientMutationId} }`, nil, false}, + {"unused variables", `mutation { addComment(input:{subjectId:"I_unknown",body:"hi"}){clientMutationId} }`, map[string]any{"repo": "acme/closed"}, false}, + {"unresolved", `mutation { addComment(input:{subjectId:"I_unknown",body:"hi"}){clientMutationId} }`, nil, false}, + } { + t.Run(tc.name, func(t *testing.T) { + _, refused := p.graphQLAgentRepoScopeRefusal("schema", scopeGraphQLBody(tc.query, tc.vars)) + if refused != tc.refused { + t.Fatalf("refused=%v, want %v", refused, tc.refused) + } + }) + } + for _, selected := range []string{"Read", "Write"} { + body, _ := json.Marshal(map[string]any{"query": `query Read { repository(owner:"acme",name:"closed"){id} } mutation Write { addComment(input:{subjectId:"I_closed",body:"hi"}){clientMutationId} }`, "operationName": selected}) + _, refused := p.graphQLAgentRepoScopeRefusal("schema", body) + if refused != (selected == "Write") { + t.Fatalf("operationName=%s refused=%v", selected, refused) + } + } + if _, refused := p.graphQLAgentRepoScopeRefusal("schema", []byte(`not json`)); refused { + t.Fatal("unparseable request gained a new scope denial") + } +} + +func TestGraphQLRepoScopeCacheAttribution(t *testing.T) { + p := newTestProxy() + lookup := scopeGraphQLBody(`query($owner:String!,$name:String!){ r:repository(owner:$owner,name:$name){ key:id issue(number:1){ key:id } } }`, map[string]any{"owner": "acme", "name": "widgets"}) + p.observeGraphQLRepos("schema", lookup, []byte(`{"data":{"r":{"key":"R1","issue":{"key":"I1"}}}}`)) + if p.graphQLRepos.get("schema", "R1") != "acme/widgets" || p.graphQLRepos.get("schema", "I1") != "acme/widgets" { + t.Fatal("aliased lookup did not resolve IDs") + } + if p.graphQLRepos.get("other", "I1") != "" { + t.Fatal("cache leaked across agents") + } + + lookup = scopeGraphQLBody(`query { nodes(ids:["I2"]){ ... on Issue { id repository { nameWithOwner } } } }`, nil) + p.observeGraphQLRepos("schema", lookup, []byte(`{"data":{"nodes":[{"id":"I2","repository":{"nameWithOwner":"acme/other"}}]}}`)) + if p.graphQLRepos.get("schema", "I2") != "acme/other" { + t.Fatal("node's repository did not resolve") + } + // A caller can alias free-form body text to a trusted-looking key, but + // cannot turn it into an id or a repo in the cache. + lookup = scopeGraphQLBody(`query { repository(owner:"acme",name:"widgets"){issue(number:1){id:body}} }`, nil) + p.observeGraphQLRepos("schema", lookup, []byte(`{"data":{"repository":{"issue":{"id":"forged"}}}}`)) + if p.graphQLRepos.get("schema", "forged") != "" { + t.Fatal("body alias poisoned ID cache") + } + // A read with caller-chosen repo metadata must not rebind an existing ID. + p.graphQLAgentRepoScopeRefusal("schema", scopeGraphQLBody(`query { node(id:"I2"){id} }`, map[string]any{"repo": "acme/widgets"})) + if p.graphQLRepos.get("schema", "I2") != "acme/other" { + t.Fatal("request poisoned cache") + } + p.observeGraphQLRepos("schema", lookup, []byte(`not json`)) +} + +func TestGraphQLRepoScopeLivePolicyAndCacheBounds(t *testing.T) { + p := newTestProxy() + p.graphQLRepos.put("schema", "I", "acme/widgets") + body := scopeGraphQLBody(`mutation { closeIssue(input:{issueId:"I"}){clientMutationId} }`, nil) + for _, allowed := range []bool{true, false, true} { + p.SetAgentRepoScopeFunc(func(string, string) bool { return allowed }) + if _, refused := p.graphQLAgentRepoScopeRefusal("schema", body); refused == allowed { + t.Fatalf("scope update allowed=%v refused=%v", allowed, refused) + } + } + for i := 0; i < graphQLRepoCacheLimit+5; i++ { + p.graphQLRepos.put("schema", fmt.Sprint(i), "acme/widgets") + } + if len(p.graphQLRepos.repos) > graphQLRepoCacheLimit { + t.Fatal("unbounded cache") + } + capture := &graphQLRepoCapture{ReadCloser: io.NopCloser(strings.NewReader(strings.Repeat("x", graphQLRepoResponseLimit+1)))} + if _, err := io.Copy(io.Discard, capture); err != nil { + t.Fatal(err) + } + if !capture.tooLarge || capture.buf.Len() != 0 { + t.Fatal("oversized response was retained") + } +} diff --git a/src/crux/proxy/rules.go b/src/crux/proxy/rules.go index 9f8db7751f..e1a8b9ca6b 100644 --- a/src/crux/proxy/rules.go +++ b/src/crux/proxy/rules.go @@ -531,10 +531,10 @@ func repoAutoMergeOffReason(repo string) string { // - Independent of ACMM mode. Scope is roster membership, not autonomy: a // MERGE-mode agent is refused on a repo it does not serve exactly as firmly // as an ADVISORY one, which is why this is not another row in the mode table. -// - Same repo extraction as RepoFilterAllowed, and the same blind spot: a path -// with no repo in it (notably POST /graphql, whose target repo is in the -// body) is not matched. GraphQL writes stay gated by mode; closing that seam -// is a separate change and is called out in the docs rather than half-done. +// - Path-based extraction, as in RepoFilterAllowed. The GraphQL request arm +// separately resolves mutation targets from arguments and cached node IDs, +// then calls this same check with a synthetic /repos/{owner}/{name} path. +// Queries and unresolved mutations do not reach this scope check. // // serves is a live predicate rather than a snapshot so a scope edited in the // dashboard is in force on the very next request, with nothing to re-wire. Nil, diff --git a/src/crux/proxy/testdata/graphql-gh-repo-scope.json b/src/crux/proxy/testdata/graphql-gh-repo-scope.json new file mode 100644 index 0000000000..863e1766ab --- /dev/null +++ b/src/crux/proxy/testdata/graphql-gh-repo-scope.json @@ -0,0 +1,273 @@ +[ + { + "command": "gh issue create", + "lookup": { + "query": "query RepositoryInfo($owner:String!,$name:String!){repository(owner:$owner,name:$name){id}}", + "variables": { + "owner": "acme", + "name": "widgets" + } + }, + "response": { + "data": { + "repository": { + "id": "R_widgets" + } + } + }, + "write": { + "query": "mutation IssueCreate($input:CreateIssueInput!){createIssue(input:$input){issue{id url}}}", + "variables": { + "input": { + "repositoryId": "R_widgets", + "title": "A real title", + "body": "A useful description" + } + } + } + }, + { + "command": "gh issue comment", + "lookup": { + "query": "query IssueByNumber($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){issue(number:$number){id title url}}}", + "variables": { + "owner": "acme", + "name": "widgets", + "number": 42 + } + }, + "response": { + "data": { + "repository": { + "issue": { + "id": "I_widgets", + "title": "A real title", + "url": "https://github.com/acme/widgets/issues/42" + } + } + } + }, + "write": { + "query": "mutation CommentAdd($input:AddCommentInput!){addComment(input:$input){commentEdge{node{url}}}}", + "variables": { + "input": { + "subjectId": "I_widgets", + "body": "A useful comment" + } + } + } + }, + { + "command": "gh issue edit", + "lookup": { + "query": "query IssueByNumber($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){issue(number:$number){id title url}}}", + "variables": { + "owner": "acme", + "name": "widgets", + "number": 42 + } + }, + "response": { + "data": { + "repository": { + "issue": { + "id": "I_widgets", + "title": "A real title", + "url": "https://github.com/acme/widgets/issues/42" + } + } + } + }, + "write": { + "query": "mutation IssueUpdate($input:UpdateIssueInput!){updateIssue(input:$input){issue{id}}}", + "variables": { + "input": { + "id": "I_widgets", + "title": "An updated title" + } + } + } + }, + { + "command": "gh issue close", + "lookup": { + "query": "query IssueByNumber($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){issue(number:$number){id state}}}", + "variables": { + "owner": "acme", + "name": "widgets", + "number": 42 + } + }, + "response": { + "data": { + "repository": { + "issue": { + "id": "I_widgets", + "state": "OPEN" + } + } + } + }, + "write": { + "query": "mutation IssueClose($input:CloseIssueInput!){closeIssue(input:$input){issue{id}}}", + "variables": { + "input": { + "issueId": "I_widgets", + "stateReason": "COMPLETED" + } + } + } + }, + { + "command": "gh pr merge", + "lookup": { + "query": "query PullRequestByNumber($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){pullRequest(number:$number){id state headRefOid}}}", + "variables": { + "owner": "acme", + "name": "widgets", + "number": 43 + } + }, + "response": { + "data": { + "repository": { + "pullRequest": { + "id": "PR_widgets", + "state": "OPEN", + "headRefOid": "abc123" + } + } + } + }, + "write": { + "query": "mutation PullRequestMerge($input:MergePullRequestInput!){mergePullRequest(input:$input){pullRequest{id}}}", + "variables": { + "input": { + "pullRequestId": "PR_widgets", + "mergeMethod": "SQUASH" + } + } + } + }, + { + "command": "gh pr create", + "lookup": { + "query": "query RepositoryInfo($owner:String!,$name:String!){repository(owner:$owner,name:$name){id}}", + "variables": { + "owner": "acme", + "name": "widgets" + } + }, + "response": { + "data": { + "repository": { + "id": "R_widgets" + } + } + }, + "write": { + "query": "mutation PullRequestCreate($input:CreatePullRequestInput!){createPullRequest(input:$input){pullRequest{id url}}}", + "variables": { + "input": { + "repositoryId": "R_widgets", + "title": "A real title", + "body": "A useful description", + "baseRefName": "main", + "headRefName": "feature" + } + } + } + }, + { + "command": "gh pr comment", + "lookup": { + "query": "query PullRequestByNumber($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){pullRequest(number:$number){id state headRefOid}}}", + "variables": { + "owner": "acme", + "name": "widgets", + "number": 43 + } + }, + "response": { + "data": { + "repository": { + "pullRequest": { + "id": "PR_widgets", + "state": "OPEN", + "headRefOid": "abc123" + } + } + } + }, + "write": { + "query": "mutation CommentAdd($input:AddCommentInput!){addComment(input:$input){commentEdge{node{url}}}}", + "variables": { + "input": { + "subjectId": "PR_widgets", + "body": "A useful comment" + } + } + } + }, + { + "command": "gh pr edit", + "lookup": { + "query": "query PullRequestByNumber($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){pullRequest(number:$number){id state headRefOid}}}", + "variables": { + "owner": "acme", + "name": "widgets", + "number": 43 + } + }, + "response": { + "data": { + "repository": { + "pullRequest": { + "id": "PR_widgets", + "state": "OPEN", + "headRefOid": "abc123" + } + } + } + }, + "write": { + "query": "mutation PullRequestUpdate($input:UpdatePullRequestInput!){updatePullRequest(input:$input){pullRequest{id}}}", + "variables": { + "input": { + "pullRequestId": "PR_widgets", + "title": "An updated title" + } + } + } + }, + { + "command": "gh pr close", + "lookup": { + "query": "query PullRequestByNumber($owner:String!,$name:String!,$number:Int!){repository(owner:$owner,name:$name){pullRequest(number:$number){id state headRefOid}}}", + "variables": { + "owner": "acme", + "name": "widgets", + "number": 43 + } + }, + "response": { + "data": { + "repository": { + "pullRequest": { + "id": "PR_widgets", + "state": "OPEN", + "headRefOid": "abc123" + } + } + } + }, + "write": { + "query": "mutation PullRequestClose($input:ClosePullRequestInput!){closePullRequest(input:$input){pullRequest{id}}}", + "variables": { + "input": { + "pullRequestId": "PR_widgets" + } + } + } + } +] diff --git a/src/crux/proxy/testdata/graphql-gh-repo-scope.md b/src/crux/proxy/testdata/graphql-gh-repo-scope.md new file mode 100644 index 0000000000..07df33f2e3 --- /dev/null +++ b/src/crux/proxy/testdata/graphql-gh-repo-scope.md @@ -0,0 +1,17 @@ +# gh repository-scope document fixtures + +`graphql-gh-repo-scope.json` preserves the lookup → mutation document shapes for +`gh issue create/comment/edit/close` and `gh pr create/comment/edit/close/merge`. +These are normalized fixtures, not live credential-bearing traffic captures: +selection sets are reduced to attribution-relevant fields, IDs and content are +synthetic, and JSON whitespace is not significant. The named mutations and input +shapes follow cli/cli's `api/queries_issue.go`, `api/queries_pr.go` and generated +GraphQL operations (reference: cli/cli commit +`acee710547dcf7dfb6490afea77972187e74b819`). + +Each fixture records the lookup request, its GitHub response and the subsequent +write request. The proxy integration test replays them on separate connections, +checks that reads and in-scope writes reach upstream unchanged, and checks that +out-of-scope writes never reach upstream. It also replays each ID-only mutation +without the lookup to lock down the explicitly fail-open unresolved-ID policy. +These tests do not execute gh or contact GitHub. diff --git a/src/docs/security-model.md b/src/docs/security-model.md index 1369adfb44..974d5c228d 100644 --- a/src/docs/security-model.md +++ b/src/docs/security-model.md @@ -94,6 +94,44 @@ are enabled on a given hive. | Supply-chain guards | supply chain | `.github/workflows/*.yml`, `src/Dockerfile` | PR checks | | DCO on every commit | unattributed code | `src/scripts/check-dco-trailers.sh` | DCO check on PR | +### GraphQL repository scope + +The GitHub MITM proxy applies the **same per-agent repository scope predicate** +to attributable GraphQL mutations as to REST writes, independently of autonomy +mode. GraphQL queries are not scope-restricted: duplicate detection, linking, +search and cross-repository lookups continue to work. The selected operation +(`operationName`, when present), its arguments and variable defaults determine +which mutation inputs are inspected; comment bodies and unused variables are +not repository evidence. + +Mutation targets can name a repository directly (including +`repositoryNameWithOwner` or `repository(owner:, name:)` arguments), or use a +repository/issue/PR/comment node ID. The proxy learns ID-to-repository mappings +from successful GitHub GraphQL responses (identity or gzip), following repository +selections and their arguments, aliases and fragments. The bounded in-memory +cache is isolated by agent, shared across that agent's connections and lost +on proxy restart; it holds at most 8,192 entries and resets when full. Responses +larger than 4 MiB on the wire or after decompression, and unsupported encodings, +are relayed normally but do not seed the cache. Requests alone +cannot seed it. Scope changes take effect on the next write, without invalidating +the ID cache. Every resolved target is checked, including batched mutations. +An out-of-scope target returns REST's `403`, `X-Hive-Proxy-Blocked: true`, +`X-Hive-Proxy-Reason: agent_repo_scope` and standard denial body/violation audit. +The internal control-plane caller retains its exemption. + +**Unresolved mutation targets remain fail-open for repository scope only.** +This preserves ID-only `gh api graphql` calls, cold-cache/restarted sessions, +and document/response shapes the proxy cannot attribute. Normal `gh` lookup → +create/comment/edit/close/merge sequences are covered by document fixtures and +proxy integration tests; the preceding reads populate the cache even when the +repository is outside the agent's scope. Fail-closed is intentionally not chosen: +there is no guarantee every legitimate ID-only write has a preceding observable +lookup. Existing mode, write-surface and auto-merge gates still apply to unresolved +writes. Consequently this best-effort attribution is **not a complete security +boundary against an agent deliberately using previously unseen IDs**; token +repository permissions remain necessary to bound that case. This change does +not extend repository pause or the hive-wide REST allowlist to GraphQL. + ## 3. Agent runtime isolation **Bounded runs.** Work arrives as discrete tasks from the governor. By default