Skip to content

Commit 99cef3d

Browse files
committed
bump ver and support java 8
1 parent 750af14 commit 99cef3d

File tree

7 files changed

+53
-49
lines changed

7 files changed

+53
-49
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>io.github.pixee</groupId>
77
<artifactId>java-security-toolkit</artifactId>
8-
<version>1.0.3</version>
8+
<version>1.0.4</version>
99

1010
<name>java-security-toolkit</name>
1111
<description>a library with common security controls</description>
@@ -163,8 +163,8 @@
163163
<artifactId>maven-compiler-plugin</artifactId>
164164
<version>${versions.maven-compiler-plugin}</version>
165165
<configuration>
166-
<source>11</source>
167-
<target>11</target>
166+
<source>8</source>
167+
<target>8</target>
168168
</configuration>
169169
</plugin>
170170
<plugin>

src/main/java/io/github/pixee/security/ObjectInputFilters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static ObjectInputFilter getHardenedObjectFilter() {
4343
* @param ois the reader to secure
4444
*/
4545
public static void enableObjectFilterIfUnprotected(final ObjectInputStream ois) {
46-
var objectInputFilter = ois.getObjectInputFilter();
46+
ObjectInputFilter objectInputFilter = ois.getObjectInputFilter();
4747
if (objectInputFilter == null) {
4848
try {
4949
ois.setObjectInputFilter(basicGadgetDenylistFilter);

src/main/java/io/github/pixee/security/SystemCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,11 @@ enum CommandParsingContext {
366366
}
367367

368368
private static int findCommandSeparator(final String command) {
369-
var context = new LinkedList<CommandParsingContext>();
370-
var i = 0;
369+
LinkedList<CommandParsingContext> context = new LinkedList<>();
370+
int i = 0;
371371
context.push(CommandParsingContext.DEFAULT);
372372
while (i < command.length()) {
373-
var currentContext = context.peek();
373+
CommandParsingContext currentContext = context.peek();
374374
switch (currentContext) {
375375
case DOUBLE_QUOTE:
376376
i = eatUntilNextDoubleQuote(command, i);

src/test/java/io/github/pixee/security/DocumentBuilderFactorySecurityTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,30 @@
1010
import javax.xml.parsers.DocumentBuilderFactory;
1111
import org.apache.commons.io.FileUtils;
1212
import org.junit.jupiter.api.Test;
13+
import org.w3c.dom.Document;
1314

1415
final class DocumentBuilderFactorySecurityTest {
1516

1617
@Test
1718
void xxe_works_in_dbf() throws Exception {
18-
var exploit = generateExploit();
19-
var factory = DocumentBuilderFactory.newInstance();
19+
String exploit = generateExploit();
20+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
2021
String secretText = getSecretText(factory, exploit);
2122
assertThat("s3cr3t", equalTo(secretText)); // string is empty instead of secret!
2223
}
2324

2425
@Test
2526
void it_prevents_xxe_in_dbf() throws Exception {
26-
var exploit = generateExploit();
27-
var factory =
27+
String exploit = generateExploit();
28+
DocumentBuilderFactory factory =
2829
DocumentBuilderFactorySecurity.hardenDocumentBuilderFactory(
2930
DocumentBuilderFactory.newInstance(), false, false);
3031
String secretText = getSecretText(factory, exploit);
3132
assertThat("", equalTo(secretText)); // string is empty instead of secret!
3233
}
3334

3435
private String generateExploit() throws IOException {
35-
var exploit =
36+
String exploit =
3637
FileUtils.readFileToString(new File("src/test/resources/xxe.xml"), StandardCharsets.UTF_8);
3738
exploit =
3839
exploit.replace("$PATH$", new File("src/test/resources/secret.txt").getAbsolutePath());
@@ -43,7 +44,7 @@ private String getSecretText(final DocumentBuilderFactory factory, final String
4344
throws Exception {
4445
ByteArrayInputStream exploitStream =
4546
new ByteArrayInputStream(exploit.getBytes(StandardCharsets.UTF_8));
46-
var doc = factory.newDocumentBuilder().parse(exploitStream);
47+
Document doc = factory.newDocumentBuilder().parse(exploitStream);
4748
return doc.getDocumentElement().getTextContent();
4849
}
4950
}

src/test/java/io/github/pixee/security/ObjectInputFiltersTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class ObjectInputFiltersTest {
1919

2020
@BeforeAll
2121
static void setup() throws IOException {
22-
var baos = new ByteArrayOutputStream();
22+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
2323
gadget =
2424
new DiskFileItem(
2525
"fieldName",
@@ -29,21 +29,21 @@ static void setup() throws IOException {
2929
100,
3030
Files.createTempDirectory("adi").toFile());
3131
gadget.getOutputStream(); // needed to make the object serializable
32-
var oos = new ObjectOutputStream(baos);
32+
ObjectOutputStream oos = new ObjectOutputStream(baos);
3333
oos.writeObject(gadget);
3434
serializedGadget = baos.toByteArray();
3535
}
3636

3737
@Test
3838
void default_is_unprotected() throws Exception {
39-
var ois = new ObjectInputStream(new ByteArrayInputStream(serializedGadget));
39+
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedGadget));
4040
Object o = ois.readObject();
4141
assertThat(o, instanceOf(DiskFileItem.class));
4242
}
4343

4444
@Test
4545
void validating_ois_works() throws Exception {
46-
var ois =
46+
ObjectInputStream ois =
4747
ObjectInputFilters.createSafeObjectInputStream(new ByteArrayInputStream(serializedGadget));
4848
assertThrows(
4949
InvalidClassException.class,
@@ -55,7 +55,7 @@ void validating_ois_works() throws Exception {
5555

5656
@Test
5757
void ois_harden_works() throws Exception {
58-
var ois = new ObjectInputStream(new ByteArrayInputStream(serializedGadget));
58+
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedGadget));
5959
ObjectInputFilters.enableObjectFilterIfUnprotected(ois);
6060
assertThrows(
6161
InvalidClassException.class,
@@ -84,7 +84,7 @@ void objectinputfilter_works_when_none_present() throws Exception {
8484
*/
8585
@Test
8686
void objectinputfilter_works_and_honors_existing() throws Exception {
87-
var filter =
87+
ObjectInputFilter filter =
8888
ObjectInputFilter.Config.createFilter(
8989
"!" + BadType.class.getName() + ";" + GoodType.class.getName());
9090
{
@@ -102,7 +102,7 @@ void objectinputfilter_works_and_honors_existing() throws Exception {
102102
// make sure we still reject the bad type
103103
{
104104
byte[] serializedBadType = serialize(new BadType());
105-
var ois = new ObjectInputStream(new ByteArrayInputStream(serializedBadType));
105+
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedBadType));
106106
ois.setObjectInputFilter(
107107
ObjectInputFilters.createCombinedHardenedObjectFilter(filter)); // this is our weave
108108

@@ -117,7 +117,7 @@ void objectinputfilter_works_and_honors_existing() throws Exception {
117117
// make we still allow the good type
118118
{
119119
byte[] serializedGoodType = serialize(new GoodType());
120-
var ois = new ObjectInputStream(new ByteArrayInputStream(serializedGoodType));
120+
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedGoodType));
121121
ois.setObjectInputFilter(ObjectInputFilters.createCombinedHardenedObjectFilter(filter));
122122
GoodType goodType = (GoodType) ois.readObject();
123123
assertThat(goodType, is(notNullValue()));
@@ -148,7 +148,7 @@ void the_filter_works_as_expected() {
148148
}
149149

150150
byte[] serialize(Serializable s) throws IOException {
151-
var stream = new ByteArrayOutputStream();
151+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
152152
new ObjectOutputStream(stream).writeObject(s);
153153
return stream.toByteArray();
154154
}

src/test/java/io/github/pixee/security/XMLInputFactorySecurityTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,24 @@
1313
import javax.xml.stream.XMLInputFactory;
1414
import javax.xml.stream.XMLStreamException;
1515
import javax.xml.stream.events.StartElement;
16+
import javax.xml.stream.events.XMLEvent;
1617
import org.apache.commons.io.FileUtils;
1718
import org.junit.jupiter.api.Test;
1819

1920
final class XMLInputFactorySecurityTest {
2021

2122
@Test
2223
void xxe_works_in_xmlinputfactory() throws IOException, XMLStreamException {
23-
var exploit = generateExploit();
24-
var factory = XMLInputFactory.newFactory();
24+
String exploit = generateExploit();
25+
XMLInputFactory factory = XMLInputFactory.newFactory();
2526
String secretText = getSecretText(factory, exploit);
2627
assertThat("s3cr3t", equalTo(secretText));
2728
}
2829

2930
@Test
3031
void it_prevents_xxe_in_xmlinputfactory() throws IOException, XMLStreamException {
31-
var exploit = generateExploit();
32-
var factory = XMLInputFactorySecurity.hardenFactory(XMLInputFactory.newFactory());
32+
String exploit = generateExploit();
33+
XMLInputFactory factory = XMLInputFactorySecurity.hardenFactory(XMLInputFactory.newFactory());
3334
String secretText = getSecretText(factory, exploit);
3435
assertThat("", equalTo(secretText)); // string is empty instead of secret!
3536
}
@@ -43,8 +44,8 @@ void it_fails_when_invalid_restrictions() {
4344

4445
@Test
4546
void it_prevents_xxe_in_xmlinputfactory_doctype_only_restriction() throws IOException {
46-
var exploit = generateExploit();
47-
var factory =
47+
String exploit = generateExploit();
48+
XMLInputFactory factory =
4849
XMLInputFactorySecurity.hardenFactory(
4950
XMLInputFactory.newFactory(), Set.of(XMLRestrictions.DISALLOW_DOCTYPE));
5051
assertThrows(XMLStreamException.class, () -> getSecretText(factory, exploit));
@@ -53,16 +54,16 @@ void it_prevents_xxe_in_xmlinputfactory_doctype_only_restriction() throws IOExce
5354
@Test
5455
void it_prevents_xxe_in_xmlinputfactory_external_entity_only_restriction()
5556
throws IOException, XMLStreamException {
56-
var exploit = generateExploit();
57-
var factory =
57+
String exploit = generateExploit();
58+
XMLInputFactory factory =
5859
XMLInputFactorySecurity.hardenFactory(
5960
XMLInputFactory.newFactory(), Set.of(XMLRestrictions.DISALLOW_EXTERNAL_ENTITIES));
6061
String secretText = getSecretText(factory, exploit);
6162
assertThat("", equalTo(secretText)); // string is empty instead of secret!
6263
}
6364

6465
private String generateExploit() throws IOException {
65-
var exploit =
66+
String exploit =
6667
FileUtils.readFileToString(new File("src/test/resources/xxe.xml"), StandardCharsets.UTF_8);
6768
exploit =
6869
exploit.replace("$PATH$", new File("src/test/resources/secret.txt").getAbsolutePath());
@@ -71,15 +72,15 @@ private String generateExploit() throws IOException {
7172

7273
private String getSecretText(final XMLInputFactory factory, final String exploit)
7374
throws XMLStreamException {
74-
var xmlEventReader = factory.createXMLEventReader(new StringReader(exploit));
75+
XMLEventReader xmlEventReader = factory.createXMLEventReader(new StringReader(exploit));
7576
eatEventsUntil(xmlEventReader, StartElement.class);
7677
return xmlEventReader.getElementText();
7778
}
7879

7980
private <T> void eatEventsUntil(XMLEventReader xmlEventReader, Class<T> type)
8081
throws XMLStreamException {
8182
while (xmlEventReader.hasNext()) {
82-
var xmlEvent = xmlEventReader.nextEvent();
83+
XMLEvent xmlEvent = xmlEventReader.nextEvent();
8384
if (type.isAssignableFrom((xmlEvent.getClass()))) {
8485
return;
8586
}

src/test/java/io/github/pixee/security/ZipSecurityTest.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.InputStream;
1111
import java.nio.charset.StandardCharsets;
1212
import java.util.zip.ZipEntry;
13+
import java.util.zip.ZipInputStream;
1314
import java.util.zip.ZipOutputStream;
1415
import org.junit.jupiter.api.Test;
1516
import org.junit.jupiter.params.ParameterizedTest;
@@ -19,47 +20,48 @@ final class ZipSecurityTest {
1920

2021
@Test
2122
void it_doesnt_prevent_normal_zip_file_reads() throws IOException {
22-
var entry = new ZipEntry("normal.txt");
23-
var is = createZipFrom(entry);
23+
ZipEntry entry = new ZipEntry("normal.txt");
24+
InputStream is = createZipFrom(entry);
2425

25-
var hardenedStream = ZipSecurity.createHardenedInputStream(is, StandardCharsets.UTF_8);
26-
var retrievedEntry = hardenedStream.getNextEntry();
26+
ZipInputStream hardenedStream =
27+
ZipSecurity.createHardenedInputStream(is, StandardCharsets.UTF_8);
28+
ZipEntry retrievedEntry = hardenedStream.getNextEntry();
2729
assertThat(retrievedEntry.getName(), equalTo("normal.txt"));
2830
}
2931

3032
@ParameterizedTest
3133
@ValueSource(strings = {"dir1/dir2/../normal.txt", "dir1/../normal.txt"})
3234
void it_doesnt_prevent_normal_zip_files_with_safe_escapes(String path) throws IOException {
33-
var entry = new ZipEntry(path);
34-
var is = createZipFrom(entry);
35+
ZipEntry entry = new ZipEntry(path);
36+
InputStream is = createZipFrom(entry);
3537

36-
var hardenedStream = ZipSecurity.createHardenedInputStream(is);
37-
var retrievedEntry = hardenedStream.getNextEntry();
38+
ZipInputStream hardenedStream = ZipSecurity.createHardenedInputStream(is);
39+
ZipEntry retrievedEntry = hardenedStream.getNextEntry();
3840
assertThat(retrievedEntry.getName(), equalTo(path));
3941
}
4042

4143
@ParameterizedTest
4244
@ValueSource(strings = {"../etc/whatever", "/foo/bar/../../../proc/whatever"})
4345
void it_prevents_escapes(String path) throws IOException {
44-
var entry = new ZipEntry(path);
45-
var is = createZipFrom(entry);
46+
ZipEntry entry = new ZipEntry(path);
47+
InputStream is = createZipFrom(entry);
4648

47-
var hardenedStream = ZipSecurity.createHardenedInputStream(is);
49+
ZipInputStream hardenedStream = ZipSecurity.createHardenedInputStream(is);
4850
assertThrows(SecurityException.class, hardenedStream::getNextEntry);
4951
}
5052

5153
@Test
5254
void it_prevents_absolute_paths_in_zip_entries() throws IOException {
53-
var entry = new ZipEntry("/foo.txt");
54-
var is = createZipFrom(entry);
55+
ZipEntry entry = new ZipEntry("/foo.txt");
56+
InputStream is = createZipFrom(entry);
5557

56-
var hardenedStream = ZipSecurity.createHardenedInputStream(is);
58+
ZipInputStream hardenedStream = ZipSecurity.createHardenedInputStream(is);
5759
assertThrows(SecurityException.class, hardenedStream::getNextEntry);
5860
}
5961

6062
private InputStream createZipFrom(final ZipEntry entry) throws IOException {
61-
var os = new ByteArrayOutputStream();
62-
var zos = new ZipOutputStream(os);
63+
ByteArrayOutputStream os = new ByteArrayOutputStream();
64+
ZipOutputStream zos = new ZipOutputStream(os);
6365
zos.putNextEntry(entry);
6466
zos.closeEntry();
6567
zos.close();

0 commit comments

Comments
 (0)