@@ -353,6 +353,10 @@ private void processTarFile(List<String> filePaths, File file) throws IOExceptio
353353 byte [] buffer = new byte [512 ]; // TAR header size is always 512 bytes
354354 long currentPos = 0 ;
355355 while (currentPos < fileSize ) {
356+ if (filePaths .size () >= maxPreviewCount ) {
357+ filePaths .add ("... (too many files)" );
358+ break ;
359+ }
356360 // Read the next 512-byte header
357361 raf .seek (currentPos );
358362 raf .readFully (buffer );
@@ -382,17 +386,29 @@ private void processTarFile(List<String> filePaths, File file) throws IOExceptio
382386 * @return a TarHeader object containing file metadata
383387 */
384388 private TarHeader parseTarHeader (byte [] headerBytes ) {
385- // Extract the file name (first 100 bytes)
386- String fileName = new String (headerBytes , 0 , 100 , StandardCharsets .US_ASCII ).trim ();
389+ // Extract null-terminated file name from first 100 bytes
390+ int nameEnd = 0 ;
391+ while (nameEnd < 100 && headerBytes [nameEnd ] != 0 ) {
392+ nameEnd ++;
393+ }
394+ String fileName = new String (headerBytes , 0 , nameEnd , StandardCharsets .US_ASCII );
387395
388396 // If the file name is empty, we've reached the end of the archive
389397 if (fileName .isEmpty ()) {
390398 return null ;
391399 }
392400
393- // Extract the file size (octal value in bytes 124-135)
394- String sizeStr = new String (headerBytes , 124 , 12 , StandardCharsets .US_ASCII ).trim ();
395- long fileSize = Long .parseLong (sizeStr , 8 ); // TAR file sizes are stored in octal
401+ // Extract and sanitize octal file size from bytes 124–135
402+ String sizeStr = new String (headerBytes , 124 , 12 , StandardCharsets .US_ASCII )
403+ .replace ("\0 " , "" ).trim ();
404+
405+ long fileSize ;
406+ try {
407+ fileSize = sizeStr .isEmpty () ? 0L : Long .parseLong (sizeStr , 8 );
408+ } catch (NumberFormatException nfe ) {
409+ log .warn ("Malformed TAR size '{}', treating as 0" , sizeStr , nfe );
410+ fileSize = 0L ;
411+ }
396412
397413 return new TarHeader (fileName , fileSize );
398414 }
@@ -417,6 +433,9 @@ public void processZipFile(List<String> filePaths, File file) throws IOException
417433
418434 // Loop through all entries in the Central Directory
419435 for (long i = 0 ; i < eocd .totalEntries ; i ++) {
436+ if (filePaths .size () >= maxPreviewCount ) {
437+ break ;
438+ }
420439 long currentEntryStart = raf .getFilePointer (); // Track entry position
421440
422441 int signature = readIntLE (raf );
0 commit comments