Skip to content

Commit 0ff5887

Browse files
author
Aaron Schlesinger
committed
fix(sha.go): match exactly on git SHAs
1 parent 1e4f08f commit 0ff5887

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

pkg/gitreceive/git/sha.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package git
22

33
import (
44
"fmt"
5+
"regexp"
56
)
67

78
const (
@@ -10,12 +11,14 @@ const (
1011
fullShaLen = 40
1112
)
1213

13-
type ErrGitShaTooShort struct {
14+
var shaRegex = regexp.MustCompile(`^[\da-f]{40}$`)
15+
16+
type ErrInvalidGitSha struct {
1417
sha string
1518
}
1619

17-
func (e ErrGitShaTooShort) Error() string {
18-
return fmt.Sprintf("git sha %s was too short", e.sha)
20+
func (e ErrInvalidGitSha) Error() string {
21+
return fmt.Sprintf("git sha %s was invalid", e.sha)
1922
}
2023

2124
type SHA struct {
@@ -24,8 +27,8 @@ type SHA struct {
2427
}
2528

2629
func NewSha(rawSha string) (*SHA, error) {
27-
if len(rawSha) < fullShaLen {
28-
return nil, ErrGitShaTooShort{sha: rawSha}
30+
if !shaRegex.Match([]byte(rawSha)) {
31+
return nil, ErrInvalidGitSha{sha: rawSha}
2932
}
3033
return &SHA{Full: rawSha, Short: rawSha[0:8]}, nil
3134
}

0 commit comments

Comments
 (0)