@@ -709,28 +709,43 @@ func streamPathToBuild(repo git.Repository, in io.Reader, out io.Writer, client
709709}
710710
711711func isArchive (r * bufio.Reader ) bool {
712- data , err := r .Peek (280 )
713- if err != nil {
712+ archivesMagicNumbers := []struct {
713+ numbers []byte
714+ offset int
715+ }{ //https://en.wikipedia.org/wiki/List_of_file_signatures
716+ { // unified tar
717+ numbers : []byte {0x75 , 0x73 , 0x74 , 0x61 , 0x72 },
718+ offset : 0x101 ,
719+ },
720+ { //zip
721+ numbers : []byte {0x50 , 0x4B , 0x03 , 0x04 },
722+ },
723+ { // tar.z
724+ numbers : []byte {0x1F , 0x9D },
725+ },
726+ { // tar.z
727+ numbers : []byte {0x1F , 0xA0 },
728+ },
729+ { // bz2
730+ numbers : []byte {0x42 , 0x5A , 0x68 },
731+ },
732+ { // gzip
733+ numbers : []byte {0x1F , 0x8B },
734+ },
735+ }
736+ maxOffset := archivesMagicNumbers [0 ].offset //unified tar
737+ data , err := r .Peek (maxOffset + len (archivesMagicNumbers [0 ].numbers ))
738+ if err != nil && err != io .EOF {
714739 return false
715740 }
716- for _ , b := range [][]byte {
717- {0x50 , 0x4B , 0x03 , 0x04 }, // zip
718- {0x1F , 0x9D }, // tar.z
719- {0x1F , 0xA0 }, // tar.z
720- {0x42 , 0x5A , 0x68 }, // bz2
721- {0x1F , 0x8B , 0x08 }, // gzip
722- } {
723- if bytes .HasPrefix (data , b ) {
741+
742+ for _ , magic := range archivesMagicNumbers {
743+ if len (data ) >= magic .offset + len (magic .numbers ) &&
744+ bytes .Equal (data [magic .offset :magic .offset + len (magic .numbers )], magic .numbers ) {
724745 return true
725746 }
726747 }
727- switch {
728- // Unified TAR files have this magic number
729- case len (data ) > 257 + 5 && bytes .Equal (data [257 :257 + 5 ], []byte {0x75 , 0x73 , 0x74 , 0x61 , 0x72 }):
730- return true
731- default :
732- return false
733- }
748+ return false
734749}
735750
736751// RunStartBuildWebHook tries to trigger the provided webhook. It will attempt to utilize the current client
0 commit comments