|
| 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 | + |
| 25 | +import org.testng.annotations.BeforeTest; |
| 26 | +import org.testng.annotations.Test; |
| 27 | + |
| 28 | +import java.io.ByteArrayOutputStream; |
| 29 | +import java.io.FileOutputStream; |
| 30 | +import java.io.IOException; |
| 31 | +import java.nio.ByteBuffer; |
| 32 | +import java.nio.ByteOrder; |
| 33 | +import java.nio.channels.FileChannel; |
| 34 | +import java.nio.file.Path; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.zip.ZipEntry; |
| 37 | +import java.util.zip.ZipException; |
| 38 | +import java.util.zip.ZipFile; |
| 39 | +import java.util.zip.ZipOutputStream; |
| 40 | + |
| 41 | +import static org.testng.Assert.assertEquals; |
| 42 | +import static org.testng.Assert.expectThrows; |
| 43 | + |
| 44 | +/** |
| 45 | + * @test |
| 46 | + * @summary Validate that opening ZIP files files with invalid UTF-8 |
| 47 | + * byte sequences in the name or comment fields fails with ZipException |
| 48 | + * @run testng/othervm InvalidBytesInEntryNameOrComment |
| 49 | + */ |
| 50 | +public class InvalidBytesInEntryNameOrComment { |
| 51 | + |
| 52 | + // Offsets for navigating the CEN fields |
| 53 | + private static final int EOC_OFF = 6; // Offset from EOF to find CEN offset |
| 54 | + private static final int CEN_HDR = 45; // Size of a CEN header |
| 55 | + private static final int NLEN = 28; // Name length |
| 56 | + private static final int ELEN = 30; // Extra length |
| 57 | + private static final int CLEN = 32; // Comment length |
| 58 | + |
| 59 | + // Example invalid UTF-8 byte sequence |
| 60 | + private static final byte[] INVALID_UTF8_BYTE_SEQUENCE = {(byte) 0xF0, (byte) 0xA4, (byte) 0xAD}; |
| 61 | + |
| 62 | + // Expected ZipException regex |
| 63 | + private static final String BAD_ENTRY_NAME_OR_COMMENT = "invalid CEN header (bad entry name or comment)"; |
| 64 | + |
| 65 | + // ZIP file with invalid name field |
| 66 | + private Path invalidName; |
| 67 | + |
| 68 | + // ZIP file with invalid comment field |
| 69 | + private Path invalidComment; |
| 70 | + |
| 71 | + @BeforeTest |
| 72 | + public void setup() throws IOException { |
| 73 | + // Create a ZIP file with valid name and comment fields |
| 74 | + byte[] templateZip = templateZIP(); |
| 75 | + |
| 76 | + // Create a ZIP with a CEN name field containing an invalid byte sequence |
| 77 | + invalidName = invalidName("invalid-name.zip", templateZip); |
| 78 | + |
| 79 | + // Create a ZIP with a CEN comment field containing an invalid byte sequence |
| 80 | + invalidComment = invalidComment("invalid-comment.zip", templateZip); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Opening a ZipFile with an invalid UTF-8 byte sequence in |
| 85 | + * the name field of a CEN file header should throw a |
| 86 | + * ZipException with "bad entry name or comment" |
| 87 | + */ |
| 88 | + @Test |
| 89 | + public void shouldRejectInvalidName() throws IOException { |
| 90 | + ZipException ex = expectThrows(ZipException.class, () -> { |
| 91 | + new ZipFile(invalidName.toFile()); |
| 92 | + }); |
| 93 | + assertEquals(ex.getMessage(), BAD_ENTRY_NAME_OR_COMMENT); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Opening a ZipFile with an invalid UTF-8 byte sequence in |
| 98 | + * the comment field of a CEN file header should throw a |
| 99 | + * ZipException with "bad entry name or comment" |
| 100 | + */ |
| 101 | + @Test |
| 102 | + public void shouldRejectInvalidComment() throws IOException { |
| 103 | + ZipException ex = expectThrows(ZipException.class, () -> { |
| 104 | + new ZipFile(invalidComment.toFile()); |
| 105 | + }); |
| 106 | + assertEquals(ex.getMessage(), BAD_ENTRY_NAME_OR_COMMENT); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Make a valid ZIP file used as a template for invalid files |
| 111 | + */ |
| 112 | + private byte[] templateZIP() throws IOException { |
| 113 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 114 | + try (ZipOutputStream zo = new ZipOutputStream(bout)) { |
| 115 | + ZipEntry commentEntry = new ZipEntry("file"); |
| 116 | + commentEntry.setComment("Comment"); |
| 117 | + zo.putNextEntry(commentEntry); |
| 118 | + } |
| 119 | + return bout.toByteArray(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Make a ZIP with invalid bytes in the CEN name field |
| 124 | + */ |
| 125 | + private Path invalidName(String name, byte[] template) throws IOException { |
| 126 | + ByteBuffer buffer = copyTemplate(template); |
| 127 | + int off = cenStart(buffer); |
| 128 | + // Name field starts here |
| 129 | + int noff = off + CEN_HDR; |
| 130 | + |
| 131 | + // Write invalid bytes |
| 132 | + buffer.put(noff, INVALID_UTF8_BYTE_SEQUENCE, 0, INVALID_UTF8_BYTE_SEQUENCE.length); |
| 133 | + return writeFile(name, buffer); |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Make a copy of the ZIP template and wrap it in a little-endian |
| 139 | + * ByteBuffer |
| 140 | + */ |
| 141 | + private ByteBuffer copyTemplate(byte[] template) { |
| 142 | + return ByteBuffer.wrap(Arrays.copyOf(template, template.length)) |
| 143 | + .order(ByteOrder.LITTLE_ENDIAN); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Make a ZIP with invalid bytes in the CEN comment field |
| 148 | + */ |
| 149 | + private Path invalidComment(String name, byte[] template) throws IOException { |
| 150 | + ByteBuffer buffer = copyTemplate(template); |
| 151 | + int off = cenStart(buffer); |
| 152 | + // Need to skip past the length of the name and extra fields |
| 153 | + int nlen = buffer.getShort(off + NLEN); |
| 154 | + int elen = buffer.getShort(off + ELEN); |
| 155 | + |
| 156 | + // Comment field starts here |
| 157 | + int coff = off + CEN_HDR + nlen + elen; |
| 158 | + |
| 159 | + // Write invalid bytes |
| 160 | + buffer.put(coff, INVALID_UTF8_BYTE_SEQUENCE, 0, INVALID_UTF8_BYTE_SEQUENCE.length); |
| 161 | + return writeFile(name, buffer); |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + /** |
| 166 | + * Finds the offset of the start of the CEN directory |
| 167 | + */ |
| 168 | + private int cenStart(ByteBuffer buffer) { |
| 169 | + return buffer.getInt(buffer.capacity() - EOC_OFF); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Utility to write a ByteBuffer to disk |
| 174 | + */ |
| 175 | + private Path writeFile(String name, ByteBuffer buffer) throws IOException { |
| 176 | + Path zip = Path.of(name); |
| 177 | + try (FileChannel ch = new FileOutputStream(zip.toFile()).getChannel()) { |
| 178 | + buffer.rewind(); |
| 179 | + ch.write(buffer); |
| 180 | + } |
| 181 | + return zip; |
| 182 | + } |
| 183 | +} |
0 commit comments