|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* @test |
| 25 | + * @bug 8272746 |
| 26 | + * @summary Verify that ZipFile rejects files with CEN sizes exceeding the implementation limit |
| 27 | + * @run testng/othervm EndOfCenValidation |
| 28 | + */ |
| 29 | + |
| 30 | +import org.testng.annotations.AfterTest; |
| 31 | +import org.testng.annotations.BeforeTest; |
| 32 | +import org.testng.annotations.Test; |
| 33 | + |
| 34 | +import java.io.*; |
| 35 | +import java.nio.ByteBuffer; |
| 36 | +import java.nio.ByteOrder; |
| 37 | +import java.nio.channels.FileChannel; |
| 38 | +import java.nio.charset.StandardCharsets; |
| 39 | +import java.nio.file.Files; |
| 40 | +import java.nio.file.Path; |
| 41 | +import java.nio.file.StandardOpenOption; |
| 42 | +import java.util.Arrays; |
| 43 | +import java.util.EnumSet; |
| 44 | +import java.util.zip.ZipEntry; |
| 45 | +import java.util.zip.ZipException; |
| 46 | +import java.util.zip.ZipFile; |
| 47 | +import java.util.zip.ZipOutputStream; |
| 48 | + |
| 49 | +import static org.testng.Assert.*; |
| 50 | + |
| 51 | +/** |
| 52 | + * This test augments {@link TestTooManyEntries}. It creates sparse ZIPs where |
| 53 | + * the CEN size is inflated to the desired value. This helps this test run |
| 54 | + * fast with much less resources. |
| 55 | + * |
| 56 | + * While the CEN in these files are zero-filled and the produced ZIPs are technically |
| 57 | + * invalid, the CEN is never actually read by ZipFile since it does |
| 58 | + * 'End of central directory record' (END header) validation before reading the CEN. |
| 59 | + */ |
| 60 | +public class EndOfCenValidation { |
| 61 | + |
| 62 | + // Zip files produced by this test |
| 63 | + public static final Path CEN_TOO_LARGE_ZIP = Path.of("cen-size-too-large.zip"); |
| 64 | + public static final Path INVALID_CEN_SIZE = Path.of("invalid-zen-size.zip"); |
| 65 | + public static final Path BAD_CEN_OFFSET_ZIP = Path.of("bad-cen-offset.zip"); |
| 66 | + // Some ZipFile constants for manipulating the 'End of central directory record' (END header) |
| 67 | + private static final int ENDHDR = ZipFile.ENDHDR; // End of central directory record size |
| 68 | + private static final int ENDSIZ = ZipFile.ENDSIZ; // Offset of CEN size field within ENDHDR |
| 69 | + private static final int ENDOFF = ZipFile.ENDOFF; // Offset of CEN offset field within ENDHDR |
| 70 | + // Maximum allowed CEN size allowed by ZipFile |
| 71 | + private static int MAX_CEN_SIZE = Integer.MAX_VALUE - ENDHDR - 1; |
| 72 | + |
| 73 | + // Expected message when CEN size does not match file size |
| 74 | + private static final String INVALID_CEN_BAD_SIZE = "invalid END header (bad central directory size)"; |
| 75 | + // Expected message when CEN offset is too large |
| 76 | + private static final String INVALID_CEN_BAD_OFFSET = "invalid END header (bad central directory offset)"; |
| 77 | + // Expected message when CEN size is too large |
| 78 | + private static final String INVALID_CEN_SIZE_TOO_LARGE = "invalid END header (central directory size too large)"; |
| 79 | + |
| 80 | + // A valid ZIP file, used as a template |
| 81 | + private byte[] zipBytes; |
| 82 | + |
| 83 | + /** |
| 84 | + * Create a valid ZIP file, used as a template |
| 85 | + * @throws IOException if an error occurs |
| 86 | + */ |
| 87 | + @BeforeTest |
| 88 | + public void setup() throws IOException { |
| 89 | + zipBytes = templateZip(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Delete big files after test, in case the file system did not support sparse files. |
| 94 | + * @throws IOException if an error occurs |
| 95 | + */ |
| 96 | + @AfterTest |
| 97 | + public void cleanup() throws IOException { |
| 98 | + Files.deleteIfExists(CEN_TOO_LARGE_ZIP); |
| 99 | + Files.deleteIfExists(INVALID_CEN_SIZE); |
| 100 | + Files.deleteIfExists(BAD_CEN_OFFSET_ZIP); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Validates that an 'End of central directory record' (END header) with a CEN |
| 105 | + * length exceeding {@link #MAX_CEN_SIZE} limit is rejected |
| 106 | + * @throws IOException if an error occurs |
| 107 | + */ |
| 108 | + @Test |
| 109 | + public void shouldRejectTooLargeCenSize() throws IOException { |
| 110 | + int size = MAX_CEN_SIZE + 1; |
| 111 | + |
| 112 | + Path zip = zipWithModifiedEndRecord(size, true, 0, CEN_TOO_LARGE_ZIP); |
| 113 | + |
| 114 | + ZipException ex = expectThrows(ZipException.class, () -> { |
| 115 | + new ZipFile(zip.toFile()); |
| 116 | + }); |
| 117 | + |
| 118 | + assertEquals(ex.getMessage(), INVALID_CEN_SIZE_TOO_LARGE); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Validate that an 'End of central directory record' (END header) |
| 123 | + * where the value of the CEN size field exceeds the position of |
| 124 | + * the END header is rejected. |
| 125 | + * @throws IOException if an error occurs |
| 126 | + */ |
| 127 | + @Test |
| 128 | + public void shouldRejectInvalidCenSize() throws IOException { |
| 129 | + |
| 130 | + int size = MAX_CEN_SIZE; |
| 131 | + |
| 132 | + Path zip = zipWithModifiedEndRecord(size, false, 0, INVALID_CEN_SIZE); |
| 133 | + |
| 134 | + ZipException ex = expectThrows(ZipException.class, () -> { |
| 135 | + new ZipFile(zip.toFile()); |
| 136 | + }); |
| 137 | + |
| 138 | + assertEquals(ex.getMessage(), INVALID_CEN_BAD_SIZE); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Validate that an 'End of central directory record' (the END header) |
| 143 | + * where the value of the CEN offset field is larger than the position |
| 144 | + * of the END header minus the CEN size is rejected |
| 145 | + * @throws IOException if an error occurs |
| 146 | + */ |
| 147 | + @Test |
| 148 | + public void shouldRejectInvalidCenOffset() throws IOException { |
| 149 | + |
| 150 | + int size = MAX_CEN_SIZE; |
| 151 | + |
| 152 | + Path zip = zipWithModifiedEndRecord(size, true, 100, BAD_CEN_OFFSET_ZIP); |
| 153 | + |
| 154 | + ZipException ex = expectThrows(ZipException.class, () -> { |
| 155 | + new ZipFile(zip.toFile()); |
| 156 | + }); |
| 157 | + |
| 158 | + assertEquals(ex.getMessage(), INVALID_CEN_BAD_OFFSET); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Create an ZIP file with a single entry, then modify the CEN size |
| 163 | + * in the 'End of central directory record' (END header) to the given size. |
| 164 | + * |
| 165 | + * The CEN is optionally "inflated" with trailing zero bytes such that |
| 166 | + * its actual size matches the one stated in the END header. |
| 167 | + * |
| 168 | + * The CEN offset is optiontially adjusted by the given amount |
| 169 | + * |
| 170 | + * The resulting ZIP is technically not valid, but it does allow us |
| 171 | + * to test that large or invalid CEN sizes are rejected |
| 172 | + * @param cenSize the CEN size to put in the END record |
| 173 | + * @param inflateCen if true, zero-pad the CEN to the desired size |
| 174 | + * @param cenOffAdjust Adjust the CEN offset field of the END record with this amount |
| 175 | + * @throws IOException if an error occurs |
| 176 | + */ |
| 177 | + private Path zipWithModifiedEndRecord(int cenSize, |
| 178 | + boolean inflateCen, |
| 179 | + int cenOffAdjust, |
| 180 | + Path zip) throws IOException { |
| 181 | + |
| 182 | + // A byte buffer for reading the END |
| 183 | + ByteBuffer buffer = ByteBuffer.wrap(zipBytes.clone()).order(ByteOrder.LITTLE_ENDIAN); |
| 184 | + |
| 185 | + // Offset of the END header |
| 186 | + int endOffset = buffer.limit() - ENDHDR; |
| 187 | + |
| 188 | + // Modify the CEN size |
| 189 | + int sizeOffset = endOffset + ENDSIZ; |
| 190 | + int currentCenSize = buffer.getInt(sizeOffset); |
| 191 | + buffer.putInt(sizeOffset, cenSize); |
| 192 | + |
| 193 | + // Optionally modify the CEN offset |
| 194 | + if (cenOffAdjust != 0) { |
| 195 | + int offOffset = endOffset + ENDOFF; |
| 196 | + int currentCenOff = buffer.getInt(offOffset); |
| 197 | + buffer.putInt(offOffset, currentCenOff + cenOffAdjust); |
| 198 | + } |
| 199 | + |
| 200 | + // When creating a sparse file, the file must not already exit |
| 201 | + Files.deleteIfExists(zip); |
| 202 | + |
| 203 | + // Open a FileChannel for writing a sparse file |
| 204 | + EnumSet<StandardOpenOption> options = EnumSet.of(StandardOpenOption.CREATE_NEW, |
| 205 | + StandardOpenOption.WRITE, |
| 206 | + StandardOpenOption.SPARSE); |
| 207 | + |
| 208 | + try (FileChannel channel = FileChannel.open(zip, options)) { |
| 209 | + |
| 210 | + // Write everything up to END |
| 211 | + channel.write(buffer.slice(0, buffer.limit() - ENDHDR)); |
| 212 | + |
| 213 | + if (inflateCen) { |
| 214 | + // Inject "empty bytes" to make the actual CEN size match the END |
| 215 | + int injectBytes = cenSize - currentCenSize; |
| 216 | + channel.position(channel.position() + injectBytes); |
| 217 | + } |
| 218 | + // Write the modified END |
| 219 | + channel.write(buffer.slice(buffer.limit() - ENDHDR, ENDHDR)); |
| 220 | + } |
| 221 | + return zip; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Produce a byte array of a ZIP with a single entry |
| 226 | + * |
| 227 | + * @throws IOException if an error occurs |
| 228 | + */ |
| 229 | + private byte[] templateZip() throws IOException { |
| 230 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 231 | + try (ZipOutputStream zo = new ZipOutputStream(bout)) { |
| 232 | + ZipEntry entry = new ZipEntry("duke.txt"); |
| 233 | + zo.putNextEntry(entry); |
| 234 | + zo.write("duke".getBytes(StandardCharsets.UTF_8)); |
| 235 | + } |
| 236 | + return bout.toByteArray(); |
| 237 | + } |
| 238 | +} |
0 commit comments