package forge import ( "context" "encoding/base64" "errors" "os" "path/filepath" "strings" "testing" "amadan.net/oficina/equipo/internal/runner/workspace" ) // pushSecret is what a hub's hook may echo back: a header and a URL // that carry credentials. const pushSecret = "Authorization: Bearer s3cr3t-hub-token, see https://agent:s3cr3t-pw@amadan.net/x" // TestPushBranchKeepsGitsAnswer: a push the remote refuses comes back // with git's answer (the rejected ref line and the hub's own words) and // what kind of refusal it shows, redacted and bounded; the outcome is // as before, so the step's reason does not change. func TestPushBranchKeepsGitsAnswer(t *testing.T) { ctx := context.Background() // A hook (or a protected branch) refuses it. r := newBranchRig(t) hook := filepath.Join(r.remote, "hooks", "pre-receive") if err := os.WriteFile(hook, []byte("#!/bin/sh\necho 'GL-HOOK-ERR: equipo/tk1-hook is protected. "+pushSecret+"' >&2\nexit 1\n"), 0700); err != nil { t.Fatal(err) } err := PushBranch(ctx, r.push("equipo/tk1-hook")) var p *PushError if !errors.Is(err, ErrPushUncertain) || !errors.As(err, &p) || p.Op != "push" || p.Refusal != RefusalRefused || PushRefusal(err) != RefusalRefused { t.Fatalf("hook: %v", err) } for _, want := range []string{"[remote rejected] (pre-receive hook declined)", "GL-HOOK-ERR: equipo/tk1-hook is protected.", "Authorization: ***", "https://***@amadan.net/x"} { if !strings.Contains(err.Error(), want) { t.Fatalf("hook: %q lacks %q", err.Error(), want) } } detail := PushDetail(err) if !strings.HasPrefix(detail, "refusal: refused\npush: ") || !strings.Contains(detail, "GL-HOOK-ERR") || !strings.Contains(detail, "pre-receive hook declined") { t.Fatalf("hook detail %q", detail) } if strings.Contains(err.Error()+detail, "s3cr3t") || len(detail) > 500 { t.Fatalf("hook: not redacted or bounded: %q / %q", err.Error(), detail) } os.Remove(hook) // The hub keeps its branches fast-forward: a new attempt's commit that // does not descend from the last one is refused as such. r = newBranchRig(t) gitT(t, filepath.Dir(r.remote), nil, "--git-dir="+r.remote, "config", "receive.denyNonFastForwards", "true") if err := PushBranch(ctx, r.push("equipo/tk1-nff")); err != nil { t.Fatal(err) } b := r.push("equipo/tk1-nff") b.Key, b.Commit = "tk1/s1/2", r.commit2 err = PushBranch(ctx, b) if !errors.Is(err, ErrPushUncertain) || PushRefusal(err) != RefusalNonFastForward || !strings.Contains(err.Error(), "(non-fast-forward)") || r.head(t, b.Branch) != r.commit { t.Fatalf("non-fast-forward: %v", err) } if detail := PushDetail(err); !strings.HasPrefix(detail, "refusal: non-fast-forward\npush: ") { t.Fatalf("non-fast-forward detail %q", detail) } // The branch cannot be read: git's words, no refusal. r = newBranchRig(t) b = r.push("equipo/tk1-gone") b.Remote = func(context.Context) (workspace.Remote, error) { return workspace.Remote{URL: filepath.Join(r.root, "missing.git")}, nil } err = PushBranch(ctx, b) if !errors.As(err, &p) || p.Op != "ls-remote" || p.Refusal != "" || !strings.Contains(err.Error(), "does not appear to be a git repository") { t.Fatalf("unreadable: %v", err) } if detail := PushDetail(err); !strings.HasPrefix(detail, "ls-remote: ") { t.Fatalf("unreadable detail %q", detail) } // No lease: the confirmation's words. b = r.push("equipo/tk1-no-lease") b.Key = "tk1/s2/1" b.Confirm = func(context.Context) error { return errors.New("superseded") } if err := PushBranch(ctx, b); !errors.Is(err, ErrPushUncertain) || PushDetail(err) != "error: forge: push outcome unknown: confirming the lease: superseded" { t.Fatalf("no lease: %q", PushDetail(err)) } } // TestPushRefusalOf: git's rejected ref lines (porcelain or not) and the // hub's words decide the kind; anything else is not classified. func TestPushRefusalOf(t *testing.T) { for in, want := range map[string]string{ "!\tHEAD:refs/heads/equipo/x\t[rejected] (non-fast-forward)": RefusalNonFastForward, " ! [rejected] HEAD -> equipo/x (fetch first)": RefusalNonFastForward, "!\tHEAD:refs/heads/equipo/x\t[remote rejected] (non-fast-forward)": RefusalNonFastForward, "!\tHEAD:refs/heads/main\t[remote rejected] (pre-receive hook declined)": RefusalRefused, " ! [remote rejected] HEAD -> main (protected branch hook declined)": RefusalRefused, "remote: error: GH006: Protected branch update failed for refs/heads/main.": RefusalRefused, "remote: You are not allowed to push to the default branch; it is protected.": RefusalRefused, "!\tHEAD:refs/heads/equipo/x\t[rejected] (stale info)": "", "fatal: unable to access 'https://amadan.net/x/': The requested URL returned 502": "", } { if got := pushRefusalOf(in); got != want { t.Fatalf("pushRefusalOf(%q) = %q, want %q", in, got, want) } } } // TestPushAnswerRedacts: the git password is never in an answer, raw or // as the Basic header git would send, nor any other credential shape; // git's hints are dropped and the answer is bounded. func TestPushAnswerRedacts(t *testing.T) { // Made at run time, so no scanner takes the fixture for a key. password := strings.Join([]string{"pw", "0123456789"}, "-") basic := base64.StdEncoding.EncodeToString([]byte("x:" + password)) remote := &workspace.Remote{URL: "https://amadan.net/oficina/sample", GitPassword: password} stderr := "remote: denied for " + password + " (" + basic + ")\nhint: Updates were rejected\nerror: failed to push some refs to 'https://x:s3cr3t-pw@amadan.net/oficina/sample'\n" + strings.Repeat("word ", 400) got := pushAnswer("To https://amadan.net/oficina/sample\n!\tHEAD:refs/heads/equipo/x\t[remote rejected] (denied)\nDone", stderr, remote) if strings.Contains(got, password) || strings.Contains(got, basic) || strings.Contains(got, "s3cr3t") || strings.Contains(got, "hint:") || strings.Contains(got, "Done") || len(got) > 500 { t.Fatalf("answer %q", got) } if !strings.HasPrefix(got, "! HEAD:refs/heads/equipo/x [remote rejected] (denied) remote: denied for *** (***)") { t.Fatalf("answer %q", got) } }