|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.parser.microsoft.ooxml; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | + |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.io.ByteArrayOutputStream; |
| 24 | +import java.util.List; |
| 25 | +import java.util.zip.ZipEntry; |
| 26 | +import java.util.zip.ZipInputStream; |
| 27 | +import java.util.zip.ZipOutputStream; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import org.apache.tika.TikaTest; |
| 32 | +import org.apache.tika.io.TikaInputStream; |
| 33 | +import org.apache.tika.metadata.Metadata; |
| 34 | +import org.apache.tika.metadata.TikaCoreProperties; |
| 35 | +import org.apache.tika.parser.ParseContext; |
| 36 | +import org.apache.tika.parser.microsoft.OfficeParserConfig; |
| 37 | + |
| 38 | +/** |
| 39 | + * Guards that Tika resolves the VBA project through the OPC relationship graph, |
| 40 | + * NOT by raw-zip filename-suffix / stream order. |
| 41 | + * |
| 42 | + * <p>POI's {@code VBAMacroReader.openOOXML} selects the VBA project as the first |
| 43 | + * zip entry (in stream order) whose name ends with "vbaProject.bin", bypassing |
| 44 | + * OPC. A tool built directly on that (an AV/DLP/CDR macro scanner) can be fooled |
| 45 | + * by a decoy vbaProject.bin ordered before the real one, while Office executes |
| 46 | + * the relationship-referenced part (CWE-436 parser differential; reported against |
| 47 | + * POI by n0mi1k, fixed there as a plain bug). |
| 48 | + * |
| 49 | + * <p>Tika does not use that path: it constructs {@code VBAMacroReader} only from a |
| 50 | + * {@code POIFSFileSystem} built from the OPC-resolved {@code vbaProject} part. This |
| 51 | + * test crafts an .xlsm with a decoy vbaProject.bin (a DIFFERENT, valid VBA project |
| 52 | + * lifted from testWORD_macros.docm) placed first in stream order, and asserts Tika |
| 53 | + * still extracts the real /xl/vbaProject.bin macro. If a future change routes macro |
| 54 | + * extraction through the raw-zip path, this fails. |
| 55 | + */ |
| 56 | +public class MacroPartResolutionTest extends TikaTest { |
| 57 | + |
| 58 | + private static final String REAL_MACRO = "Sub Dirty()"; // testEXCEL_macro.xlsm |
| 59 | + private static final String DECOY_MACRO = "Sub Embolden()"; // testWORD_macros.docm |
| 60 | + |
| 61 | + private byte[] resourceBytes(String name) throws Exception { |
| 62 | + try (TikaInputStream tis = getResourceAsStream("/test-documents/" + name)) { |
| 63 | + return tis.readAllBytes(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private byte[] entryEndingWith(byte[] zip, String suffix) throws Exception { |
| 68 | + try (ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zip))) { |
| 69 | + ZipEntry e; |
| 70 | + byte[] buf = new byte[8192]; |
| 71 | + while ((e = zis.getNextEntry()) != null) { |
| 72 | + if (e.getName().toLowerCase(java.util.Locale.ROOT).endsWith(suffix)) { |
| 73 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 74 | + int n; |
| 75 | + while ((n = zis.read(buf)) > 0) { |
| 76 | + bos.write(buf, 0, n); |
| 77 | + } |
| 78 | + return bos.toByteArray(); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + throw new IllegalStateException("no entry ending with " + suffix); |
| 83 | + } |
| 84 | + |
| 85 | + private String allContent(List<Metadata> list) { |
| 86 | + StringBuilder sb = new StringBuilder(); |
| 87 | + for (Metadata m : list) { |
| 88 | + String c = m.get(TikaCoreProperties.TIKA_CONTENT); |
| 89 | + if (c != null) { |
| 90 | + sb.append(c).append('\n'); |
| 91 | + } |
| 92 | + } |
| 93 | + return sb.toString(); |
| 94 | + } |
| 95 | + |
| 96 | + private List<Metadata> parseWithMacros(byte[] bytes) throws Exception { |
| 97 | + ParseContext context = new ParseContext(); |
| 98 | + OfficeParserConfig config = new OfficeParserConfig(); |
| 99 | + config.setExtractMacros(true); |
| 100 | + context.set(OfficeParserConfig.class, config); |
| 101 | + return getRecursiveMetadata(TikaInputStream.get(bytes), new Metadata(), context, false); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testVbaProjectResolvedViaOpcNotStreamOrder() throws Exception { |
| 106 | + byte[] xlsm = resourceBytes("testEXCEL_macro.xlsm"); |
| 107 | + // A different, valid VBA project to act as the decoy. |
| 108 | + byte[] decoyVba = entryEndingWith(resourceBytes("testWORD_macros.docm"), "vbaproject.bin"); |
| 109 | + |
| 110 | + // Sanity: the two projects have distinct, non-overlapping macro signatures. |
| 111 | + String realOnly = allContent(parseWithMacros(xlsm)); |
| 112 | + assertTrue(realOnly.contains(REAL_MACRO), "baseline xlsm should expose real macro"); |
| 113 | + assertFalse(realOnly.contains(DECOY_MACRO), "baseline xlsm must not contain decoy macro"); |
| 114 | + |
| 115 | + // Craft an .xlsm whose FIRST *vbaProject.bin entry (stream order) is the |
| 116 | + // undeclared decoy, with the real, relationship-referenced /xl/vbaProject.bin |
| 117 | + // kept intact later in the stream. |
| 118 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 119 | + try (ZipOutputStream zos = new ZipOutputStream(bos)) { |
| 120 | + zos.putNextEntry(new ZipEntry("xl/decoy/vbaProject.bin")); // undeclared, first in stream |
| 121 | + zos.write(decoyVba); |
| 122 | + zos.closeEntry(); |
| 123 | + try (ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(xlsm))) { |
| 124 | + ZipEntry e; |
| 125 | + byte[] buf = new byte[8192]; |
| 126 | + while ((e = zis.getNextEntry()) != null) { |
| 127 | + zos.putNextEntry(new ZipEntry(e.getName())); |
| 128 | + int n; |
| 129 | + while ((n = zis.read(buf)) > 0) { |
| 130 | + zos.write(buf, 0, n); |
| 131 | + } |
| 132 | + zos.closeEntry(); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + String crafted = allContent(parseWithMacros(bos.toByteArray())); |
| 138 | + // Tika must extract the OPC-resolved real macro, and must NOT have been |
| 139 | + // steered to the stream-order decoy. |
| 140 | + assertTrue(crafted.contains(REAL_MACRO), |
| 141 | + "Tika must extract the OPC-resolved /xl/vbaProject.bin macro"); |
| 142 | + assertFalse(crafted.contains(DECOY_MACRO), |
| 143 | + "Tika must not extract the stream-order decoy vbaProject.bin macro"); |
| 144 | + } |
| 145 | +} |
0 commit comments