| 1 | package forge |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/base64" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 13 | ) |
| 14 | |
| 15 | // pushSecret is what a hub's hook may echo back: a header and a URL |
| 16 | // that carry credentials. |
| 17 | const pushSecret = "Authorization: Bearer s3cr3t-hub-token, see https://agent:s3cr3t-pw@amadan.net/x" |
| 18 | |
| 19 | // TestPushBranchKeepsGitsAnswer: a push the remote refuses comes back |
| 20 | // with git's answer (the rejected ref line and the hub's own words) and |
| 21 | // what kind of refusal it shows, redacted and bounded; the outcome is |
| 22 | // as before, so the step's reason does not change. |
| 23 | func TestPushBranchKeepsGitsAnswer(t *testing.T) { |
| 24 | ctx := context.Background() |
| 25 | |
| 26 | // A hook (or a protected branch) refuses it. |
| 27 | r := newBranchRig(t) |
| 28 | hook := filepath.Join(r.remote, "hooks", "pre-receive") |
| 29 | 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 { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | err := PushBranch(ctx, r.push("equipo/tk1-hook")) |
| 33 | var p *PushError |
| 34 | if !errors.Is(err, ErrPushUncertain) || !errors.As(err, &p) || p.Op != "push" || p.Refusal != RefusalRefused || PushRefusal(err) != RefusalRefused { |
| 35 | t.Fatalf("hook: %v", err) |
| 36 | } |
| 37 | for _, want := range []string{"[remote rejected] (pre-receive hook declined)", "GL-HOOK-ERR: equipo/tk1-hook is protected.", "Authorization: ***", "https://***@amadan.net/x"} { |
| 38 | if !strings.Contains(err.Error(), want) { |
| 39 | t.Fatalf("hook: %q lacks %q", err.Error(), want) |
| 40 | } |
| 41 | } |
| 42 | detail := PushDetail(err) |
| 43 | if !strings.HasPrefix(detail, "refusal: refused\npush: ") || !strings.Contains(detail, "GL-HOOK-ERR") || !strings.Contains(detail, "pre-receive hook declined") { |
| 44 | t.Fatalf("hook detail %q", detail) |
| 45 | } |
| 46 | if strings.Contains(err.Error()+detail, "s3cr3t") || len(detail) > 500 { |
| 47 | t.Fatalf("hook: not redacted or bounded: %q / %q", err.Error(), detail) |
| 48 | } |
| 49 | os.Remove(hook) |
| 50 | |
| 51 | // The hub keeps its branches fast-forward: a new attempt's commit that |
| 52 | // does not descend from the last one is refused as such. |
| 53 | r = newBranchRig(t) |
| 54 | gitT(t, filepath.Dir(r.remote), nil, "--git-dir="+r.remote, "config", "receive.denyNonFastForwards", "true") |
| 55 | if err := PushBranch(ctx, r.push("equipo/tk1-nff")); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | b := r.push("equipo/tk1-nff") |
| 59 | b.Key, b.Commit = "tk1/s1/2", r.commit2 |
| 60 | err = PushBranch(ctx, b) |
| 61 | if !errors.Is(err, ErrPushUncertain) || PushRefusal(err) != RefusalNonFastForward || !strings.Contains(err.Error(), "(non-fast-forward)") || r.head(t, b.Branch) != r.commit { |
| 62 | t.Fatalf("non-fast-forward: %v", err) |
| 63 | } |
| 64 | if detail := PushDetail(err); !strings.HasPrefix(detail, "refusal: non-fast-forward\npush: ") { |
| 65 | t.Fatalf("non-fast-forward detail %q", detail) |
| 66 | } |
| 67 | |
| 68 | // The branch cannot be read: git's words, no refusal. |
| 69 | r = newBranchRig(t) |
| 70 | b = r.push("equipo/tk1-gone") |
| 71 | b.Remote = func(context.Context) (workspace.Remote, error) { |
| 72 | return workspace.Remote{URL: filepath.Join(r.root, "missing.git")}, nil |
| 73 | } |
| 74 | err = PushBranch(ctx, b) |
| 75 | if !errors.As(err, &p) || p.Op != "ls-remote" || p.Refusal != "" || !strings.Contains(err.Error(), "does not appear to be a git repository") { |
| 76 | t.Fatalf("unreadable: %v", err) |
| 77 | } |
| 78 | if detail := PushDetail(err); !strings.HasPrefix(detail, "ls-remote: ") { |
| 79 | t.Fatalf("unreadable detail %q", detail) |
| 80 | } |
| 81 | |
| 82 | // No lease: the confirmation's words. |
| 83 | b = r.push("equipo/tk1-no-lease") |
| 84 | b.Key = "tk1/s2/1" |
| 85 | b.Confirm = func(context.Context) error { return errors.New("superseded") } |
| 86 | if err := PushBranch(ctx, b); !errors.Is(err, ErrPushUncertain) || PushDetail(err) != "error: forge: push outcome unknown: confirming the lease: superseded" { |
| 87 | t.Fatalf("no lease: %q", PushDetail(err)) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // TestPushRefusalOf: git's rejected ref lines (porcelain or not) and the |
| 92 | // hub's words decide the kind; anything else is not classified. |
| 93 | func TestPushRefusalOf(t *testing.T) { |
| 94 | for in, want := range map[string]string{ |
| 95 | "!\tHEAD:refs/heads/equipo/x\t[rejected] (non-fast-forward)": RefusalNonFastForward, |
| 96 | " ! [rejected] HEAD -> equipo/x (fetch first)": RefusalNonFastForward, |
| 97 | "!\tHEAD:refs/heads/equipo/x\t[remote rejected] (non-fast-forward)": RefusalNonFastForward, |
| 98 | "!\tHEAD:refs/heads/main\t[remote rejected] (pre-receive hook declined)": RefusalRefused, |
| 99 | " ! [remote rejected] HEAD -> main (protected branch hook declined)": RefusalRefused, |
| 100 | "remote: error: GH006: Protected branch update failed for refs/heads/main.": RefusalRefused, |
| 101 | "remote: You are not allowed to push to the default branch; it is protected.": RefusalRefused, |
| 102 | "!\tHEAD:refs/heads/equipo/x\t[rejected] (stale info)": "", |
| 103 | "fatal: unable to access 'https://amadan.net/x/': The requested URL returned 502": "", |
| 104 | } { |
| 105 | if got := pushRefusalOf(in); got != want { |
| 106 | t.Fatalf("pushRefusalOf(%q) = %q, want %q", in, got, want) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // TestPushAnswerRedacts: the git password is never in an answer, raw or |
| 112 | // as the Basic header git would send, nor any other credential shape; |
| 113 | // git's hints are dropped and the answer is bounded. |
| 114 | func TestPushAnswerRedacts(t *testing.T) { |
| 115 | // Made at run time, so no scanner takes the fixture for a key. |
| 116 | password := strings.Join([]string{"pw", "0123456789"}, "-") |
| 117 | basic := base64.StdEncoding.EncodeToString([]byte("x:" + password)) |
| 118 | remote := &workspace.Remote{URL: "https://amadan.net/oficina/sample", GitPassword: password} |
| 119 | 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) |
| 120 | got := pushAnswer("To https://amadan.net/oficina/sample\n!\tHEAD:refs/heads/equipo/x\t[remote rejected] (denied)\nDone", stderr, remote) |
| 121 | if strings.Contains(got, password) || strings.Contains(got, basic) || strings.Contains(got, "s3cr3t") || strings.Contains(got, "hint:") || strings.Contains(got, "Done") || len(got) > 500 { |
| 122 | t.Fatalf("answer %q", got) |
| 123 | } |
| 124 | if !strings.HasPrefix(got, "! HEAD:refs/heads/equipo/x [remote rejected] (denied) remote: denied for *** (***)") { |
| 125 | t.Fatalf("answer %q", got) |
| 126 | } |
| 127 | } |
| 128 | |