|
| 1 | +From 9906874a23919a81eff097d84fdb8f98525ac880 Mon Sep 17 00:00:00 2001 |
| 2 | +From: dduzgun-security <deniz.duzgun@hashicorp.com> |
| 3 | +Date: Thu, 20 Jun 2024 10:06:56 -0400 |
| 4 | +Subject: [PATCH 1/2] recreate git config during update to prevent config |
| 5 | + alteration |
| 6 | + |
| 7 | +Modified to apply to vendored code by: Sumedh Sharma <sumsharma@microsoft.com> |
| 8 | + - Adjusted paths to work for vendored version |
| 9 | + - Removed test code since it is not included in vendor trace |
| 10 | +--- |
| 11 | + vendor/github.com/hashicorp/go-getter/get_git.go | 81 +++++++++++++++---- |
| 12 | + 1 file changed, 67 insertions(+), 14 deletions(-) |
| 13 | + |
| 14 | +diff --git a/vendor/github.com/hashicorp/go-getter/get_git.go b/vendor/github.com/hashicorp/go-getter/get_git.go |
| 15 | +index 5227db7..51a898b 100644 |
| 16 | +--- a/vendor/github.com/hashicorp/go-getter/get_git.go |
| 17 | ++++ b/vendor/github.com/hashicorp/go-getter/get_git.go |
| 18 | +@@ -125,7 +125,7 @@ func (g *GitGetter) Get(dst string, u *url.URL) error { |
| 19 | + return err |
| 20 | + } |
| 21 | + if err == nil { |
| 22 | +- err = g.update(ctx, dst, sshKeyFile, ref, depth) |
| 23 | ++ err = g.update(ctx, dst, sshKeyFile, u, ref, depth) |
| 24 | + } else { |
| 25 | + err = g.clone(ctx, dst, sshKeyFile, u, ref, depth) |
| 26 | + } |
| 27 | +@@ -228,28 +228,64 @@ func (g *GitGetter) clone(ctx context.Context, dst, sshKeyFile string, u *url.UR |
| 28 | + return nil |
| 29 | + } |
| 30 | + |
| 31 | +-func (g *GitGetter) update(ctx context.Context, dst, sshKeyFile, ref string, depth int) error { |
| 32 | +- // Determine if we're a branch. If we're NOT a branch, then we just |
| 33 | +- // switch to master prior to checking out |
| 34 | +- cmd := exec.CommandContext(ctx, "git", "show-ref", "-q", "--verify", "refs/heads/"+ref) |
| 35 | ++func (g *GitGetter) update(ctx context.Context, dst, sshKeyFile string, u *url.URL, ref string, depth int) error { |
| 36 | ++ // Remove all variations of .git directories |
| 37 | ++ err := removeCaseInsensitiveGitDirectory(dst) |
| 38 | ++ if err != nil { |
| 39 | ++ return err |
| 40 | ++ } |
| 41 | ++ |
| 42 | ++ // Initialize the git repository |
| 43 | ++ cmd := exec.CommandContext(ctx, "git", "init") |
| 44 | ++ cmd.Dir = dst |
| 45 | ++ err = getRunCommand(cmd) |
| 46 | ++ if err != nil { |
| 47 | ++ return err |
| 48 | ++ } |
| 49 | ++ |
| 50 | ++ // Add the git remote |
| 51 | ++ cmd = exec.CommandContext(ctx, "git", "remote", "add", "origin", "--", u.String()) |
| 52 | ++ cmd.Dir = dst |
| 53 | ++ err = getRunCommand(cmd) |
| 54 | ++ if err != nil { |
| 55 | ++ return err |
| 56 | ++ } |
| 57 | ++ |
| 58 | ++ // Fetch the remote ref |
| 59 | ++ cmd = exec.CommandContext(ctx, "git", "fetch", "--tags") |
| 60 | ++ cmd.Dir = dst |
| 61 | ++ err = getRunCommand(cmd) |
| 62 | ++ if err != nil { |
| 63 | ++ return err |
| 64 | ++ } |
| 65 | ++ |
| 66 | ++ // Fetch the remote ref |
| 67 | ++ cmd = exec.CommandContext(ctx, "git", "fetch", "origin", "--", ref) |
| 68 | + cmd.Dir = dst |
| 69 | ++ err = getRunCommand(cmd) |
| 70 | ++ if err != nil { |
| 71 | ++ return err |
| 72 | ++ } |
| 73 | + |
| 74 | +- if getRunCommand(cmd) != nil { |
| 75 | +- // Not a branch, switch to default branch. This will also catch |
| 76 | +- // non-existent branches, in which case we want to switch to default |
| 77 | +- // and then checkout the proper branch later. |
| 78 | +- ref = findDefaultBranch(ctx, dst) |
| 79 | ++ // Reset the branch to the fetched ref |
| 80 | ++ cmd = exec.CommandContext(ctx, "git", "reset", "--hard", "FETCH_HEAD") |
| 81 | ++ cmd.Dir = dst |
| 82 | ++ err = getRunCommand(cmd) |
| 83 | ++ if err != nil { |
| 84 | ++ return err |
| 85 | + } |
| 86 | + |
| 87 | +- // We have to be on a branch to pull |
| 88 | +- if err := g.checkout(ctx, dst, ref); err != nil { |
| 89 | ++ // Checkout ref branch |
| 90 | ++ err = g.checkout(ctx, dst, ref) |
| 91 | ++ if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | ++ // Pull the latest changes from the ref branch |
| 96 | + if depth > 0 { |
| 97 | +- cmd = exec.CommandContext(ctx, "git", "pull", "--depth", strconv.Itoa(depth), "--ff-only") |
| 98 | ++ cmd = exec.CommandContext(ctx, "git", "pull", "origin", "--depth", strconv.Itoa(depth), "--ff-only", "--", ref) |
| 99 | + } else { |
| 100 | +- cmd = exec.CommandContext(ctx, "git", "pull", "--ff-only") |
| 101 | ++ cmd = exec.CommandContext(ctx, "git", "pull", "origin", "--ff-only", "--", ref) |
| 102 | + } |
| 103 | + |
| 104 | + cmd.Dir = dst |
| 105 | +@@ -374,3 +410,20 @@ func checkGitVersion(ctx context.Context, min string) error { |
| 106 | + |
| 107 | + return nil |
| 108 | + } |
| 109 | ++ |
| 110 | ++// removeCaseInsensitiveGitDirectory removes all .git directory variations |
| 111 | ++func removeCaseInsensitiveGitDirectory(dst string) error { |
| 112 | ++ files, err := os.ReadDir(dst) |
| 113 | ++ if err != nil { |
| 114 | ++ return fmt.Errorf("Failed to read the destination directory %s during git update", dst) |
| 115 | ++ } |
| 116 | ++ for _, f := range files { |
| 117 | ++ if strings.EqualFold(f.Name(), ".git") && f.IsDir() { |
| 118 | ++ err := os.RemoveAll(filepath.Join(dst, f.Name())) |
| 119 | ++ if err != nil { |
| 120 | ++ return fmt.Errorf("Failed to remove the .git directory in the destination directory %s during git update", dst) |
| 121 | ++ } |
| 122 | ++ } |
| 123 | ++ } |
| 124 | ++ return nil |
| 125 | ++} |
| 126 | +-- |
| 127 | +2.25.1 |
| 128 | + |
0 commit comments