|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/github/gh-actions-lock/internal/ghapi/httpmock" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +// TestNoOnboard_BareSHAOnTrackedAction: under --no-onboard a bare SHA on an |
| 17 | +// action the workflow already locks is a ref change, not onboarding. A SHA |
| 18 | +// on upstream history is pinned in place of the old entry; an impostor SHA |
| 19 | +// blocks with an error and nothing is written. Neither may drop the old pin |
| 20 | +// and pass, which is what Dependabot SHA bumps hit. |
| 21 | +func TestNoOnboard_BareSHAOnTrackedAction(t *testing.T) { |
| 22 | + const ( |
| 23 | + oldSHA = "eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871" |
| 24 | + newSHA = "11bd71901bbe5b1630ceea73d27597364c9af683" |
| 25 | + impostor = "11d0dfc58a242e392d9695aa26812e1d1eb8e439" |
| 26 | + ) |
| 27 | + tests := []struct { |
| 28 | + name string |
| 29 | + locked string |
| 30 | + uses string |
| 31 | + blocked bool |
| 32 | + }{ |
| 33 | + {"sha to sha", "actions/checkout@" + oldSHA + "=sha1-" + oldSHA, newSHA, false}, |
| 34 | + {"tag to sha", "actions/checkout@v4.2.1=sha1-" + oldSHA, newSHA, false}, |
| 35 | + {"impostor sha", "actions/checkout@" + oldSHA + "=sha1-" + oldSHA, impostor, true}, |
| 36 | + } |
| 37 | + for _, tt := range tests { |
| 38 | + for _, proxy := range []bool{false, true} { |
| 39 | + t.Run(tt.name+"/"+map[bool]string{false: "graphql", true: "dependabot proxy"}[proxy], func(t *testing.T) { |
| 40 | + reg := trackedSHAStubs(tt.uses, newSHA) |
| 41 | + if proxy { |
| 42 | + t.Setenv("GH_ACTIONS_LOCK_DEPENDABOT_PROXY", "1") |
| 43 | + useDefaultTransport(t, reg) |
| 44 | + } |
| 45 | + path := writeTempWorkflow(t, ` |
| 46 | +name: ci |
| 47 | +on: push |
| 48 | +jobs: |
| 49 | + test: |
| 50 | + runs-on: ubuntu-latest |
| 51 | + steps: |
| 52 | + - uses: actions/checkout@`+tt.uses+` |
| 53 | +`, tt.locked) |
| 54 | + lockBefore := readTempLockfilePins(t) |
| 55 | + wfBefore, err := os.ReadFile(path) |
| 56 | + require.NoError(t, err) |
| 57 | + |
| 58 | + stdout, stderr, runErr := runCommandWithHTTP(t, reg, |
| 59 | + "--no-onboard", "--no-narrow", "--no-interactive", "--json=findings", path) |
| 60 | + var out struct { |
| 61 | + Findings []struct { |
| 62 | + Category string `json:"category"` |
| 63 | + Severity string `json:"severity"` |
| 64 | + } `json:"findings"` |
| 65 | + } |
| 66 | + require.NoError(t, json.Unmarshal([]byte(stdout), &out), stdout+stderr) |
| 67 | + var cats []string |
| 68 | + for _, f := range out.Findings { |
| 69 | + cats = append(cats, f.Category+"/"+f.Severity) |
| 70 | + } |
| 71 | + assert.NotContains(t, cats, "onboarding-required/info", "a tracked action is not onboarding") |
| 72 | + |
| 73 | + if proxy { |
| 74 | + for _, req := range reg.Requests { |
| 75 | + assert.NotContains(t, req.URL.Path, "graphql", "the proxy path is REST-only") |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + wfAfter, err := os.ReadFile(path) |
| 80 | + require.NoError(t, err) |
| 81 | + assert.Equal(t, string(wfBefore), string(wfAfter)) |
| 82 | + lock := readTempLockfilePins(t) |
| 83 | + if tt.blocked { |
| 84 | + require.ErrorIs(t, runErr, errSilent, stderr) |
| 85 | + assert.Contains(t, cats, "unreachable-pin/error") |
| 86 | + assert.Equal(t, lockBefore, lock, "nothing is written") |
| 87 | + return |
| 88 | + } |
| 89 | + require.NoError(t, runErr, stderr) |
| 90 | + assert.Contains(t, lock, "'actions/checkout@"+tt.uses+"':") |
| 91 | + assert.Contains(t, lock, "sha1-"+tt.uses) |
| 92 | + assert.NotContains(t, lock, oldSHA) |
| 93 | + }) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// trackedSHAStubs answers both the GraphQL and the anonymous REST paths for |
| 99 | +// actions/checkout, with head as the only branch and tag commit. Stubs are |
| 100 | +// one-shot, so each is registered once per pass the run makes. |
| 101 | +func trackedSHAStubs(uses, head string) *httpmock.Registry { |
| 102 | + reg := &httpmock.Registry{} |
| 103 | + raw := func(body string) httpmock.Responder { |
| 104 | + return func(req *http.Request) (*http.Response, error) { |
| 105 | + return &http.Response{StatusCode: http.StatusOK, Request: req, Header: http.Header{}, |
| 106 | + Body: io.NopCloser(strings.NewReader(body))}, nil |
| 107 | + } |
| 108 | + } |
| 109 | + status := "identical" |
| 110 | + if uses != head { |
| 111 | + status = "diverged" |
| 112 | + } |
| 113 | + compare := map[string]any{"status": status, "merge_base_commit": map[string]any{"sha": head}} |
| 114 | + for range 8 { |
| 115 | + reg.Register(httpmock.GraphQLForRepo("actions", "checkout"), httpmock.JSONResponse(map[string]any{ |
| 116 | + "data": map[string]any{"a0": testRepoResponse("actions/checkout", uses, nodeActionYAML)}, |
| 117 | + })) |
| 118 | + reg.Register(httpmock.REST("GET", `repos/actions/checkout$`), httpmock.JSONResponse(map[string]any{ |
| 119 | + "full_name": "actions/checkout", "default_branch": "main", "id": 1, "owner": map[string]any{"id": 1}, |
| 120 | + })) |
| 121 | + reg.Register(httpmock.REST("GET", `repos/actions/checkout/commits/`), httpmock.JSONResponse(map[string]any{"sha": uses})) |
| 122 | + reg.Register(httpmock.REST("GET", `repos/actions/checkout/contents/action\.yml`), raw(nodeActionYAML)) |
| 123 | + reg.Register(httpmock.REST("GET", `repos/actions/checkout/git/ref/heads/main`), httpmock.JSONResponse(map[string]any{ |
| 124 | + "ref": "refs/heads/main", "object": map[string]any{"sha": head, "type": "commit"}, |
| 125 | + })) |
| 126 | + reg.Register(httpmock.REST("GET", `repos/actions/checkout/tags`), httpmock.JSONResponse(httpmock.TagListResponse("v4.2.2", head))) |
| 127 | + reg.Register(httpmock.REST("GET", `repos/actions/checkout/releases`), httpmock.JSONResponse([]map[string]any{})) |
| 128 | + reg.Register(httpmock.REST("GET", `repos/actions/checkout/branches`), httpmock.JSONResponse(httpmock.BranchListResponse("main", head))) |
| 129 | + reg.Register(httpmock.REST("GET", `repos/actions/checkout/compare/`), httpmock.JSONResponse(compare)) |
| 130 | + } |
| 131 | + return reg |
| 132 | +} |
0 commit comments