Skip to content

Git designed for AI agents.

hook, docs: a land batch is held to the refs/graft/ reservation; AGENT-SECURITY corrections

Federico Elia0165c489faaa
diff --git a/doc/graft/AGENT-SECURITY.md b/doc/graft/AGENT-SECURITY.md
index 97366b4..782aded 100644
--- a/doc/graft/AGENT-SECURITY.md
+++ b/doc/graft/AGENT-SECURITY.md
@@ -174,12 +174,18 @@ layers that enforce it, in the order a request meets them:
push, and `TestPolicyHookDecidesForAgentUpdates` layer 4 alone (the real hook,
run by `git update-ref` with the payload of an agent's write, a symbolic
reference included), both in standalone mode and outside it. The pre-receive
-hook judges the policy only; it does not check the reservation, because no
-agent write reaches it with a `refs/graft/` update.
+hook does not check the reservation: it judges the policy in standalone mode
+(GitLab's `/allowed` outside it), and it sees the names that were pushed, not
+what git will write. A push to `refs/heads/alias`, a symbolic reference to
+`refs/graft/x`, reaches it as `refs/heads/alias` and would write
+`refs/graft/x`; the reference-transaction hook, which sees `refs/graft/x`,
+refuses it. The combined update of a land batch, which runs without an agent
+identity, is held to the reservation by the hook as well.
Every other RPC that is not on the allowlist (for example the other
-`OperationService` RPCs such as `UserMergeBranch`, or `SSHService`) is denied
-by default and can be enabled with `graft.agent_rpcs`.
+`OperationService` RPCs such as `UserMergeBranch`, or the other `SSHService`
+RPCs) is denied by default and can be enabled with `graft.agent_rpcs`, except
+the RPCs that can never be enabled (above), `SSHUploadArchive` among them.
A call outside the allowlist fails with `PermissionDenied`. The error does
not reveal more than that the method is not allowed for agents.
diff --git a/internal/gitaly/hook/referencetransaction.go b/internal/gitaly/hook/referencetransaction.go
index e323abe..fb4b603 100644
--- a/internal/gitaly/hook/referencetransaction.go
+++ b/internal/gitaly/hook/referencetransaction.go
@@ -505,19 +505,22 @@ func (m *GitLabHookManager) graftPolicyDecision(env []string, changes []byte) (s
// call updates no references. The skip therefore gives it no new reach.
// Only the batch operation carries members, so any other update is still
// checked here.
- if meta.Operation == changelog.OperationLandBatch && len(meta.Members) > 0 {
- return "", nil
- }
-
- if !meta.Agent || meta.Actor == "" {
- return "", nil
- }
+ batch := meta.Operation == changelog.OperationLandBatch && len(meta.Members) > 0
+ agent := meta.Agent && meta.Actor != ""
// refs/graft/ is the server's, whatever the policy says and whether or not the server runs
// in standalone mode: it is not a rule of the policy but an invariant of the server, for
- // every agent.
- if message := graftReservedDenial(changes); message != "" {
- return message, nil
+ // every agent. A batch runs without an identity but lands the changes of its members, so it
+ // is held to the reservation too, before the batch skip below: a batch only ever updates
+ // its branch, and this keeps it so whatever builds it.
+ if batch || agent {
+ if message := graftReservedDenial(changes); message != "" {
+ return message, nil
+ }
+ }
+
+ if batch || !agent {
+ return "", nil
}
if !policyApplies {
return "", nil
diff --git a/internal/gitaly/hook/referencetransaction_policy_test.go b/internal/gitaly/hook/referencetransaction_policy_test.go
index ee8d970..f166c63 100644
--- a/internal/gitaly/hook/referencetransaction_policy_test.go
+++ b/internal/gitaly/hook/referencetransaction_policy_test.go
@@ -250,6 +250,28 @@ func TestEnforceGraftPolicy_refsGraftIsReserved(t *testing.T) {
"outside standalone mode the policy does not apply")
}
+// TestEnforceGraftPolicy_batchIsHeldToTheReservation: the combined update of a land batch runs
+// without an identity and skips the policy (its members were admitted one by one), but not the
+// reservation of refs/graft/, in every mode.
+func TestEnforceGraftPolicy_batchIsHeldToTheReservation(t *testing.T) {
+ t.Parallel()
+
+ ctx := testhelper.Context(t)
+ payload := `{"graft_changelog":{"operation":"land_batch","members":[{"original_commit":"a","commit":"b","actor":"agent-a"}]}}`
+ env := []string{gitcmd.EnvHooksPayload + "=" + base64.StdEncoding.EncodeToString([]byte(payload))}
+
+ for name, graft := range map[string]config.Graft{
+ "standalone, policy denies": {Standalone: true, Policy: []config.GraftPolicyRule{{Allow: false}}},
+ "not standalone": {Agents: []config.GraftAgent{{Name: "agent-a"}}},
+ } {
+ m := graftPolicyManager(t, graft)
+ err := m.enforceGraftPolicy(ctx, env, policyChanges("refs/graft/green/main"))
+ require.Equal(t, codes.PermissionDenied, status.Code(err), name)
+ require.ErrorContains(t, err, "reserved for the server", name)
+ require.NoError(t, m.enforceGraftPolicy(ctx, env, policyChanges("refs/heads/main")), "%s: the batch skips the policy", name)
+ }
+}
+
// TestCheckAgentPolicy checks the policy check of the RPCs that skip the pre-receive hook: the
// reservation of refs/graft/ refuses an agent in every mode, the rest of the policy only in
// standalone mode, and callers that are not agents are not restricted.