Skip to content

Commit e20134c

Browse files
authored
Merge pull request #26 from aryan9600/aryan9600/verify-artifact
Verify source artifact checksum
2 parents cb922ee + c0f13eb commit e20134c

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

controllers/gitrepository_watcher.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"bytes"
2021
"context"
22+
"crypto/sha1"
23+
"crypto/sha256"
2124
"fmt"
25+
"io"
2226
"io/ioutil"
2327
"net/http"
2428
"os"
@@ -118,11 +122,40 @@ func (r *GitRepositoryWatcher) fetchArtifact(ctx context.Context, repository sou
118122
return "", fmt.Errorf("failed to download artifact, status: %s", resp.Status)
119123
}
120124

125+
var buf bytes.Buffer
126+
127+
// verify checksum matches origin
128+
if err := r.verifyArtifact(repository.GetArtifact(), &buf, resp.Body); err != nil {
129+
return "", err
130+
}
131+
121132
// extract
122-
summary, err := untar.Untar(resp.Body, dir)
133+
summary, err := untar.Untar(&buf, dir)
123134
if err != nil {
124135
return "", fmt.Errorf("faild to untar artifact, error: %w", err)
125136
}
126137

127138
return summary, nil
128139
}
140+
141+
func (r *GitRepositoryWatcher) verifyArtifact(artifact *sourcev1.Artifact, buf *bytes.Buffer, reader io.Reader) error {
142+
hasher := sha256.New()
143+
144+
// for backwards compatibility with source-controller v0.17.2 and older
145+
if len(artifact.Checksum) == 40 {
146+
hasher = sha1.New()
147+
}
148+
149+
// compute checksum
150+
mw := io.MultiWriter(hasher, buf)
151+
if _, err := io.Copy(mw, reader); err != nil {
152+
return err
153+
}
154+
155+
if checksum := fmt.Sprintf("%x", hasher.Sum(nil)); checksum != artifact.Checksum {
156+
return fmt.Errorf("failed to verify artifact: computed checksum '%s' doesn't match advertised '%s'",
157+
checksum, artifact.Checksum)
158+
}
159+
160+
return nil
161+
}

0 commit comments

Comments
 (0)