Git designed for AI agents.
graft: a revision is one line to git, and agents cannot make it two
Federico Eliaf99096690c8d
diff --git a/doc/graft/AGENT-SECURITY.md b/doc/graft/AGENT-SECURITY.mdindex 2f311e4..5df6cd7 100644--- a/doc/graft/AGENT-SECURITY.md+++ b/doc/graft/AGENT-SECURITY.md@@ -81,6 +81,35 @@ agents may call by default, so a new field cannot escape the filter; RPCs addedwith `graft.agent_rpcs` are checked by field name. Other callers still seegraft-verify's green references, which are documented for consumers.+Revisions reach git one per line on its standard input, so the line is what git+reads, not what the request said. Four layers keep that from being a way past+the hiding:++1. **Input validation (every caller).** A revision with an ASCII control+ character (below space, or DEL) is refused with `InvalidArgument`, the+ pseudo-revisions `--glob=`, `--branches=` and `--tags=` and their patterns+ included, and so is a line feed or carriage return in the path of a+ path-scoped revision (`rev:path`). Before this, those patterns took any bytes,+ and `--glob=x` followed by a line feed and `refs/graft/green/main` was a+ second revision, naming a hidden reference that the request check had read as+ part of the pattern. Compatibility: this applies to other callers too. A+ control character in a revision never named a reference (`git+ check-ref-format` forbids them), and a line break in the path of+ `ListBlobs` or `ListLFSPointers` could not be read by `git rev-list --stdin`+ as a path anyway. The three RPCs whose revisions git reads NUL-delimited+ (`git cat-file -Z`: `CheckObjectsExist`, `DiffBlobs`, `CheckBlobsGenerated`) keep+ accepting a line break in a path (`AllowLineBreaksInPath`), as their tests+ require. A path with a tab, a space or other special characters is allowed+ everywhere.+2. **The sink.** `gitpipe.Revlist`, the one place that joins revisions into+ `git rev-list --stdin`, refuses a revision with a line feed, a carriage return+ or a NUL, whatever its caller validated.+3. **The filter reads lines as git does.** The `--exclude-hidden` flag is put+ before a line that git reads as `--all` or `--glob=...`: without its line+ feed and the carriage return before it, and cut at the first NUL.+4. **The request check splits at the same separators.** `RevisionHidden` also+ splits at line feed, carriage return, tab and NUL.+What the hiding covers, and what it does not: it covers the reference **names**under `refs/graft/` and the reachability that goes through them. An agent canneither address those references nor walk to their objects through them, so itdiff --git a/internal/command/command.go b/internal/command/command.goindex 97d0a40..abdad86 100644--- a/internal/command/command.go+++ b/internal/command/command.go@@ -388,13 +388,16 @@ func New(ctx context.Context, logger log.Logger, nameAndArgs []string, opts ...O// If requested, we will set up the command such that `Write()` can be called on it directly. Otherwise,// we simply pass as stdin whatever the user has asked us to set up. If no `stdin` was set up, the command// will implicitly read from `/dev/null`.+ // startStdinCopy starts the copy of a filtered stdin, once the process started: until then+ // nothing runs that could outlive a command that failed to start.+ startStdinCopy := func() {}switch _, ok := cfg.stdin.(stdinSentinel); {case ok && cfg.stdinFilter != nil:pipe, err := cmd.StdinPipe()if err != nil {return nil, fmt.Errorf("creating stdin pipe: %w", err)}- command.writer = filteredStdin(pipe, cfg.stdinFilter)+ command.writer, startStdinCopy = filteredStdin(pipe, cfg.stdinFilter)case ok:pipe, err := cmd.StdinPipe()if err != nil {@@ -437,6 +440,7 @@ func New(ctx context.Context, logger log.Logger, nameAndArgs []string, opts ...Oif err := cmd.Start(); err != nil {return nil, fmt.Errorf("starting process %v: %w", cmd.Args, err)}+ startStdinCopy()go trackMaxRssAnon(ctx, cmd.Process.Pid, &command.maxRssAnon, command.processExitedCh)@@ -834,23 +838,26 @@ func (c *Command) exitStatus() (int, syscall.Signal) {}// filteredStdin returns the writer of a command's stdin that reaches pipe, the stdin of the-// process, through filter. A goroutine copies the filtered stream into pipe and closes pipe when-// the writer is closed. When the process stops reading, the copy fails, and the writes after that-// fail with its error, as writes to pipe itself would: a writer never blocks on a process that is-// gone.-func filteredStdin(pipe io.WriteCloser, filter func(io.Reader) io.Reader) io.WriteCloser {+// process, through filter, and the function that starts the copy, which the caller calls once the+// process started. The copy is a goroutine that copies the filtered stream into pipe and closes+// pipe when the writer is closed. When the process stops reading, the copy fails, and the writes+// after that fail with its error, as writes to pipe itself would: a writer never blocks on a+// process that is gone. A command that fails to start never calls the function, and so leaves no+// goroutine behind.+func filteredStdin(pipe io.WriteCloser, filter func(io.Reader) io.Reader) (io.WriteCloser, func()) {r, w := io.Pipe()- go func() {- _, err := io.Copy(pipe, filter(r))- if closeErr := pipe.Close(); err == nil {- err = closeErr- }- if err == nil {- err = io.ErrClosedPipe- }- r.CloseWithError(err)- }()- return w+ return w, func() { go copyFilteredStdin(r, pipe, filter) }+}++func copyFilteredStdin(r *io.PipeReader, pipe io.WriteCloser, filter func(io.Reader) io.Reader) {+ _, err := io.Copy(pipe, filter(r))+ if closeErr := pipe.Close(); err == nil {+ err = closeErr+ }+ if err == nil {+ err = io.ErrClosedPipe+ }+ r.CloseWithError(err)}type stdinSentinel struct{}diff --git a/internal/command/command_test.go b/internal/command/command_test.goindex 19063c0..0a2290f 100644--- a/internal/command/command_test.go+++ b/internal/command/command_test.go@@ -13,6 +13,7 @@ import ("runtime""strings""sync"+ "sync/atomic""syscall""testing""testing/iotest"@@ -270,6 +271,35 @@ func TestNew_stdinFilter(t *testing.T) {require.NoError(t, cmd.Wait())}+// TestNew_stdinFilterStartsAfterTheProcess: a command that fails to start leaves no goroutine that+// copies a filtered stdin: the copy, which blocks until the writer is closed, starts only when the+// process did.+func TestNew_stdinFilterStartsAfterTheProcess(t *testing.T) {+ t.Parallel()++ ctx := testhelper.Context(t)+ var filtered atomic.Bool+ filter := func(r io.Reader) io.Reader {+ filtered.Store(true)+ return r+ }++ _, err := New(ctx, testhelper.SharedLogger(t), []string{"/no/such/binary"}, WithSetupStdin(), WithStdinFilter(filter))+ require.Error(t, err)+ time.Sleep(200 * time.Millisecond)+ require.False(t, filtered.Load(), "the filter ran although the process never started")++ // And it does run once the process started.+ var buf bytes.Buffer+ cmd, err := New(ctx, testhelper.SharedLogger(t), []string{"cat"}, WithSetupStdin(), WithStdinFilter(filter), WithStdout(&buf))+ require.NoError(t, err)+ _, err = fmt.Fprint(cmd, "x")+ require.NoError(t, err)+ require.NoError(t, cmd.Wait())+ require.True(t, filtered.Load())+ require.Equal(t, "x", buf.String())+}+func TestCommand_closeStdin(t *testing.T) {t.Parallel()diff --git a/internal/git/gitcmd/agent_walk.go b/internal/git/gitcmd/agent_walk.goindex bd025ea..afdbfd6 100644--- a/internal/git/gitcmd/agent_walk.go+++ b/internal/git/gitcmd/agent_walk.go@@ -95,10 +95,21 @@ func readsRevisionsFromStdin(sc Command) bool {return false}+// gitStdinLine returns the line of a revision list on git's stdin as git reads it: without its line+// feed and the carriage return before it (strbuf_getline), and cut at the first NUL, which ends a+// C string. A filter that compares anything else would be told "--all\r" is not "--all" when git+// takes it for "--all".+func gitStdinLine(line string) string {+ line = strings.TrimSuffix(line, "\n")+ line = strings.TrimSuffix(line, "\r")+ line, _, _ = strings.Cut(line, "\x00")+ return line+}+// excludeHiddenRefsOnStdin is the stdin of a command that reads revisions there, one per line,-// with excludeHiddenFlag on a line of its own before each --all and --glob=<pattern>. A line "--"-// ends the revisions: paths follow, which are left alone. It reads r as it is read, a line at a-// time, and runs nothing of its own.+// with excludeHiddenFlag on a line of its own before each --all and --glob=<pattern>, the lines+// parsed as git parses them (gitStdinLine). A line "--" ends the revisions: paths follow, which are+// left alone. It reads r as it is read, a line at a time, and runs nothing of its own.func excludeHiddenRefsOnStdin(r io.Reader) io.Reader {return &excludeHiddenReader{in: bufio.NewReader(r)}}@@ -118,7 +129,7 @@ func (r *excludeHiddenReader) Read(p []byte) (int, error) {}var line stringline, r.err = r.in.ReadString('\n')- revision := strings.TrimSuffix(line, "\n")+ revision := gitStdinLine(line)if !r.paths && (revision == "--all" || strings.HasPrefix(revision, "--glob=")) {r.pending = append(r.pending, excludeHiddenFlag+"\n"...)}diff --git a/internal/git/gitcmd/agent_walk_git_test.go b/internal/git/gitcmd/agent_walk_git_test.goindex 6e8169b..3e874f8 100755--- a/internal/git/gitcmd/agent_walk_git_test.go+++ b/internal/git/gitcmd/agent_walk_git_test.go@@ -83,6 +83,22 @@ func TestAgentWalk_leavesHiddenRefsOut(t *testing.T) {})}+ // Lines that git reads as "--all" or "--glob=...", written so that a filter that compares+ // anything else lets them through (review of b985c82): a carriage return, a NUL.+ for desc, stdin := range map[string]string{+ "--all with a carriage return": "--all\r\n",+ "--all with a NUL": "--all\x00\n",+ "--all with a NUL and text": "--all\x00junk\n",+ "--glob with a carriage": "--glob=refs/*\r\n",+ "--glob after another line": "main\n--glob=refs/*\r\n",+ } {+ t.Run("stdin "+desc, func(t *testing.T) {+ sc := gitcmd.Command{Name: "rev-list", Flags: []gitcmd.Option{gitcmd.Flag{Name: "--stdin"}}}+ require.NotContains(t, run(t, agent, sc, stdin), secret.String())+ require.Contains(t, run(t, ctx, sc, stdin), secret.String(), "another caller's walk reaches it")+ })+ }+t.Run("--branches is left alone", func(t *testing.T) {out := run(t, agent, gitcmd.Command{Name: "rev-list", Args: []string{"--branches"}}, "")require.Contains(t, out, main.String())diff --git a/internal/git/gitcmd/agent_walk_test.go b/internal/git/gitcmd/agent_walk_test.goindex fb82e18..1b0cfd5 100755--- a/internal/git/gitcmd/agent_walk_test.go+++ b/internal/git/gitcmd/agent_walk_test.go@@ -67,6 +67,45 @@ func TestExcludeHiddenRefs(t *testing.T) {require.Contains(t, args, excludeHiddenFlag)}+// TestGitStdinLine: a line is what git reads: without its line feed, without the carriage return+// before it, and cut at the first NUL.+func TestGitStdinLine(t *testing.T) {+ t.Parallel()++ for in, want := range map[string]string{+ "--all": "--all",+ "--all\n": "--all",+ "--all\r\n": "--all",+ "--all\r": "--all",+ "--all\x00": "--all",+ "--all\x00\n": "--all",+ "--all\x00junk\r\n": "--all",+ "--all\r\r\n": "--all\r",+ "--glob=x\x00y\n": "--glob=x",+ "main\n": "main",+ "": "",+ } {+ require.Equal(t, want, gitStdinLine(in), "%q", in)+ }+}++// TestExcludeHiddenRefsOnStdin_linesAsGitReadsThem: the lines "--all\r", "--all\x00" and the like are+// "--all" to git, so each gets the flag before it, and "--glob=" with them too.+func TestExcludeHiddenRefsOnStdin_linesAsGitReadsThem(t *testing.T) {+ t.Parallel()++ for _, line := range []string{"--all\r", "--all\x00", "--all\x00junk", "--glob=refs/*\r", "--glob=refs/*\x00x"} {+ out, err := io.ReadAll(excludeHiddenRefsOnStdin(strings.NewReader("main\n" + line + "\nmain2\n")))+ require.NoError(t, err)+ require.Equal(t, "main\n"+excludeHiddenFlag+"\n"+line+"\nmain2\n", string(out), "%q", line)+ }++ // After a line "--" with a carriage return too, the lines are paths.+ out, err := io.ReadAll(excludeHiddenRefsOnStdin(strings.NewReader("--\r\n--all\n")))+ require.NoError(t, err)+ require.Equal(t, "--\r\n--all\n", string(out))+}+// TestExcludeHiddenRefsOnStdin: on stdin, the flag goes on a line before each --all and --glob=,// until the line "--" after which paths follow; the stream is otherwise unchanged, read in any// chunks, and an error of the input reaches the reader.diff --git a/internal/git/gitpipe/revision.go b/internal/git/gitpipe/revision.goindex 183d539..1a101ce 100644--- a/internal/git/gitpipe/revision.go+++ b/internal/git/gitpipe/revision.go@@ -296,6 +296,18 @@ func Revlist(go func() {defer close(resultChan)+ // The revisions go to git-rev-list(1) one per line on its stdin: whatever a caller validated,+ // a revision with a line break is more than one revision, and one with a NUL is read only up+ // to it. The guard is here, at the sink, so that it holds for every caller.+ for _, revision := range revisions {+ if strings.ContainsAny(revision, "\n\r\x00") {+ sendRevisionResult(ctx, resultChan, RevisionResult{+ err: fmt.Errorf("revision %q: a revision can't contain a line break or NUL", revision),+ })+ return+ }+ }+if cfg.trailers && cfg.objects {sendRevisionResult(ctx, resultChan, RevisionResult{err: fmt.Errorf("WithTrailers is incompatible with WithObjects"),diff --git a/internal/git/gitpipe/revision_test.go b/internal/git/gitpipe/revision_test.goindex 0b47145..b9a2a31 100644--- a/internal/git/gitpipe/revision_test.go+++ b/internal/git/gitpipe/revision_test.go@@ -1052,3 +1052,40 @@ func TestRevisionResult_IsBlob(t *testing.T) {})}}++// TestRevlist_refusesRevisionsThatAreMoreThanOneLine: revisions go to git-rev-list(1) one per line+// on its stdin, so a revision with a line feed, a carriage return or a NUL is refused at the sink,+// whatever the caller validated: it would be another revision, or cut short.+func TestRevlist_refusesRevisionsThatAreMoreThanOneLine(t *testing.T) {+ t.Parallel()++ ctx := testhelper.Context(t)+ cfg := testcfg.Build(t)+ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{SkipCreationViaService: true})+ repo := localrepo.NewTestRepo(t, cfg, repoProto)+ commit := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"))+ hidden := gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(commit), gittest.WithReference("refs/graft/green/main"))++ for desc, revisions := range map[string][]string{+ "a line feed": {"--glob=x\nrefs/graft/green/main"},+ "a carriage return": {"main\rrefs/graft/green/main"},+ "a NUL": {"--all\x00"},+ "after a good one": {"main", "main:path\nrefs/graft/green/main"},+ "a pattern and a NL": {"--branches=x\n" + hidden.String()},+ } {+ it := Revlist(ctx, repo, revisions)+ for it.Next() {+ require.FailNow(t, "a revision was listed", "%s: %v", desc, it.Result())+ }+ require.ErrorContains(t, it.Err(), "a revision can't contain a line break or NUL", desc)+ }++ // Control: ordinary revisions are listed.+ it := Revlist(ctx, repo, []string{"main"})+ var listed []string+ for it.Next() {+ listed = append(listed, it.Result().OID.String())+ }+ require.NoError(t, it.Err())+ require.Equal(t, []string{commit.String()}, listed)+}diff --git a/internal/git/revision.go b/internal/git/revision.goindex 1206e80..d043371 100644--- a/internal/git/revision.go+++ b/internal/git/revision.go@@ -9,6 +9,7 @@ type validateRevisionConfig struct {allowEmpty boolallowPathScopedRevision boolallowPseudoRevisions bool+ allowLineBreaksInPath bool}// ValidateRevisionOption is an option that can be passed to ValidateRevision.@@ -23,14 +24,25 @@ func AllowEmptyRevision() ValidateRevisionOption {}// AllowPathScopedRevision changes ValidateRevision to allow path-scoped revisions like-// `HEAD:README.md`. Note that path-scoped revisions may contain any character except for NUL bytes.-// Most importantly, a path-scoped revision may contain newlines.+// `HEAD:README.md`. The path of a path-scoped revision may contain any character except for NUL+// bytes and, unless AllowLineBreaksInPath is given, line breaks: revisions travel to git one per+// line on its standard input (git-rev-list(1) --stdin), so a line break in a revision would be+// another revision, and NUL ends the line for git.func AllowPathScopedRevision() ValidateRevisionOption {return func(cfg *validateRevisionConfig) {cfg.allowPathScopedRevision = true}}+// AllowLineBreaksInPath changes ValidateRevision to allow a line feed or a carriage return in the+// path of a path-scoped revision. Only a caller whose revisions git reads NUL-delimited may give+// it (git-cat-file(1) -Z): for any other, a line break ends the revision.+func AllowLineBreaksInPath() ValidateRevisionOption {+ return func(cfg *validateRevisionConfig) {+ cfg.allowLineBreaksInPath = true+ }+}+// AllowPseudoRevision changes ValidateRevision to allow pseudo-revisions understood by// git-rev-list(1). This includes options like `--all`, `--not`, `--branches` or `--glob`.func AllowPseudoRevision() ValidateRevisionOption {@@ -47,25 +59,13 @@ func ValidateRevision(revision []byte, opts ...ValidateRevisionOption) error {opt(&cfg)}- if cfg.allowPseudoRevisions {- switch {- case bytes.Equal(revision, []byte("--all")):- return nil- case bytes.Equal(revision, []byte("--not")):- return nil- case bytes.Equal(revision, []byte("--branches")):- return nil- case bytes.Equal(revision, []byte("--tags")):- return nil- case bytes.Equal(revision, []byte("--alternate-refs")):- return nil- case bytes.HasPrefix(revision, []byte("--branches=")):- return nil- case bytes.HasPrefix(revision, []byte("--tags=")):- return nil- case bytes.HasPrefix(revision, []byte("--glob=")):- return nil+ if cfg.allowPseudoRevisions && isPseudoRevision(revision) {+ // The pattern after "=" is a reference pattern: it has no control character, and above all+ // no line break or NUL, which would make the rest of it another revision on git's stdin.+ if hasControlCharacter(revision) {+ return fmt.Errorf("revision can't contain control characters")}+ return nil}if bytes.HasPrefix(revision, []byte("-")) {@@ -76,9 +76,13 @@ func ValidateRevision(revision []byte, opts ...ValidateRevisionOption) error {}if cfg.allowPathScopedRevision {- // We don't need to validate the path component, if any, given that it may contain- // all bytes except for the NUL byte which we already checked for above.- revision, _, _ = bytes.Cut(revision, []byte(":"))+ // The path component, if any, may contain all bytes except for the NUL byte which we+ // already checked for above, and line breaks, which separate revisions on git's stdin.+ var path []byte+ revision, path, _ = bytes.Cut(revision, []byte(":"))+ if !cfg.allowLineBreaksInPath && bytes.ContainsAny(path, "\n\r") {+ return fmt.Errorf("path of a revision can't contain a line break")+ }}if !cfg.allowEmpty && len(revision) == 0 {@@ -87,6 +91,9 @@ func ValidateRevision(revision []byte, opts ...ValidateRevisionOption) error {if bytes.ContainsAny(revision, " \t\n\r") {return fmt.Errorf("revision can't contain whitespace")}+ if hasControlCharacter(revision) {+ return fmt.Errorf("revision can't contain control characters")+ }if bytes.Contains(revision, []byte(":")) {return fmt.Errorf("revision can't contain ':'")}@@ -96,3 +103,31 @@ func ValidateRevision(revision []byte, opts ...ValidateRevisionOption) error {return nil}++// isPseudoRevision reports whether revision is a pseudo-revision understood by git-rev-list(1) that+// AllowPseudoRevision allows.+func isPseudoRevision(revision []byte) bool {+ switch {+ case bytes.Equal(revision, []byte("--all")),+ bytes.Equal(revision, []byte("--not")),+ bytes.Equal(revision, []byte("--branches")),+ bytes.Equal(revision, []byte("--tags")),+ bytes.Equal(revision, []byte("--alternate-refs")),+ bytes.HasPrefix(revision, []byte("--branches=")),+ bytes.HasPrefix(revision, []byte("--tags=")),+ bytes.HasPrefix(revision, []byte("--glob=")):+ return true+ }+ return false+}++// hasControlCharacter reports whether b holds an ASCII control character: below space, or DEL.+// References cannot contain one (git check-ref-format), so no revision of a reference does.+func hasControlCharacter(b []byte) bool {+ for _, c := range b {+ if c < 0x20 || c == 0x7f {+ return true+ }+ }+ return false+}diff --git a/internal/git/revision_test.go b/internal/git/revision_test.goindex 76fab16..5189158 100644--- a/internal/git/revision_test.go+++ b/internal/git/revision_test.go@@ -98,12 +98,118 @@ func TestValidateRevision(t *testing.T) {expectedErr: fmt.Errorf("empty revision"),},{- desc: "path-scoped path may contain arbitrary characters",- revision: "refs/heads/main:path\n\t\r :\\",+ desc: "path-scoped path may contain whitespace and special characters",+ revision: "refs/heads/main:path\t :\\\x01\x7f",opts: []ValidateRevisionOption{AllowPathScopedRevision(),},},+ {+ // Revisions travel to git one per line on its stdin: a line break in the path would be+ // another revision (a reference the request check never saw).+ desc: "path-scoped path does not allow a line feed",+ revision: "refs/heads/main:path\nrefs/graft/green/main",+ opts: []ValidateRevisionOption{+ AllowPathScopedRevision(),+ },+ expectedErr: fmt.Errorf("path of a revision can't contain a line break"),+ },+ {+ // git-cat-file -Z reads revisions NUL-delimited, so its callers may keep a line break.+ desc: "path-scoped path allows a line break with the option",+ revision: "refs/heads/main:path\nfoo\r",+ opts: []ValidateRevisionOption{+ AllowPathScopedRevision(),+ AllowLineBreaksInPath(),+ },+ },+ {+ desc: "the option allows no line break before the path",+ revision: "refs/heads\nmain:path",+ opts: []ValidateRevisionOption{+ AllowPathScopedRevision(),+ AllowLineBreaksInPath(),+ },+ expectedErr: fmt.Errorf("revision can't contain whitespace"),+ },+ {+ desc: "the option allows no NUL in the path",+ revision: "refs/heads/main:path\x00",+ opts: []ValidateRevisionOption{+ AllowPathScopedRevision(),+ AllowLineBreaksInPath(),+ },+ expectedErr: fmt.Errorf("revision can't contain NUL"),+ },+ {+ desc: "path-scoped path does not allow a carriage return",+ revision: "refs/heads/main:path\r",+ opts: []ValidateRevisionOption{+ AllowPathScopedRevision(),+ },+ expectedErr: fmt.Errorf("path of a revision can't contain a line break"),+ },+ {+ desc: "path-scoped path does not allow NUL",+ revision: "refs/heads/main:path\x00",+ opts: []ValidateRevisionOption{+ AllowPathScopedRevision(),+ },+ expectedErr: fmt.Errorf("revision can't contain NUL"),+ },+ {+ desc: "control character in a revision",+ revision: "refs/heads/ma\x01in",+ expectedErr: fmt.Errorf("revision can't contain control characters"),+ },+ {+ desc: "DEL in a revision",+ revision: "refs/heads/ma\x7fin",+ expectedErr: fmt.Errorf("revision can't contain control characters"),+ },+ {+ desc: "control character in a path-scoped revision before the path",+ revision: "refs/heads/ma\x01in:path",+ opts: []ValidateRevisionOption{+ AllowPathScopedRevision(),+ },+ expectedErr: fmt.Errorf("revision can't contain control characters"),+ },+ {+ desc: "pseudo-revision pattern with a line feed",+ revision: "--glob=x\nrefs/graft/green/main",+ opts: []ValidateRevisionOption{+ AllowPseudoRevision(),+ },+ expectedErr: fmt.Errorf("revision can't contain control characters"),+ },+ {+ // Not the pseudo-revision --all: a different word, refused as any option is.+ desc: "pseudo-revision with a carriage return",+ revision: "--all\r",+ opts: []ValidateRevisionOption{+ AllowPseudoRevision(),+ },+ expectedErr: fmt.Errorf("revision can't start with '-'"),+ },+ {+ // Not the pseudo-revision --all: a different word, refused as any option is.+ desc: "pseudo-revision with NUL",+ revision: "--all\x00",+ opts: []ValidateRevisionOption{+ AllowPseudoRevision(),+ },+ expectedErr: fmt.Errorf("revision can't start with '-'"),+ },+ {+ desc: "--branches= pattern with a line feed",+ revision: "--branches=x\nrefs/graft/proposals/0123456789abcdef/1",+ opts: []ValidateRevisionOption{+ AllowPseudoRevision(),+ AllowPathScopedRevision(),+ },+ expectedErr: fmt.Errorf("revision can't contain control characters"),+ },{desc: "disallowed pseudo-revision",revision: "--all",diff --git a/internal/gitaly/server/auth/agent_refs_test.go b/internal/gitaly/server/auth/agent_refs_test.goindex d309d2c..cdfb9a5 100755--- a/internal/gitaly/server/auth/agent_refs_test.go+++ b/internal/gitaly/server/auth/agent_refs_test.go@@ -50,9 +50,13 @@ func TestAgentRefFields_everyAllowedRPCIsClassified(t *testing.T) {unclassified = append(unclassified, string(m.FullName())+"."+string(fd.Name())+" ("+method+")")}case protoreflect.MessageKind, protoreflect.GroupKind:- if !fd.IsMap() {- walk(fd.Message())+ if fd.IsMap() {+ // The filter does not look into maps: a map field of an agent RPC must be+ // classified, and given its handling, when one appears.+ unclassified = append(unclassified, string(fd.FullName())+" (map field, "+method+")")+ continue}+ walk(fd.Message())}}}@@ -90,6 +94,11 @@ func TestCheckAgentRefs(t *testing.T) {"FindBranch of a branch named graft/": {&gitalypb.FindBranchRequest{Name: []byte("graft/x")}, false},"UserCommitFiles content": {&gitalypb.UserCommitFilesRequest{UserCommitFilesRequestPayload: &gitalypb.UserCommitFilesRequest_Action{Action: &gitalypb.UserCommitFilesAction{UserCommitFilesActionPayload: &gitalypb.UserCommitFilesAction_Content{Content: []byte("see refs/graft/green/main")}}}}, false},"GetObjects of an object ID": {&gitalypb.GetObjectsRequest{Oids: []string{"0123456789abcdef0123456789abcdef01234567"}}, false},+ "ListCommits, a name after a line": {&gitalypb.ListCommitsRequest{Revisions: []string{"--glob=x\nrefs/graft/proposals/0123456789abcdef/1"}}, true},+ "ListCommits, a short name after one": {&gitalypb.ListCommitsRequest{Revisions: []string{"--branches=x\ngraft/proposals/0123456789abcdef/1"}}, true},+ "ListBlobs, a name after a line": {&gitalypb.ListBlobsRequest{Revisions: []string{"--glob=x\nrefs/graft/green/main"}}, true},+ "ListCommits, a name after a CR": {&gitalypb.ListCommitsRequest{Revisions: []string{"--glob=x\rrefs/graft/green/main"}}, true},+ "ListCommits, a name after a NUL": {&gitalypb.ListCommitsRequest{Revisions: []string{"--glob=x\x00refs/graft/green/main"}}, true},} {err := checkAgentRefs(agentContext(), tc.req)if tc.denied {diff --git a/internal/gitaly/service/analysis/check_generated.go b/internal/gitaly/service/analysis/check_generated.goindex a11e125..c6101e3 100644--- a/internal/gitaly/service/analysis/check_generated.go+++ b/internal/gitaly/service/analysis/check_generated.go@@ -147,7 +147,8 @@ func validateCheckBlobsGeneratedRequest(req *gitalypb.CheckBlobsGeneratedRequest}for _, blob := range req.GetBlobs() {- if err := git.ValidateRevision(blob.GetRevision(), git.AllowPathScopedRevision()); err != nil {+ // git-cat-file reads the revisions NUL-delimited (-Z), so a line break in a path is data.+ if err := git.ValidateRevision(blob.GetRevision(), git.AllowPathScopedRevision(), git.AllowLineBreaksInPath()); err != nil {return err}diff --git a/internal/gitaly/service/commit/agent_injection_service_test.go b/internal/gitaly/service/commit/agent_injection_service_test.gonew file mode 100755index 0000000..6928d83--- /dev/null+++ b/internal/gitaly/service/commit/agent_injection_service_test.go@@ -0,0 +1,110 @@+package commit++import (+ "context"+ "errors"+ "io"+ "testing"++ "github.com/stretchr/testify/require"+ "google.golang.org/grpc"+ "google.golang.org/grpc/codes"+ "google.golang.org/grpc/metadata"+ "google.golang.org/grpc/status"++ "github.com/feder-cr/git-ai/internal/git/gittest"+ "github.com/feder-cr/git-ai/internal/gitaly/service"+ "github.com/feder-cr/git-ai/internal/gitaly/service/blob"+ "github.com/feder-cr/git-ai/internal/gitaly/service/ref"+ "github.com/feder-cr/git-ai/internal/gitaly/service/repository"+ "github.com/feder-cr/git-ai/internal/testhelper"+ "github.com/feder-cr/git-ai/internal/testhelper/testcfg"+ "github.com/feder-cr/git-ai/internal/testhelper/testserver"+ "github.com/feder-cr/git-ai/proto/go/gitalypb"+)++// TestRevisionInjection_rpcsRefuseLineBreaks: revisions travel to git one per line on its stdin, so+// a revision with a line feed, a carriage return or a NUL is more than one revision to git, or one+// that the filter that leaves refs/graft/ out of an agent's walks reads differently from git. The+// pseudo-revision patterns after "=" took any bytes, and the request check read the whole as one+// name: "--glob=x\nrefs/graft/green/main" named a hidden reference without the check seeing it.+// Every RPC that sends revisions to git over stdin refuses them now, for every caller, with+// InvalidArgument, and no proposal is returned.+func TestRevisionInjection_rpcsRefuseLineBreaks(t *testing.T) {+ t.Parallel()++ ctx := testhelper.Context(t)+ cfg := testcfg.Build(t)+ cfg.SocketPath = testserver.RunGitalyServer(t, cfg, func(srv *grpc.Server, deps *service.Dependencies) {+ gitalypb.RegisterCommitServiceServer(srv, NewServer(deps))+ gitalypb.RegisterBlobServiceServer(srv, blob.NewServer(deps))+ gitalypb.RegisterRefServiceServer(srv, ref.NewServer(deps))+ gitalypb.RegisterRepositoryServiceServer(srv, repository.NewServer(deps))+ }, testserver.WithInterceptors(agentInterceptors))+ conn := dial(t, cfg.SocketPath)+ commits := gitalypb.NewCommitServiceClient(conn)+ blobs := gitalypb.NewBlobServiceClient(conn)+ refs := gitalypb.NewRefServiceClient(conn)++ repo, repoPath := gittest.CreateRepository(t, ctx, cfg)+ main := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("main"),+ gittest.WithTreeEntries(gittest.TreeEntry{Mode: "100644", Path: "a", Content: "a\n"}))+ gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(main),+ gittest.WithReference("refs/graft/proposals/0123456789abcdef/1"), gittest.WithMessage("graft: proposal\n"),+ gittest.WithTreeEntries(gittest.TreeEntry{Mode: "100644", Path: "a", Content: "proposed by a model\n"}))+ agent := metadata.AppendToOutgoingContext(ctx, testAgentHeader, "agent-a")++ drain := func(recv func() error) error {+ for {+ if err := recv(); err != nil {+ if errors.Is(err, io.EOF) {+ return nil+ }+ return err+ }+ }+ }+ rpcs := map[string]func(context.Context, ...string) error{+ "ListCommits": func(ctx context.Context, revs ...string) error {+ stream, err := commits.ListCommits(ctx, &gitalypb.ListCommitsRequest{Repository: repo, Revisions: revs})+ require.NoError(t, err)+ return drain(func() error { _, err := stream.Recv(); return err })+ },+ "ListBlobs": func(ctx context.Context, revs ...string) error {+ stream, err := blobs.ListBlobs(ctx, &gitalypb.ListBlobsRequest{Repository: repo, Revisions: revs, Limit: 100})+ require.NoError(t, err)+ return drain(func() error { _, err := stream.Recv(); return err })+ },+ "ListLFSPointers": func(ctx context.Context, revs ...string) error {+ stream, err := blobs.ListLFSPointers(ctx, &gitalypb.ListLFSPointersRequest{Repository: repo, Revisions: revs, Limit: 100})+ require.NoError(t, err)+ return drain(func() error { _, err := stream.Recv(); return err })+ },+ "GetTagSignatures": func(ctx context.Context, revs ...string) error {+ stream, err := refs.GetTagSignatures(ctx, &gitalypb.GetTagSignaturesRequest{Repository: repo, TagRevisions: revs})+ require.NoError(t, err)+ return drain(func() error { _, err := stream.Recv(); return err })+ },+ }+ injections := map[string][]string{+ "CR after --all": {"--glob=x\n--all\r", "main"},+ "NUL after --all": {"--glob=x\n--all\x00", "main"},+ "name after a line feed": {"--glob=x\nrefs/graft/proposals/0123456789abcdef/1"},+ "short name after a line": {"--branches=x\ngraft/proposals/0123456789abcdef/1"},+ "tags pattern, then a name": {"--tags=x\nrefs/graft/green/main"},+ "CR alone in the pattern": {"--glob=refs/*\r--all"},+ "NUL in the pattern": {"--glob=refs/*\x00"},+ }++ for name, call := range rpcs {+ for desc, revs := range injections {+ for who, callCtx := range map[string]context.Context{"agent": agent, "other caller": ctx} {+ err := call(callCtx, revs...)+ require.Error(t, err, "%s %s as %s", name, desc, who)+ require.Equal(t, codes.InvalidArgument, status.Code(err), "%s %s as %s: %v", name, desc, who, err)+ }+ }+ // Control: an ordinary pattern is not refused.+ require.NoError(t, call(agent, "--glob=refs/heads/*"), name)+ }+}diff --git a/internal/gitaly/service/commit/check_objects_exist.go b/internal/gitaly/service/commit/check_objects_exist.goindex e9ae731..283fb06 100644--- a/internal/gitaly/service/commit/check_objects_exist.go+++ b/internal/gitaly/service/commit/check_objects_exist.go@@ -48,7 +48,8 @@ func (s *server) CheckObjectsExist(// Note: we have already fetched the first request containing revisions further up,// so we only fetch the next request at the end of this loop.for _, revision := range request.GetRevisions() {- if err := git.ValidateRevision(revision, git.AllowPathScopedRevision()); err != nil {+ // git-cat-file reads the revisions NUL-delimited (-Z), so a line break in a path is data.+ if err := git.ValidateRevision(revision, git.AllowPathScopedRevision(), git.AllowLineBreaksInPath()); err != nil {return structerr.NewInvalidArgument("invalid revision: %w", err).WithMetadata("revision", string(revision))}diff --git a/internal/gitaly/service/diff/diff_blobs.go b/internal/gitaly/service/diff/diff_blobs.goindex da60f9d..2550705 100644--- a/internal/gitaly/service/diff/diff_blobs.go+++ b/internal/gitaly/service/diff/diff_blobs.go@@ -405,7 +405,8 @@ func blobInfo() (git.ObjectID, error) {// Since only blobs are allowed, only path-scoped revisions and blob IDs are accepted.if bytes.Contains(revision, []byte(":")) {- if err := git.ValidateRevision(revision, git.AllowPathScopedRevision()); err != nil {+ // git-cat-file reads the revisions NUL-delimited (-Z), so a line break in a path is data.+ if err := git.ValidateRevision(revision, git.AllowPathScopedRevision(), git.AllowLineBreaksInPath()); err != nil {return "", fmt.Errorf("validating path-scoped revision: %w", err)}} else {diff --git a/internal/graft/agentrefs/agentrefs.go b/internal/graft/agentrefs/agentrefs.goindex 2e0747a..ec188bd 100644--- a/internal/graft/agentrefs/agentrefs.go+++ b/internal/graft/agentrefs/agentrefs.go@@ -23,8 +23,9 @@ func Hidden(ref string) bool {}// RevisionHidden reports whether a revision, reference or pattern an agent sends names a reference-// under HiddenPrefix: a part of its revision (before a ":path", between "..", "...", "^", "~", "@{"-// or "=", as in --glob=<pattern>) that starts with refs/graft/, or with graft/<namespace>/ for a namespace of the server+// under HiddenPrefix: a part of its revision (before a ":path", between "..", "...", "^", "~", "@{",+// "=" as in --glob=<pattern>, or a space, line break, tab or NUL, which git takes as the end of a+// revision or of a line) that starts with refs/graft/, or with graft/<namespace>/ for a namespace of the server// (ServerNamespaces), which git completes to refs/graft/<namespace>/. Other short names under// graft/ are branches (the workspace's temporary branches are graft/<agent>/...): git completes// them under refs/heads/, since nothing else exists under refs/graft/. The path after the first@@ -32,7 +33,8 @@ func Hidden(ref string) bool {func RevisionHidden(rev string) bool {rev, _, _ = strings.Cut(rev, ":")for _, part := range strings.FieldsFunc(rev, func(r rune) bool {- return r == '.' || r == '^' || r == '~' || r == '@' || r == '{' || r == '}' || r == ' ' || r == '='+ return r == '.' || r == '^' || r == '~' || r == '@' || r == '{' || r == '}' || r == ' ' || r == '=' ||+ r == '\n' || r == '\r' || r == '\t' || r == 0}) {part = strings.TrimPrefix(part, "!")if strings.HasPrefix(part, HiddenPrefix) {diff --git a/internal/graft/agentrefs/agentrefs_test.go b/internal/graft/agentrefs/agentrefs_test.goindex f551e13..4d844f6 100644--- a/internal/graft/agentrefs/agentrefs_test.go+++ b/internal/graft/agentrefs/agentrefs_test.go@@ -6,30 +6,37 @@ func TestRevisionHidden(t *testing.T) {t.Parallel()for rev, want := range map[string]bool{- "refs/graft/proposals/0123/abcd": true,- "refs/graft/green/main": true,- "refs/graft/*": true,- "graft/green/main": true, // git completes it to refs/graft/green/main- "graft/proposals/0123/abcd": true,- "graft/alice/6446a710c36ed315": false, // a workspace's temporary branch- "graft/greenhouse": false,- "refs/graft/green/main:README.md": true,- "main..refs/graft/green/main": true,- "refs/graft/green/main^{commit}": true,- "refs/graft/green/main~2": true,- "^refs/graft/green/main": true,- "refs/heads/main": false,- "refs/heads/graft/x": false,- "main": false,- "refs/*": false,- "--glob=refs/graft/*": true, // a pattern after =- "--glob=graft/proposals/*": true,- "--glob=refs/*": false, // gitcmd leaves refs/graft/ out of the walk- "--all": false, // likewise- "0123456789abcdef0123456789abcdef01234": false,- "main:refs/notes/graft": false,- "main:graft/x.go": false, // a path, not a reference- "main:refs/graft/x": false, // a path too+ "refs/graft/proposals/0123/abcd": true,+ "refs/graft/green/main": true,+ "refs/graft/*": true,+ "graft/green/main": true, // git completes it to refs/graft/green/main+ "graft/proposals/0123/abcd": true,+ "graft/alice/6446a710c36ed315": false, // a workspace's temporary branch+ "graft/greenhouse": false,+ "refs/graft/green/main:README.md": true,+ "main..refs/graft/green/main": true,+ "refs/graft/green/main^{commit}": true,+ "refs/graft/green/main~2": true,+ "^refs/graft/green/main": true,+ "refs/heads/main": false,+ "refs/heads/graft/x": false,+ "main": false,+ "refs/*": false,+ "--glob=x\nrefs/graft/green/main": true, // a line feed, a carriage return, a tab, a NUL+ "--glob=x\rrefs/graft/green/main": true, // each ends a revision for git+ "--glob=x\trefs/graft/green/main": true,+ "--glob=x\x00refs/graft/green/main": true,+ "--glob=x\ngraft/green/main": true,+ "--branches=x\nrefs/graft/proposals/1/1": true,+ "refs/heads/main\nrefs/heads/other": false,+ "--glob=refs/graft/*": true, // a pattern after =+ "--glob=graft/proposals/*": true,+ "--glob=refs/*": false, // gitcmd leaves refs/graft/ out of the walk+ "--all": false, // likewise+ "0123456789abcdef0123456789abcdef01234": false,+ "main:refs/notes/graft": false,+ "main:graft/x.go": false, // a path, not a reference+ "main:refs/graft/x": false, // a path too} {if got := RevisionHidden(rev); got != want {t.Errorf("RevisionHidden(%q) = %v, want %v", rev, got, want)