diff --git a/core/src/main/java/software/coley/cafedude/io/AttributeReader.java b/core/src/main/java/software/coley/cafedude/io/AttributeReader.java index 77795ae..4ba562c 100644 --- a/core/src/main/java/software/coley/cafedude/io/AttributeReader.java +++ b/core/src/main/java/software/coley/cafedude/io/AttributeReader.java @@ -26,6 +26,7 @@ import java.io.DataInputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -154,6 +155,14 @@ private Attribute read(@Nonnull AttributeContext context) throws IOException { return null; } } + // Check for attributes present in the wrong contexts. + if (reader.doDropBadContextAttributes()) { + Collection allowedContexts = AttributeContexts.getAllowedContexts(name.getText()); + if (!allowedContexts.contains(context)) { + logger.debug("Found '{}' in invalid context {}", name.getText(), context.name()); + return null; + } + } switch (name.getText()) { case AttributeConstants.CODE: return readCode(); diff --git a/core/src/main/java/software/coley/cafedude/io/ClassFileReader.java b/core/src/main/java/software/coley/cafedude/io/ClassFileReader.java index fd8e077..1ebb859 100644 --- a/core/src/main/java/software/coley/cafedude/io/ClassFileReader.java +++ b/core/src/main/java/software/coley/cafedude/io/ClassFileReader.java @@ -29,6 +29,7 @@ public class ClassFileReader { private IndexableByteStream is; // config private boolean dropForwardVersioned = true; + private boolean dropBadContextAttributes = true; private boolean dropEofAttributes = true; private boolean dropDupeAnnotations = true; @@ -350,6 +351,21 @@ public void setDropForwardVersioned(boolean dropForwardVersioned) { this.dropForwardVersioned = dropForwardVersioned; } + /** + * @return {@code true} if attributes declared in the wrongs contexts should be removed. + */ + public boolean doDropBadContextAttributes() { + return dropBadContextAttributes; + } + + /** + * @param dropBadContextAttributes + * {@code true} if attributes declared in the wrong contexts should be removed. + */ + public void setDropBadContextAttributes(boolean dropBadContextAttributes) { + this.dropBadContextAttributes = dropBadContextAttributes; + } + /** * @return {@code true} if attributes that when parsed yield EOF exceptions should be removed. */ diff --git a/core/src/test/java/software/coley/cafedude/MiscTest.java b/core/src/test/java/software/coley/cafedude/MiscTest.java new file mode 100644 index 0000000..405627b --- /dev/null +++ b/core/src/test/java/software/coley/cafedude/MiscTest.java @@ -0,0 +1,39 @@ +package coley.software.cafedude; + +import software.coley.cafedude.io.ClassFileReader; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Miscellaneous tests for checking that the library does not crash on specifically designed tests. + */ +public class MiscTest { + @ParameterizedTest + @MethodSource("supply") + public void testNoErrors(File sample) { + try { + byte[] code = Files.readAllBytes(sample.toPath()); + assertDoesNotThrow(() -> { + new ClassFileReader().read(code); + }, "Library crashes when reading: " + sample.getName()); + } catch (IOException error) { + fail("Failed to read class, IO error", error); + } + } + + /** + * @return Test class files. + */ + public static List supply() { + // Intention may be to add more files in the future, if more crashes are found. + return Arrays.asList(new File[] { new File("src/test/resources/samples/obfuscated/misc/fakecode.class") }); + } +} \ No newline at end of file diff --git a/core/src/test/resources/samples/obfuscated/misc/fakecode.class b/core/src/test/resources/samples/obfuscated/misc/fakecode.class new file mode 100644 index 0000000..98eca32 Binary files /dev/null and b/core/src/test/resources/samples/obfuscated/misc/fakecode.class differ