From 9faa8329260a073ffbcb6dd3d8fb5987636a0cae Mon Sep 17 00:00:00 2001 From: clickin Date: Wed, 21 Oct 2026 11:17:27 +0900 Subject: [PATCH] [COMPRESS-645] Reject an incomplete TAR record before the end of the archive TarArchiveInputStream.readRecord() returned null both at a clean end of input and when fewer than 512 bytes were available, so input shorter than one record, such as a small .tar.gz passed to the TAR reader, was reported as an empty archive. Throw EOFException for a partial record unless the EOF marker has already been read. Generated-by: OpenAI GPT-6 Astra Assisted-By: Claude Opus 5.5 --- .../archivers/tar/TarArchiveInputStream.java | 3 ++ .../tar/TarArchiveInputStreamTest.java | 52 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java index 7607d5ccf4a..0f9ff39043a 100644 --- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java @@ -700,6 +700,9 @@ protected byte[] readRecord() throws IOException { final int readCount = IOUtils.read(in, recordBuffer); count(readCount); if (readCount != getRecordSize()) { + if (readCount > 0 && !isAtEOF()) { + throw new EOFException(String.format("Truncated TAR archive: Expected %,d bytes in a record, actual %,d", getRecordSize(), readCount)); + } return null; } return recordBuffer; diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java index 896e31e7e14..3ff2db7a8a3 100644 --- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java @@ -445,6 +445,58 @@ void testRejectsArchivesWithNegativeSizes() throws Exception { } } + @ParameterizedTest + @ValueSource(ints = { 1, 511 }) + void testRejectsIncompleteRecord(final int size) throws IOException { + final byte[] data = new byte[size]; + Arrays.fill(data, (byte) 'x'); + try (TarArchiveInputStream archive = TarArchiveInputStream.builder().setByteArray(data).get()) { + assertThrows(EOFException.class, archive::getNextEntry); + } + } + + @Test + void testRejectsIncompleteRecordAfterEntry() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + try (TarArchiveOutputStream archive = new TarArchiveOutputStream(output)) { + archive.putArchiveEntry(new TarArchiveEntry("empty")); + archive.closeArchiveEntry(); + } + final byte[] data = Arrays.copyOf(output.toByteArray(), TarConstants.DEFAULT_RCDSIZE + 1); + data[TarConstants.DEFAULT_RCDSIZE] = 'x'; + try (TarArchiveInputStream archive = TarArchiveInputStream.builder().setByteArray(data).get()) { + assertEquals("empty", archive.getNextEntry().getName()); + assertThrows(EOFException.class, archive::getNextEntry); + } + } + + @ParameterizedTest + @ValueSource(ints = { 0, 512, 513, 1023, 1024 }) + void testEmptyArchiveAndPartialPadding(final int size) throws IOException { + try (TarArchiveInputStream archive = TarArchiveInputStream.builder().setByteArray(new byte[size]).get()) { + assertNull(archive.getNextEntry()); + } + } + + @Test + void testArchiveWithoutEofMarker() throws IOException { + final byte[] data = new byte[TarConstants.DEFAULT_RCDSIZE]; + new TarArchiveEntry("empty").writeEntryHeader(data); + try (TarArchiveInputStream archive = TarArchiveInputStream.builder().setByteArray(data).get()) { + assertEquals("empty", archive.getNextEntry().getName()); + assertNull(archive.getNextEntry()); + } + } + + @Test + void testPartialGarbageAfterEofMarker() throws IOException { + final byte[] data = new byte[TarConstants.DEFAULT_RCDSIZE + 1]; + data[TarConstants.DEFAULT_RCDSIZE] = 'x'; + try (TarArchiveInputStream archive = TarArchiveInputStream.builder().setByteArray(data).get()) { + assertNull(archive.getNextEntry()); + } + } + /** * This test ensures the implementation is reading the padded last block if a tool has added one to an archive */