Harden module installs against untrusted Puppetfiles

- reject option-like :git URLs and refs; pass -- before git positionals
- link modules without following symlinks; reject nested module paths
- judge worktree completeness by an out-of-tree done file
- key git mirrors by URL hash and forge releases by forge base
- bound every git and forge call with the -j semaphore
This commit is contained in:
2026-11-08 17:52:07 +11:00
parent fc5d90b684
commit 01fa51110b
5 changed files with 327 additions and 80 deletions
+20 -11
View File
@@ -9,7 +9,7 @@ import (
"strings"
)
func git(ctx context.Context, dir string, args ...string) (string, error) {
func runGit(ctx context.Context, dir string, args ...string) (string, error) {
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
@@ -21,19 +21,28 @@ func git(ctx context.Context, dir string, args ...string) (string, error) {
return strings.TrimSpace(out.String()), nil
}
func syncMirror(ctx context.Context, mirror, url string, refspecs ...string) error {
// git holds a -j slot only for the subprocess itself, so callers never nest slots.
func (d *Deployer) git(ctx context.Context, dir string, args ...string) (out string, err error) {
err = d.limit(func() error {
out, err = d.runGit(ctx, dir, args...)
return err
})
return out, err
}
func (d *Deployer) syncMirror(ctx context.Context, mirror, url string, refspecs ...string) error {
if _, err := os.Stat(mirror); os.IsNotExist(err) {
if _, err := git(ctx, "", "init", "-q", "--bare", mirror); err != nil {
if _, err := d.git(ctx, "", "init", "-q", "--bare", "--", mirror); err != nil {
return err
}
}
args := append([]string{"fetch", "-q", "--prune", "--force", url}, refspecs...)
_, err := git(ctx, mirror, args...)
args := append([]string{"fetch", "-q", "--prune", "--force", "--", url}, refspecs...)
_, err := d.git(ctx, mirror, args...)
return err
}
func branches(ctx context.Context, mirror string) (map[string]string, error) {
out, err := git(ctx, mirror, "for-each-ref", "--format=%(objectname) %(refname)", "refs/heads/")
func (d *Deployer) branches(ctx context.Context, mirror string) (map[string]string, error) {
out, err := d.git(ctx, mirror, "for-each-ref", "--format=%(objectname) %(refname)", "refs/heads/")
if err != nil {
return nil, err
}
@@ -47,16 +56,16 @@ func branches(ctx context.Context, mirror string) (map[string]string, error) {
return m, nil
}
func revParse(ctx context.Context, mirror string, candidates ...string) (string, error) {
func (d *Deployer) revParse(ctx context.Context, mirror string, candidates ...string) (string, error) {
for _, c := range candidates {
if sha, err := git(ctx, mirror, "rev-parse", "--verify", "--quiet", c+"^{commit}"); err == nil {
if sha, err := d.git(ctx, mirror, "rev-parse", "--verify", "--quiet", "--end-of-options", c+"^{commit}"); err == nil {
return sha, nil
}
}
return "", fmt.Errorf("cannot resolve %s", strings.Join(candidates, " or "))
}
func removeWorktree(ctx context.Context, mirror, path string) error {
_, _ = git(ctx, mirror, "worktree", "remove", "--force", path)
func (d *Deployer) removeWorktree(ctx context.Context, mirror, path string) error {
_, _ = d.git(ctx, mirror, "worktree", "remove", "--force", "--", path)
return os.RemoveAll(path)
}