Skip to content

Git designed for AI agents.

graft: check the agent policy before a resolution is checked or verified

Federico Eliae9e320926919
diff --git a/internal/gitaly/service/operations/land_policy_test.go b/internal/gitaly/service/operations/land_policy_test.go
new file mode 100644
index 0000000..c11acdc
--- /dev/null
+++ b/internal/gitaly/service/operations/land_policy_test.go
@@ -0,0 +1,43 @@
+package operations
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ "google.golang.org/grpc/codes"
+ "google.golang.org/grpc/status"
+
+ gitalycfg "github.com/feder-cr/git-ai/internal/gitaly/config"
+ "github.com/feder-cr/git-ai/internal/graft/identity"
+)
+
+// TestLandPrecheckResolution_policyFirst: an agent the policy does not let update the branch gets
+// PermissionDenied before its tree is verified (the resolver's check runs behind the same line):
+// verify.command does not run, so that a denied agent cannot hold the repository's verify slot.
+func TestLandPrecheckResolution_policyFirst(t *testing.T) {
+ t.Parallel()
+
+ vdir, vargv := countingVerify(t, "exit 0")
+ rcfg := withVerify(gitalycfg.GraftResolver{}, vargv, "")
+ fx := newResolveFixture(t, rcfg)
+ s := fx.server(t, rcfg)
+ s.graft = gitalycfg.Graft{Standalone: true, Policy: []gitalycfg.GraftPolicyRule{{Actor: "agent-7", Refs: []string{"refs/heads/other"}, Allow: true}}}
+ ctx := identity.NewContext(fx.ctx, identity.Identity{Name: "agent-7", Source: identity.SourceAgent})
+ in := fx.request("Land")
+ in.Resolution = resolutionOf(fx, "agent-a\n", "agent-b\n")
+
+ pc, err := s.landPrecheckResolution(ctx, in, fx.repo)
+ require.Nil(t, pc)
+ require.Equal(t, codes.PermissionDenied, status.Code(err), "err = %v", err)
+ require.Contains(t, err.Error(), `policy denies "agent-7"`)
+ require.Equal(t, 0, verifyRuns(t, vdir), "nothing was verified")
+
+ // An agent the policy lets update the branch gets its resolution checked and verified.
+ s.graft.Policy = []gitalycfg.GraftPolicyRule{{Actor: "agent-7", Refs: []string{"refs/heads/**"}, Allow: true}}
+ pc, err = s.landPrecheckResolution(ctx, in, fx.repo)
+ require.NoError(t, err)
+ require.NotNil(t, pc)
+ require.Empty(t, pc.skipped)
+ require.NotEmpty(t, pc.verified, "verifyErr = %v", pc.verifyErr)
+ require.Equal(t, 1, verifyRuns(t, vdir))
+}
diff --git a/internal/gitaly/service/operations/land_reland.go b/internal/gitaly/service/operations/land_reland.go
index 89407e4..71b0b9b 100644
--- a/internal/gitaly/service/operations/land_reland.go
+++ b/internal/gitaly/service/operations/land_reland.go
@@ -15,6 +15,7 @@ import (
"github.com/feder-cr/git-ai/internal/git"
"github.com/feder-cr/git-ai/internal/git/localrepo"
+ "github.com/feder-cr/git-ai/internal/gitaly/hook"
"github.com/feder-cr/git-ai/internal/graft/identity"
"github.com/feder-cr/git-ai/internal/graft/resolver"
"github.com/feder-cr/git-ai/internal/log"
@@ -191,6 +192,15 @@ func (s *Server) landPrecheckResolution(ctx context.Context, in *gitalypb.UserLa
return &landPrecheck{skipped: fmt.Sprintf("commit_id %q does not resolve to a commit", in.GetCommitId())}, nil
}
+ // An agent that may not update the branch gets nothing checked or verified: the check and the
+ // verification run the operator's code, and the policy decides on the actor and the reference
+ // alone, so it comes first, as in landResolveAt. Otherwise a denied agent could hold the
+ // repository's verify slot, and its tree would run in the operator's sandbox.
+ reference := git.NewReferenceNameFromBranchName(string(in.GetBranch()))
+ if err := hook.CheckAgentPolicy(ctx, s.graft, fmt.Sprintf("%s %s %s\n", tip, commitID, reference)); err != nil {
+ return nil, err
+ }
+
// A resolution for a tip the branch has left, or for a change that already landed, needs no
// check: the lane refuses or answers it at once, as it reads the tip again under the lane.
// The branch is read as the lane reads it (landingAttempt.Tip): a symbolic reference resolves