Merge pull request #8459 from benebsiny/cli-8009

Verify the downloaded zip for `gh run view --log`
This commit is contained in:
Andy Feller 2023-12-15 08:41:09 -05:00 committed by GitHub
commit c626f9039b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View file

@ -3,6 +3,7 @@ package view
import (
"archive/zip"
"bufio"
"bytes"
"errors"
"fmt"
"io"
@ -29,7 +30,7 @@ import (
type runLogCache interface {
Exists(string) bool
Create(string, io.ReadCloser) error
Create(string, io.Reader) error
Open(string) (*zip.ReadCloser, error)
}
@ -41,7 +42,7 @@ func (rlc) Exists(path string) bool {
}
return true
}
func (rlc) Create(path string, content io.ReadCloser) error {
func (rlc) Create(path string, content io.Reader) error {
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
return fmt.Errorf("could not create cache: %w", err)
@ -442,7 +443,19 @@ func getRunLog(cache runLogCache, httpClient *http.Client, repo ghrepo.Interface
}
defer resp.Close()
err = cache.Create(filepath, resp)
data, err := io.ReadAll(resp)
if err != nil {
return nil, err
}
respReader := bytes.NewReader(data)
// Check if the response is a valid zip format
_, err = zip.NewReader(respReader, respReader.Size())
if err != nil {
return nil, err
}
err = cache.Create(filepath, respReader)
if err != nil {
return nil, err
}

View file

@ -1494,7 +1494,7 @@ type testRunLogCache struct{}
func (testRunLogCache) Exists(path string) bool {
return false
}
func (testRunLogCache) Create(path string, content io.ReadCloser) error {
func (testRunLogCache) Create(path string, content io.Reader) error {
return nil
}
func (testRunLogCache) Open(path string) (*zip.ReadCloser, error) {