1+ package io .github .pixee .security ;
2+
3+ import org .junit .jupiter .api .BeforeEach ;
4+ import org .junit .jupiter .api .Test ;
5+
6+ import javax .naming .Context ;
7+ import javax .naming .NamingException ;
8+
9+ import static org .hamcrest .CoreMatchers .is ;
10+ import static org .hamcrest .MatcherAssert .assertThat ;
11+ import static org .junit .jupiter .api .Assertions .assertThrows ;
12+ import static org .mockito .Mockito .*;
13+
14+ final class JNDITest {
15+
16+ private Context context ;
17+ private final Object NAMED_OBJECT = new Object ();
18+ private final Object JAVA_OBJECT = new Object ();
19+ private final Object LDAP_OBJECT = new Object ();
20+ private final Object RMI_OBJECT = new Object ();
21+
22+ @ BeforeEach
23+ void setup () throws NamingException {
24+ context = mock (Context .class );
25+ when (context .lookup ("simple_name" )).thenReturn (NAMED_OBJECT );
26+ when (context .lookup ("java:comp/env" )).thenReturn (JAVA_OBJECT );
27+ when (context .lookup ("ldap://localhost:1389/ou=system" )).thenReturn (LDAP_OBJECT );
28+ when (context .lookup ("rmi://localhost:1099/evil" )).thenReturn (RMI_OBJECT );
29+ }
30+
31+ @ Test
32+ void it_limits_resources_by_name () throws NamingException {
33+ JNDI .LimitedContext limitedContext = JNDI .limitedContextByResourceName (context , J8ApiBridge .setOf ("simple_name" ));
34+ assertThat (limitedContext .lookup ("simple_name" ), is (NAMED_OBJECT ));
35+ assertThrows (SecurityException .class , () -> limitedContext .lookup ("anything_else" ));
36+ verify (context , times (1 )).lookup (anyString ());
37+ }
38+
39+ @ Test
40+ void it_limits_resources_by_protocol () throws NamingException {
41+ JNDI .LimitedContext onlyJavaContext = JNDI .limitedContextByProtocol (context , J8ApiBridge .setOf (UrlProtocol .JAVA ));
42+ assertThat (onlyJavaContext .lookup ("java:comp/env" ), is (JAVA_OBJECT ));
43+ assertThrows (SecurityException .class , () -> onlyJavaContext .lookup ("ldap://localhost:1389/ou=system" ));
44+ assertThrows (SecurityException .class , () -> onlyJavaContext .lookup ("rmi://localhost:1099/evil" ));
45+
46+ JNDI .LimitedContext onlyLdapContext = JNDI .limitedContextByProtocol (context , J8ApiBridge .setOf (UrlProtocol .LDAP ));
47+ assertThat (onlyLdapContext .lookup ("ldap://localhost:1389/ou=system" ), is (LDAP_OBJECT ));
48+ assertThrows (SecurityException .class , () -> onlyLdapContext .lookup ("java:comp/env" ));
49+ assertThrows (SecurityException .class , () -> onlyLdapContext .lookup ("rmi://localhost:1099/evil" ));
50+
51+ JNDI .LimitedContext onlyLdapAndJavaContext = JNDI .limitedContextByProtocol (context , J8ApiBridge .setOf (UrlProtocol .JAVA , UrlProtocol .LDAP ));
52+ assertThat (onlyLdapAndJavaContext .lookup ("ldap://localhost:1389/ou=system" ), is (LDAP_OBJECT ));
53+ assertThat (onlyLdapAndJavaContext .lookup ("java:comp/env" ), is (JAVA_OBJECT ));
54+ assertThrows (SecurityException .class , () -> onlyLdapAndJavaContext .lookup ("rmi://localhost:1099/evil" ));
55+ }
56+
57+ @ Test
58+ void default_limits_rmi_and_ldap () throws NamingException {
59+ JNDI .LimitedContext defaultLimitedContext = JNDI .limitedContext (context );
60+ assertThat (defaultLimitedContext .lookup ("java:comp/env" ), is (JAVA_OBJECT ));
61+ assertThrows (SecurityException .class , () -> defaultLimitedContext .lookup ("rmi://localhost:1099/evil" ));
62+ assertThrows (SecurityException .class , () -> defaultLimitedContext .lookup ("ldap://localhost:1389/ou=system" ));
63+ }
64+
65+ }
0 commit comments