Skip to content

Commit 3b20813

Browse files
committed
introduced animal sniffer and took away java 8 deps
1 parent 8ad8c77 commit 3b20813

18 files changed

+130
-60
lines changed

pom.xml

Lines changed: 32 additions & 2 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.4</version>
8+
<version>1.0.5</version>
99

1010
<name>java-security-toolkit</name>
1111
<description>a library with common security controls</description>
@@ -90,14 +90,19 @@
9090
<artifactId>commons-collections4</artifactId>
9191
<version>4.4</version>
9292
</dependency>
93+
<dependency>
94+
<groupId>org.codehaus.mojo</groupId>
95+
<artifactId>animal-sniffer-annotations</artifactId>
96+
<version>1.23</version>
97+
<optional>true</optional>
98+
</dependency>
9399
<!-- needed for testing deserialization protection -->
94100
<dependency>
95101
<groupId>commons-fileupload</groupId>
96102
<artifactId>commons-fileupload</artifactId>
97103
<version>1.5</version>
98104
<scope>test</scope>
99105
</dependency>
100-
101106
<dependency>
102107
<groupId>org.junit.jupiter</groupId>
103108
<artifactId>junit-jupiter-api</artifactId>
@@ -167,6 +172,27 @@
167172
<target>8</target>
168173
</configuration>
169174
</plugin>
175+
<plugin>
176+
<groupId>org.codehaus.mojo</groupId>
177+
<artifactId>animal-sniffer-maven-plugin</artifactId>
178+
<version>1.23</version>
179+
<configuration>
180+
<signature>
181+
<groupId>org.codehaus.mojo.signature</groupId>
182+
<artifactId>java18</artifactId>
183+
<version>1.0</version>
184+
</signature>
185+
</configuration>
186+
<executions>
187+
<execution>
188+
<id>animal-sniffer</id>
189+
<phase>test</phase>
190+
<goals>
191+
<goal>check</goal>
192+
</goals>
193+
</execution>
194+
</executions>
195+
</plugin>
170196
<plugin>
171197
<groupId>org.jacoco</groupId>
172198
<artifactId>jacoco-maven-plugin</artifactId>
@@ -256,6 +282,10 @@
256282
<plugin>
257283
<artifactId>maven-compiler-plugin</artifactId>
258284
</plugin>
285+
<plugin>
286+
<groupId>org.codehaus.mojo</groupId>
287+
<artifactId>animal-sniffer-maven-plugin</artifactId>
288+
</plugin>
259289
<plugin>
260290
<groupId>org.jacoco</groupId>
261291
<artifactId>jacoco-maven-plugin</artifactId>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.pixee.security;
22

3+
import static io.github.pixee.security.J8ApiBridge.listOf;
4+
35
import java.util.List;
46
import javax.xml.parsers.DocumentBuilderFactory;
57

@@ -47,7 +49,7 @@ public static DocumentBuilderFactory hardenDocumentBuilderFactory(
4749
}
4850

