Skip to content

Commit f5462c2

Browse files
tonistiigicrazy-max
authored andcommitted
git: harden ref arg handling
Validate user-provided refs once during identifier construction and reject option-like refs with leading '-'. There is no known attack related to previous core, patch is to make ref handling more robust and improve errors. Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com> (cherry picked from commit e7f8093e1b386ffe711c8468ca8cdde8cfea0c72) (cherry picked from commit d4ea5ef18829b57ee0a52620cd11a8c3ea5db01d)
1 parent 71577a5 commit f5462c2

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

source/git/source.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ func (gs *Source) Identifier(scheme, ref string, attrs map[string]string, platfo
136136
id.VerifySignature.IgnoreSignedTag = v == "true"
137137
}
138138
}
139+
if err := validateGitRef(id.Ref); err != nil {
140+
return nil, err
141+
}
139142

140143
return id, nil
141144
}
@@ -548,10 +551,9 @@ func (gs *gitSourceHandler) resolveMetadata(ctx context.Context, jobCtx solver.J
548551
return nil, err
549552
}
550553
}
551-
552554
// TODO: should we assume that remote tag is immutable? add a timer?
553555

554-
buf, err := tmpGit.Run(ctx, "ls-remote", gs.src.Remote, ref, ref+"^{}")
556+
buf, err := tmpGit.Run(ctx, "ls-remote", "--", gs.src.Remote, ref, ref+"^{}")
555557
if err != nil {
556558
return nil, errors.Wrapf(err, "failed to fetch remote %s", urlutil.RedactCredentials(remote))
557559
}
@@ -862,11 +864,10 @@ func (gs *gitSourceHandler) tryRemoteFetch(ctx context.Context, g session.Group,
862864
}
863865
gs.src.Ref = ref
864866
}
865-
866867
doFetch := true
867868
if gitutil.IsCommitSHA(ref) {
868869
// skip fetch if commit already exists
869-
if _, err := git.Run(ctx, "cat-file", "-e", ref+"^{commit}"); err == nil {
870+
if _, err := git.Run(ctx, "cat-file", "-e", "--", ref+"^{commit}"); err == nil {
870871
doFetch = false
871872
}
872873
}
@@ -896,7 +897,7 @@ func (gs *gitSourceHandler) tryRemoteFetch(ctx context.Context, g session.Group,
896897
if gitutil.IsCommitSHA(ref) {
897898
args = append(args, ref)
898899
} else {
899-
args = append(args, "--force", ref+":"+targetRef)
900+
args = append(args, "--force", "--", ref+":"+targetRef)
900901
}
901902
if _, err := git.Run(ctx, args...); err != nil {
902903
err := errors.Wrapf(err, "failed to fetch remote %s", urlutil.RedactCredentials(gs.src.Remote))
@@ -1043,7 +1044,7 @@ func (gs *gitSourceHandler) checkout(ctx context.Context, repo *gitRepo, g sessi
10431044
} else {
10441045
pullref += ":" + pullref
10451046
}
1046-
_, err = checkoutGit.Run(ctx, "fetch", "-u", "--depth=1", "origin", pullref)
1047+
_, err = checkoutGit.Run(ctx, "fetch", "-u", "--depth=1", "--", "origin", pullref)
10471048
if err != nil {
10481049
return nil, err
10491050
}
@@ -1169,6 +1170,13 @@ func isUnableToUpdateLocalRef(err error) bool {
11691170
strings.Contains(msg, "refname conflict")
11701171
}
11711172

1173+
func validateGitRef(ref string) error {
1174+
if strings.HasPrefix(ref, "-") {
1175+
return errors.Errorf("invalid git ref %q", ref)
1176+
}
1177+
return nil
1178+
}
1179+
11721180
func (gs *gitSourceHandler) emptyGitCli(ctx context.Context, g session.Group, opts ...gitutil.Option) (*gitutil.GitCLI, func() error, error) {
11731181
var cleanups []func() error
11741182
cleanup := func() error {

0 commit comments

Comments
 (0)