@@ -103,7 +103,7 @@ func (tr *Reader) next() (*Header, error) {
103103 continue // This is a meta header affecting the next header
104104 case TypeGNULongName , TypeGNULongLink :
105105 format .mayOnlyBe (FormatGNU )
106- realname , err := io . ReadAll (tr )
106+ realname , err := readSpecialFile (tr )
107107 if err != nil {
108108 return nil , err
109109 }
@@ -293,7 +293,7 @@ func mergePAX(hdr *Header, paxHdrs map[string]string) (err error) {
293293// parsePAX parses PAX headers.
294294// If an extended header (type 'x') is invalid, ErrHeader is returned
295295func parsePAX (r io.Reader ) (map [string ]string , error ) {
296- buf , err := io . ReadAll (r )
296+ buf , err := readSpecialFile (r )
297297 if err != nil {
298298 return nil , err
299299 }
@@ -828,6 +828,16 @@ func tryReadFull(r io.Reader, b []byte) (n int, err error) {
828828 return n , err
829829}
830830
831+ // readSpecialFile is like io.ReadAll except it returns
832+ // ErrFieldTooLong if more than maxSpecialFileSize is read.
833+ func readSpecialFile (r io.Reader ) ([]byte , error ) {
834+ buf , err := io .ReadAll (io .LimitReader (r , maxSpecialFileSize + 1 ))
835+ if len (buf ) > maxSpecialFileSize {
836+ return nil , ErrFieldTooLong
837+ }
838+ return buf , err
839+ }
840+
831841// discard skips n bytes in r, reporting an error if unable to do so.
832842func discard (r io.Reader , n int64 ) error {
833843 // If possible, Seek to the last byte before the end of the data section.
0 commit comments