4951
private static final List<String> externalEntityFeatures =
50-
List.of(
52+
listOf(
5153
"http://apache.org/xml/features/disallow-doctype-decl",
5254
"http://apache.org/xml/features/disallow-doctype-decl",
5355
"http://apache.org/xml/features/nonvalidating/load-external-dtd",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private Filenames() {}
4242
* File Upload</a>
4343
*/
4444
public static String toSimpleFileName(final String fileName) {
45-
if (fileName == null || fileName.isBlank()) {
45+
if (fileName == null || "".equals(fileName.trim())) {
4646
// this file name may cause issues with the apis they'll be used in but we can't help so don't
4747
// try
4848
return fileName;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.pixee.security;
22

3+
import static io.github.pixee.security.J8ApiBridge.setOf;
4+
35
import java.net.URL;
46
import java.util.Set;
57
import java.util.regex.Pattern;
@@ -33,7 +35,7 @@ public boolean isAllowed(final String host) {
3335
}
3436

3537
private final Set<String> knownInfrastructureTargets =
36-
Set.of("192.168.1.1", "3232235777", "169.254.169.254", "2852039166");
38+
setOf("192.168.1.1", "3232235777", "169.254.169.254", "2852039166");
3739
};
3840

3941
/**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.github.pixee.security;
2+
3+
import java.util.*;
4+
5+
final class J8ApiBridge {
6+
7+
private J8ApiBridge() {}
8+
9+
/** A replacement API for Set.of(), which doesn't exist in Java 8, which we target. */
10+
static <T> Set<T> setOf(final T... t) {
11+
return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(t)));
12+
}
13+
14+
/** A replacement API for List.of(), which doesn't exist in Java 8, which we target. */
15+
static <T> List<T> listOf(final T... t) {
16+
return Collections.unmodifiableList(new ArrayList<>(Arrays.asList(t)));
17+
}
18+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.ObjectInputStream;
77
import java.util.Objects;
88
import org.apache.commons.io.serialization.ValidatingObjectInputStream;
9+
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
910

1011
/**
1112
* This type exposes helper methods that will help defend against Java deserialization attacks.
@@ -14,6 +15,7 @@
1415
* href="https://cheatsheetseries.owasp.org/cheatsheets/Deserialization_Cheat_Sheet.html">OWASP
1516
* Cheat Sheet</a>.
1617
*/
18+
@IgnoreJRERequirement
1719
public final class ObjectInputFilters {
1820

1921
private ObjectInputFilters() {}
@@ -78,6 +80,7 @@ public static ObjectInputFilter createCombinedHardenedObjectFilter(
7880
return new CombinedObjectInputFilter(existingFilter);
7981
}
8082

83+
@IgnoreJRERequirement
8184
private static class CombinedObjectInputFilter implements ObjectInputFilter {
8285
private final ObjectInputFilter originalFilter;
8386

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.pixee.security;
22

3+
import static io.github.pixee.security.J8ApiBridge.setOf;
4+
35
import java.lang.reflect.Modifier;
46
import java.util.Set;
57

@@ -12,7 +14,7 @@ public final class Reflection {
1214
private Reflection() {}
1315

1416
private static final Set<ReflectionRestrictions> defaultRestrictions =
15-
Set.of(ReflectionRestrictions.MUST_NOT_INVOLVE_CODE_EXECUTION);
17+
setOf(ReflectionRestrictions.MUST_NOT_INVOLVE_CODE_EXECUTION);
1618

1719
/**
1820
* Provide the default restrictions for loading a type that will work for the vast majority of
@@ -99,14 +101,14 @@ public static Class<?> loadAndVerify(
99101
}
100102

101103
private static final Set<Class<?>> codeLoadingTypes =
102-
Set.of(
104+
setOf(
103105
java.lang.Runtime.class,
104106
java.lang.ProcessBuilder.class,
105107
java.lang.Class.class,
106108
java.lang.ClassLoader.class);
107109

108110
private static final Set<String> codeLoadingPackages =
109-
Set.of(
111+
setOf(
110112
"java.lang.invoke.",
111113
"org.apache.commons.collections.functors.",
112114
"bsh.",

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package io.github.pixee.security;
22

3+
import static io.github.pixee.security.J8ApiBridge.listOf;
4+
import static io.github.pixee.security.J8ApiBridge.setOf;
5+
36
import java.io.File;
47
import java.io.IOException;
58
import java.util.LinkedList;
@@ -17,7 +20,7 @@ private SystemCommand() {}
1720
* @return a set of restrictions suitable for general use
1821
*/
1922
public static Set<SystemCommandRestrictions> defaultRestrictions() {
20-
return Set.of(
23+
return setOf(
2124
SystemCommandRestrictions.PREVENT_COMMAND_CHAINING,
2225
SystemCommandRestrictions.PREVENT_ARGUMENTS_TARGETING_SENSITIVE_FILES);
2326
}
@@ -453,10 +456,10 @@ private static boolean isShell(final String commandToken) {
453456
return SHELL_FILE_NAMES.contains(commandFile.getName());
454457
}
455458

456-
private static final List<String> SHELL_FILE_NAMES = List.of("bash", "sh", "zsh", "csh", "tcsh");
459+
private static final List<String> SHELL_FILE_NAMES = listOf("bash", "sh", "zsh", "csh", "tcsh");
457460

458461
private static final List<String> BANNED_EXECUTABLES =
459-
List.of(
462+
listOf(
460463
// reverse shells, exfiltration, downloading malware
461464
"nc",
462465
"curl",
@@ -466,7 +469,7 @@ private static boolean isShell(final String commandToken) {
466469
"rpm");
467470

468471
private static final List<String> SENSITIVE_FILE_NAMES =
469-
List.of(
472+
listOf(
470473
"/etc/passwd",
471474
"/etc/shadow",
472475
"/etc/group",

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.pixee.security;
22

3+
import static io.github.pixee.security.J8ApiBridge.listOf;
4+
35
import java.util.List;
46
import java.util.stream.Collectors;
57
import java.util.stream.Stream;
@@ -64,7 +66,7 @@ public static boolean isUnwanted(final String className) {
6466

6567
/** A list of types known to be involved in deserialization and remote code execution attacks. */
6668
private static final List<String> gadgets =
67-
List.of(
69+
listOf(
6870
" org.apache.commons.beanutils.BeanComparator".substring(1),
6971
" org.apache.commons.collections.functors.ChainedTransformer".substring(1),
7072
" org.apache.commons.collections.functors.ConstantTransformer".substring(1),
@@ -128,7 +130,7 @@ public static boolean isUnwanted(final String className) {
128130
* remote code execution attacks.
129131
*/
130132
private static final List<String> gadgetPrefixes =
131-
List.of(
133+
listOf(
132134
"com.sun.rowset.JdbcRowSetImpl$",
133135
"java.rmi.registry.Registry$",
134136
"java.rmi.server.ObjID$",

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.pixee.security;
22

3+
import static io.github.pixee.security.J8ApiBridge.setOf;
4+
35
import java.net.MalformedURLException;
46
import java.net.URL;
57
import java.net.URLStreamHandler;
@@ -17,7 +19,7 @@ public final class Urls {
1719
* This is a convenience {@link Set} provided for most people who probably only want to allow
1820
* HTTP-based protocols.
1921
*/
20-
public static Set<UrlProtocol> HTTP_PROTOCOLS = Set.of(UrlProtocol.HTTPS, UrlProtocol.HTTP);
22+
public static Set<UrlProtocol> HTTP_PROTOCOLS = setOf(UrlProtocol.HTTPS, UrlProtocol.HTTP);
2123

2224
public static URL create(
2325
final String url, final Set<UrlProtocol> allowedProtocols, final HostValidator validator)

0 commit comments

Comments
 (0)