2424/** Provides static functions that configure the behavior of bug detectors provided by Jazzer. */
2525public final class BugDetectors {
2626 private static final AtomicReference <BiPredicate <String , Integer >> currentPolicy =
27- getConnectionPermittedReference ();
27+ getSanitizerVariable (
28+ "com.code_intelligence.jazzer.sanitizers.ServerSideRequestForgery" ,
29+ "connectionPermitted" );
2830
2931 /**
3032 * Allows all network connections.
@@ -78,55 +80,12 @@ public static SilentCloseable allowNetworkConnections() {
7880 */
7981 public static SilentCloseable allowNetworkConnections (
8082 BiPredicate <String , Integer > connectionPermitted ) {
81- if (connectionPermitted == null ) {
82- throw new IllegalArgumentException ("connectionPermitted must not be null" );
83- }
84- if (currentPolicy == null ) {
85- throw new IllegalStateException ("Failed to set network connection policy" );
86- }
87- BiPredicate <String , Integer > previousPolicy = currentPolicy .getAndSet (connectionPermitted );
88- return () -> {
89- if (!currentPolicy .compareAndSet (connectionPermitted , previousPolicy )) {
90- throw new IllegalStateException (
91- "Failed to reset network connection policy - using try-with-resources is highly"
92- + " recommended" );
93- }
94- };
95- }
96-
97- private static AtomicReference <BiPredicate <String , Integer >> getConnectionPermittedReference () {
98- try {
99- Class <?> ssrfSanitizer =
100- Class .forName ("com.code_intelligence.jazzer.sanitizers.ServerSideRequestForgery" );
101- return (AtomicReference <BiPredicate <String , Integer >>)
102- ssrfSanitizer .getField ("connectionPermitted" ).get (null );
103- } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e ) {
104- System .err .println ("WARN: " );
105- e .printStackTrace ();
106- return null ;
107- }
83+ return setSanitizerVariable (connectionPermitted , currentPolicy );
10884 }
10985
11086 // File path traversal sanitizer control
11187 private static final AtomicReference <Supplier <Path >> currentPathTraversalTarget =
112- getFilePathTraversalTarget ();
113-
114- /**
115- * Returns the current target for file path traversal sanitization.
116- *
117- * @return a supplier that provides the target directory for file path traversal sanitization.
118- */
119- private static AtomicReference <Supplier <Path >> getFilePathTraversalTarget () {
120- try {
121- Class <?> pathTraversalSanitizer =
122- Class .forName ("com.code_intelligence.jazzer.sanitizers.FilePathTraversal" );
123- return (AtomicReference <Supplier <Path >>) pathTraversalSanitizer .getField ("target" ).get (null );
124- } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e ) {
125- System .err .println ("WARN: " );
126- e .printStackTrace ();
127- return null ;
128- }
129- }
88+ getSanitizerVariable ("com.code_intelligence.jazzer.sanitizers.FilePathTraversal" , "target" );
13089
13190 /**
13291 * Sets the target for file path traversal sanitization.
@@ -149,19 +108,33 @@ private static AtomicReference<Supplier<Path>> getFilePathTraversalTarget() {
149108 * @return a {@link SilentCloseable} that restores the previously set target when closed
150109 */
151110 public static SilentCloseable setFilePathTraversalTarget (Supplier <Path > pathTraversalTarget ) {
152- if (pathTraversalTarget == null ) {
153- throw new IllegalArgumentException ("pathTraversalTarget must not be null" );
111+ return setSanitizerVariable (pathTraversalTarget , currentPathTraversalTarget );
112+ }
113+
114+ private static <T > AtomicReference <T > getSanitizerVariable (
115+ String sanitizerClassName , String fieldName ) {
116+ try {
117+ return (AtomicReference <T >) Class .forName (sanitizerClassName ).getField (fieldName ).get (null );
118+ } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e ) {
119+ System .err .println ("WARN: " );
120+ e .printStackTrace ();
121+ return null ;
122+ }
123+ }
124+
125+ private static <T > SilentCloseable setSanitizerVariable (
126+ T newValue , AtomicReference <T > currentValue ) {
127+ if (newValue == null ) {
128+ throw new IllegalArgumentException ("sanitizer variable must not be null" );
154129 }
155- if (currentPathTraversalTarget == null ) {
156- throw new IllegalStateException ("Failed to set path traversal target " );
130+ if (currentValue == null ) {
131+ throw new IllegalStateException ("Failed to set sanitizer variable " );
157132 }
158- Supplier <Path > previousPathTraversalTarget =
159- currentPathTraversalTarget .getAndSet (pathTraversalTarget );
133+ T previousValue = currentValue .getAndSet (newValue );
160134 return () -> {
161- if (!currentPathTraversalTarget .compareAndSet (
162- pathTraversalTarget , previousPathTraversalTarget )) {
135+ if (!currentValue .compareAndSet (newValue , previousValue )) {
163136 throw new IllegalStateException (
164- "Failed to reset path traversal target - using try-with-resources is highly"
137+ "Failed to reset sanitizer variable - using try-with-resources is highly"
165138 + " recommended" );
166139 }
167140 };
0 commit comments