Skip to content

Commit

Permalink
start-build: remove false alarm warning for small archive binary input
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Wozniak committed Jun 26, 2018
1 parent e004e65 commit 4b7cc00
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions pkg/oc/cli/cmd/startbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,28 +702,43 @@ func streamPathToBuild(repo git.Repository, in io.Reader, out io.Writer, client
}

func isArchive(r *bufio.Reader) bool {
data, err := r.Peek(280)
if err != nil {
archivesMagicNumbers := []struct {
numbers []byte
offset int
}{
{ // unified tar
numbers: []byte{0x75, 0x73, 0x74, 0x61, 0x72},
offset: 0x101,
},
{ //zip
numbers: []byte{0x50, 0x4B, 0x03, 0x04},
},
{ // tar.z
numbers: []byte{0x1F, 0x9D},
},
{ // tar.z
numbers: []byte{0x1F, 0xA0},
},
{ // bz2
numbers: []byte{0x42, 0x5A, 0x68},
},
{ // gzip
numbers: []byte{0x1F, 0x8B, 0x08},
},
}
maxOffset := archivesMagicNumbers[0].offset //unified tar
data, err := r.Peek(maxOffset + len(archivesMagicNumbers[0].numbers))
if err != nil && err != io.EOF {
return false
}
for _, b := range [][]byte{
{0x50, 0x4B, 0x03, 0x04}, // zip
{0x1F, 0x9D}, // tar.z
{0x1F, 0xA0}, // tar.z
{0x42, 0x5A, 0x68}, // bz2
{0x1F, 0x8B, 0x08}, // gzip
} {
if bytes.HasPrefix(data, b) {

for _, magic := range archivesMagicNumbers {
if len(data) >= magic.offset+len(magic.numbers) &&
bytes.Equal(data[magic.offset:magic.offset+len(magic.numbers)], magic.numbers) {
return true
}
}
switch {
// Unified TAR files have this magic number
case len(data) > 257+5 && bytes.Equal(data[257:257+5], []byte{0x75, 0x73, 0x74, 0x61, 0x72}):
return true
default:
return false
}
return false
}

// RunStartBuildWebHook tries to trigger the provided webhook. It will attempt to utilize the current client
Expand Down

0 comments on commit 4b7cc00

Please sign in to comment.